Documentation
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use std::{
    sync::{Arc, atomic},
    time::{Duration, SystemTime},
};

use super::{
    error::{BusError, ProcError},
    queue::{InternalMsgQueue, SendError},
    service::{ProcService, ServiceError, ServiceTable},
};
use tracing::{Level, Span, event, info_span, span};

/// Expose Tvf trait
pub use prosa_utils::msg::tvf::Tvf;

/// Internal ProSA message that define all message type that can be received by the main ProSA processor
#[derive(Debug)]
pub enum InternalMainMsg<M>
where
    M: Sized + Clone + Tvf,
{
    /// Message to register a new spawned processor queue
    NewProcQueue(ProcService<M>),
    /// Message to indicate that a the processor stopped (and the error if there is any), delete all processor queues
    DeleteProc(u32, Option<Box<dyn ProcError + Send + Sync>>),
    /// Message to indicate that a the processor queue stopped, delete the processor queue
    DeleteProcQueue(u32, u32),
    /// Message to declare new service(s) with their service name and the processor id (the processor should have been declared). Declare service(s) for the whole processor
    NewProcService(Vec<String>, u32),
    /// Message to declare new service(s) with their service name, the processor id (the processor should have been declared), and the queue id
    NewService(Vec<String>, u32, u32),
    /// Message to unregister a service for all the processor. Message that contain the service name and the processor id
    DeleteProcService(Vec<String>, u32),
    /// Message to unregister service(s) for a processor queue. Message that contain service(s) name(s), the processor id, and the queue id
    DeleteService(Vec<String>, u32, u32),
    /// Command to ask an action or a status to the main processor
    Command(String),
    /// Internal call for shutdown (with a reason)
    Shutdown(String),
}

/// Internal ProSA message that define all message type that can be received by a processor
#[derive(Debug)]
pub enum InternalMsg<M>
where
    M: Sized + Clone + Tvf,
{
    /// Request Data message to process
    Request(RequestMsg<M>),
    /// Response of a data request message
    Response(ResponseMsg<M>),
    /// Response of a data request message by an error
    Error(ErrorMsg<M>),
    /// Command to ask an actiion or a status to the processor
    Command(String),
    /// Message to ask the processor to reload its configuration
    Config,
    /// Message to ask the processor to reload its service table
    Service(Arc<ServiceTable<M>>),
    /// Message to ask the processor to shutdown
    Shutdown,
}

#[cfg_attr(doc, aquamarine::aquamarine)]
/// Trait that define a ProSA Msg use to send transactions
///
/// ```mermaid
/// sequenceDiagram
///     Client->>Service: RequestMsg
///     alt is ok
///         Service->>Client: ResponseMsg
///     else is error
///         Service->>Client: ErrorMsg
///     end
/// ```
pub trait Msg<M>
where
    M: Sized + Clone + Tvf,
{
    /// Getter of the message id
    fn get_id(&self) -> u64;
    /// Getter of the service name
    fn get_service(&self) -> &String;
    /// Getter of the span of the message (use for metrics)
    fn get_span(&self) -> &Span;
    /// Getter of the mutable span of the message (use to add informations for metrics)
    fn get_span_mut(&mut self) -> &mut Span;
    /// Enter the span and push metadata in it
    fn enter_span(&self) -> span::Entered<'_>;
    /// Return the elapsed time corresponding to the processing time (duration since the request creation)
    fn elapsed(&self) -> Duration;
    /// Getter of the message content. Return an error if the data have been taken
    fn get_data(&self) -> Result<&M, BusError>;
    /// Getter of the mutable message content. Return an error if the data have been taken
    fn get_data_mut(&mut self) -> Result<&mut M, BusError>;
    /// Takes the data out of the message.
    ///
    /// ```
    /// use prosa::core::msg::{Msg, Tvf};
    ///
    /// fn process_msg<T, M>(mut msg: T)
    /// where
    ///     M: Sized + Clone + Tvf,
    ///     T: Msg<M>,
    /// {
    ///     if msg.get_data().is_ok() {
    ///         let data = msg.take_data();
    ///         assert!(data.is_some());
    ///         assert!(msg.get_data().is_err());
    ///     } else {
    ///         assert!(msg.take_data().is_none());
    ///     }
    /// }
    /// ```
    fn take_data(&mut self) -> Option<M>;
    /// Takes the value out of the message, but only if the predicate evaluates to true on a mutable reference to the data.
    /// This method operates similar to [`Msg<M>::take_data`] but conditional.
    ///
    /// ```
    /// use prosa::core::msg::{Msg, Tvf};
    ///
    /// fn process_msg<T, M>(mut msg: T)
    /// where
    ///     M: Sized + Clone + Tvf,
    ///     T: Msg<M>,
    /// {
    ///     if msg.get_data().is_ok() {
    ///         let data = msg.take_data_if(|data| data.contains(42));
    ///         // Return the data only if its contain a field 42.
    ///     }
    /// }
    /// ```
    fn take_data_if<P>(&mut self, predicate: P) -> Option<M>
    where
        P: FnOnce(&mut M) -> bool;
}

pub(crate) static ATOMIC_INTERNAL_MSG_ID: atomic::AtomicU64 = atomic::AtomicU64::new(0);

/// ProSA request message that define a data message that need to be process by a processor
#[derive(Debug)]
pub struct RequestMsg<M>
where
    M: Sized + Clone + Tvf,
{
    id: u64,
    service: String,
    span: Span,
    data: Option<M>,
    begin_time: SystemTime,
    response_queue: InternalMsgQueue<M>,
}

impl<M> Msg<M> for RequestMsg<M>
where
    M: Sized + Clone + Tvf,
{
    fn get_id(&self) -> u64 {
        self.id
    }

    fn get_service(&self) -> &String {
        &self.service
    }

    fn get_span(&self) -> &Span {
        &self.span
    }

    fn get_span_mut(&mut self) -> &mut Span {
        &mut self.span
    }

    fn enter_span(&self) -> span::Entered<'_> {
        self.span.enter()
    }

    fn elapsed(&self) -> Duration {
        self.begin_time.elapsed().unwrap_or(Duration::new(0, 0))
    }

    fn get_data(&self) -> Result<&M, BusError> {
        self.data.as_ref().ok_or(BusError::NoData)
    }

    fn get_data_mut(&mut self) -> Result<&mut M, BusError> {
        self.data.as_mut().ok_or(BusError::NoData)
    }

    fn take_data(&mut self) -> Option<M> {
        self.data.take()
    }

    fn take_data_if<P>(&mut self, predicate: P) -> Option<M>
    where
        P: FnOnce(&mut M) -> bool,
    {
        self.data.take_if(predicate)
    }
}

impl<M> RequestMsg<M>
where
    M: Sized + Clone + Tvf,
{
    /// Method to create a new RequestMessage
    pub fn new(service: String, data: M, response_queue: impl Into<InternalMsgQueue<M>>) -> Self {
        let begin_time = SystemTime::now();
        let span = info_span!("prosa::Msg", service = service);
        RequestMsg {
            id: ATOMIC_INTERNAL_MSG_ID.fetch_add(1, atomic::Ordering::Relaxed),
            service,
            data: Some(data),
            begin_time,
            span,
            response_queue: response_queue.into(),
        }
    }

    /// Method to create a new RequestMessage with a specific trace Id
    pub fn new_with_trace_id(
        service: String,
        data: M,
        response_queue: impl Into<InternalMsgQueue<M>>,
        trace_id: tracing::span::Id,
    ) -> Self {
        let begin_time = SystemTime::now();
        let span = info_span!(parent: trace_id, "prosa::Msg", service = service);
        RequestMsg {
            id: ATOMIC_INTERNAL_MSG_ID.fetch_add(1, atomic::Ordering::Relaxed),
            service,
            data: Some(data),
            begin_time,
            span,
            response_queue: response_queue.into(),
        }
    }

    /// Method to return the response to the called processor
    pub fn return_to_sender(mut self, resp: M) -> Result<(), SendError<M>> {
        let response_queue = self.response_queue.take();
        response_queue
            .send(InternalMsg::Response(ResponseMsg::from_request(self, resp)))
            .map_err(|e| {
                e.map(|i| {
                    if let InternalMsg::Response(mut resp) = i {
                        resp.take_data().unwrap()
                    } else {
                        panic!("Expected InternalMsg::Response")
                    }
                })
            })
    }

    /// Method to return an error to the called processor
    /// You can specify a return data
    pub fn return_error_to_sender(
        mut self,
        data: Option<M>,
        err: ServiceError,
    ) -> Result<(), SendError<Option<M>>> {
        let response_queue = self.response_queue.take();
        response_queue
            .send(InternalMsg::Error(ErrorMsg::from_request(self, data, err)))
            .map_err(|e| {
                e.map(|i| {
                    if let InternalMsg::Error(mut err) = i {
                        err.take_data()
                    } else {
                        panic!("Expected InternalMsg::Error")
                    }
                })
            })
    }

    /// Method to return a result to the called processor
    /// If Ok, return the response data, else return the error
    pub fn return_result_to_sender(
        self,
        result: Result<M, ServiceError>,
    ) -> Result<(), SendError<Option<M>>> {
        match result {
            Ok(resp) => self.return_to_sender(resp).map_err(|e| e.map(|m| Some(m))),
            Err(err) => self.return_error_to_sender(None, err),
        }
    }
}

/// ProSA request message that define a data message that need to be process by a processor
#[derive(Debug)]
pub struct ResponseMsg<M>
where
    M: Sized + Clone + Tvf,
{
    id: u64,
    service: String,
    span: Span,
    response_time: SystemTime,
    data: Option<M>,
}

impl<M> ResponseMsg<M>
where
    M: Sized + Clone + Tvf,
{
    /// Method to create a `ResponseMsg` from a [`RequestMsg`]
    pub fn from_request(request: RequestMsg<M>, resp_data: M) -> Self {
        ResponseMsg {
            id: request.id,
            service: request.service,
            span: request.span,
            response_time: request.begin_time,
            data: Some(resp_data),
        }
    }
}

impl<M> Msg<M> for ResponseMsg<M>
where
    M: Sized + Clone + Tvf,
{
    fn get_id(&self) -> u64 {
        self.id
    }

    fn get_service(&self) -> &String {
        &self.service
    }

    fn get_span(&self) -> &Span {
        &self.span
    }

    fn get_span_mut(&mut self) -> &mut Span {
        &mut self.span
    }

    fn enter_span(&self) -> span::Entered<'_> {
        self.span.enter()
    }

    fn elapsed(&self) -> Duration {
        self.response_time.elapsed().unwrap_or(Duration::new(0, 0))
    }

    fn get_data(&self) -> Result<&M, BusError> {
        self.data.as_ref().ok_or(BusError::NoData)
    }

    fn get_data_mut(&mut self) -> Result<&mut M, BusError> {
        self.data.as_mut().ok_or(BusError::NoData)
    }

    fn take_data(&mut self) -> Option<M> {
        self.data.take()
    }

    fn take_data_if<P>(&mut self, predicate: P) -> Option<M>
    where
        P: FnOnce(&mut M) -> bool,
    {
        self.data.take_if(predicate)
    }
}

/// ProSA request message that define a data message that need to be process by a processor
#[derive(Debug)]
pub struct ErrorMsg<M>
where
    M: Sized + Clone + Tvf,
{
    id: u64,
    service: String,
    span: Span,
    error_time: SystemTime,
    data: Option<M>,
    err: ServiceError,
}

impl<M> ErrorMsg<M>
where
    M: Sized + Clone + Tvf,
{
    /// Method to create an `ErrorMsg` from a [`RequestMsg`] (if the request encounter an error)
    pub fn from_request(request: RequestMsg<M>, data: Option<M>, err: ServiceError) -> Self {
        ErrorMsg {
            id: request.id,
            service: request.service,
            span: request.span,
            error_time: request.begin_time,
            data: data.or(request.data),
            err,
        }
    }

    /// Getter of the service error
    pub fn get_err(&self) -> &ServiceError {
        &self.err
    }

    /// Consume the message and return the service error
    pub fn into_err(self) -> ServiceError {
        self.err
    }
}

impl<M> Msg<M> for ErrorMsg<M>
where
    M: Sized + Clone + Tvf,
{
    fn get_id(&self) -> u64 {
        self.id
    }

    fn get_service(&self) -> &String {
        &self.service
    }

    fn get_span(&self) -> &Span {
        &self.span
    }

    fn get_span_mut(&mut self) -> &mut Span {
        &mut self.span
    }

    fn enter_span(&self) -> span::Entered<'_> {
        let enter = self.span.enter();
        event!(Level::WARN, "{}", self.err);
        enter
    }

    fn elapsed(&self) -> Duration {
        self.error_time.elapsed().unwrap_or(Duration::new(0, 0))
    }

    fn get_data(&self) -> Result<&M, BusError> {
        self.data.as_ref().ok_or(BusError::NoData)
    }

    fn get_data_mut(&mut self) -> Result<&mut M, BusError> {
        self.data.as_mut().ok_or(BusError::NoData)
    }

    fn take_data(&mut self) -> Option<M> {
        self.data.take()
    }

    fn take_data_if<P>(&mut self, predicate: P) -> Option<M>
    where
        P: FnOnce(&mut M) -> bool,
    {
        self.data.take_if(predicate)
    }
}