sfo-cmd-server 0.3.2

command server implement
Documentation
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
mod client;

use bucky_raw_codec::{RawDecode, RawEncode, RawFixedBytes};
use callback_result::CallbackWaiter;
pub use client::*;
use num::{FromPrimitive, ToPrimitive};
use sfo_pool::{
    ClassifiedWorker, ClassifiedWorkerFactory, ClassifiedWorkerGuard, WorkerClassification,
};
use std::collections::{HashMap, VecDeque};
use std::hash::Hash;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::sync::Notify;

mod classified_client;
pub use classified_client::*;

use crate::errors::CmdResult;
use crate::{CmdBody, CmdHandler, CmdTunnelMeta, PeerId, TunnelId};

pub trait CmdSend<M: CmdTunnelMeta>: Send + 'static {
    fn get_tunnel_meta(&self) -> Option<Arc<M>>;
    fn get_remote_peer_id(&self) -> PeerId;
}

pub trait SendGuard<M: CmdTunnelMeta, S: CmdSend<M>>: Send + 'static + Deref<Target = S> {}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum TunnelBorrowState {
    Idle,
    Borrowed,
}

struct TunnelRuntimeEntry {
    state: TunnelBorrowState,
    waiters: VecDeque<Arc<Notify>>,
}

pub(crate) enum TunnelReserveResult {
    Acquired,
    Wait(Arc<Notify>),
    Missing,
}

#[derive(Default)]
pub(crate) struct TunnelRuntimeRegistry {
    state: Mutex<HashMap<TunnelId, TunnelRuntimeEntry>>,
}

impl TunnelRuntimeRegistry {
    pub(crate) fn new() -> Arc<Self> {
        Arc::new(Self::default())
    }

    pub(crate) fn reserve_existing(&self, tunnel_id: TunnelId) -> TunnelReserveResult {
        let mut state = self.state.lock().unwrap();
        match state.get_mut(&tunnel_id) {
            Some(entry) => match entry.state {
                TunnelBorrowState::Idle => {
                    entry.state = TunnelBorrowState::Borrowed;
                    TunnelReserveResult::Acquired
                }
                TunnelBorrowState::Borrowed => {
                    let notify = Arc::new(Notify::new());
                    entry.waiters.push_back(notify.clone());
                    TunnelReserveResult::Wait(notify)
                }
            },
            None => TunnelReserveResult::Missing,
        }
    }

    pub(crate) fn mark_borrowed(&self, tunnel_id: TunnelId) -> bool {
        let mut state = self.state.lock().unwrap();
        match state.get_mut(&tunnel_id) {
            Some(entry) => match entry.state {
                TunnelBorrowState::Idle => {
                    entry.state = TunnelBorrowState::Borrowed;
                    true
                }
                TunnelBorrowState::Borrowed => false,
            },
            None => {
                state.insert(
                    tunnel_id,
                    TunnelRuntimeEntry {
                        state: TunnelBorrowState::Borrowed,
                        waiters: VecDeque::new(),
                    },
                );
                true
            }
        }
    }

    pub(crate) fn remove(&self, tunnel_id: TunnelId) {
        let waiters = {
            let mut state = self.state.lock().unwrap();
            state
                .remove(&tunnel_id)
                .map(|entry| entry.waiters.into_iter().collect::<Vec<_>>())
                .unwrap_or_default()
        };
        for waiter in waiters {
            waiter.notify_one();
        }
    }

    pub(crate) fn release(&self, tunnel_id: TunnelId, alive: bool) {
        let (waiter, waiters) = {
            let mut state = self.state.lock().unwrap();
            match state.get_mut(&tunnel_id) {
                Some(entry) if alive => {
                    entry.state = TunnelBorrowState::Idle;
                    (entry.waiters.pop_front(), Vec::new())
                }
                Some(_) => {
                    let entry = state.remove(&tunnel_id).unwrap();
                    (None, entry.waiters.into_iter().collect::<Vec<_>>())
                }
                None => (None, Vec::new()),
            }
        };
        if let Some(waiter) = waiter {
            waiter.notify_one();
        }
        for waiter in waiters {
            waiter.notify_one();
        }
    }

    pub(crate) fn clear(&self) {
        let waiters = {
            let mut state = self.state.lock().unwrap();
            state
                .drain()
                .flat_map(|(_, entry)| entry.waiters.into_iter())
                .collect::<Vec<_>>()
        };
        for waiter in waiters {
            waiter.notify_one();
        }
    }
}

pub struct TrackedSendGuard<
    C: WorkerClassification,
    M: CmdTunnelMeta,
    S: ClassifiedWorker<C> + CmdSend<M>,
    F: ClassifiedWorkerFactory<C, S>,
> {
    worker_guard: Option<ClassifiedWorkerGuard<C, S, F>>,
    runtime_tunnels: Arc<TunnelRuntimeRegistry>,
    tunnel_id: TunnelId,
    _p: PhantomData<M>,
}

impl<
    C: WorkerClassification,
    M: CmdTunnelMeta,
    S: ClassifiedWorker<C> + CmdSend<M>,
    F: ClassifiedWorkerFactory<C, S>,
> TrackedSendGuard<C, M, S, F>
{
    pub(crate) fn new(
        worker_guard: ClassifiedWorkerGuard<C, S, F>,
        runtime_tunnels: Arc<TunnelRuntimeRegistry>,
        tunnel_id: TunnelId,
    ) -> Self {
        Self {
            worker_guard: Some(worker_guard),
            runtime_tunnels,
            tunnel_id,
            _p: PhantomData,
        }
    }
}

impl<
    C: WorkerClassification,
    M: CmdTunnelMeta,
    S: ClassifiedWorker<C> + CmdSend<M>,
    F: ClassifiedWorkerFactory<C, S>,
> Deref for TrackedSendGuard<C, M, S, F>
{
    type Target = S;

    fn deref(&self) -> &Self::Target {
        self.worker_guard.as_ref().unwrap().deref()
    }
}

impl<
    C: WorkerClassification,
    M: CmdTunnelMeta,
    S: ClassifiedWorker<C> + CmdSend<M>,
    F: ClassifiedWorkerFactory<C, S>,
> DerefMut for TrackedSendGuard<C, M, S, F>
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.worker_guard.as_mut().unwrap().deref_mut()
    }
}

impl<
    C: WorkerClassification,
    M: CmdTunnelMeta,
    S: ClassifiedWorker<C> + CmdSend<M>,
    F: ClassifiedWorkerFactory<C, S>,
> Drop for TrackedSendGuard<C, M, S, F>
{
    fn drop(&mut self) {
        if let Some(worker_guard) = self.worker_guard.take() {
            let alive = worker_guard.is_work();
            drop(worker_guard);
            self.runtime_tunnels.release(self.tunnel_id, alive);
        }
    }
}

impl<
    C: WorkerClassification,
    M: CmdTunnelMeta,
    S: ClassifiedWorker<C> + CmdSend<M>,
    F: ClassifiedWorkerFactory<C, S>,
> SendGuard<M, S> for TrackedSendGuard<C, M, S, F>
{
}

#[async_trait::async_trait]
pub trait CmdClient<
    LEN: RawEncode
        + for<'a> RawDecode<'a>
        + Copy
        + RawFixedBytes
        + Sync
        + Send
        + 'static
        + FromPrimitive
        + ToPrimitive,
    CMD: RawEncode + for<'a> RawDecode<'a> + Copy + RawFixedBytes + Sync + Send + 'static + Eq + Hash,
    M: CmdTunnelMeta,
    S: CmdSend<M>,
    G: SendGuard<M, S>,
>: Send + Sync + 'static
{
    fn register_cmd_handler(&self, cmd: CMD, handler: impl CmdHandler<LEN, CMD>);
    async fn send(&self, cmd: CMD, version: u8, body: &[u8]) -> CmdResult<()>;
    async fn send_with_resp(
        &self,
        cmd: CMD,
        version: u8,
        body: &[u8],
        timeout: Duration,
    ) -> CmdResult<CmdBody>;
    async fn send_parts(&self, cmd: CMD, version: u8, body: &[&[u8]]) -> CmdResult<()>;
    async fn send_parts_with_resp(
        &self,
        cmd: CMD,
        version: u8,
        body: &[&[u8]],
        timeout: Duration,
    ) -> CmdResult<CmdBody>;
    #[deprecated(note = "use send_parts instead")]
    async fn send2(&self, cmd: CMD, version: u8, body: &[&[u8]]) -> CmdResult<()> {
        self.send_parts(cmd, version, body).await
    }
    #[deprecated(note = "use send_parts_with_resp instead")]
    async fn send2_with_resp(
        &self,
        cmd: CMD,
        version: u8,
        body: &[&[u8]],
        timeout: Duration,
    ) -> CmdResult<CmdBody> {
        self.send_parts_with_resp(cmd, version, body, timeout).await
    }
    async fn send_cmd(&self, cmd: CMD, version: u8, body: CmdBody) -> CmdResult<()>;
    async fn send_cmd_with_resp(
        &self,
        cmd: CMD,
        version: u8,
        body: CmdBody,
        timeout: Duration,
    ) -> CmdResult<CmdBody>;
    async fn send_by_specify_tunnel(
        &self,
        tunnel_id: TunnelId,
        cmd: CMD,
        version: u8,
        body: &[u8],
    ) -> CmdResult<()>;
    async fn send_by_specify_tunnel_with_resp(
        &self,
        tunnel_id: TunnelId,
        cmd: CMD,
        version: u8,
        body: &[u8],
        timeout: Duration,
    ) -> CmdResult<CmdBody>;
    async fn send_parts_by_specify_tunnel(
        &self,
        tunnel_id: TunnelId,
        cmd: CMD,
        version: u8,
        body: &[&[u8]],
    ) -> CmdResult<()>;
    async fn send_parts_by_specify_tunnel_with_resp(
        &self,
        tunnel_id: TunnelId,
        cmd: CMD,
        version: u8,
        body: &[&[u8]],
        timeout: Duration,
    ) -> CmdResult<CmdBody>;
    #[deprecated(note = "use send_parts_by_specify_tunnel instead")]
    async fn send2_by_specify_tunnel(
        &self,
        tunnel_id: TunnelId,
        cmd: CMD,
        version: u8,
        body: &[&[u8]],
    ) -> CmdResult<()> {
        self.send_parts_by_specify_tunnel(tunnel_id, cmd, version, body)
            .await
    }
    #[deprecated(note = "use send_parts_by_specify_tunnel_with_resp instead")]
    async fn send2_by_specify_tunnel_with_resp(
        &self,
        tunnel_id: TunnelId,
        cmd: CMD,
        version: u8,
        body: &[&[u8]],
        timeout: Duration,
    ) -> CmdResult<CmdBody> {
        self.send_parts_by_specify_tunnel_with_resp(tunnel_id, cmd, version, body, timeout)
            .await
    }
    async fn send_cmd_by_specify_tunnel(
        &self,
        tunnel_id: TunnelId,
        cmd: CMD,
        version: u8,
        body: CmdBody,
    ) -> CmdResult<()>;
    async fn send_cmd_by_specify_tunnel_with_resp(
        &self,
        tunnel_id: TunnelId,
        cmd: CMD,
        version: u8,
        body: CmdBody,
        timeout: Duration,
    ) -> CmdResult<CmdBody>;
    async fn clear_all_tunnel(&self);
    async fn get_send(&self, tunnel_id: TunnelId) -> CmdResult<G>;
}

#[async_trait::async_trait]
pub trait ClassifiedCmdClient<
    LEN: RawEncode
        + for<'a> RawDecode<'a>
        + Copy
        + RawFixedBytes
        + Sync
        + Send
        + 'static
        + FromPrimitive
        + ToPrimitive,
    CMD: RawEncode + for<'a> RawDecode<'a> + Copy + RawFixedBytes + Sync + Send + 'static + Eq + Hash,
    C: WorkerClassification,
    M: CmdTunnelMeta,
    S: CmdSend<M>,
    G: SendGuard<M, S>,
>: CmdClient<LEN, CMD, M, S, G>
{
    async fn send_by_classified_tunnel(
        &self,
        classification: C,
        cmd: CMD,
        version: u8,
        body: &[u8],
    ) -> CmdResult<()>;
    async fn send_by_classified_tunnel_with_resp(
        &self,
        classification: C,
        cmd: CMD,
        version: u8,
        body: &[u8],
        timeout: Duration,
    ) -> CmdResult<CmdBody>;
    async fn send_parts_by_classified_tunnel(
        &self,
        classification: C,
        cmd: CMD,
        version: u8,
        body: &[&[u8]],
    ) -> CmdResult<()>;
    async fn send_parts_by_classified_tunnel_with_resp(
        &self,
        classification: C,
        cmd: CMD,
        version: u8,
        body: &[&[u8]],
        timeout: Duration,
    ) -> CmdResult<CmdBody>;
    #[deprecated(note = "use send_parts_by_classified_tunnel instead")]
    async fn send2_by_classified_tunnel(
        &self,
        classification: C,
        cmd: CMD,
        version: u8,
        body: &[&[u8]],
    ) -> CmdResult<()> {
        self.send_parts_by_classified_tunnel(classification, cmd, version, body)
            .await
    }
    #[deprecated(note = "use send_parts_by_classified_tunnel_with_resp instead")]
    async fn send2_by_classified_tunnel_with_resp(
        &self,
        classification: C,
        cmd: CMD,
        version: u8,
        body: &[&[u8]],
        timeout: Duration,
    ) -> CmdResult<CmdBody> {
        self.send_parts_by_classified_tunnel_with_resp(classification, cmd, version, body, timeout)
            .await
    }
    async fn send_cmd_by_classified_tunnel(
        &self,
        classification: C,
        cmd: CMD,
        version: u8,
        body: CmdBody,
    ) -> CmdResult<()>;
    async fn send_cmd_by_classified_tunnel_with_resp(
        &self,
        classification: C,
        cmd: CMD,
        version: u8,
        body: CmdBody,
        timeout: Duration,
    ) -> CmdResult<CmdBody>;
    async fn find_tunnel_id_by_classified(&self, classification: C) -> CmdResult<TunnelId>;
    async fn get_send_by_classified(&self, classification: C) -> CmdResult<G>;
}

pub(crate) type RespWaiter = CallbackWaiter<u128, CmdBody>;
pub(crate) type RespWaiterRef = Arc<RespWaiter>;

pub(crate) fn gen_resp_id<
    CMD: RawEncode + for<'a> RawDecode<'a> + Copy + RawFixedBytes + Sync + Send + 'static,
>(
    tunnel_id: TunnelId,
    cmd: CMD,
    seq: u32,
) -> u128 {
    let cmd_buf = cmd.raw_encode_to_buffer().unwrap();
    let mut cmd = cmd_buf.len() as u64;
    for chunk in cmd_buf.chunks(8) {
        let mut buf = [0u8; 8];
        buf[..chunk.len()].copy_from_slice(chunk);
        cmd = cmd.rotate_left(13) ^ u64::from_be_bytes(buf);
    }
    ((tunnel_id.value() as u128) << 96) | ((seq as u128) << 64) | (cmd as u128)
}

pub(crate) fn gen_seq() -> u32 {
    rand::random::<u32>()
}

#[cfg(test)]
mod tests {
    use super::gen_resp_id;
    use crate::TunnelId;

    #[test]
    fn resp_id_changes_with_seq() {
        let id1 = gen_resp_id(TunnelId::from(7), 0x11u8, 1);
        let id2 = gen_resp_id(TunnelId::from(7), 0x11u8, 2);
        assert_ne!(id1, id2);
    }

    #[test]
    fn resp_id_changes_with_cmd() {
        let id1 = gen_resp_id(TunnelId::from(7), 0x11u8, 5);
        let id2 = gen_resp_id(TunnelId::from(7), 0x12u8, 5);
        assert_ne!(id1, id2);
    }

    #[test]
    fn resp_id_changes_with_tunnel() {
        let id1 = gen_resp_id(TunnelId::from(7), 0x11u8, 5);
        let id2 = gen_resp_id(TunnelId::from(8), 0x11u8, 5);
        assert_ne!(id1, id2);
    }

    #[test]
    fn resp_id_changes_with_long_cmd_suffix() {
        let id1 = gen_resp_id(
            TunnelId::from(7),
            0x1122_3344_5566_7788_0000_0000_0000_0001u128,
            5,
        );
        let id2 = gen_resp_id(
            TunnelId::from(7),
            0x1122_3344_5566_7788_0000_0000_0000_0002u128,
            5,
        );
        assert_ne!(id1, id2);
    }
}