/
sample_server.rs
220 lines (179 loc) · 6.76 KB
/
sample_server.rs
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
// pub mod asynchronous;
// // Must come before modules using the macros!
// mod macros;
// mod client;
// mod destinations;
// mod error;
// mod frame_handler;
use little_stomper::asynchronous::client::ClientSession;
use little_stomper::asynchronous::destinations::{AsyncDestinations, DestinationType};
use little_stomper::asynchronous::inmemory::InMemDestination;
use little_stomper::client::{DefaultClient, DefaultClientFactory};
use little_stomper::destinations::Destinations;
use little_stomper::error::StomperError;
use sender_sink::wrappers::UnboundedSenderSink;
// use futures_util::StreamExt;
use futures::future::{FutureExt, TryFutureExt};
use futures::sink::SinkExt;
use futures::stream::{StreamExt, TryStreamExt};
use std::future::ready;
use log::info;
use std::future::Future;
use std::sync::Arc;
use std::{env, io::Error};
use tokio_stream::wrappers::UnboundedReceiverStream;
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::mpsc::{self, UnboundedSender};
use tokio_tungstenite::{tungstenite::Message, WebSocketStream};
struct WebSocketMessageHandler {
// where to call back to
message_channel: UnboundedSender<Message>,
}
impl WebSocketMessageHandler {
pub fn new(message_channel: UnboundedSender<Message>) -> WebSocketMessageHandler {
WebSocketMessageHandler { message_channel }
}
pub fn into_filter_map(
self,
) -> impl Fn(Message) -> std::future::Ready<Result<Option<Vec<u8>>, StomperError>> {
move |msg| ready(self.handle_message(msg))
}
fn handle_message(&self, msg: Message) -> Result<Option<Vec<u8>>, StomperError> {
Ok(match msg {
Message::Ping(bytes) => {
self.message_channel
.send(Message::Pong(bytes))
.map_err(|_| StomperError {
message: "Unable to send return message".to_owned(),
})?;
None
}
Message::Pong(_) => {
// Not sure what to do, ignore for now
None
}
Message::Close(_) => {
info!("Client connection dropped");
// Need to shut the whole shebang down - we'll forward the message to achieve that
if self.message_channel.send(msg).is_err() {
info!("Trying to close channel; alread closed");
};
return Err(StomperError {
message: "WebSocketChannel closed".to_owned(),
});
}
Message::Text(text) => Some(text.into_bytes()),
Message::Binary(bytes) => Some(bytes),
Message::Frame(_) => {
// Not sure what to do, ignore for now
None
}
})
}
}
pub fn handle_websocket<T: Destinations<Client = DefaultClient> + 'static>(
websocket: WebSocketStream<tokio::net::TcpStream>,
destinations: T,
) -> impl Future<Output = Result<(), StomperError>> + Send {
let (socket_tx, ws_stream) = websocket.split();
let (ws_tx, ws_rx) = mpsc::unbounded_channel();
let pong_channel = ws_tx.clone();
//let ping_channel = ws_tx.clone();
//ping_channel.send(Message::Ping(vec![])).unwrap();
// tokio::spawn(async move {
// let mut interval = time::interval(Duration::from_secs(1));
// loop {
// interval.tick().await;
// if let Err(e) = ping_channel.send(Message::Ping(vec![])) {
// info!("Error sending ping: {}", e);
// }
// }
// });
let ws_response_processor_handle = tokio::task::spawn(async move {
UnboundedReceiverStream::new(ws_rx)
// Stop when a close message is received
.take_while(|msg: &Message| ready(!msg.is_close()))
.map(Ok)
.forward(socket_tx)
.unwrap_or_else(|err| {
info!("Error: {}", err);
})
.await;
});
// Transform the stream of websocket messages into a stream of byte arrays for the Stomp part;
// handle non-Stomp message types along the way
let bytes_stream = ws_stream
.map_err(|err| StomperError {
message: format!("Websocket error: {}", err.to_string()),
})
.inspect_err(|err| log::error!("Error: {:?}", err))
.try_filter_map(WebSocketMessageHandler::new(pong_channel).into_filter_map())
.boxed();
let sink = UnboundedSenderSink::from(ws_tx)
.with(|bytes: Vec<u8>| ready(Ok(Message::text(String::from_utf8(bytes).unwrap()))));
ClientSession::process_stream(
bytes_stream,
Box::pin(sink),
destinations,
DefaultClientFactory {},
)
.then(move |_| async {
ws_response_processor_handle
.await
.map_err(|_| StomperError {
message: "Error awaiting WebSocket response handler".to_owned(),
})
})
}
#[tokio::main]
async fn main() -> Result<(), Error> {
let _ = env_logger::try_init();
let addr = env::args()
.nth(1)
.unwrap_or_else(|| "127.0.0.1:3030".to_string());
// Create the event loop and TCP listener we'll accept connections on.
let try_socket = TcpListener::bind(&addr).await;
let listener = try_socket.expect("Failed to bind");
info!("Listening on: {}", addr);
let destinations =
AsyncDestinations::<InMemDestination>::start(Arc::new(InMemDestination::create)).await;
while let Ok((stream, _)) = listener.accept().await {
tokio::spawn(accept_connection(stream, destinations.clone()));
}
Ok(())
}
async fn accept_connection<D: DestinationType + 'static>(
stream: TcpStream,
destinations: AsyncDestinations<D>,
) {
let addr = stream
.peer_addr()
.expect("connected streams should have a peer address");
info!("Peer address: {}", addr);
let ws_stream = tokio_tungstenite::accept_async(stream)
.await
.expect("Error during the websocket handshake occurred");
info!("New WebSocket connection: {}", addr);
if let Err(err) = handle_websocket(ws_stream, destinations).await {
log::error!("Error handling websocket: {}", err);
}
info!(" Connection Ended: {}", addr);
}
#[cfg(test)]
mod test {
use super::WebSocketMessageHandler;
use tokio::sync::mpsc;
use tokio_tungstenite::tungstenite::Message;
#[tokio::test]
async fn message_hander_handles_ping() {
let (tx, mut rx) = mpsc::unbounded_channel();
let filter_map = WebSocketMessageHandler::new(tx).into_filter_map();
filter_map(Message::Ping(b"0".to_vec())).await.unwrap();
let msg = rx.recv().await;
if let Some(Message::Pong(bytes)) = msg {
assert_eq!(b"0".to_vec(), bytes);
} else {
panic!("Ping did not Pong!");
}
}
}