1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use parking_lot::RwLock;

use std::sync::Arc;

use crate::types::ShortString;

#[derive(Clone, Debug, Default)]
pub struct ChannelStatus {
  inner: Arc<RwLock<Inner>>,
}

impl ChannelStatus {
  pub fn is_initializing(&self) -> bool {
    self.inner.read().state == ChannelState::Initial
  }

  pub fn is_closing(&self) -> bool {
    self.inner.read().state == ChannelState::Closing
  }

  pub fn is_connected(&self) -> bool {
    !&[ChannelState::Initial, ChannelState::Closing, ChannelState::Closed, ChannelState::Error].contains(&self.inner.read().state)
  }

  pub fn confirm(&self) -> bool {
    self.inner.read().confirm
  }

  pub(crate) fn set_confirm(&self) {
    self.inner.write().confirm = true
  }

  pub fn state(&self) -> ChannelState {
    self.inner.read().state.clone()
  }

  pub(crate) fn set_state(&self, state: ChannelState) {
    self.inner.write().state = state
  }

  pub(crate) fn set_send_flow(&self, flow: bool) {
    self.inner.write().send_flow = flow;
  }

  pub(crate) fn flow(&self) -> bool {
    self.inner.read().send_flow
  }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ChannelState {
    Initial,
    Connected,
    Closing,
    Closed,
    Error,
    SendingContent(usize),
    WillReceiveContent(Option<ShortString>, Option<ShortString>),
    ReceivingContent(Option<ShortString>, Option<ShortString>, usize),
}

impl Default for ChannelState {
  fn default() -> Self {
    ChannelState::Initial
  }
}

#[derive(Debug)]
struct Inner {
  confirm:   bool,
  send_flow: bool,
  state:     ChannelState,
}

impl Default for Inner {
  fn default() -> Self {
    Self {
      confirm:   false,
      send_flow: true,
      state:     ChannelState::default(),
    }
  }
}