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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

//! # libqaul
//!
//! Library for qaul.net

use filetime::FileTime;
use futures::prelude::*;
use futures::{future::FutureExt, pin_mut, select};
use futures_ticker::Ticker;
use state::Storage;
use std::collections::BTreeMap;
use std::fs::File;
use std::path::Path;
use std::time::Duration;

// crate modules
pub mod api;
mod connections;
pub mod node;
mod router;
mod rpc;
mod services;
pub mod storage;
pub mod utilities;

use connections::{ble::Ble, internet::Internet, ConnectionModule, Connections};
use node::Node;
use router::{
    feed_requester, flooder, info::RouterInfo, neighbours::Neighbours, user_requester, Router,
};
use rpc::sys::Sys;
use rpc::Rpc;
use services::messaging::Messaging;
use services::Services;
use utilities::filelogger::FileLogger;
use utilities::timestamp::Timestamp;

/// check this when the library finished initializing
static INITIALIZED: Storage<bool> = Storage::new();

/// default configs
static DEFCONFIGS: Storage<BTreeMap<String, String>> = Storage::new();

/// To see logs on android we need the android logger
#[cfg(target_os = "android")]
extern crate log;
#[cfg(target_os = "android")]
use log::Level;

#[cfg(target_os = "android")]
extern crate android_logger;
#[cfg(target_os = "android")]
use android_logger::Config;

/// Get default config values
pub fn get_default_config(pattern: &str) -> Option<String> {
    let def_config = DEFCONFIGS.get();
    if let Some(v) = def_config.get(&pattern.to_string()) {
        return Some(v.clone());
    }
    None
}

/// Events of the async loop
enum EventType {
    Rpc(bool),
    Sys(bool),
    Flooding(bool),
    FeedRequest(bool),
    FeedResponse(bool),
    UserRequest(bool),
    UserResponse(bool),
    RoutingInfo(bool),
    ReConnecting(bool),
    RoutingTable(bool),
    Messaging(bool),
    Retransmit(bool),
}

/// initialize and start libqaul with a optional custom configuration options
/// and poll all the necessary modules
///
/// Input Values:
///
/// * Provide a path where libqaul can save all data.
/// * Optionally you can provide the following configuration values:
///   * listening port of the Internet connection module (default = randomly assigned)
pub async fn start(storage_path: String, def_config: Option<BTreeMap<String, String>>) -> () {
    log::trace!("start initializing libqaul");

    if let Some(def_cfg) = def_config {
        DEFCONFIGS.set(def_cfg.clone());
    } else {
        DEFCONFIGS.set(BTreeMap::new());
    }

    // initialize rpc system
    let libqaul_rpc_receive = Rpc::init();
    let libqaul_sys_receive = Sys::init();

    // initialize storage module.
    // This will initialize configuration & data base
    storage::Storage::init(storage_path.clone());

    // --- initialize logger ---
    // prepare logger path
    // the path of the log file follows the following naming convention:
    // error_234324232.log
    let path = Path::new(&storage_path);
    let log_path = path.join("logs");

    // create log directory if missing
    std::fs::create_dir_all(&log_path).unwrap();

    // create log file name
    let log_file_name: String =
        "error_".to_string() + Timestamp::get_timestamp().to_string().as_str() + ".log";
    let log_file_path = log_path.join(log_file_name);

    // maintain log files
    let paths = std::fs::read_dir(log_path).unwrap();
    // --- logger init-end ---

    let mut logfiles: BTreeMap<i64, String> = BTreeMap::new();
    let mut logfile_times: Vec<i64> = vec![];
    for path in paths {
        let filename = String::from(path.as_ref().unwrap().path().to_str().unwrap());
        let metadata = std::fs::metadata(filename.clone()).unwrap();
        //print!("path={}", path.unwrap().path().display());
        let mtime = FileTime::from_last_modification_time(&metadata);
        //println!("{}", mtime.seconds());
        logfile_times.push(mtime.seconds());
        logfiles.insert(mtime.seconds(), filename);
    }
    logfile_times.sort();

    if logfile_times.len() > 2 {
        for i in 0..(logfile_times.len() - 2) {
            if let Some(filename) = logfiles.get(&logfile_times[i]) {
                std::fs::remove_file(std::path::Path::new(filename)).unwrap();
            }
        }
    }

    // logging on android with android logger
    #[cfg(target_os = "android")]
    {
        let env_logger = Box::new(android_logger::AndroidLogger::new(
            Config::default().with_min_level(Level::Info),
        ));
        let w_logger = FileLogger::new(*simplelog::WriteLogger::new(
            simplelog::LevelFilter::Error,
            simplelog::Config::default(),
            File::create(log_file_path).unwrap(),
        ));
        multi_log::MultiLogger::init(vec![env_logger, Box::new(w_logger)], log::Level::Info)
            .unwrap();
    }

    // logging on ios
    #[cfg(target_os = "ios")]
    {
        let env_logger = Box::new(
            pretty_env_logger::formatted_builder()
                .filter(None, log::LevelFilter::Info)
                .build(),
        );
        let w_logger = FileLogger::new(*simplelog::WriteLogger::new(
            simplelog::LevelFilter::Error,
            simplelog::Config::default(),
            File::create(log_file_path).unwrap(),
        ));
        multi_log::MultiLogger::init(vec![env_logger, Box::new(w_logger)], log::Level::Info)
            .unwrap();
    }

    // only use the simple logger on desktop systems
    #[cfg(not(any(target_os = "android", target_os = "ios")))]
    {
        // find rust env var
        let mut env_log_level = String::from("error");
        for (key, value) in std::env::vars() {
            if key == "RUST_LOG" {
                env_log_level = value;
                break;
            }
        }

        // define log level
        let mut level_filter = log::LevelFilter::Error;
        if env_log_level == "warn" {
            level_filter = log::LevelFilter::Warn;
        } else if env_log_level == "debug" {
            level_filter = log::LevelFilter::Debug;
        } else if env_log_level == "info" {
            level_filter = log::LevelFilter::Info;
        } else if env_log_level == "trace" {
            level_filter = log::LevelFilter::Trace;
        }

        let env_logger = Box::new(
            pretty_env_logger::formatted_builder()
                .filter(None, level_filter)
                .build(),
        );
        let w_logger = FileLogger::new(*simplelog::WriteLogger::new(
            simplelog::LevelFilter::Error,
            simplelog::Config::default(),
            File::create(log_file_path).unwrap(),
        ));
        multi_log::MultiLogger::init(vec![env_logger, Box::new(w_logger)], log::Level::Info)
            .unwrap();
    }

    log::trace!("test log to ensure that logging is working");

    // initialize node & user accounts
    Node::init();

    // initialize router
    Router::init();

    // initialize Connection Modules
    let conn = Connections::init().await;
    let mut internet = conn.internet.unwrap();
    let mut lan = conn.lan.unwrap();

    // initialize services
    Services::init();

    // check RPC once every 10 milliseconds
    // TODO: interval is only in unstable. Use it once it is stable.
    //       https://docs.rs/async-std/1.5.0/async_std/stream/fn.interval.html
    //let mut rpc_interval = async_std::stream::interval(Duration::from_millis(10));
    let mut rpc_ticker = Ticker::new(Duration::from_millis(10));

    // check SYS once every 10 milliseconds
    // TODO: interval is only in unstable. Use it once it is stable.
    //       https://docs.rs/async-std/1.5.0/async_std/stream/fn.interval.html
    //let mut rpc_interval = async_std::stream::interval(Duration::from_millis(10));
    let mut sys_ticker = Ticker::new(Duration::from_millis(10));

    // check flooding message queue periodically
    let mut flooding_ticker = Ticker::new(Duration::from_millis(100));

    // check feed request to neighbour
    let mut feedreq_ticker = Ticker::new(Duration::from_millis(100));

    // check feed request to neighbour
    let mut feedresp_ticker = Ticker::new(Duration::from_millis(100));

    // check user request to neighbour
    let mut userreq_ticker = Ticker::new(Duration::from_millis(100));

    // check user request to neighbour
    let mut userresp_ticker = Ticker::new(Duration::from_millis(100));

    // send routing info periodically to neighbours
    let mut routing_info_ticker = Ticker::new(Duration::from_millis(100));

    // try to connect to intertnet neighbour if there is no connection in internet
    let mut connection_ticker = Ticker::new(Duration::from_millis(1000));

    // re-create routing table periodically
    let mut routing_table_ticker = Ticker::new(Duration::from_millis(1000));

    // manage the message sending
    let mut messaging_ticker = Ticker::new(Duration::from_millis(10));

    // manage the message retransmit
    let mut retransmit_ticker = Ticker::new(Duration::from_millis(1000));

    // set initialized flag
    INITIALIZED.set(true);

    log::trace!("initializing finished, start event loop");

    loop {
        let evt = {
            let lan_fut = lan.swarm.next().fuse();
            let internet_fut = internet.swarm.next().fuse();
            let rpc_fut = rpc_ticker.next().fuse();
            let sys_fut = sys_ticker.next().fuse();
            let flooding_fut = flooding_ticker.next().fuse();
            let feedreq_fut = feedreq_ticker.next().fuse();
            let feedresp_fut = feedresp_ticker.next().fuse();
            let userreq_fut = userreq_ticker.next().fuse();
            let userresp_fut = userresp_ticker.next().fuse();
            let routing_info_fut = routing_info_ticker.next().fuse();
            let connection_fut = connection_ticker.next().fuse();
            let routing_table_fut = routing_table_ticker.next().fuse();
            let messaging_fut = messaging_ticker.next().fuse();
            let retransmit_fut = retransmit_ticker.next().fuse();

            // This Macro is shown wrong by Rust-Language-Server > 0.2.400
            // You need to downgrade to version 0.2.400 if this happens to you
            pin_mut!(
                lan_fut,
                internet_fut,
                rpc_fut,
                sys_fut,
                flooding_fut,
                feedreq_fut,
                feedresp_fut,
                userreq_fut,
                userresp_fut,
                routing_info_fut,
                connection_fut,
                routing_table_fut,
                messaging_fut,
                retransmit_fut,
            );

            select! {
                lan_event = lan_fut => {
                    //log::trace!("Unhandled lan connection module event: {:?}", lan_event);
                    match lan_event.unwrap() {
                        libp2p::swarm::SwarmEvent::ConnectionClosed{peer_id, ..} => {
                            //remove from neighbour table, after then scheduler will auto remove this neighbour
                            log::trace!("lan connection closed: {:?}", peer_id);
                            Neighbours::delete(ConnectionModule::Lan, peer_id);
                        },
                        libp2p::swarm::SwarmEvent::BannedPeer {peer_id, ..} => {
                            //remove from neighbour table, after then scheduler will auto remove this neighbour
                            log::trace!("lan connection banned: {:?}", peer_id);
                            Neighbours::delete(ConnectionModule::Lan, peer_id);
                        },
                        libp2p::swarm::SwarmEvent::Behaviour(behaviour) => {
                            lan.swarm.behaviour_mut().process_events(behaviour);
                        }
                        _ => {}
                    }
                    None
                },
                internet_event = internet_fut => {
                    //log::trace!("Unhandled internet connection module event: {:?}", internet_event);
                    match internet_event.unwrap() {
                        libp2p::swarm::SwarmEvent::OutgoingConnectionError{error, ..} => {
                            // Get list of addresses which we failed to connect to
                            // Since `UnknownPeerUnreachableAddr` error was removed, we need to parse
                            // list of outgoing connection errors to get list of addresses
                            match error {
                                libp2p::swarm::DialError::Transport(unreachable_addrs) => {
                                    for (addr, _) in unreachable_addrs {
                                        Internet::add_reconnection(addr);
                                    }
                                },
                                _ => {}
                            }
                        }
                        libp2p::swarm::SwarmEvent::ConnectionEstablished{peer_id, endpoint, ..} =>{
                            //remove from attempting connections
                            match endpoint{
                                libp2p::core::ConnectedPoint::Dialer{address, ..} =>{
                                    log::info!("connection established! peer={}, endpoint={}", peer_id.to_base58(), address.to_string());
                                    Internet::remove_reconnection(address.clone());
                                    Internet::add_connection(address.to_string(), &peer_id);
                                }
                                _ => {}
                            }
                        }
                        libp2p::swarm::SwarmEvent::ConnectionClosed{peer_id, endpoint, ..} => {
                            // remove from neighbour table, after then scheduler will auto remove this neighbour
                            log::trace!("internet connection closed: {:?}", peer_id);
                            Neighbours::delete(ConnectionModule::Internet, peer_id);

                            // add new reconnection
                            match endpoint {
                                libp2p::core::ConnectedPoint::Dialer{address, ..} =>{
                                    Internet::add_reconnection(address);
                                }
                                _ => {}
                            }
                        }
                        libp2p::swarm::SwarmEvent::BannedPeer {peer_id, ..} => {
                            //remove from neighbour table, after then scheduler will auto remove this neighbour
                            log::trace!("internet connection banned: {:?}", peer_id);
                            Neighbours::delete(ConnectionModule::Internet, peer_id);
                        },
                        libp2p::swarm::SwarmEvent::Behaviour(behaviour) => {
                            internet.swarm.behaviour_mut().process_events(behaviour);
                        }
                        _ => {}
                    }
                    None
                },
                _rpc_event = rpc_fut => Some(EventType::Rpc(true)),
                _sys_event = sys_fut => Some(EventType::Sys(true)),
                _flooding_event = flooding_fut => Some(EventType::Flooding(true)),
                _feedreq_event = feedreq_fut => Some(EventType::FeedRequest(true)),
                _feedresp_event = feedresp_fut => Some(EventType::FeedResponse(true)),
                _userreq_event = userreq_fut => Some(EventType::UserRequest(true)),
                _userresp_event = userresp_fut => Some(EventType::UserResponse(true)),
                _routing_info_event = routing_info_fut => Some(EventType::RoutingInfo(true)),
                _connection_event = connection_fut => Some(EventType::ReConnecting(true)),
                _routing_table_event = routing_table_fut => Some(EventType::RoutingTable(true)),
                _messaging_event = messaging_fut => Some(EventType::Messaging(true)),
                _retransmit_event = retransmit_fut => Some(EventType::Retransmit(true)),
            }
        };

        if let Some(event) = evt {
            match event {
                EventType::Rpc(_) => {
                    if let Ok(rpc_message) = libqaul_rpc_receive.try_recv() {
                        // we received a message, send it to RPC crate
                        Rpc::process_received_message(
                            rpc_message,
                            Some(&mut lan),
                            Some(&mut internet),
                        )
                        .await;
                    }
                }
                EventType::Sys(_) => {
                    if let Ok(sys_message) = libqaul_sys_receive.try_recv() {
                        // we received a message, send it to RPC crate
                        Sys::process_received_message(
                            sys_message,
                            Some(&mut lan),
                            Some(&mut internet),
                        );
                    }
                }
                EventType::Flooding(_) => {
                    // send messages in the flooding queue
                    // get sending queue
                    let mut flooder = flooder::FLOODER.get().write().unwrap();

                    // loop over messages to send & flood them
                    while let Some(msg) = flooder.to_send.pop_front() {
                        // check which swarm to send to
                        if !matches!(msg.incoming_via, ConnectionModule::Lan) {
                            lan.swarm
                                .behaviour_mut()
                                .floodsub
                                .publish(msg.topic.clone(), msg.message.clone());
                        }
                        if !matches!(msg.incoming_via, ConnectionModule::Internet) {
                            internet
                                .swarm
                                .behaviour_mut()
                                .floodsub
                                .publish(msg.topic.clone(), msg.message.clone());
                        }
                        if !matches!(msg.incoming_via, ConnectionModule::Ble) {
                            Ble::send_feed_message(msg.topic, msg.message);
                        }
                    }
                }
                EventType::FeedRequest(_) => {
                    // send messages in the flooding queue
                    // get sending queue
                    let mut feed_requester = feed_requester::FEEDREQUESTER.get().write().unwrap();

                    // loop over messages to send & flood them
                    while let Some(request) = feed_requester.to_send.pop_front() {
                        let connection_module = Neighbours::is_neighbour(&request.neighbour_id);
                        if connection_module == ConnectionModule::None {
                            log::error!(
                                "sending feed requests, node is not a neighbour anymore: {:?}",
                                request.neighbour_id
                            );
                            continue;
                        }
                        //make dataMessaging
                        let data = RouterInfo::create_feed_request(&request.feed_ids);
                        match connection_module {
                            ConnectionModule::Lan => lan
                                .swarm
                                .behaviour_mut()
                                .qaul_info
                                .send_qaul_info_message(request.neighbour_id, data),
                            ConnectionModule::Internet => internet
                                .swarm
                                .behaviour_mut()
                                .qaul_info
                                .send_qaul_info_message(request.neighbour_id, data),
                            ConnectionModule::Ble => {
                                Ble::send_routing_info(request.neighbour_id, data);
                            }
                            ConnectionModule::Local => {}
                            ConnectionModule::None => {}
                        }
                    }
                }
                EventType::FeedResponse(_) => {
                    // send messages in the flooding queue
                    // get sending queue
                    let mut feed_responser = feed_requester::FEEDRESPONSER.get().write().unwrap();

                    // loop over messages to send & flood them
                    while let Some(request) = feed_responser.to_send.pop_front() {
                        let connection_module = Neighbours::is_neighbour(&request.neighbour_id);
                        if connection_module == ConnectionModule::None {
                            log::error!(
                                "sending feed requests, node is not a neighbour anymore: {:?}",
                                request.neighbour_id
                            );
                            continue;
                        }

                        //make data
                        let data = RouterInfo::create_feed_response(&request.feeds);
                        match connection_module {
                            ConnectionModule::Lan => lan
                                .swarm
                                .behaviour_mut()
                                .qaul_info
                                .send_qaul_info_message(request.neighbour_id, data),
                            ConnectionModule::Internet => internet
                                .swarm
                                .behaviour_mut()
                                .qaul_info
                                .send_qaul_info_message(request.neighbour_id, data),
                            ConnectionModule::Ble => {
                                Ble::send_routing_info(request.neighbour_id, data);
                            }
                            ConnectionModule::Local => {}
                            ConnectionModule::None => {}
                        }
                    }
                }
                EventType::UserRequest(_) => {
                    // send messages in the flooding queue
                    // get sending queue
                    let mut user_requester = user_requester::USERREQUESTER.get().write().unwrap();

                    // loop over messages to send & flood them
                    while let Some(request) = user_requester.to_send.pop_front() {
                        let connection_module = Neighbours::is_neighbour(&request.neighbour_id);
                        if connection_module == ConnectionModule::None {
                            log::error!(
                                "sending feed requests, node is not a neighbour anymore: {:?}",
                                request.neighbour_id
                            );
                            continue;
                        }
                        //make dataMessaging
                        let data = RouterInfo::create_user_request(&request.user_ids);
                        match connection_module {
                            ConnectionModule::Lan => lan
                                .swarm
                                .behaviour_mut()
                                .qaul_info
                                .send_qaul_info_message(request.neighbour_id, data),
                            ConnectionModule::Internet => internet
                                .swarm
                                .behaviour_mut()
                                .qaul_info
                                .send_qaul_info_message(request.neighbour_id, data),
                            ConnectionModule::Ble => {
                                Ble::send_routing_info(request.neighbour_id, data);
                            }
                            ConnectionModule::Local => {}
                            ConnectionModule::None => {}
                        }
                    }
                }
                EventType::UserResponse(_) => {
                    // send messages in the flooding queue
                    // get sending queue
                    let mut user_responser = user_requester::USERRESPONSER.get().write().unwrap();

                    // loop over messages to send & flood them
                    while let Some(request) = user_responser.to_send.pop_front() {
                        let connection_module = Neighbours::is_neighbour(&request.neighbour_id);
                        if connection_module == ConnectionModule::None {
                            log::error!(
                                "sending feed requests, node is not a neighbour anymore: {:?}",
                                request.neighbour_id
                            );
                            continue;
                        }

                        //make data
                        let data = RouterInfo::create_user_response(&request.users);
                        match connection_module {
                            ConnectionModule::Lan => lan
                                .swarm
                                .behaviour_mut()
                                .qaul_info
                                .send_qaul_info_message(request.neighbour_id, data),
                            ConnectionModule::Internet => internet
                                .swarm
                                .behaviour_mut()
                                .qaul_info
                                .send_qaul_info_message(request.neighbour_id, data),
                            ConnectionModule::Ble => {
                                Ble::send_routing_info(request.neighbour_id, data);
                            }
                            ConnectionModule::Local => {}
                            ConnectionModule::None => {}
                        }
                    }
                }

                EventType::RoutingInfo(_) => {
                    // send routing info to neighbours
                    // check scheduler
                    if let Some((neighbour_id, connection_module, data)) =
                        RouterInfo::check_scheduler()
                    {
                        log::trace!(
                            "sending routing information via {:?} to {:?}, {:?}",
                            connection_module,
                            neighbour_id,
                            Timestamp::get_timestamp()
                        );
                        // send routing information
                        match connection_module {
                            ConnectionModule::Lan => lan
                                .swarm
                                .behaviour_mut()
                                .qaul_info
                                .send_qaul_info_message(neighbour_id, data),
                            ConnectionModule::Internet => internet
                                .swarm
                                .behaviour_mut()
                                .qaul_info
                                .send_qaul_info_message(neighbour_id, data),
                            ConnectionModule::Ble => {
                                Ble::send_routing_info(neighbour_id, data);
                            }
                            ConnectionModule::Local => {}
                            ConnectionModule::None => {}
                        }
                    }
                }
                EventType::ReConnecting(_) => {
                    if let Some(addr) = Internet::check_reconnection() {
                        log::trace!("redial....: {:?}", addr);
                        Internet::peer_redial(&addr, &mut internet.swarm).await;
                        Internet::set_redialed(&addr);
                    }

                    if let Some((addr, enabled)) = Internet::check_change_connection() {
                        if let Some(peer_id) = Internet::peerid_from_address(addr.to_string()) {
                            if !enabled {
                                internet.swarm.ban_peer_id(peer_id.clone());
                            } else {
                                internet.swarm.unban_peer_id(peer_id.clone());
                            }
                        }
                    }
                }
                EventType::RoutingTable(_) => {
                    // create new routing table
                    router::connections::ConnectionTable::create_routing_table();
                }
                EventType::Messaging(_) => {
                    // send scheduled messages
                    if let Some((neighbour_id, connection_module, data)) =
                        Messaging::check_scheduler()
                    {
                        log::trace!(
                            "sending messaging message via {:?} to {}",
                            connection_module,
                            neighbour_id.to_base58()
                        );
                        // send messaging message via the best module
                        match connection_module {
                            ConnectionModule::Lan => {
                                lan.swarm
                                    .behaviour_mut()
                                    .qaul_messaging
                                    .send_qaul_messaging_message(neighbour_id, data);
                            }
                            ConnectionModule::Internet => {
                                internet
                                    .swarm
                                    .behaviour_mut()
                                    .qaul_messaging
                                    .send_qaul_messaging_message(neighbour_id, data);
                            }
                            ConnectionModule::Ble => {
                                Ble::send_messaging_message(neighbour_id, data);
                            }
                            ConnectionModule::Local => {
                                let message = qaul_messaging::types::QaulMessagingReceived {
                                    received_from: neighbour_id,
                                    data,
                                };
                                // forward to messaging module
                                Messaging::received(message);
                            }
                            ConnectionModule::None => {
                                // TODO: DTN behaviour
                                // reschedule it for the moment
                            }
                        }
                    }
                }
                EventType::Retransmit(_) => {
                    services::messaging::retransmit::MessagingRetransmit::process();
                }
            }
        }
    }
}