dnet-utils 0.1.1

Utilities for dnet
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
//! Pipe messages from one transport into another and vice versa.

use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

use dportable::{spawn, value::Notifier};
use futures::{channel::oneshot, future::FusedFuture, select, FutureExt, Sink, SinkExt, StreamExt};

use crate::ConditionalSend;

/// Helper trait for transports that can be piped.
pub trait Transport<Incoming, Outgoing, Error>:
    dnet_base::Transport<Incoming, Outgoing, Error> + ConditionalSend + Unpin + 'static
{
}
impl<T, Incoming, Outgoing, Error> Transport<Incoming, Outgoing, Error> for T where
    T: dnet_base::Transport<Incoming, Outgoing, Error> + ConditionalSend + Unpin + 'static
{
}

/// Helper trait for pipe-able transport message.
pub trait Message: Clone + ConditionalSend + 'static {}
impl<T> Message for T where T: Clone + ConditionalSend + 'static {}

/// Helper trait for pipe-able transport error.
pub trait Error: ConditionalSend + 'static {}
impl<T> Error for T where T: ConditionalSend + 'static {}

/// Strategy of handling message passing errors.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub enum ErrorHandlingStrategy {
    /// Ignore the error.
    #[default]
    Ignore,

    /// Try again to send/receive message.
    Retry,

    /// Close pipe.
    Close,
}

/// Callback called when an error is encountered while trying to send a message
/// through the transport.
///
/// **NOTE**: it also receives [dnet_base::Error::Closed] errors and
/// they need to be handled properly (most likely by returning
/// [ErrorHandlingStrategy::Close]).
pub trait SendErrorCallback<Message, Error>: ConditionalSend + 'static {
    /// Handle sending error.
    ///
    /// **NOTE**: it also receives [dnet_base::Error::Closed] errors and
    /// they need to be handled properly (most likely by returning
    /// [ErrorHandlingStrategy::Close]).
    fn on_send_error(
        &mut self,
        message: &Message,
        error: dnet_base::Error<Error>,
    ) -> ErrorHandlingStrategy;
}

impl<T, Message, Error> SendErrorCallback<Message, Error> for T
where
    T: FnMut(&Message, dnet_base::Error<Error>) -> ErrorHandlingStrategy
        + ConditionalSend
        + 'static,
{
    fn on_send_error(
        &mut self,
        message: &Message,
        error: dnet_base::Error<Error>,
    ) -> ErrorHandlingStrategy {
        (self)(message, error)
    }
}

/// Callback called when an error is encountered while
/// trying to receive a message from the transport.
pub trait ReceiveErrorCallback<Error>: ConditionalSend + 'static {
    /// Handle receiving error.
    fn on_receive_error(&mut self, error: Error) -> ErrorHandlingStrategy;
}

impl<T, Error> ReceiveErrorCallback<Error> for T
where
    T: FnMut(Error) -> ErrorHandlingStrategy + ConditionalSend + 'static,
{
    fn on_receive_error(&mut self, error: Error) -> ErrorHandlingStrategy {
        (self)(error)
    }
}

/// Default [SendErrorCallback].
///
/// It ignores errors (except [dnet_base::Error::Closed] error - which results in
/// closing pipe).
#[derive(Debug)]
pub struct DefaultSendErrorCallback;

impl<Message, Error> SendErrorCallback<Message, Error> for DefaultSendErrorCallback {
    fn on_send_error(
        &mut self,
        _message: &Message,
        error: dnet_base::Error<Error>,
    ) -> ErrorHandlingStrategy {
        match error {
            dnet_base::Error::Closed => ErrorHandlingStrategy::Close,
            dnet_base::Error::Other(_) => ErrorHandlingStrategy::Ignore,
        }
    }
}

/// Default [ReceiveErrorCallback].
///
/// It ignores errors.
#[derive(Debug)]
pub struct DefaultReceiveErrorCallback;

impl<Error> ReceiveErrorCallback<Error> for DefaultReceiveErrorCallback {
    fn on_receive_error(&mut self, _error: Error) -> ErrorHandlingStrategy {
        ErrorHandlingStrategy::Ignore
    }
}

/// Pipe message passing error handler.
pub struct ErrorHandler<Message, Error> {
    /// Callback called when an error is encountered while trying to send a message
    /// through the transport.
    pub send_error_callback: Box<dyn SendErrorCallback<Message, Error>>,

    /// Callback called when an error is encountered while trying to receive a message
    /// from the transport.
    pub receive_error_callback: Box<dyn ReceiveErrorCallback<Error>>,
}

impl<Message, Error> ErrorHandler<Message, Error> {
    /// Create new error handler.
    pub fn new<S, R>(send_error_callback: S, receive_error_callback: R) -> Self
    where
        S: SendErrorCallback<Message, Error>,
        R: ReceiveErrorCallback<Error>,
    {
        ErrorHandler {
            send_error_callback: Box::new(send_error_callback),
            receive_error_callback: Box::new(receive_error_callback),
        }
    }
}

impl<Message, Error> Default for ErrorHandler<Message, Error> {
    fn default() -> Self {
        ErrorHandler::new(DefaultSendErrorCallback, DefaultReceiveErrorCallback)
    }
}

/// Pipe sending messages from one transport into another and vice versa.
#[derive(Debug)]
pub struct Pipe {
    stop_sender: Option<oneshot::Sender<()>>,
    keep_open: bool,
    closed: Notifier,
}

impl Pipe {
    /// Create new pipe sending messages from one transport into another and vice versa.
    pub fn new<A, B, M1, M2, E1, E2>(
        a: A,
        b: B,
        mut a_error_handler: ErrorHandler<M2, E1>,
        mut b_error_handler: ErrorHandler<M1, E2>,
    ) -> Self
    where
        A: Transport<M1, M2, E1> + Unpin,
        B: Transport<M2, M1, E2> + Unpin,
        M1: Message,
        M2: Message,
        E1: Error,
        E2: Error,
    {
        let (stop_sender, mut stop_receiver) = oneshot::channel();
        let stop_sender = Some(stop_sender);
        let closed = Notifier::new();
        let closed_clone = closed.clone();
        spawn(async move {
            let (mut sender_a, receiver_a) = a.split();
            let mut receiver_a = receiver_a.fuse();
            let (mut sender_b, receiver_b) = b.split();
            let mut receiver_b = receiver_b.fuse();
            let mut should_close = false;
            loop {
                select! {
                    a = receiver_a.next() => {
                        handle_receive_result(
                            &mut sender_b,
                            a,
                            &mut a_error_handler.receive_error_callback,
                            &mut b_error_handler.send_error_callback,
                            &mut should_close
                        ).await;
                    }
                    b = receiver_b.next() => {
                        handle_receive_result(
                            &mut sender_a,
                            b,
                            &mut b_error_handler.receive_error_callback,
                            &mut a_error_handler.send_error_callback,
                            &mut should_close
                        ).await;
                    }
                    result = stop_receiver => {
                        if result.is_ok() {
                            should_close = true
                        }
                    }
                }
                if should_close {
                    break;
                }
            }
            closed_clone.notify();
        });
        Pipe {
            stop_sender,
            keep_open: false,
            closed,
        }
    }

    /// Is pipe still open.
    pub fn open(&self) -> bool {
        !self.closed.already_notified()
    }

    /// Stop message interchange.
    pub fn break_pipe(mut self) {
        self.keep_open = false;
        // drop(self)
    }

    /// Keep pipe open after drop.
    pub fn keep_open(&mut self) {
        self.keep_open = true;
    }
}

impl Drop for Pipe {
    fn drop(&mut self) {
        if !self.keep_open {
            if let Some(sender) = self.stop_sender.take() {
                let _ = sender.send(());
            }
        }
    }
}

impl Future for Pipe {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        self.closed.poll_unpin(cx)
    }
}

impl FusedFuture for Pipe {
    fn is_terminated(&self) -> bool {
        self.closed.is_terminated()
    }
}

/// Pipe messages from one transport into another and vice versa.
///
/// Pipe created with this function ignores errors.
pub fn pipe<A, B, M1, M2, E1, E2>(a: A, b: B) -> Pipe
where
    A: Transport<M1, M2, E1> + Unpin,
    B: Transport<M2, M1, E2> + Unpin,
    M1: Message,
    M2: Message,
    E1: Error,
    E2: Error,
{
    Pipe::new(a, b, Default::default(), Default::default())
}

async fn handle_receive_result<S, M, ER, ES>(
    sender: &mut S,
    result: Option<Result<M, ER>>,
    receive_error_callback: &mut Box<dyn ReceiveErrorCallback<ER>>,
    send_error_callback: &mut Box<dyn SendErrorCallback<M, ES>>,
    should_close: &mut bool,
) where
    S: Sink<M, Error = dnet_base::Error<ES>> + Unpin,
    M: Message,
    ES: Error,
    ER: Error,
{
    if let Some(result) = result {
        match result {
            Ok(message) => {
                send(sender, message, send_error_callback, should_close).await;
            }
            Err(error) => {
                let strategy = receive_error_callback.on_receive_error(error);
                if matches!(strategy, ErrorHandlingStrategy::Close) {
                    *should_close = true;
                }
            }
        }
    } else {
        *should_close = true;
    }
}

async fn send<S, M, E>(
    sender: &mut S,
    message: M,
    send_error_callback: &mut Box<dyn SendErrorCallback<M, E>>,
    should_close: &mut bool,
) where
    S: Sink<M, Error = dnet_base::Error<E>> + Unpin,
    M: Message,
    E: Error,
{
    while let Err(error) = sender.send(message.clone()).await {
        match send_error_callback.on_send_error(&message, error) {
            ErrorHandlingStrategy::Ignore => {
                break;
            }
            ErrorHandlingStrategy::Retry => {
                continue;
            }
            ErrorHandlingStrategy::Close => {
                *should_close = true;
                return;
            }
        }
    }
    *should_close = false;
}

#[cfg(test)]
mod tests {
    use dnet_base::Receive;
    use dnet_tests::{dtest, dtest_configure};
    use futures::SinkExt;

    use crate::channel::{transports, ChannelTransport};

    use super::{pipe, Message, Pipe};

    dtest_configure!();

    fn create_transports<A, B>() -> (ChannelTransport<A, B>, ChannelTransport<B, A>, Pipe)
    where
        A: Message,
        B: Message,
    {
        let (out_a, to_pipe_a) = transports();
        let (out_b, to_pipe_b) = transports();
        let pipe = pipe(to_pipe_a, to_pipe_b);
        (out_a, out_b, pipe)
    }

    #[dtest]
    async fn test_transport() {
        let (left, right, _pipe) = create_transports();
        dnet_tests::test_transport(left, right).await;
    }

    #[dtest]
    async fn test_unit_message() {
        let (left, right, _pipe) = create_transports();
        dnet_tests::test_unit_message(left, right).await;
    }

    #[dtest]
    async fn test_stream() {
        let (left, right, _pipe) = create_transports();
        dnet_tests::test_stream(left, right).await;
    }

    #[dtest]
    async fn test_pipe_drop() {
        let (mut left, mut right, pipe) = create_transports();

        dnet_tests::init_logging(&mut left, &mut right);

        left.send(1).await.unwrap();
        right.send(1).await.unwrap();
        assert_eq!(left.receive().await.unwrap(), 1);
        assert_eq!(right.receive().await.unwrap(), 1);
        drop(pipe);
        assert!(matches!(
            left.receive().await,
            Err(dnet_base::Error::Closed)
        ));
        assert!(matches!(
            right.receive().await,
            Err(dnet_base::Error::Closed)
        ));
    }
}