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
use std::{collections::HashMap, marker::PhantomData, num::NonZeroUsize, task::Poll};

use async_trait::async_trait;
use futures::{
    channel::mpsc::{channel, Sender},
    lock::Mutex,
    Stream, StreamExt,
};
use libp2p_identity::PeerId;
use rasi::task::spawn_ok;

use crate::{driver_wrapper, AutoNAT, Switch, XStackId};

/// A xstack event mediator driver must implement the `Driver-*` traits in this module.
pub mod event_syscall {

    use std::num::NonZeroUsize;

    use async_trait::async_trait;
    use futures::Stream;

    use crate::XStackId;

    use super::{Event, EventStream};

    #[async_trait]
    pub trait DriverEventMediator: Sync + Send {
        /// Create new event receiver.
        async fn bind(&self, id: XStackId, event_name: &str, buffer: NonZeroUsize) -> EventStream;

        /// Close the event stream by id.
        async fn close(&self, id: &XStackId);

        /// Dispatch the event to the register listeners.
        async fn raise(&self, event: Event);
    }

    ///
    pub trait DriverEventStream: Stream<Item = Event> + Unpin {}

    impl<S> DriverEventStream for S where S: Stream<Item = Event> + Unpin {}
}

driver_wrapper!(
    ["A type wrapper of [`EventMediator`](event_syscall::DriverEventMediator)"]
    EventMediator[event_syscall::DriverEventMediator]
);

driver_wrapper!(
    ["A type wrapper of [`EventStream`](event_syscall::DriverEventStream)"]
    EventStream[event_syscall::DriverEventStream]
);

static EVENT_CONNECTED: &str = "/xstack/connected";
static EVENT_HANDSHAKE_SUCCESS: &str = "/xstack/handshake_success";
static EVENT_HANDSHAKE_FAILED: &str = "/xstack/handshake_failed";
static EVENT_NETWORK: &str = "/xstack/network";

/// A `Switch` event must implement this trait.
pub trait ToEventArgument {
    /// Event argument type.
    type Argument;

    fn name() -> &'static str;
    fn into_argument(event: Event) -> Self::Argument;
}

/// Variant of switch events.
#[derive(Debug, Clone)]
pub enum Event {
    /// The connection state changed.
    Connected {
        conn_id: String,
        peer_id: PeerId,
    },

    HandshakeSuccess {
        conn_id: String,
        peer_id: PeerId,
    },

    HandshakeFailed {
        conn_id: String,
        peer_id: PeerId,
    },

    /// The network state changed.
    Network(AutoNAT),
}

impl Event {
    /// Get the event name.
    pub fn to_name(&self) -> &'static str {
        match self {
            Event::Network(_) => &EVENT_NETWORK,
            Event::Connected {
                conn_id: _,
                peer_id: _,
            } => &EVENT_CONNECTED,
            Event::HandshakeSuccess {
                conn_id: _,
                peer_id: _,
            } => &EVENT_HANDSHAKE_SUCCESS,
            Event::HandshakeFailed {
                conn_id: _,
                peer_id: _,
            } => &EVENT_HANDSHAKE_FAILED,
        }
    }
}

/// events of switch.
pub mod events {
    use libp2p_identity::PeerId;

    use crate::AutoNAT;

    use super::{
        Event, ToEventArgument, EVENT_CONNECTED, EVENT_HANDSHAKE_FAILED, EVENT_HANDSHAKE_SUCCESS,
        EVENT_NETWORK,
    };

    pub struct Connected;

    impl ToEventArgument for Connected {
        type Argument = (String, PeerId);

        fn name() -> &'static str {
            &EVENT_CONNECTED
        }

        fn into_argument(event: super::Event) -> Self::Argument {
            match event {
                Event::Connected { conn_id, peer_id } => (conn_id, peer_id),
                _ => panic!("not here"),
            }
        }
    }

    pub struct HandshakeSuccess;

    impl ToEventArgument for HandshakeSuccess {
        type Argument = (String, PeerId);

        fn name() -> &'static str {
            &EVENT_HANDSHAKE_SUCCESS
        }

        fn into_argument(event: super::Event) -> Self::Argument {
            match event {
                Event::HandshakeSuccess { conn_id, peer_id } => (conn_id, peer_id),
                _ => panic!("not here"),
            }
        }
    }

    pub struct HandshakeFailed;

    impl ToEventArgument for HandshakeFailed {
        type Argument = (String, PeerId);

        fn name() -> &'static str {
            &EVENT_HANDSHAKE_FAILED
        }

        fn into_argument(event: super::Event) -> Self::Argument {
            match event {
                Event::HandshakeFailed { conn_id, peer_id } => (conn_id, peer_id),
                _ => panic!("not here"),
            }
        }
    }

    pub struct Network;

    impl ToEventArgument for Network {
        type Argument = AutoNAT;

        fn name() -> &'static str {
            &EVENT_NETWORK
        }

        fn into_argument(event: super::Event) -> Self::Argument {
            match event {
                Event::Network(value) => value,
                _ => panic!("not here"),
            }
        }
    }
}

pub struct EventSourceCloser {
    id: XStackId,
    switch: Switch,
}

impl EventSourceCloser {
    pub async fn close(&self) {
        self.switch.event_mediator.close(&self.id).await
    }
}

/// A [`stream`](futures::Stream) that accept a kined of event `E`
pub struct EventSource<E> {
    id: XStackId,
    switch: Switch,
    stream: EventStream,
    _marker: PhantomData<E>,
}

impl<E> Drop for EventSource<E> {
    fn drop(&mut self) {
        let closer = self.to_closer();

        spawn_ok(async move { closer.close().await })
    }
}

impl<E> Stream for EventSource<E>
where
    E: ToEventArgument + Unpin,
{
    type Item = E::Argument;

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Self::Item>> {
        match self.stream.poll_next_unpin(cx) {
            std::task::Poll::Ready(argument) => Poll::Ready(argument.map(E::into_argument)),
            std::task::Poll::Pending => Poll::Pending,
        }
    }
}

impl<E> EventSource<E> {
    /// Create a new closer for this `EventSource`
    pub fn to_closer(&self) -> EventSourceCloser {
        EventSourceCloser {
            id: self.id,
            switch: self.switch.clone(),
        }
    }
}

impl<E> EventSource<E>
where
    E: ToEventArgument,
{
    /// Create a new `EventSource` and bind to provides `switch`.
    pub async fn bind_with(switch: &Switch, buffer: NonZeroUsize) -> Self {
        let id = XStackId::default();

        let stream = switch.event_mediator.bind(id, E::name(), buffer).await;

        Self {
            id,
            switch: switch.clone(),
            stream,
            _marker: Default::default(),
        }
    }

    /// Create a new `EventSource` and bind to global context `switch`.
    #[cfg(feature = "global_register")]
    #[cfg_attr(docsrs, doc(cfg(feature = "global_register")))]
    pub async fn bind(buffer: NonZeroUsize) -> Self {
        use crate::global_switch;

        Self::bind_with(global_switch(), buffer).await
    }
}

/// The default [`EventMediator`] implementation, it guarantees that event messages will not be lost.
#[derive(Default)]
pub struct SyncEventMediator(Mutex<HashMap<String, HashMap<XStackId, Sender<Event>>>>);

#[async_trait]
impl event_syscall::DriverEventMediator for SyncEventMediator {
    async fn bind(&self, id: XStackId, event_name: &str, buffer: NonZeroUsize) -> EventStream {
        let (sender, receiver) = channel(buffer.into());

        let mut raw = self.0.lock().await;

        if let Some(senders) = raw.get_mut(event_name) {
            senders.insert(id, sender);
        } else {
            let mut map = HashMap::new();
            map.insert(id, sender);
            raw.insert(event_name.to_owned(), map);
        }

        receiver.into()
    }

    async fn raise(&self, event: Event) {
        let senders = self
            .0
            .lock()
            .await
            .get(event.to_name())
            .map(|senders| senders.clone());

        if let Some(senders) = senders {
            let mut disconnected = vec![];

            for (id, mut sender) in senders {
                log::trace!("raise.... {}, {:?}", id, event);

                if let Err(err) = sender.try_send(event.clone()) {
                    if err.is_disconnected() {
                        disconnected.push(id);
                    }

                    log::warn!("dispatch event {} full", event.to_name());
                }
            }

            let mut raw = self.0.lock().await;

            if let Some(map) = raw.get_mut(event.to_name()) {
                for id in disconnected {
                    map.remove(&id);
                }
            }
        }
    }

    async fn close(&self, id: &XStackId) {
        let mut raw = self.0.lock().await;

        for map in raw.values_mut() {
            if map.remove(id).is_some() {
                return;
            }
        }
    }
}