flmodules 0.10.0

Modules used in fledger
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
456
//! Handles all the incoming messages and produces messages for the modules
//! connected to [Network].

use core::panic;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt::Display};
use tokio::sync::watch;

use flarch::{
    broker::SubsystemHandler,
    nodeids::{NodeID, U256},
    platform_async_trait,
    web_rtc::{
        messages::PeerInfo,
        node_connection::{Direction, NCInput, NCOutput},
        websocket::{WSClientIn, WSClientOut},
        WebRTCConnIn, WebRTCConnOut,
    },
};

use crate::{
    network::{
        broker::{ConnStats, NetworkConnectionState, NetworkIn, NetworkOut, MODULE_NAME},
        signal::{MessageAnnounce, WSSignalMessageFromNode, WSSignalMessageToNode, SIGNAL_VERSION},
    },
    nodeconfig::NodeConfig,
    router::messages::NetworkWrapper,
};

#[derive(Debug, Serialize, Deserialize)]
pub(super) enum ModuleMessage {
    SignalIn(WSClientOut),
    SignalOut(WSClientIn),
}

#[derive(Debug, Clone, PartialEq)]
pub(super) enum InternIn {
    Network(NetworkIn),
    WebSocket(WSClientOut),
    WebRTC(WebRTCConnOut),
    Tick,
}

#[derive(Debug, Clone, PartialEq)]
pub(super) enum InternOut {
    Network(NetworkOut),
    WebSocket(WSClientIn),
    WebRTC(WebRTCConnIn),
}

const UPDATE_INTERVAL: usize = 60;

pub(super) struct Intern {
    node_config: NodeConfig,
    get_update: usize,
    connections: HashMap<NodeID, IOConnection>,
    tx: Option<watch::Sender<Vec<NodeID>>>,
}

impl Intern {
    pub async fn start(
        node_config: NodeConfig,
    ) -> anyhow::Result<(Self, watch::Receiver<Vec<NodeID>>)> {
        let (tx, rx) = watch::channel(vec![]);
        Ok((
            Self {
                node_config,
                get_update: UPDATE_INTERVAL,
                connections: HashMap::new(),
                tx: Some(tx),
            },
            rx,
        ))
    }

    /// Processes incoming messages from the signalling server.
    /// This can be either messages requested by this node, or connection
    /// setup requests from another node.
    fn msg_ws(&mut self, msg: WSClientOut) -> Vec<InternOut> {
        let msg_node_str = match msg {
            WSClientOut::Message(msg) => msg,
            WSClientOut::Error(e) => {
                log::warn!("Websocket client error: {e}");
                return vec![];
            }
            _ => return vec![],
        };
        let msg_node =
            if let Ok(msg_node) = serde_json::from_str::<WSSignalMessageToNode>(&msg_node_str) {
                msg_node
            } else {
                return vec![];
            };
        match msg_node {
            WSSignalMessageToNode::Challenge(version, challenge) => {
                if version != SIGNAL_VERSION {
                    panic!(
                        "Wrong signal-server version: got {version}, expected {SIGNAL_VERSION}."
                    );
                }
                let ma = MessageAnnounce {
                    version,
                    challenge,
                    node_info: self.node_config.info.clone(),
                    signature: self.node_config.sign(challenge.to_bytes()),
                };
                vec![
                    WSSignalMessageFromNode::Announce(ma),
                    WSSignalMessageFromNode::ListIDsRequest,
                ]
                .into_vec()
            }
            WSSignalMessageToNode::ListIDsReply(list) => {
                vec![NetworkOut::NodeListFromWS(list).into()]
            }
            WSSignalMessageToNode::PeerSetup(pi) => {
                let own_id = self.node_config.info.get_id();
                let remote_node = match pi.get_remote(&own_id) {
                    Some(id) => id,
                    None => {
                        log::warn!("Got PeerSetup where this node is not involved");
                        return vec![];
                    }
                };
                let mut out = vec![];
                let dir = pi.get_direction(&own_id);
                if self.connected_only(&remote_node, &dir) {
                    out.push(NetworkOut::Disconnected(remote_node.clone()).into());
                    self.send_connections();
                }
                self.connection_state_set(remote_node.clone(), &dir, ConnectionState::Setup);
                out.push(InternOut::WebRTC(WebRTCConnIn::Message(
                    remote_node,
                    NCInput::Setup(dir, pi.message),
                )));
                out
            }
            WSSignalMessageToNode::SystemConfig(fledger_config) => {
                vec![NetworkOut::SystemConfig(fledger_config)].into_vec()
            }
            WSSignalMessageToNode::Error(e) => vec![InternOut::Network(NetworkOut::Error(e))],
        }
    }

    fn msg_net(&mut self, msg: NetworkIn) -> Vec<InternOut> {
        match msg {
            NetworkIn::MessageToNode(id, msg_nw) => {
                log::trace!(
                    "msg_call: {}->{}: {:?} / {:?}",
                    self.node_config.info.get_id(),
                    id,
                    msg_nw,
                    self.connections
                );

                let mut out = vec![];
                if let Ok(s) = serde_yaml::to_string(&msg_nw) {
                    if !self.connected(&id) {
                        out.append(&mut self.connect(&id));
                    }
                    out.push(InternOut::WebRTC(WebRTCConnIn::Message(
                        id,
                        NCInput::Text(s),
                    )));
                }
                out
            }
            NetworkIn::StatsToWS(ss) => WSSignalMessageFromNode::NodeStats(ss.clone()).into(),
            NetworkIn::WSUpdateListRequest => WSSignalMessageFromNode::ListIDsRequest.into(),
            NetworkIn::Connect(id) => self.connect(&id),
            NetworkIn::Disconnect(id) => self.disconnect(&id),
        }
    }

    fn msg_tick(&mut self) -> Vec<InternOut> {
        self.get_update -= 1;
        (self.get_update == 0)
            .then(|| {
                self.get_update = UPDATE_INTERVAL;
                vec![WSSignalMessageFromNode::ListIDsRequest.into()]
            })
            .unwrap_or(vec![])
    }

    fn msg_wrapper(&mut self, id: NodeID, msg: String) -> Vec<InternOut> {
        match serde_yaml::from_str::<NetworkWrapper>(&msg) {
            Ok(nw) => {
                if let Some(net_msg) = nw.unwrap_yaml::<ModuleMessage>(MODULE_NAME) {
                    match net_msg {
                        ModuleMessage::SignalIn(_) => todo!(),
                        ModuleMessage::SignalOut(_) => todo!(),
                    }
                }
                return vec![NetworkOut::MessageFromNode(id, nw).into()];
            }
            Err(e) => log::debug!("Couldn't unwrap {msg}: {e:?}"),
        }
        vec![]
    }

    fn msg_rtc(&mut self, id: NodeID, msg_nc: NCOutput) -> Vec<InternOut> {
        // log::info!("NC Message from {id}: {msg_nc}");
        match msg_nc {
            NCOutput::Connected(dir) => {
                let new_connection = !self.connected(&id);
                self.connection_state_set(id, &dir, ConnectionState::Connected);
                if new_connection {
                    vec![NetworkOut::Connected(id).into()]
                } else {
                    vec![]
                }
            }
            NCOutput::Disconnected(dir) => {
                let new_disconnection = self.connected_only(&id, &dir);
                self.connection_state_set(id, &dir, ConnectionState::None);
                if new_disconnection {
                    vec![NetworkOut::Disconnected(id).into()]
                } else {
                    vec![]
                }
            }
            NCOutput::Text(msg) => {
                if !self.connected(&id) {
                    log::warn!("Got text for unconnected node ({id})");
                }
                self.msg_wrapper(id, msg)
            }
            NCOutput::State(dir, state) => {
                vec![NetworkOut::ConnectionState(NetworkConnectionState {
                    id,
                    dir,
                    s: ConnStats {
                        type_local: state.type_local,
                        type_remote: state.type_remote,
                        signaling: state.signaling,
                        rx_bytes: state.rx_bytes,
                        tx_bytes: state.tx_bytes,
                        delay_ms: state.delay_ms,
                    },
                })
                .into()]
            }
            NCOutput::Setup(dir, pm) => {
                let mut id_init = self.node_config.info.get_id();
                let mut id_follow = id;
                if dir == Direction::Incoming {
                    (id_init, id_follow) = (id_follow, id_init);
                }
                vec![WSSignalMessageFromNode::PeerSetup(PeerInfo {
                    id_init,
                    id_follow,
                    message: pm,
                })
                .into()]
            }
            _ => vec![],
        }
    }

    /// Connect to the given node.
    fn connect(&mut self, dst: &U256) -> Vec<InternOut> {
        if self.connected(dst) {
            log::trace!("Already connected to {}", dst);
            vec![]
        } else {
            if self.connection_state_check(dst, &Direction::Outgoing, &ConnectionState::Setup) {
                // TODO: with the Helium browser, this message appears a lot. And then Helium doesn't
                // connect with webrtc.
                log::warn!(
                    "Asking to connect to {dst} while connection setup is in place - starting new setup"
                );
            } else {
                self.connection_state_set(*dst, &Direction::Outgoing, ConnectionState::Setup);
            }
            vec![InternOut::WebRTC(WebRTCConnIn::Connect(*dst))]
        }
    }

    /// Disconnects from a given node.
    fn disconnect(&mut self, dst: &U256) -> Vec<InternOut> {
        if !self.connected(dst) {
            log::trace!("Already disconnected from {}", dst);
            vec![]
        } else {
            self.connection_state_set(*dst, &Direction::Incoming, ConnectionState::None);
            self.connection_state_set(*dst, &Direction::Outgoing, ConnectionState::None);
            self.send_connections();
            return vec![
                NetworkOut::Disconnected(*dst).into(),
                InternOut::WebRTC(WebRTCConnIn::Message(*dst, NCInput::Disconnect)),
            ];
        }
    }

    fn send_connections(&mut self) {
        self.tx.clone().map(|tx| {
            tx.send(
                self.connections
                    .iter()
                    .filter_map(|(id, _)| self.connected(id).then(|| id.clone()))
                    .collect::<Vec<_>>(),
            )
            .is_err()
            .then(|| self.tx = None)
        });
    }
    fn connected_only(&self, node: &NodeID, dir: &Direction) -> bool {
        self.connection_state_check(node, dir, &ConnectionState::Connected)
            && !self.connection_state_check(node, &dir.other(), &ConnectionState::Connected)
    }

    fn connected(&self, node: &NodeID) -> bool {
        self.connection_state_check_any(node, &ConnectionState::Connected)
    }

    fn connection_state_check(
        &self,
        node: &NodeID,
        dir: &Direction,
        state: &ConnectionState,
    ) -> bool {
        self.connections.get(node).map(|ioc| ioc.get(dir)) == Some(state)
    }

    fn connection_state_check_any(&self, node: &NodeID, state: &ConnectionState) -> bool {
        self.connection_state_check(node, &Direction::Incoming, state)
            || self.connection_state_check(node, &Direction::Outgoing, state)
    }

    fn connection_state_set(&mut self, node: NodeID, dir: &Direction, state: ConnectionState) {
        // log::info!(
        //     "New connection-state for node {} in direction {:?} = {:?}",
        //     node,
        //     dir,
        //     state
        // );
        self.connections
            .entry(node)
            .and_modify(|ioc| ioc.set(dir, state.clone()))
            .or_insert(IOConnection::new(dir, state));
    }
}

#[derive(Debug, Clone, PartialEq)]
enum ConnectionState {
    None,
    Setup,
    Connected,
}

#[derive(Debug, Clone)]
struct IOConnection {
    incoming: ConnectionState,
    outgoing: ConnectionState,
}

impl IOConnection {
    fn new(dir: &Direction, state: ConnectionState) -> Self {
        let mut ioc = IOConnection {
            incoming: ConnectionState::None,
            outgoing: ConnectionState::None,
        };
        ioc.set(dir, state);
        ioc
    }

    fn get(&self, dir: &Direction) -> &ConnectionState {
        match dir {
            Direction::Incoming => &self.incoming,
            Direction::Outgoing => &self.outgoing,
        }
    }

    fn set(&mut self, dir: &Direction, state: ConnectionState) {
        match dir {
            Direction::Incoming => self.incoming = state,
            Direction::Outgoing => self.outgoing = state,
        }
    }
}

#[platform_async_trait()]
impl SubsystemHandler<InternIn, InternOut> for Intern {
    async fn messages(&mut self, msgs: Vec<InternIn>) -> Vec<InternOut> {
        let id = self.node_config.info.get_id();
        msgs.into_iter()
            .inspect(|msg| log::trace!("{id}: Processing message {msg}",))
            .flat_map(|msg| match msg {
                InternIn::WebSocket(ws) => self.msg_ws(ws),
                InternIn::WebRTC(WebRTCConnOut::Message(id, msg)) => self.msg_rtc(id, msg),
                InternIn::Network(net) => self.msg_net(net),
                InternIn::Tick => self.msg_tick(),
            })
            .collect::<Vec<_>>()
    }
}

impl From<NetworkOut> for InternOut {
    fn from(value: NetworkOut) -> Self {
        InternOut::Network(value)
    }
}

impl From<WSClientIn> for InternOut {
    fn from(value: WSClientIn) -> Self {
        InternOut::WebSocket(value)
    }
}

impl From<WebRTCConnIn> for InternOut {
    fn from(value: WebRTCConnIn) -> Self {
        InternOut::WebRTC(value)
    }
}

impl From<WSSignalMessageFromNode> for InternOut {
    fn from(msg: WSSignalMessageFromNode) -> Self {
        InternOut::WebSocket(WSClientIn::Message(serde_json::to_string(&msg).unwrap()))
    }
}

impl From<WSSignalMessageFromNode> for Vec<InternOut> {
    fn from(msg: WSSignalMessageFromNode) -> Self {
        vec![msg.into()]
    }
}

impl Display for InternIn {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            InternIn::Network(network_in) => {
                write!(f, "InternalIn::Network({})", network_in)
            }
            InternIn::Tick => write!(f, "InternalIn::Tick"),
            InternIn::WebSocket(wsclient_out) => {
                write!(f, "InternalIn::WebSocket({:?})", wsclient_out)
            }
            InternIn::WebRTC(web_rtcconn_output) => {
                write!(f, "InternalIn::WebRTC({:?})", web_rtcconn_output)
            }
        }
    }
}

pub trait IntoVec<D> {
    fn into_vec(self) -> Vec<D>;
}

impl<E, D> IntoVec<D> for Vec<E>
where
    D: From<E>,
{
    fn into_vec(self) -> Vec<D> {
        self.into_iter().map(std::convert::Into::into).collect()
    }
}