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
use std::{fmt::Display, marker::PhantomData};

use futures::{channel::mpsc, StreamExt};
use log::warn;

use crate::{
    network::{
        data::{Network as DataNetwork, SendError},
        Data,
    },
    Recipient,
};

/// For sending arbitrary messages.
pub trait Sender<D: Data>: Sync + Send + Clone {
    fn send(&self, data: D, recipient: Recipient) -> Result<(), SendError>;
}

#[derive(Clone)]
pub struct MapSender<D, S> {
    sender: S,
    _phantom: PhantomData<D>,
}

pub trait SenderMap<FromData: Data>: Sender<FromData> {
    fn map(self) -> MapSender<FromData, Self> {
        MapSender {
            sender: self,
            _phantom: PhantomData,
        }
    }
}

impl<D: Data, S: Sender<D>> SenderMap<D> for S {}

impl<D: Data, S: Sender<D>, IntoD: Data + Into<D>> Sender<IntoD> for MapSender<D, S> {
    fn send(&self, data: IntoD, recipient: Recipient) -> Result<(), SendError> {
        self.sender.send(data.into(), recipient)
    }
}

/// For receiving arbitrary messages.
#[async_trait::async_trait]
pub trait Receiver<D: Data>: Sync + Send {
    async fn next(&mut self) -> Option<D>;
}

pub struct MapReceiver<D, R> {
    receiver: R,
    _phantom: PhantomData<D>,
}

pub trait ReceiverMap<FromData: Data>: Receiver<FromData> + Sized {
    fn map(self) -> MapReceiver<FromData, Self> {
        MapReceiver {
            receiver: self,
            _phantom: PhantomData,
        }
    }
}

impl<D: Data, R: Receiver<D>> ReceiverMap<D> for R {}

#[async_trait::async_trait]
impl<D: Data, R: Receiver<D>, FromD: Data + TryFrom<D>> Receiver<FromD> for MapReceiver<D, R>
where
    FromD::Error: Display,
{
    async fn next(&mut self) -> Option<FromD> {
        loop {
            let data = self.receiver.next().await;
            let data = match data {
                Some(data) => data,
                None => return None,
            };
            match TryFrom::try_from(data) {
                Ok(message) => return Some(message),
                Err(e) => {
                    warn!(target: "phron-network", "Error decoding message in MapReceiver: {}", e)
                }
            }
        }
    }
}

/// A bare version of network components.
pub trait Network<D: Data>: Sync + Send {
    type S: Sender<D>;
    type R: Receiver<D>;

    fn into(self) -> (Self::S, Self::R);
}

pub trait NetworkExt<D: Data>: Network<D> + AsRef<Self::S> + AsMut<Self::R> {}

impl<D: Data, N: Network<D> + AsRef<N::S> + AsMut<N::R>> NetworkExt<D> for N {}

#[async_trait::async_trait]
impl<D: Data, N: NetworkExt<D>> DataNetwork<D> for N {
    fn send(&self, data: D, recipient: Recipient) -> Result<(), SendError> {
        self.as_ref().send(data, recipient)
    }

    async fn next(&mut self) -> Option<D> {
        self.as_mut().next().await
    }
}

pub trait NetworkMap<D: Data, IntoD: Data>: Network<D> {
    type MappedNetwork: Network<IntoD>;

    fn map(self) -> Self::MappedNetwork;
}

impl<D: Data, IntoD: Data + Into<D> + TryFrom<D>, N: Network<D>> NetworkMap<D, IntoD> for N
where
    IntoD::Error: Display,
{
    type MappedNetwork = SimpleNetwork<IntoD, MapReceiver<D, N::R>, MapSender<D, N::S>>;

    fn map(self) -> Self::MappedNetwork {
        let (sender, receiver) = self.into();
        SimpleNetwork::new(receiver.map(), sender.map())
    }
}

#[async_trait::async_trait]
impl<D: Data> Sender<D> for mpsc::UnboundedSender<(D, Recipient)> {
    fn send(&self, data: D, recipient: Recipient) -> Result<(), SendError> {
        self.unbounded_send((data, recipient))
            .map_err(|_| SendError::SendFailed)
    }
}

#[async_trait::async_trait]
impl<D: Data> Receiver<D> for mpsc::UnboundedReceiver<D> {
    async fn next(&mut self) -> Option<D> {
        StreamExt::next(self).await
    }
}

pub struct SimpleNetwork<D: Data, R: Receiver<D>, S: Sender<D>> {
    receiver: R,
    sender: S,
    _phantom: PhantomData<D>,
}

impl<D: Data, R: Receiver<D>, S: Sender<D>> SimpleNetwork<D, R, S> {
    pub fn new(receiver: R, sender: S) -> Self {
        SimpleNetwork {
            receiver,
            sender,
            _phantom: PhantomData,
        }
    }
}

impl<D: Data, R: Receiver<D>, S: Sender<D>> AsRef<S> for SimpleNetwork<D, R, S> {
    fn as_ref(&self) -> &S {
        &self.sender
    }
}

impl<D: Data, R: Receiver<D>, S: Sender<D>> AsMut<R> for SimpleNetwork<D, R, S> {
    fn as_mut(&mut self) -> &mut R {
        &mut self.receiver
    }
}

impl<D: Data, R: Receiver<D>, S: Sender<D>> Network<D> for SimpleNetwork<D, R, S> {
    type S = S;

    type R = R;

    fn into(self) -> (Self::S, Self::R) {
        (self.sender, self.receiver)
    }
}

#[cfg(test)]
mod tests {
    use futures::{
        channel::mpsc::{self, UnboundedReceiver, UnboundedSender},
        StreamExt,
    };
    use parity_scale_codec::{Decode, Encode};

    use super::{DataNetwork, NetworkMap, Receiver, Sender};
    use crate::{
        network::{
            data::{
                component::{Network, ReceiverMap, SenderMap},
                SendError,
            },
            Data,
        },
        Recipient,
    };

    #[tokio::test]
    async fn test_receiver_implementation() {
        let (sender, mut receiver) = mpsc::unbounded();

        let val = 1234;
        sender.unbounded_send(val).unwrap();
        let received = Receiver::<u64>::next(&mut receiver).await;
        assert_eq!(Some(val), received);
    }

    #[derive(Decode, Encode, Clone, PartialEq, Eq, Debug, Copy)]
    enum FromType {
        A,
        B,
    }

    #[derive(Decode, Encode, Clone, PartialEq, Eq, Debug, Copy)]
    struct IntoType {}

    impl TryFrom<FromType> for IntoType {
        type Error = &'static str;

        fn try_from(value: FromType) -> Result<Self, Self::Error> {
            match value {
                FromType::A => Ok(IntoType {}),
                FromType::B => Err("we support only convertion from the FromType::A"),
            }
        }
    }

    impl From<IntoType> for FromType {
        fn from(_value: IntoType) -> Self {
            FromType::A
        }
    }

    struct TestNetwork<D> {
        sender: TestSender<D>,
        receiver: TestReceiver<D>,
    }

    impl<D: Data> Network<D> for TestNetwork<D> {
        type S = TestSender<D>;
        type R = TestReceiver<D>;

        fn into(self) -> (Self::S, Self::R) {
            (self.sender, self.receiver)
        }
    }

    impl<D: Data> AsMut<<TestNetwork<D> as Network<D>>::R> for TestNetwork<D> {
        fn as_mut(&mut self) -> &mut <TestNetwork<D> as Network<D>>::R {
            &mut self.receiver
        }
    }

    impl<D: Data> AsRef<<TestNetwork<D> as Network<D>>::S> for TestNetwork<D> {
        fn as_ref(&self) -> &<TestNetwork<D> as Network<D>>::S {
            &self.sender
        }
    }

    #[derive(Clone)]
    struct TestSender<D>(UnboundedSender<D>);

    impl<D: Data> Sender<D> for TestSender<D> {
        fn send(&self, data: D, _: Recipient) -> Result<(), SendError> {
            self.0
                .unbounded_send(data)
                .map_err(|_| SendError::SendFailed)
        }
    }

    struct TestReceiver<D>(UnboundedReceiver<D>);

    #[async_trait::async_trait]
    impl<D: Data> Receiver<D> for TestReceiver<D> {
        async fn next(&mut self) -> Option<D> {
            StreamExt::next(&mut self.0).await
        }
    }

    #[tokio::test]
    async fn test_receiver_map_allows_to_receive_mapped_data() {
        let (sender, receiver) = mpsc::unbounded();
        let mut mapped_receiver = ReceiverMap::map(receiver);

        let from_data = FromType::A;
        let into_data = IntoType {};

        sender.unbounded_send(from_data).unwrap();

        let received = Receiver::next(&mut mapped_receiver).await;
        assert_eq!(Some(into_data), received);
    }

    #[tokio::test]
    async fn test_map_sender_allows_to_send_mapped_data() {
        let (sender, mut receiver) = mpsc::unbounded();
        let sender = TestSender(sender);

        let from_data = FromType::A;
        let into_data = IntoType {};
        let recipient = Recipient::Everyone;

        let mapped_sender = sender.map();
        mapped_sender.send(into_data, recipient).unwrap();

        let received = StreamExt::next(&mut receiver).await;
        assert_eq!(Some(from_data), received);
    }

    #[tokio::test]
    async fn test_mapped_receiver_only_returns_convertable_values() {
        let (sender, receiver) = mpsc::unbounded();
        let receiver = TestReceiver(receiver);

        let from_data = FromType::A;
        let into_data = IntoType {};

        sender.unbounded_send(FromType::B).unwrap();
        sender.unbounded_send(FromType::B).unwrap();
        sender.unbounded_send(from_data).unwrap();
        sender.close_channel();

        let mut mapped_receiver = ReceiverMap::<FromType>::map(receiver);

        let received = mapped_receiver.next().await;
        assert_eq!(Some(into_data), received);
        let received: Option<IntoType> = mapped_receiver.next().await;
        assert_eq!(None, received);
    }

    #[tokio::test]
    async fn test_mapped_networks_are_able_to_send_and_receive_data() {
        let (sender_for_network, receiver_for_other_network) = mpsc::unbounded();
        let (sender_for_other_network, receiver_for_network) = mpsc::unbounded();

        let mut network = TestNetwork {
            sender: TestSender(sender_for_network),
            receiver: TestReceiver(receiver_for_network),
        };
        let other_network = TestNetwork {
            sender: TestSender(sender_for_other_network),
            receiver: TestReceiver(receiver_for_other_network),
        };
        let mut mapped_network = other_network.map();

        let from_data = FromType::A;
        let into_data = IntoType {};
        let recipient = Recipient::Everyone;

        network.send(from_data, recipient.clone()).unwrap();
        let received = mapped_network.next().await;
        assert_eq!(Some(into_data), received);

        mapped_network.send(into_data, recipient).unwrap();
        let received = network.next().await;
        assert_eq!(Some(from_data), received);
    }
}