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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
use std::time::Duration;
use const_format::formatcp;
use tokio::sync::{mpsc, oneshot, watch};
use super::operation::{self, SessionOperation, WatchReceiver};
use super::session::{AuthResponser, OperationState, Session};
use super::types::{SessionId, SessionState, WatchedEvent};
use crate::acl::{Acl, AuthUser};
use crate::error::{ConnectError, Error};
pub use crate::proto::EnsembleUpdate;
use crate::proto::{
self,
AuthPacket,
CreateRequest,
DeleteRequest,
ExistsRequest,
GetAclResponse,
GetChildren2Response,
GetChildrenRequest,
GetRequest,
OpCode,
PersistentWatchRequest,
ReconfigRequest,
RootedPath,
SetAclRequest,
SetDataRequest,
Stat,
SyncRequest,
};
use crate::record::{self, Record, StaticRecord};
use crate::util::{self, Ref as _};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CreateMode {
Persistent,
PersistentSequential,
Ephemeral,
EphemeralSequential,
Container,
}
impl CreateMode {
fn is_sequential(self) -> bool {
return self == CreateMode::PersistentSequential || self == CreateMode::EphemeralSequential;
}
fn is_container(self) -> bool {
return self == CreateMode::Container;
}
fn as_flags(self, ttl: bool) -> i32 {
use CreateMode::*;
match self {
Persistent => {
if ttl {
5
} else {
0
}
},
PersistentSequential => {
if ttl {
6
} else {
2
}
},
Ephemeral => 1,
EphemeralSequential => 3,
Container => 4,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AddWatchMode {
Persistent,
PersistentRecursive,
}
impl From<AddWatchMode> for proto::AddWatchMode {
fn from(mode: AddWatchMode) -> proto::AddWatchMode {
match mode {
AddWatchMode::Persistent => proto::AddWatchMode::Persistent,
AddWatchMode::PersistentRecursive => proto::AddWatchMode::PersistentRecursive,
}
}
}
pub struct CreateOptions<'a> {
mode: CreateMode,
acls: &'a [Acl],
ttl: Option<Duration>,
}
const TTL_MAX_MILLIS: u128 = 0x00FFFFFFFFFF;
impl<'a> CreateOptions<'a> {
pub fn new(mode: CreateMode, acls: &'a [Acl]) -> CreateOptions<'a> {
return CreateOptions { mode, acls, ttl: None };
}
pub fn with_ttl(&'a mut self, ttl: Duration) -> &'a mut Self {
self.ttl = Some(ttl);
self
}
fn validate(&'a self) -> Result<(), Error> {
if let Some(ref ttl) = self.ttl {
if self.mode != CreateMode::Persistent && self.mode != CreateMode::PersistentSequential {
return Err(Error::BadArguments(&"ttl can only be specified with persistent node"));
} else if ttl.is_zero() {
return Err(Error::BadArguments(&"ttl is zero"));
} else if ttl.as_millis() > TTL_MAX_MILLIS {
return Err(Error::BadArguments(&formatcp!("ttl cannot larger than {}", TTL_MAX_MILLIS)));
}
}
if self.acls.is_empty() {
return Err(Error::InvalidAcl);
}
return Ok(());
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CreateSequence(pub i32);
impl std::fmt::Display for CreateSequence {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:010}", self.0)
}
}
#[derive(Clone, Debug)]
pub struct StateWatcher {
receiver: watch::Receiver<SessionState>,
}
impl StateWatcher {
fn new(receiver: watch::Receiver<SessionState>) -> StateWatcher {
return StateWatcher { receiver };
}
pub fn state(&mut self) -> SessionState {
let state = self.receiver.borrow_and_update();
return *state;
}
pub async fn changed(&mut self) -> SessionState {
self.receiver.changed().await.unwrap();
return self.state();
}
pub fn peek_state(&self) -> SessionState {
let state = self.receiver.borrow();
return *state;
}
}
#[derive(Debug)]
pub struct OneshotWatcher {
root_len: usize,
receiver: oneshot::Receiver<WatchedEvent>,
}
impl OneshotWatcher {
pub async fn changed(self) -> WatchedEvent {
let mut event = self.receiver.await.unwrap();
event.drain_root_len(self.root_len);
return event;
}
}
#[derive(Debug)]
pub struct PersistentWatcher {
root_len: usize,
receiver: mpsc::UnboundedReceiver<WatchedEvent>,
}
impl PersistentWatcher {
pub async fn changed(&mut self) -> WatchedEvent {
let mut event = self.receiver.recv().await.unwrap();
event.drain_root_len(self.root_len);
return event;
}
}
impl WatchReceiver {
pub fn into_oneshot(self, root: &str) -> OneshotWatcher {
match self {
WatchReceiver::None => unreachable!("expect oneshot watcher, got none watcher"),
WatchReceiver::Oneshot(receiver) => OneshotWatcher { root_len: root.len(), receiver },
WatchReceiver::Persistent(_) => {
unreachable!("expect oneshot watcher, got persistent watcher")
},
}
}
pub fn into_persistent(self, root: &str) -> PersistentWatcher {
match self {
WatchReceiver::None => unreachable!("expect oneshot watcher, got none watcher"),
WatchReceiver::Oneshot(_) => {
unreachable!("expect oneshot watcher, got oneshot watcher")
},
WatchReceiver::Persistent(receiver) => PersistentWatcher { root_len: root.len(), receiver },
}
}
}
impl Drop for PersistentWatcher {
fn drop(&mut self) {}
}
#[derive(Clone, Debug)]
pub struct Client {
root: String,
session: (SessionId, Vec<u8>),
session_timeout: Duration,
requester: mpsc::Sender<SessionOperation>,
auth_requester: mpsc::Sender<(AuthPacket, AuthResponser)>,
state_watcher: StateWatcher,
}
impl Client {
const CONFIG_NODE: &'static str = "/zookeeper/config";
pub async fn connect(cluster: &str, timeout: Duration) -> Result<Client, ConnectError> {
return ClientBuilder::new(timeout).connect(cluster).await;
}
pub(crate) fn new(
root: String,
session: (SessionId, Vec<u8>),
timeout: Duration,
requester: mpsc::Sender<SessionOperation>,
auth_requester: mpsc::Sender<(AuthPacket, AuthResponser)>,
state_receiver: watch::Receiver<SessionState>,
) -> Client {
let state_watcher = StateWatcher::new(state_receiver);
Client { root, session, session_timeout: timeout, requester, auth_requester, state_watcher }
}
fn validate_path<'a>(&self, path: &'a str) -> Result<(&'a str, bool), Error> {
return util::validate_path(self.root.as_str(), path, false);
}
fn validate_sequential_path<'a>(&self, path: &'a str) -> Result<(&'a str, bool), Error> {
return util::validate_path(&self.root, path, true);
}
pub fn session_id(&self) -> SessionId {
return self.session.0;
}
pub fn session_password(&self) -> &[u8] {
return self.session.1.as_slice();
}
pub fn session_timeout(&self) -> Duration {
return self.session_timeout;
}
pub fn state(&self) -> SessionState {
return self.state_watcher.peek_state();
}
pub fn state_watcher(&self) -> StateWatcher {
return self.state_watcher.clone();
}
pub fn chroot(mut self, root: &str) -> Result<Client, Client> {
let is_zookeeper_root = match util::validate_path("", root, false) {
Err(_) => return Err(self),
Ok((_, is_zookeeper_root)) => is_zookeeper_root,
};
self.root.clear();
if !is_zookeeper_root {
self.root.push_str(root);
}
return Ok(self);
}
async fn request(&self, code: OpCode, body: &impl Record) -> Result<(Vec<u8>, WatchReceiver), Error> {
let (operation, receiver) = operation::build_state_operation(code, body);
if self.requester.send(operation).await.is_err() {
let state = self.state();
return Err(state.to_error());
}
return receiver.await.unwrap();
}
fn parse_sequence(client_path: &str, path: &str) -> Result<CreateSequence, Error> {
if let Some(sequence_path) = client_path.strip_prefix(path) {
match sequence_path.parse::<i32>() {
Err(_) => Err(Error::UnexpectedError(format!("sequential node get no i32 path {}", client_path))),
Ok(i) => Ok(CreateSequence(i)),
}
} else {
return Err(Error::UnexpectedError(format!(
"sequential path {} does not contain prefix path {}",
client_path, path
)));
}
}
pub async fn create(
&self,
path: &str,
data: &[u8],
options: &CreateOptions<'_>,
) -> Result<(Stat, CreateSequence), Error> {
options.validate()?;
let create_mode = options.mode;
let sequential = create_mode.is_sequential();
let (leaf, _) = if sequential { self.validate_sequential_path(path)? } else { self.validate_path(path)? };
let ttl = options.ttl.map(|ttl| ttl.as_millis() as i64).unwrap_or(0);
let op_code = if ttl != 0 {
OpCode::CreateTtl
} else if create_mode.is_container() {
OpCode::CreateContainer
} else {
OpCode::Create2
};
let flags = create_mode.as_flags(ttl != 0);
let request = CreateRequest { path: RootedPath::new(&self.root, leaf), data, acls: options.acls, flags, ttl };
let (body, _) = self.request(op_code, &request).await?;
let mut buf = body.as_slice();
let server_path = record::unmarshal_entity::<&str>(&"server path", &mut buf)?;
let client_path = util::strip_root_path(server_path, &self.root)?;
let sequence = if sequential { Self::parse_sequence(client_path, path)? } else { CreateSequence(-1) };
let stat = record::unmarshal::<Stat>(&mut buf)?;
return Ok((stat, sequence));
}
pub async fn delete(&self, path: &str, expected_version: Option<i32>) -> Result<(), Error> {
let (leaf, _) = self.validate_path(path)?;
if leaf.is_empty() {
return Err(Error::BadArguments(&"can not delete root node"));
}
let request =
DeleteRequest { path: RootedPath::new(&self.root, leaf), version: expected_version.unwrap_or(-1) };
self.request(OpCode::Delete, &request).await?;
return Ok(());
}
async fn get_data_internally(
&self,
root: &str,
leaf: &str,
watch: bool,
) -> Result<(Vec<u8>, Stat, WatchReceiver), Error> {
let request = GetRequest { path: RootedPath::new(root, leaf), watch };
let (mut body, watcher) = self.request(OpCode::GetData, &request).await?;
let data_len = body.len() - Stat::record_len();
let mut stat_buf = &body[data_len..];
let stat = record::unmarshal(&mut stat_buf)?;
body.truncate(data_len);
drop(body.drain(..4));
return Ok((body, stat, watcher));
}
pub async fn get_data(&self, path: &str) -> Result<(Vec<u8>, Stat), Error> {
let (leaf, _) = self.validate_path(path)?;
let (data, stat, _) = self.get_data_internally(&self.root, leaf, false).await?;
return Ok((data, stat));
}
pub async fn get_and_watch_data(&self, path: &str) -> Result<(Vec<u8>, Stat, OneshotWatcher), Error> {
let (leaf, _) = self.validate_path(path)?;
let (data, stat, watch_receiver) = self.get_data_internally(&self.root, leaf, true).await?;
return Ok((data, stat, watch_receiver.into_oneshot(&self.root)));
}
async fn check_stat_internally(&self, path: &str, watch: bool) -> Result<(Option<Stat>, WatchReceiver), Error> {
let (leaf, _) = self.validate_path(path)?;
let request = ExistsRequest { path: RootedPath::new(&self.root, leaf), watch };
let (body, watcher) = self.request(OpCode::Exists, &request).await?;
let mut buf = body.as_slice();
let stat = record::try_deserialize(&mut buf)?;
return Ok((stat, watcher));
}
pub async fn check_stat(&self, path: &str) -> Result<Option<Stat>, Error> {
let (stat, _) = self.check_stat_internally(path, false).await?;
return Ok(stat);
}
pub async fn check_and_watch_stat(&self, path: &str) -> Result<(Option<Stat>, OneshotWatcher), Error> {
let (stat, watch_receiver) = self.check_stat_internally(path, true).await?;
return Ok((stat, watch_receiver.into_oneshot(&self.root)));
}
pub async fn set_data(&self, path: &str, data: &[u8], expected_version: Option<i32>) -> Result<Stat, Error> {
let (leaf, _) = self.validate_path(path)?;
let request =
SetDataRequest { path: RootedPath::new(&self.root, leaf), data, version: expected_version.unwrap_or(-1) };
let (body, _) = self.request(OpCode::SetData, &request).await?;
let mut buf = body.as_slice();
let stat: Stat = record::unmarshal(&mut buf)?;
return Ok(stat);
}
async fn list_children_internally(&self, path: &str, watch: bool) -> Result<(Vec<String>, WatchReceiver), Error> {
let (leaf, _) = self.validate_path(path)?;
let request = GetChildrenRequest { path: RootedPath::new(&self.root, leaf), watch };
let (body, watcher) = self.request(OpCode::GetChildren, &request).await?;
let mut buf = body.as_slice();
let children = record::unmarshal_entity::<Vec<&str>>(&"children paths", &mut buf)?;
let children = children.into_iter().map(|child| child.to_owned()).collect();
return Ok((children, watcher));
}
pub async fn list_children(&self, path: &str) -> Result<Vec<String>, Error> {
let (children, _) = self.list_children_internally(path, false).await?;
return Ok(children);
}
pub async fn list_and_watch_children(&self, path: &str) -> Result<(Vec<String>, OneshotWatcher), Error> {
let (children, watcher) = self.list_children_internally(path, true).await?;
return Ok((children, watcher.into_oneshot(&self.root)));
}
async fn get_children_internally(
&self,
path: &str,
watch: bool,
) -> Result<(Vec<String>, Stat, WatchReceiver), Error> {
let (leaf, _) = self.validate_path(path)?;
let request = GetChildrenRequest { path: RootedPath::new(&self.root, leaf), watch };
let (body, watcher) = self.request(OpCode::GetChildren2, &request).await?;
let mut buf = body.as_slice();
let response = record::unmarshal::<GetChildren2Response>(&mut buf)?;
let children = response.children.into_iter().map(|s| s.to_owned()).collect();
return Ok((children, response.stat, watcher));
}
pub async fn get_children(&self, path: &str) -> Result<(Vec<String>, Stat), Error> {
let (children, stat, _) = self.get_children_internally(path, false).await?;
return Ok((children, stat));
}
pub async fn get_and_watch_children(&self, path: &str) -> Result<(Vec<String>, Stat, OneshotWatcher), Error> {
let (children, stat, watcher) = self.get_children_internally(path, true).await?;
return Ok((children, stat, watcher.into_oneshot(&self.root)));
}
pub async fn count_descendants_number(&self, path: &str) -> Result<usize, Error> {
let (leaf, _) = self.validate_path(path)?;
let request = RootedPath::new(&self.root, leaf);
let (body, _) = self.request(OpCode::GetAllChildrenNumber, &request).await?;
let mut buf = body.as_slice();
let n = record::unmarshal_entity::<i32>(&"all children number", &mut buf)?;
return Ok(n as usize);
}
pub async fn list_ephemerals(&self, path: &str) -> Result<Vec<String>, Error> {
let (leaf, _) = self.validate_path(path)?;
let request = RootedPath::new(&self.root, leaf);
let (body, _) = self.request(OpCode::GetEphemerals, &request).await?;
let mut buf = body.as_slice();
let mut ephemerals = record::unmarshal_entity::<Vec<String>>(&"ephemerals", &mut buf)?;
for ephemeral_path in ephemerals.iter_mut() {
util::drain_root_path(ephemeral_path, &self.root)?;
}
return Ok(ephemerals);
}
pub async fn get_acl(&self, path: &str) -> Result<(Vec<Acl>, Stat), Error> {
let (leaf, _) = self.validate_path(path)?;
let request = RootedPath::new(&self.root, leaf);
let (body, _) = self.request(OpCode::GetACL, &request).await?;
let mut buf = body.as_slice();
let response: GetAclResponse = record::unmarshal(&mut buf)?;
return Ok((response.acl, response.stat));
}
pub async fn set_acl(&self, path: &str, acl: &[Acl], expected_acl_version: Option<i32>) -> Result<Stat, Error> {
let (leaf, _) = self.validate_path(path)?;
let request =
SetAclRequest { path: RootedPath::new(&self.root, leaf), acl, version: expected_acl_version.unwrap_or(-1) };
let (body, _) = self.request(OpCode::SetACL, &request).await?;
let mut buf = body.as_slice();
let stat: Stat = record::unmarshal(&mut buf)?;
return Ok(stat);
}
pub async fn watch(&self, path: &str, mode: AddWatchMode) -> Result<PersistentWatcher, Error> {
let (leaf, _) = self.validate_path(path)?;
let proto_mode = proto::AddWatchMode::from(mode);
let request = PersistentWatchRequest { path: RootedPath::new(&self.root, leaf), mode: proto_mode.into() };
let (_, watcher) = self.request(OpCode::AddWatch, &request).await?;
return Ok(watcher.into_persistent(&self.root));
}
pub async fn sync(&self, path: &str) -> Result<(), Error> {
let (leaf, _) = self.validate_path(path)?;
let request = SyncRequest { path: RootedPath::new(&self.root, leaf) };
let (body, _) = self.request(OpCode::Sync, &request).await?;
let mut buf = body.as_slice();
record::unmarshal_entity::<&str>(&"server path", &mut buf)?;
return Ok(());
}
pub async fn auth(&self, scheme: String, auth: Vec<u8>) -> Result<(), Error> {
let (sender, receiver) = oneshot::channel();
let auth_packet = AuthPacket { scheme, auth };
if self.auth_requester.send((auth_packet, sender)).await.is_err() {
let state = self.state();
return Err(state.to_error());
}
return receiver.await.unwrap();
}
pub async fn list_auth_users(&self) -> Result<Vec<AuthUser>, Error> {
let request = ();
let (body, _) = self.request(OpCode::WhoAmI, &request).await?;
let mut buf = body.as_slice();
let authed_users = record::unmarshal_entity::<Vec<AuthUser>>(&"authed users", &mut buf)?;
return Ok(authed_users);
}
pub async fn get_config(&self) -> Result<(Vec<u8>, Stat), Error> {
let (data, stat, _) = self.get_data_internally(Self::CONFIG_NODE, Default::default(), false).await?;
return Ok((data, stat));
}
pub async fn get_and_watch_config(&self) -> Result<(Vec<u8>, Stat, OneshotWatcher), Error> {
let (data, stat, watcher) = self.get_data_internally(Self::CONFIG_NODE, Default::default(), false).await?;
return Ok((data, stat, watcher.into_oneshot(&self.root)));
}
pub async fn update_ensemble<'a, I: Iterator<Item = &'a str> + Clone>(
&self,
update: EnsembleUpdate<'a, I>,
expected_version: Option<i32>,
) -> Result<(Vec<u8>, Stat), Error> {
let request = ReconfigRequest { update, version: expected_version.unwrap_or(-1) };
let (mut body, _) = self.request(OpCode::Reconfig, &request).await?;
let mut buf = body.as_slice();
let data: &str = record::unmarshal_entity(&"reconfig data", &mut buf)?;
let stat = record::unmarshal_entity(&"reconfig stat", &mut buf)?;
let data_len = data.len();
body.truncate(data_len + 4);
drop(body.drain(..4));
return Ok((body, stat));
}
}
#[derive(Clone, Debug)]
pub struct ClientBuilder {
timeout: Duration,
authes: Vec<AuthPacket>,
readonly: bool,
}
impl ClientBuilder {
pub fn new(session_timeout: Duration) -> ClientBuilder {
return ClientBuilder { timeout: session_timeout, authes: Default::default(), readonly: false };
}
pub fn with_readonly(&mut self, readonly: bool) -> &mut ClientBuilder {
self.readonly = readonly;
self
}
pub fn with_auth(&mut self, scheme: String, auth: Vec<u8>) -> &mut ClientBuilder {
self.authes.push(AuthPacket { scheme, auth });
self
}
pub async fn connect(&mut self, cluster: &str) -> Result<Client, ConnectError> {
let (hosts, root) = util::parse_connect_string(cluster)?;
let mut buf = Vec::with_capacity(4096);
let mut connecting_state = OperationState::for_connecting();
let authes = std::mem::take(&mut self.authes);
let (mut session, state_receiver) = Session::new(self.timeout, authes, self.readonly);
let mut hosts_iter = hosts.iter().copied();
let sock = match session.start(&mut hosts_iter, &mut buf, &mut connecting_state).await {
Ok(sock) => sock,
Err(err) => {
self.authes = std::mem::take(&mut session.authes);
return Err(ConnectError::from(err));
},
};
let (sender, receiver) = mpsc::channel(512);
let (auth_sender, auth_receiver) = mpsc::channel(10);
let servers = hosts.into_iter().map(|addr| addr.to_value()).collect();
let session_info = (session.session_id, session.session_password.clone());
let session_timeout = session.session_timeout;
tokio::spawn(async move {
session.serve(servers, sock, buf, connecting_state, receiver, auth_receiver).await;
});
let client = Client::new(root.to_string(), session_info, session_timeout, sender, auth_sender, state_receiver);
return Ok(client);
}
}