1use std::{
2 pin::Pin,
3 sync::Arc,
4 task::{Context, Poll},
5};
6
7use futures::{Stream, StreamExt, channel::mpsc};
8use parking_lot::Mutex;
9use volans_core::PeerId;
10use volans_swarm::{ConnectionId, StreamProtocol, Substream};
11
12mod behavior;
13mod handler;
14mod shared;
15
16pub use behavior::Behavior;
17pub use handler::Handler;
18
19#[derive(Debug, thiserror::Error)]
20#[error("The protocol is already registered")]
21pub struct AlreadyRegistered;
22
23pub struct IncomingStreams {
24 receiver: mpsc::Receiver<(PeerId, ConnectionId, Substream)>,
25}
26
27impl IncomingStreams {
28 pub(crate) fn new(receiver: mpsc::Receiver<(PeerId, ConnectionId, Substream)>) -> Self {
29 Self { receiver }
30 }
31}
32
33impl Stream for IncomingStreams {
34 type Item = (PeerId, ConnectionId, Substream);
35
36 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
37 self.receiver.poll_next_unpin(cx)
38 }
39}
40
41#[derive(Clone)]
42pub struct Acceptor {
43 shared: Arc<Mutex<shared::Shared>>,
44}
45
46impl Acceptor {
47 fn new(shared: Arc<Mutex<shared::Shared>>) -> Self {
48 Self { shared }
49 }
50
51 pub fn accept(
52 &mut self,
53 protocol: StreamProtocol,
54 ) -> Result<IncomingStreams, AlreadyRegistered> {
55 shared::Shared::lock(&self.shared).accept(protocol)
56 }
57}