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
use super::BrokerShutdown;
#[cfg(feature = "statistics")]
use super::BrokerStatistics;
use crate::conn::{Connection, ConnectionEvent, ConnectionHandle, EstablishError};
use crate::conn_id::ConnectionIdManager;
use crate::core::message::{ConnectReply, Message};
use crate::core::transport::{AsyncTransport, AsyncTransportExt};
use crate::core::{
    Deserialize, DeserializeError, Serialize, SerializedValue, SerializedValueSlice,
};
use futures_channel::mpsc;
#[cfg(feature = "statistics")]
use futures_channel::oneshot;
use futures_util::sink::SinkExt;

/// Handle of an active broker.
///
/// `BrokerHandle`s are used to interact with an active [`Broker`](crate::Broker). The first
/// `BrokerHandle` can be acquired from the `Broker` with [`handle`](crate::Broker::handle), and
/// from then on, `BrokerHandle`s can be cloned cheaply.
///
/// The `Broker` will automatically shut down when the last `BrokerHandle` has been dropped and
/// while there are no active clients.
#[derive(Debug, Clone)]
pub struct BrokerHandle {
    send: mpsc::Sender<ConnectionEvent>,
    ids: ConnectionIdManager,
}

impl BrokerHandle {
    pub(crate) fn new(send: mpsc::Sender<ConnectionEvent>) -> BrokerHandle {
        BrokerHandle {
            send,
            ids: ConnectionIdManager::new(),
        }
    }

    /// Establishes a new connection.
    ///
    /// This method performs the initial connection setup and Aldrin handshake between broker and
    /// client. If successful, the resulting [`Connection`] must be [`run`](Connection::run) and
    /// polled to completion, much like the [`Broker`](crate::Broker) itself.
    ///
    /// The Aldrin protocol allows client and broker to exchange custom data during the
    /// handshake. This function will ignore the client's data and send `()` back. If you
    /// need to inspect the data and possibly reject some clients, then use
    /// [`begin_connect`](Self::begin_connect).
    ///
    /// # Examples
    ///
    /// ```
    /// # use aldrin_test::tokio::TestBroker;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // Create an AsyncTransport to a new incoming connection:
    /// // let t = ...
    ///
    /// # let mut broker_handle = TestBroker::new();
    /// # let (t, t2) = aldrin_broker::core::channel::unbounded();
    /// # let client_join = tokio::spawn(aldrin::Client::connect(t2));
    /// // Establish a connection to the client:
    /// let connection = broker_handle.connect(t).await?;
    ///
    /// // Run the connection:
    /// tokio::spawn(connection.run());
    ///
    /// // The connection is now active and the client is fully connected.
    /// # let client = client_join.await??;
    /// # tokio::spawn(client.run());
    /// # broker_handle.join().await;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn connect<T>(&mut self, t: T) -> Result<Connection<T>, EstablishError<T::Error>>
    where
        T: AsyncTransport + Unpin,
    {
        self.begin_connect(t).await?.accept_serialize(&()).await
    }

    /// Begins establishing a new connection.
    ///
    /// Unlike [`connect`](Self::connect), this function will not automatically establish a
    /// connection. It will only receive the client's initial connection message. This allows you to
    /// inspect the client's custom data and accept or reject the client.
    pub async fn begin_connect<T>(
        &mut self,
        mut t: T,
    ) -> Result<PendingConnection<T>, EstablishError<T::Error>>
    where
        T: AsyncTransport + Unpin,
    {
        let connect = match t.receive().await.map_err(EstablishError::Transport)? {
            Message::Connect(connect) => connect,
            msg => return Err(EstablishError::UnexpectedMessageReceived(msg)),
        };

        if connect.version != crate::core::VERSION {
            t.send_and_flush(Message::ConnectReply(ConnectReply::VersionMismatch(
                crate::core::VERSION,
            )))
            .await
            .ok();

            return Err(EstablishError::IncompatibleVersion(connect.version));
        }

        Ok(PendingConnection::new(self.clone(), t, connect.value))
    }

    /// Shuts down the broker.
    ///
    /// This method informs the [`Broker`](crate::Broker) that it should initiate shutdown, but
    /// doesn't block until `Broker` has done so. The `Broker` will cleanly shut down all
    /// connections, before the [`Broker::run`](crate::Broker::run) returns.
    ///
    /// # Examples
    ///
    /// ```
    /// use aldrin_broker::Broker;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let broker = Broker::new();
    /// let mut handle = broker.handle().clone();
    /// let join = tokio::spawn(broker.run());
    ///
    /// // Tell the broker to shutdown:
    /// handle.shutdown().await;
    ///
    /// // `run` will return as soon as all connections have been shut down:
    /// join.await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn shutdown(&mut self) {
        self.send.send(ConnectionEvent::ShutdownBroker).await.ok();
    }

    /// Shuts down the broker when the last client disconnects.
    ///
    /// This method informs the [`Broker`](crate::Broker) that it should shutdown as soon as there
    /// are no active clients left.
    ///
    /// Calling this method does not prevent new connections. It also doesn't actively tell the
    /// connected clients to shut down.
    ///
    /// # Examples
    ///
    /// ```
    /// use aldrin_broker::Broker;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let broker = Broker::new();
    /// let mut handle = broker.handle().clone();
    /// let join = tokio::spawn(broker.run());
    ///
    /// // Tell the broker to shutdown when it becomes idle:
    /// handle.shutdown_idle().await;
    ///
    /// // `run` will return as soon as the last client disconnects:
    /// join.await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn shutdown_idle(&mut self) {
        self.send
            .send(ConnectionEvent::ShutdownIdleBroker)
            .await
            .ok();
    }

    /// Shuts down a specific connection.
    ///
    /// Similar to the other shutdown methods, this method will only initiate shutdown of the
    /// [`Connection`] specified by `conn` and then return before it has actually shut down.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aldrin_test::tokio::TestBroker;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // Create an AsyncTransport to a new incoming connection:
    /// // let t = ...
    ///
    /// # let mut broker_handle = TestBroker::new();
    /// # let (t, t2) = aldrin_broker::core::channel::unbounded();
    /// # let client_join = tokio::spawn(aldrin::Client::connect(t2));
    /// // Establish a connection to the client:
    /// let connection = broker_handle.connect(t).await?;
    ///
    /// // Get a handle to the connection:
    /// let connection_handle = connection.handle().clone();
    ///
    /// // Run the connection:
    /// let connection_join = tokio::spawn(connection.run());
    /// # let client = client_join.await??;
    /// # tokio::spawn(client.run());
    ///
    /// // Tell the broker to shut down the connection again:
    /// broker_handle.shutdown_connection(&connection_handle).await?;
    /// connection_join.await??;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn shutdown_connection(
        &mut self,
        conn: &ConnectionHandle,
    ) -> Result<(), BrokerShutdown> {
        self.send
            .send(ConnectionEvent::ShutdownConnection(conn.id().clone()))
            .await
            .map_err(|_| BrokerShutdown)
    }

    /// Gets the current broker statistics.
    ///
    /// Some statistics are measured over the time interval between two calls to this function. Such
    /// statistics will be reset to 0 when this function is called.
    ///
    /// # Examples
    ///
    /// ```
    /// # use aldrin_test::tokio::TestBroker;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let mut broker_handle = TestBroker::new();
    /// let statistics = broker_handle.take_statistics().await?;
    ///
    /// // Calculate the duration over which the statistics were measured.
    /// let time_diff = statistics.end - statistics.start;
    ///
    /// println!("The current number of connections is {}.", statistics.num_connections);
    /// println!(
    ///     "{} connections were added within the last {} seconds.",
    ///     statistics.connections_added,
    ///     time_diff.as_secs_f32()
    /// );
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "statistics")]
    #[cfg_attr(docsrs, doc(cfg(feature = "statistics")))]
    pub async fn take_statistics(&mut self) -> Result<BrokerStatistics, BrokerShutdown> {
        let (send, recv) = oneshot::channel();
        self.send
            .send(ConnectionEvent::TakeStatistics(send))
            .await
            .map_err(|_| BrokerShutdown)?;
        recv.await.map_err(|_| BrokerShutdown)
    }
}

/// A pending client connection, that hasn't been accepted or rejected yet.
///
/// This type is acquired by [`BrokerHandle::begin_connect`]. It allows inspection of the [client's
/// custom data](Self::client_data) and to [accept](Self::accept) or [reject](Self::reject) a
/// client.
///
/// Dropping this type will simply also drop the transport. No message will be sent back to the
/// client in this case.
#[derive(Debug)]
pub struct PendingConnection<T: AsyncTransport + Unpin> {
    handle: BrokerHandle,
    t: T,
    client_data: SerializedValue,
}

impl<T: AsyncTransport + Unpin> PendingConnection<T> {
    fn new(handle: BrokerHandle, t: T, client_data: SerializedValue) -> Self {
        Self {
            handle,
            t,
            client_data,
        }
    }

    /// Returns the client's data.
    pub fn client_data(&self) -> &SerializedValueSlice {
        &self.client_data
    }

    /// Deserializes the client's data.
    pub fn deserialize_client_data<D: Deserialize>(&self) -> Result<D, DeserializeError> {
        self.client_data.deserialize()
    }

    /// Accepts a client and sends custom data back to it.
    ///
    /// The resulting [`Connection`] must be [`run`](Connection::run) and polled to completion, much
    /// like the [`Broker`](crate::Broker) itself.
    pub async fn accept(
        mut self,
        broker_data: SerializedValue,
    ) -> Result<Connection<T>, EstablishError<T::Error>> {
        self.t
            .send_and_flush(Message::ConnectReply(ConnectReply::Ok(broker_data)))
            .await
            .map_err(EstablishError::Transport)?;

        let id = self.handle.ids.acquire();
        let (send, recv) = mpsc::unbounded();

        self.handle
            .send
            .send(ConnectionEvent::NewConnection(id.clone(), send))
            .await
            .map_err(|_| EstablishError::Shutdown)?;

        let conn = Connection::new(self.t, id, self.handle.send, recv);

        Ok(conn)
    }

    /// Accepts a client and sends custom data back to it.
    ///
    /// The resulting [`Connection`] must be [`run`](Connection::run) and polled to completion, much
    /// like the [`Broker`](crate::Broker) itself.
    pub async fn accept_serialize<D: Serialize + ?Sized>(
        self,
        broker_data: &D,
    ) -> Result<Connection<T>, EstablishError<T::Error>> {
        let broker_data = SerializedValue::serialize(broker_data)?;
        self.accept(broker_data).await
    }

    /// Rejects a client and sends custom data back to it.
    pub async fn reject(
        mut self,
        broker_data: SerializedValue,
    ) -> Result<(), EstablishError<T::Error>> {
        self.t
            .send_and_flush(Message::ConnectReply(ConnectReply::Rejected(broker_data)))
            .await
            .map_err(EstablishError::Transport)?;

        Ok(())
    }

    /// Rejects a client and sends custom data back to it.
    pub async fn reject_serialize<D: Serialize + ?Sized>(
        self,
        broker_data: &D,
    ) -> Result<(), EstablishError<T::Error>> {
        let broker_data = SerializedValue::serialize(broker_data)?;
        self.reject(broker_data).await
    }
}