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
use std::{
    collections::{HashMap, VecDeque},
    io::Result,
    sync::Arc,
};

use async_trait::async_trait;
use futures::{lock::Mutex, stream::unfold, Stream};
use futures_map::KeyWaitMap;
use rasi::task::spawn_ok;

use crate::{driver_wrapper, global_switch, Error, ProtocolStream, Switch, XStackId};

/// A libp2p protocol dispather driver must implement the `Driver-*` traits in this module.
pub mod stream_syscall {
    use std::io::Result;

    use async_trait::async_trait;

    use crate::ProtocolStream;

    use super::XStackId;

    #[async_trait]
    pub trait DriverStreamDispatcher: Sync + Send {
        async fn bind(&self, id: XStackId, protocols: &[String]) -> Result<()>;

        async fn close(&self, id: XStackId);

        async fn accept(&self, id: XStackId) -> Result<(ProtocolStream, String)>;

        async fn handshake(&self, conn_id: &str);

        async fn handshake_success(&self, conn_id: &str);
        async fn handshake_failed(&self, conn_id: &str);

        async fn dispatch(&self, stream: ProtocolStream, protocol_id: String);

        async fn protos(&self) -> Vec<String>;
    }
}

driver_wrapper!(
    ["A type wrapper of [`DriverStreamDispatcher`](stream_syscall::DriverStreamDispatcher)"]
    StreamDispatcher[stream_syscall::DriverStreamDispatcher]
);

/// A listener of protocol streams.
pub struct ProtocolListener {
    switch: Switch,
    id: XStackId,
}

impl ProtocolListener {
    pub fn new(switch: Switch, id: XStackId) -> Self {
        Self { switch, id }
    }

    #[cfg(feature = "global_register")]
    #[cfg_attr(docsrs, doc(cfg(feature = "global_register")))]
    pub async fn bind<I>(protocols: I) -> crate::Result<Self>
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
    {
        Self::bind_with(global_switch(), protocols).await
    }
    pub async fn bind_with<I>(switch: &Switch, protocols: I) -> crate::Result<Self>
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
    {
        switch.bind(protocols).await
    }

    /// Create a listener closer.
    pub fn to_closer(&self) -> ProtocolListenerCloser {
        ProtocolListenerCloser {
            switch: self.switch.clone(),
            id: self.id,
        }
    }

    pub async fn accept(&self) -> Result<(ProtocolStream, String)> {
        self.switch.stream_dispatcher.accept(self.id).await
    }

    pub fn into_incoming(self) -> impl Stream<Item = Result<(ProtocolStream, String)>> + Unpin {
        Box::pin(unfold(self, |listener| async move {
            let res = listener.accept().await;
            Some((res, listener))
        }))
    }
}

impl Drop for ProtocolListener {
    fn drop(&mut self) {
        let closer = self.to_closer();

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

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

impl ProtocolListenerCloser {
    pub async fn close(&self) {
        self.switch.stream_dispatcher.close(self.id).await;
    }
}

#[derive(Default)]
struct RawMutexStreamDispatcher {
    is_closed: bool,
    inbound_streams: HashMap<XStackId, VecDeque<(ProtocolStream, String)>>,
    protos: HashMap<String, XStackId>,
    unauth_streams: HashMap<String, Vec<(ProtocolStream, String)>>,
}

/// The default implementation of [`StreamDispatcher`]
#[derive(Default)]
pub struct MutexStreamDispatcher {
    raw: Arc<Mutex<RawMutexStreamDispatcher>>,
    event_map: Arc<KeyWaitMap<XStackId, ()>>,
}

impl Drop for MutexStreamDispatcher {
    fn drop(&mut self) {
        let raw = self.raw.clone();
        let event_map = self.event_map.clone();
        spawn_ok(async move {
            raw.lock().await.is_closed = true;
            event_map.cancel_all();
        });
    }
}

#[async_trait]
impl stream_syscall::DriverStreamDispatcher for MutexStreamDispatcher {
    async fn handshake(&self, conn_id: &str) {
        self.raw
            .lock()
            .await
            .unauth_streams
            .insert(conn_id.to_owned(), vec![]);
    }

    async fn handshake_success(&self, conn_id: &str) {
        let streams = self.raw.lock().await.unauth_streams.remove(conn_id);

        if let Some(streams) = streams {
            for (stream, protocol_id) in streams {
                self.dispatch(stream, protocol_id).await;
            }
        }
    }
    async fn handshake_failed(&self, conn_id: &str) {
        let streams = self.raw.lock().await.unauth_streams.remove(conn_id);

        if let Some(streams) = streams {
            for (stream, protocol_id) in streams {
                log::warn!(
                    "drop stream from={} protocol_id={}",
                    stream.peer_addr(),
                    protocol_id
                );
            }
        }
    }
    async fn bind(&self, id: XStackId, protocols: &[String]) -> Result<()> {
        let mut raw = self.raw.lock().await;

        if raw.is_closed {
            return Err(std::io::Error::new(
                std::io::ErrorKind::BrokenPipe,
                "dispatcher is drainning",
            ));
        }

        for proto in protocols {
            if raw.protos.contains_key(proto) {
                return Err(Error::ProtocolListenerBind(proto.to_owned()).into());
            }
        }

        for proto in protocols {
            raw.protos.insert(proto.to_owned(), id);
        }

        raw.inbound_streams.insert(id, Default::default());

        Ok(())
    }

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

        let keys = raw
            .protos
            .iter()
            .filter_map(|(proto, value)| {
                if *value == id {
                    Some(proto.clone())
                } else {
                    None
                }
            })
            .collect::<Vec<_>>();

        for key in keys {
            raw.protos.remove(&key);
        }

        raw.inbound_streams.remove(&id);

        drop(raw);

        self.event_map.insert(id, ());
    }

    async fn accept(&self, id: XStackId) -> Result<(ProtocolStream, String)> {
        loop {
            let mut raw = self.raw.lock().await;

            if let Some(streams) = raw.inbound_streams.get_mut(&id) {
                if let Some(stream) = streams.pop_front() {
                    return Ok(stream);
                }
            } else {
                return Err(Error::ProtocolListener(id.0).into());
            }

            self.event_map.wait(&id, raw).await;
        }
    }

    async fn dispatch(&self, stream: ProtocolStream, protocol_id: String) {
        let mut raw = self.raw.lock().await;

        if let Some(cached) = raw.unauth_streams.get_mut(stream.conn_id()) {
            cached.push((stream, protocol_id));
            return;
        }

        if let Some(id) = raw.protos.get(&protocol_id).map(|id| id.clone()) {
            raw.inbound_streams
                .get_mut(&id)
                .expect("Consistency Guarantee")
                .push_back((stream, protocol_id));

            drop(raw);

            self.event_map.insert(id, ());
        } else {
            log::warn!(
                "drop stream from={}, unhandled protocol={}",
                stream.peer_addr(),
                protocol_id
            );
        }
    }

    async fn protos(&self) -> Vec<String> {
        self.raw.lock().await.protos.keys().cloned().collect()
    }
}