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
use std::any::Any;
use std::sync::mpsc::{channel, Receiver};
use std::sync::Arc;
use std::thread;
use std::thread::JoinHandle;
use std::time::Duration;
use log::{debug, error, info, trace, warn};
use rumqttc::{Event, EventLoop, Publish};
use serde::de::DeserializeOwned;
use crate::analyse::analyser::Analyser;
use crate::analyse::configuration::Configuration;
use crate::analyse::item::Item;
use crate::monitor;
use crate::mqtt::mqtt_client::{listen, Client};
use crate::mqtt::{mqtt_client, mqtt_router};
use crate::reception::exchange::collective_perception_message::CollectivePerceptionMessage;
use crate::reception::exchange::cooperative_awareness_message::CooperativeAwarenessMessage;
use crate::reception::exchange::decentralized_environmental_notification_message::DecentralizedEnvironmentalNotificationMessage;
use crate::reception::exchange::Exchange;
use crate::reception::information::Information;
use crate::reception::typed::Typed;
use crate::reception::Reception;
pub async fn run<T: Analyser>(
mqtt_host: &str,
mqtt_port: u16,
mqtt_client_id: &str,
mqtt_username: Option<&str>,
mqtt_password: Option<&str>,
mqtt_root_topic: &str,
) {
loop {
let topic_list = vec![
format!("{}/v2x/cam", mqtt_root_topic),
format!("{}/v2x/cpm", mqtt_root_topic),
format!("{}/v2x/denm", mqtt_root_topic),
format!("{}/info", mqtt_root_topic),
];
let (mut client, event_loop) = mqtt_client::Client::new(
mqtt_host,
mqtt_port,
mqtt_client_id,
mqtt_username,
mqtt_password,
);
let configuration = Arc::new(Configuration::new(mqtt_client_id.to_string()));
mqtt_client_subscribe(&topic_list, &mut client).await;
let (event_receiver, mqtt_client_listen_handle) = mqtt_client_listen_thread(event_loop);
let (item_receiver, monitoring_receiver, information_receiver, mqtt_router_dispatch_handle) =
mqtt_router_dispatch_thread(topic_list, event_receiver);
let monitor_reception_handle = monitor_thread(
"received_on".to_string(),
configuration.clone(),
monitoring_receiver,
);
let (publish_item_receiver, publish_monitoring_receiver, analyser_generate_handle) =
analyser_generate_thread::<T>(configuration.clone(), item_receiver);
let reader_configure_handle =
reader_configure_thread(configuration.clone(), information_receiver);
let monitor_publish_handle = monitor_thread(
"sent_on".to_string(),
configuration.clone(),
publish_monitoring_receiver,
);
mqtt_client_publish(publish_item_receiver, &mut client).await;
debug!("mqtt_client_listen_handler joining...");
mqtt_client_listen_handle.await.unwrap();
debug!("mqtt_router_dispatch_handler joining...");
mqtt_router_dispatch_handle.join().unwrap();
debug!("monitor_reception_handle joining...");
monitor_reception_handle.join().unwrap();
debug!("reader_configure_handler joining...");
reader_configure_handle.join().unwrap();
debug!("analyser_generate_handler joining...");
analyser_generate_handle.join().unwrap();
debug!("monitor_publish_handle joining...");
monitor_publish_handle.join().unwrap();
warn!("loop done");
tokio::time::sleep(Duration::from_secs(5)).await;
}
}
fn mqtt_client_listen_thread(
event_loop: EventLoop,
) -> (Receiver<Event>, tokio::task::JoinHandle<()>) {
info!("starting mqtt client listening...");
let (event_sender, event_receiver) = channel();
let handle = tokio::task::spawn(async move {
trace!("mqtt client listening closure entering...");
listen(event_loop, event_sender).await;
trace!("mqtt client listening closure finished");
});
info!("mqtt client listening started");
(event_receiver, handle)
}
fn mqtt_router_dispatch_thread(
topic_list: Vec<String>,
event_receiver: Receiver<Event>,
) -> (
Receiver<Item<Exchange>>,
Receiver<Item<Exchange>>,
Receiver<Item<Information>>,
JoinHandle<()>,
) {
info!("starting mqtt router dispatching...");
let (exchange_sender, exchange_receiver) = channel();
let (monitoring_sender, monitoring_receiver) = channel();
let (information_sender, information_receiver) = channel();
let handle = thread::Builder::new()
.name("mqtt-router-dispatcher".into())
.spawn(move || {
trace!("mqtt router dispatching closure entering...");
let router = &mut mqtt_router::Router::new();
if let Some(cam_topic) = topic_list
.iter()
.find(|&r| r.contains(CooperativeAwarenessMessage::get_type().as_str()))
{
router.add_route(cam_topic, deserialize::<Exchange>);
}
if let Some(denm_topic) = topic_list.iter().find(|&r| {
r.contains(DecentralizedEnvironmentalNotificationMessage::get_type().as_str())
}) {
router.add_route(denm_topic, deserialize::<Exchange>);
}
if let Some(cpm_topic) = topic_list
.iter()
.find(|&r| r.contains(CollectivePerceptionMessage::get_type().as_str()))
{
router.add_route(cpm_topic, deserialize::<Exchange>);
}
if let Some(info_topic) = topic_list
.iter()
.find(|&r| r.contains(Information::get_type().as_str()))
{
router.add_route(info_topic, deserialize::<Information>);
}
for event in event_receiver {
match router.handle_event(event) {
Some((topic, reception)) => {
if reception.is::<Exchange>() {
if let Ok(exchange) = reception.downcast::<Exchange>() {
let item = Item {
topic,
reception: unbox(exchange),
};
match monitoring_sender.send(item.clone()) {
Ok(()) => trace!("mqtt monitoring sent"),
Err(error) => {
error!("stopped to send mqtt monitoring: {}", error);
break;
}
}
match exchange_sender.send(item) {
Ok(()) => trace!("mqtt exchange sent"),
Err(error) => {
error!("stopped to send mqtt exchange: {}", error);
break;
}
}
}
} else if let Ok(information) = reception.downcast::<Information>() {
match information_sender.send(Item {
topic,
reception: unbox(information),
}) {
Ok(()) => trace!("mqtt information sent"),
Err(error) => {
error!("stopped to send mqtt information: {}", error);
break;
}
}
}
}
None => debug!("no mqtt response to send"),
}
}
trace!("mqtt router dispatching closure finished");
})
.unwrap();
info!("mqtt router dispatching started");
(
exchange_receiver,
monitoring_receiver,
information_receiver,
handle,
)
}
fn monitor_thread(
direction: String,
configuration: Arc<Configuration>,
exchange_receiver: Receiver<Item<Exchange>>,
) -> JoinHandle<()> {
info!("starting monitor reception thread...");
let handle = thread::Builder::new()
.name("monitor-reception".into())
.spawn(move || {
trace!("monitor reception entering...");
for item in exchange_receiver {
monitor::monitor(
&item.reception,
direction.as_str(),
configuration.component_name(None),
format!(
"{}/{}/{}",
configuration.gateway_component_name(),
item.topic.project_base(),
item.reception.source_uuid
),
);
}
})
.unwrap();
info!("monitor reception thread started");
handle
}
fn unbox<T>(value: Box<T>) -> T {
*value
}
fn analyser_generate_thread<T: Analyser>(
configuration: Arc<Configuration>,
exchange_receiver: Receiver<Item<Exchange>>,
) -> (
Receiver<Item<Exchange>>,
Receiver<Item<Exchange>>,
JoinHandle<()>,
) {
info!("starting analyser generation...");
let (publish_sender, publish_receiver) = channel();
let (monitoring_sender, monitoring_receiver) = channel();
let handle = thread::Builder::new()
.name("analyser-generator".into())
.spawn(move || {
trace!("analyser generation closure entering...");
let mut analyser = T::new(configuration);
for item in exchange_receiver {
for publish_item in analyser.analyze(item) {
match publish_sender.send(publish_item.clone()) {
Ok(()) => trace!("publish sent"),
Err(error) => {
error!("stopped to send publish: {}", error);
break;
}
}
match monitoring_sender.send(publish_item) {
Ok(()) => trace!("monitoring sent"),
Err(error) => {
error!("stopped to send monitoring: {}", error);
break;
}
}
}
trace!("analyser generation closure finished");
}
})
.unwrap();
info!("analyser generation started");
(publish_receiver, monitoring_receiver, handle)
}
fn reader_configure_thread(
configuration: Arc<Configuration>,
information_receiver: Receiver<Item<Information>>,
) -> JoinHandle<()> {
info!("starting reader configuration...");
let handle = thread::Builder::new()
.name("reader-configurator".into())
.spawn(move || {
trace!("reader configuration closure entering...");
for item in information_receiver {
info!(
"we received an information on the topic {}: {:?}",
item.topic, item.reception
);
configuration.update(item.reception);
}
trace!("reader configuration closure finished");
})
.unwrap();
info!("reader configuration started");
handle
}
fn deserialize<T>(publish: Publish) -> Option<Box<dyn Any + 'static + Send>>
where
T: DeserializeOwned + Reception + 'static + Send,
{
match String::from_utf8(publish.payload.to_vec()) {
Ok(message) => {
let message_str = message.as_str();
match serde_json::from_str::<T>(message_str) {
Ok(message) => {
trace!("message parsed");
return Some(Box::new(message));
}
Err(e) => warn!("parse error({}) on: {}", e, message_str),
}
}
Err(e) => warn!("format error: {}", e),
}
Option::None
}
async fn mqtt_client_subscribe(topic_list: &Vec<String>, client: &mut Client) {
info!("mqtt client subscribing starting...");
let mut topic_subscription_list = Vec::new();
if let Some(cam_topic) = topic_list
.iter()
.find(|&r| r.contains(CooperativeAwarenessMessage::get_type().as_str()))
{
topic_subscription_list.push(format!("{}/+/#", cam_topic));
}
if let Some(denm_topic) = topic_list
.iter()
.find(|&r| r.contains(DecentralizedEnvironmentalNotificationMessage::get_type().as_str()))
{
topic_subscription_list.push(format!("{}/+/#", denm_topic));
}
if let Some(cpm_topic) = topic_list
.iter()
.find(|&r| r.contains(CollectivePerceptionMessage::get_type().as_str()))
{
topic_subscription_list.push(format!("{}/+/#", cpm_topic));
}
if let Some(info_topic) = topic_list
.iter()
.find(|&r| r.contains(Information::get_type().as_str()))
{
topic_subscription_list.push(format!("{}/broker", info_topic));
}
client.subscribe(topic_subscription_list).await;
info!("mqtt client subscribing finished");
}
async fn mqtt_client_publish(publish_item_receiver: Receiver<Item<Exchange>>, client: &mut Client) {
info!("mqtt client publishing starting...");
for item in publish_item_receiver {
debug!("we received a publish");
client.publish(item).await;
debug!("we forwarded the publish");
}
info!("mqtt client publishing finished");
}