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
/*
* Software Name : libits-client
* SPDX-FileCopyrightText: Copyright (c) Orange SA
* SPDX-License-Identifier: MIT
*
* This software is distributed under the MIT license,
* see the "LICENSE.txt" file for more details or https://opensource.org/license/MIT/
*
* Authors: see CONTRIBUTORS.md
*/
use crateConfiguration;
use crateExchange;
use crateTopic;
use cratePacket;
use crateSequenceNumber;
use ;
/// Structures implementing this trait can be used in [crate::client::application::pipeline::run]
/// to treat messages and eventually send or update other ones
///
/// Analyzer implementing structs must be able to run in parallel to be able to treat messages
/// faster than they arrive
/// All members are thus shared using [Arc] and [RwLock] when they can be modified by the analyzer
///
/// Example:
/// ```
/// use std::fmt::{Display, Formatter};
/// use std::str::FromStr;
/// use std::sync::{Arc, RwLock};
/// use libits::client::application::analyzer::Analyzer;
/// use libits::client::configuration::Configuration;use libits::exchange::Exchange;
/// use libits::exchange::message::Message;
/// use libits::exchange::sequence_number::SequenceNumber;
/// use libits::transport::mqtt::topic::Topic;
/// use libits::transport::packet::Packet;
///
/// struct Counts {
/// pub pedestrians: u32,
/// pub vehicles: u32,
/// }
///
/// struct CounterAnalyzer {
/// configuration: Arc<Configuration>,
/// context: Arc<RwLock<Counts>>,
/// }
///
/// #[derive(Clone, Default, Debug, PartialEq, Eq, Hash)]
/// struct StringTopic {
/// topic: String,
/// }
/// impl FromStr for StringTopic {
/// type Err = ();
/// fn from_str(s: &str) -> Result<Self, Self::Err> {
/// Ok(Self { topic: String::from(s)})
/// }
/// }
/// impl Display for StringTopic { ///
/// fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
/// write!(f, "{}", self.topic)
/// }
/// }
/// impl Topic for StringTopic {
/// fn as_route(&self) -> String {
/// self.topic.to_string()
/// }
/// }
///
/// impl Analyzer<StringTopic, Counts> for CounterAnalyzer {
/// fn new(configuration: Arc<Configuration>, context: Arc<RwLock<Counts>>, _: Arc<RwLock<SequenceNumber>>) -> Self where Self: Sized {
/// Self {
/// configuration,
/// context,
/// }
/// }
///
/// fn analyze(&mut self, packet: Packet<StringTopic, Exchange>) -> Vec<Packet<StringTopic, Exchange>> {
/// match packet.payload.message {
/// Message::CAM(cam) => {
/// match cam.basic_container.station_type {
/// 1 => self.context.write().unwrap().pedestrians += 1,
/// 5 | 6 | 7 => self.context.write().unwrap().vehicles += 1,
/// _ => ()
/// }
/// }
/// _ => ()
/// }
/// Vec::new()
/// }
/// }
/// ```