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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use super::stream::{Stream, StreamKind};
use super::message::{Message, MessageData, MessageKind};
use super::streamer::RawStreamer;
use super::error::UnrecoverableError;

use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;
use std::task::Poll;
use std::future::poll_fn;

use tokio::sync::mpsc;
use tokio::time::{interval, Duration};

use tracing::error;

pub use fire::util::PinnedFuture;
use fire::{Response, Data};
use fire::header::Method;
use fire::routes::{RawRoute, HyperRequest};
use fire::ws::{self, WebSocket, JsonError};


#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Request {
	action: Cow<'static, str>,
	kind: StreamKind
}

pub trait IntoStreamHandler {
	type Stream: Stream;
	type Handler: StreamHandler;

	fn into_handler(self) -> Self::Handler;
}

pub trait StreamHandler {
	fn validate_data(&self, _data: &Data) {}

	/// every MessageData needs to correspond with the StreamTrait
	/// 
	/// ## Warning
	/// Your not allowed to drop streamer before you return from the function
	/// else that may leed to a busy loop in the StreamServer (todo improve that)
	fn handle<'a>(
		&'a self,
		req: MessageData,
		streamer: RawStreamer,
		data: &'a Data
	) -> PinnedFuture<'a, Result<MessageData, UnrecoverableError>>;
}

pub struct StreamServer {
	uri: &'static str,
	inner: Arc<HashMap<Request, Box<dyn StreamHandler + Send + Sync>>>
}

impl StreamServer {
	pub fn new(uri: &'static str) -> Self {
		Self {
			uri,
			inner: Arc::new(HashMap::new())
		}
	}

	pub fn insert<H>(&mut self, handler: H)
	where
		H: IntoStreamHandler,
		H::Handler: StreamHandler + Send + Sync + 'static
	{
		Arc::get_mut(&mut self.inner).unwrap().insert(
			Request {
				action: H::Stream::ACTION.into(),
				kind: H::Stream::KIND
			},
			Box::new(handler.into_handler())
		);
	}
}

impl RawRoute for StreamServer {
	fn check(&self, req: &HyperRequest) -> bool {
		req.method() == Method::GET &&
		fire::routes::check_static(req.uri().path(), self.uri)
	}

	fn call<'a>(
		&'a self,
		req: &'a mut HyperRequest,
		data: &'a Data
	) -> PinnedFuture<'a, Option<fire::Result<Response>>> {
		PinnedFuture::new(async move {
			let (on_upgrade, ws_accept) = match ws::util::upgrade(req) {
				Ok(o) => o,
				Err(e) => return Some(Err(e))
			};

			let handlers = self.inner.clone();
			let data = data.clone();

			// we need to spawn a future because
			// upgrade on can only be fufilled after
			// we send SWITCHING_PROTOCOLS
			tokio::task::spawn(async move {
				match on_upgrade.await {
					Ok(upgraded) => {
						let ws = WebSocket::new(upgraded).await;

						trace!("connection upgraded");
						
						let res = handle_connection(handlers, ws, data).await;
						if let Err(e) = res {
							error!("websocket connection failed with {:?}", e);
						}

					},
					Err(e) => ws::util::upgrade_error(e)
				}
			});

			Some(Ok(ws::util::switching_protocols(ws_accept)))
		})
	}
}

async fn handle_connection(
	handlers: Arc<HashMap<Request, Box<dyn StreamHandler + Send + Sync>>>,
	mut ws: WebSocket,
	data: Data
) -> Result<(), UnrecoverableError> {
	let mut receivers = Receivers::new();
	let mut senders = Senders::new();
	// data: (Request, MessageData)
	let (close_tx, mut close_rx) = mpsc::channel(10);
	let mut ping_interval = interval(Duration::from_secs(30));

	loop {
		tokio::select! {
			msg = ws.deserialize() => {
				let msg: Message = match msg {
					Ok(None) => return Ok(()),
					Ok(Some(m)) => m,
					Err(JsonError::ConnectionError(e)) => {
						return Err(e.to_string().into())
					},
					Err(JsonError::SerdeError(e)) => {
						error!("could not deserialize message {:?}", e);
						// json error just ignore the message
						continue
					}
				};

				trace!("received message {:?}", msg);

				let req = Request {
					action: msg.action,
					kind: msg.kind.into()
				};

				match msg.kind {
					k @ MessageKind::SenderRequest |
					k @ MessageKind::ReceiverRequest => {
						// no handler
						if !handlers.contains_key(&req) {
							error!("no handler for {:?} found", req);
							ws.serialize(&Message {
								kind: msg.kind.into_close(),
								action: req.action.clone(),
								data: MessageData::null()
							}).await.map_err(|e| e.to_string())?;
							continue
						}

						// we know the handler exists
						let (tx, rx) = mpsc::channel(10);

						let streamer = match req.kind {
							// the client want's to send us data
							StreamKind::Sender => {
								if !senders.insert(req.clone(), tx) {
									// the sender already exist
									// don't create a new handler
									continue
								}
								RawStreamer::receiver(rx)
							},
							// the client want's to receive data from us
							StreamKind::Receiver => {
								if !receivers.insert(req.clone(), rx) {
									// the handler already exists
									continue
								}
								RawStreamer::sender(tx)
							}
						};

						// let's send a success message
						ws.serialize(&Message {
							kind: k,
							action: req.action.clone(),
							data: MessageData::null()
						}).await.map_err(|e| e.to_string())?;

						let data = data.clone();
						let handlers = handlers.clone();
						let msg_data = msg.data;
						let close_tx = close_tx.clone();

						// the first task only catches panics
						// and the seconds starts the handler
						// we could also detect a panic when trying to send
						// or receive via a mpsc channel.
						// but that could lead to multiple close messages being
						// sent when the task succesfully exists
						tokio::spawn(async move {
							let panic_close_tx = close_tx.clone();
							let panic_req = req.clone();

							let r = tokio::spawn(async move {

								let handler = match handlers.get(&req) {
									Some(h) => h,
									None => unreachable!()
								};

								let r = handler.handle(
									msg_data,
									streamer,
									&data
								).await;
								match r {
									Ok(m) => {
										let _ = close_tx.send((req, m)).await;
									},
									Err(e) => {
										error!("stream handler unrecoverable \
											error {:?}", e
										);
										let _ = close_tx.send(
											(req, MessageData::null())
										).await;
									}
								}
							}).await;

							if r.is_err() {
								// some error happened so let's send a close req
								let _ = panic_close_tx.send(
									(panic_req, MessageData::null())
								).await;
							}
						});
						
					},
					MessageKind::SenderMessage => {
						// if a handler is already closed don't do anything
						// since it is guaranteed to get closed via close_tx
						// if a handler does not exist
						// this is a protocol error since you would get a
						// a 
						let _ = senders.send(&req, msg.data).await;
					},
					MessageKind::ReceiverMessage => {
						// we should not receive this message
						// this is a protocol error
					},
					MessageKind::SenderClose => {
						senders.remove(&req);
					},
					MessageKind::ReceiverClose => {
						receivers.remove(&req);
					}
				}
			},
			(req, data) = receivers.recv(), if !receivers.is_empty() => {
				ws.serialize(&Message {
					kind: req.kind.into_kind_message(),
					action: req.action,
					data: data
				}).await.map_err(|e| e.to_string())?;
			},
			_ping = ping_interval.tick() => {
				ws.ping().await
					.map_err(|e| e.to_string())?;
			},
			msg = close_rx.recv() => {
				// cannot fail since we always have a close_tx
				let (req, data) = msg.unwrap();

				match req.kind {
					StreamKind::Sender => {
						if senders.remove(&req) {
							ws.serialize(&Message {
								kind: MessageKind::SenderClose,
								action: req.action,
								data: data
							}).await.map_err(|e| e.to_string())?;
						}
					},
					StreamKind::Receiver => {
						if receivers.remove(&req) {
							ws.serialize(&Message {
								kind: MessageKind::ReceiverClose,
								action: req.action,
								data: data
							}).await.map_err(|e| e.to_string())?;
						}
					}
				}
			}
		}
	}
}

struct Receivers {
	inner: HashMap<Request, mpsc::Receiver<MessageData>>,
	// we use a recv queue to make polling more fair since we poll
	// all futures and check if they have available data and the store everything
	// in the queue
	// the problem here is that we don't return on the first one and always poll
	// every future (which is not great)
	recv_queue: Vec<(Request, MessageData)>
}

impl Receivers {
	pub fn new() -> Self {
		Self {
			inner: HashMap::new(),
			recv_queue: vec![]
		}
	}

	pub fn is_empty(&self) -> bool {
		self.inner.is_empty() && self.recv_queue.is_empty()
	}

	/// if no receivers exist this will wait for every
	/// you should don't call recv when `is_empty` returns `true`
	pub async fn recv(&mut self) -> (Request, MessageData) {
		if let Some(msg) = self.recv_queue.pop() {
			return msg;
		}

		debug_assert!(!self.inner.is_empty(), "will wait for ever");

		poll_fn(|ctx| {
			for (req, rx) in self.inner.iter_mut() {
				match rx.poll_recv(ctx) {
					Poll::Pending => continue,
					Poll::Ready(Some(data)) => {
						self.recv_queue.push((req.clone(), data))
					},
					// todo maybe we should remove those
					// but since the receiver will probably quickly be removed
					// it should not be a problem
					Poll::Ready(None) => continue
				}
			}

			match self.recv_queue.pop() {
				Some(m) => Poll::Ready(m),
				None => Poll::Pending
			}
		}).await
	}

	pub fn insert(
		&mut self,
		req: Request,
		recv: mpsc::Receiver<MessageData>
	) -> bool {
		if self.inner.contains_key(&req) {
			return false
		}

		self.inner.insert(req, recv).is_none()
	}

	pub fn remove(&mut self, req: &Request) -> bool {
		self.inner.remove(req).is_some()
	}
}

struct Senders {
	inner: HashMap<Request, mpsc::Sender<MessageData>>
}

impl Senders {
	pub fn new() -> Self {
		Self {
			inner: HashMap::new()
		}
	}

	pub fn insert(
		&mut self,
		req: Request,
		sender: mpsc::Sender<MessageData>
	) -> bool {
		if self.inner.contains_key(&req) {
			return false
		}

		self.inner.insert(req, sender).is_none()
	}

	pub async fn send(&mut self, req: &Request, data: MessageData) {
		if let Some(sender) = self.inner.get(req) {
			// todo should we send an error here??
			let _ = sender.send(data).await;
		}
	}

	pub fn remove(&mut self, req: &Request) -> bool {
		self.inner.remove(req).is_some()
	}
}