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
pub mod application_agent;
pub mod bundlepack;
pub mod helpers;
pub mod peer;
pub mod processing;
pub mod store;

use crate::cla::ConvergenceLayerAgent;
use crate::core::bundlepack::Constraint;
pub use crate::core::peer::{DtnPeer, PeerType};
use crate::core::store::BundleStore;
use crate::routing::RoutingAgentsEnum;
use crate::{routing_notify, store_delete_expired, store_get_bundle, store_get_metadata, CLAS};
pub use crate::{store_has_item, store_push_bundle};
use crate::{RoutingNotifcation, CONFIG};
use crate::{PEERS, STORE};
use application_agent::ApplicationAgent;
use bp7::EndpointID;
use log::debug;
use log::trace;
use log::{error, info};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Instant;

use crate::core::application_agent::ApplicationAgentEnum;

use self::bundlepack::BundlePack;
use self::processing::forward;

#[derive(Debug, Serialize, Deserialize, Default, Clone)]
pub struct DtnStatistics {
    pub incoming: u64,
    pub dups: u64,
    pub outgoing: u64,
    pub delivered: u64,
    pub failed: u64,
    pub broken: u64,
}

impl DtnStatistics {
    pub fn new() -> DtnStatistics {
        DtnStatistics {
            incoming: 0,
            dups: 0,
            outgoing: 0,
            delivered: 0,
            failed: 0,
            broken: 0,
        }
    }
}
#[derive(Debug)]
pub struct DtnCore {
    pub endpoints: Vec<ApplicationAgentEnum>,
    pub service_list: HashMap<u8, String>,
    pub routing_agent: RoutingAgentsEnum,
}

impl Default for DtnCore {
    fn default() -> Self {
        Self::new()
    }
}

impl DtnCore {
    pub fn new() -> DtnCore {
        DtnCore {
            endpoints: Vec::new(),
            service_list: HashMap::new(),
            //routing_agent: crate::routing::flooding::FloodingRoutingAgent::new().into(),
            routing_agent: crate::routing::epidemic::EpidemicRoutingAgent::new().into(),
        }
    }

    pub fn register_application_agent(&mut self, aa: ApplicationAgentEnum) {
        if self.is_in_endpoints(aa.eid()) {
            info!("Application agent already registered for EID: {}", aa.eid());
        } else {
            info!("Registered new application agent for EID: {}", aa.eid());
            self.endpoints.push(aa);
        }
    }
    pub fn unregister_application_agent(&mut self, aa: ApplicationAgentEnum) {
        info!("Unregistered application agent for EID: {}", aa.eid());
        self.endpoints
            .iter()
            .position(|n| n.eid() == aa.eid())
            .map(|e| self.endpoints.remove(e));
    }
    pub fn eids(&self) -> Vec<String> {
        self.endpoints.iter().map(|e| e.eid().to_string()).collect()
    }
    pub fn bundle_ids(&self) -> Vec<String> {
        (*STORE.lock()).all_ids()
    }
    pub fn bundle_count(&self) -> usize {
        (*STORE.lock()).count() as usize
    }
    pub fn bundle_full_meta(&self) -> Vec<String> {
        (*STORE.lock()).src_dst_ts()
    }
    pub fn is_in_endpoints(&self, eid: &EndpointID) -> bool {
        for aa in self.endpoints.iter() {
            if eid == aa.eid() {
                return true;
            }
        }
        false
    }
    pub fn get_endpoint_mut(&mut self, eid: &EndpointID) -> Option<&mut ApplicationAgentEnum> {
        self.endpoints.iter_mut().find(|aa| eid == aa.eid())
    }
    pub fn get_endpoint(&self, eid: &EndpointID) -> Option<&ApplicationAgentEnum> {
        self.endpoints.iter().find(|&aa| eid == aa.eid())
    }
}

/// Removes peers from global peer list that haven't been seen in a while.
pub async fn process_peers() {
    let mut dropped: Vec<EndpointID> = Vec::new();

    (*PEERS.lock()).retain(|_k, v| {
        let val = v.still_valid();
        if !val {
            info!(
                "Have not seen {} @ {} in a while, removing it from list of known peers",
                v.eid, v.addr
            );

            dropped.push(v.eid.clone());
        }
        v.con_type == PeerType::Static || val
    });

    for eid in dropped {
        if let Err(err) = routing_notify(RoutingNotifcation::DroppedPeer(eid)).await {
            error!("Error while dropping peer: {}", err);
        }
    }
}

/// Reprocess bundles in store
pub async fn process_bundles() {
    let now_total = Instant::now();

    store_delete_expired();

    let active_cla = (*CLAS.lock()).iter().any(|p| p.accepting());
    if !active_cla {
        debug!("No active/push CLA, not forwarding any bundles");
        debug!("time to process bundles: {:?}", now_total.elapsed());
        return;
    }

    let forwarding_bids: Vec<String> = (*STORE.lock()).forwarding();

    let mut forwarding_bundles: Vec<BundlePack> = forwarding_bids
        .iter()
        .filter_map(|bid| store_get_metadata(bid))
        .filter(|bp| !bp.has_constraint(Constraint::Deleted))
        .collect();

    // process them in chronological order
    forwarding_bundles.sort_unstable_by(|a, b| a.creation_time.cmp(&b.creation_time));

    let num_bundles = forwarding_bundles.len();

    if CONFIG.lock().parallel_bundle_processing {
        let mut tasks = Vec::new();
        for bp in forwarding_bundles {
            let bpid = bp.id().to_string();
            let task_handle = tokio::spawn(async move {
                let now = Instant::now();
                if let Err(err) = forward(bp).await {
                    error!("Error forwarding bundle: {}", err);
                }
                trace!("Forwarding time: {:?} for {}", now.elapsed(), bpid);
            });
            tasks.push(task_handle);
        }
        use futures::future::join_all;

        join_all(tasks).await;
    } else {
        for bp in forwarding_bundles {
            let bpid = bp.id().to_string();

            let now = Instant::now();
            if let Err(err) = forward(bp).await {
                error!("Error forwarding bundle: {}", err);
            }
            trace!("Forwarding time: {:?} for {}", now.elapsed(), bpid);
        }
    }

    debug!(
        "time to process {} bundles: {:?}",
        num_bundles,
        now_total.elapsed()
    );
    //forwarding_bundle_ids.iter().for_each(|bpid| {});
}