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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::error::Error;
use crate::util;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct SessionId(pub i64);
impl std::fmt::Display for SessionId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:#x}", self.0)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, strum::Display)]
pub enum SessionState {
Disconnected,
SyncConnected,
AuthFailed,
ConnectedReadOnly,
Expired,
Closed,
}
impl SessionState {
pub(crate) fn from_server(state: i32) -> Result<SessionState, Error> {
let session_state = match state {
3 => SessionState::SyncConnected,
_ => return Err(Error::UnexpectedError(format!("keeper state value should not be {}", state))),
};
return Ok(session_state);
}
pub fn is_terminated(self) -> bool {
use SessionState::*;
match self {
AuthFailed | Expired | Closed => return true,
_ => return false,
};
}
pub(crate) fn is_connected(self) -> bool {
return self == SessionState::SyncConnected || self == SessionState::ConnectedReadOnly;
}
pub(crate) fn to_error(self) -> Error {
match self {
SessionState::Disconnected => Error::ConnectionLoss,
SessionState::AuthFailed => Error::AuthFailed,
SessionState::Expired => Error::SessionExpired,
SessionState::Closed => Error::ClientClosed,
_ => Error::UnexpectedError(format!("expect error state, got {:?}", self)),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WatchedEvent {
pub event_type: EventType,
pub session_state: SessionState,
pub path: String,
}
impl WatchedEvent {
pub(crate) fn drain_root_len(&mut self, root_len: usize) {
if self.event_type != EventType::Session && root_len != 0 {
util::drain_root_len(&mut self.path, root_len);
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, strum::Display)]
pub enum EventType {
Session,
NodeCreated,
NodeDeleted,
NodeDataChanged,
NodeChildrenChanged,
}
impl EventType {
pub(crate) fn from_server(i: i32) -> Result<EventType, Error> {
let event_type = match i {
1 => EventType::NodeCreated,
2 => EventType::NodeDeleted,
3 => EventType::NodeDataChanged,
4 => EventType::NodeChildrenChanged,
_ => return Err(Error::UnexpectedError(format!("event type should not be {}", i))),
};
return Ok(event_type);
}
}