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
/// Common Module
extern crate rand;

use std::collections::HashMap;
use std::collections::VecDeque;
use std::sync::mpsc::SendError;

use rand::Rng;

pub trait LifeCycle {
    fn start(&mut self);
    fn restart(&mut self);
    fn pause(&mut self);
    fn unpause(&mut self);
    fn stop(&mut self);
    fn graceful_stop(&mut self);
}

pub trait Service {
    fn handle(&mut self, op: &String, env: &Envelope);
}

pub trait Producer {
    fn send(&mut self, env: Box<Envelope>);
}

pub trait Consumer {
    fn receive(&mut self) -> Box<Envelope>;
}

pub struct LogConsumer {

}

impl LogConsumer {
    pub fn new() -> Box<LogConsumer> {
        Box::new(LogConsumer {

        })
    }
}

impl Consumer for LogConsumer {
    fn receive(&mut self) -> Box<Envelope> {
        unimplemented!()
    }
}

impl Producer for LogConsumer {
    fn send(&mut self, env: Box<Envelope>) {
        unimplemented!()
    }
}

/// Maneuvering Condition
#[derive(Debug)]
pub enum ManCon {
    NEO,
    EXTREME,
    VERYHIGH,
    HIGH,
    MEDIUM,
    LOW,
    NONE,
    UNKNOWN
}

pub enum Action{POST, PUT, DELETE, GET}

pub struct Context {

}

#[derive(Debug)]
pub enum Network {
    IMS,
    LiFi,
    Bluetooth,
    WiFiDirect,
    HTTPS,
    TOR,
    I2P,
    Satellite,
    FSRadio
}

#[derive(Debug)]
pub struct Node {
    pub local_peers: HashMap<Network, Peer>
}

#[derive(Debug)]
pub struct Peer {
    pub id: String,
    pub network: Network,
    pub did: DID,
    pub port: u32
}

#[derive(Debug)]
pub struct DID {
    pub username: String,
    pub passphrase: String,
    pub passphrase2: String,
    pub address: String,
    pub algorithm: String
}

#[derive(Debug)]
pub struct Envelope {
    pub id: u64,
    /// A stack-based routing slip that can
/// be added to at any time prior to
/// completion.
    pub slip: Slip,
    /// The minimal ManCon for this message
    pub man_con: ManCon,
    /// Delay Until this time in milliseconds since epoch.
/// If min_delay and max_delay also included,
/// include a random delay after delay_until based on
/// their range.
    pub delay_until: u64,
    /// Delay for this many milliseconds as a minimum
    pub min_delay: u64,
    /// Delay for this many milliseconds as a maximum
    pub max_delay: u64,
    /// Meta-data used for assisting with routing
    pub headers: HashMap<String, String>,
    /// Data being sent to a destination
    pub payload: Option<String>
}

impl Envelope {
    pub fn new() -> Box<Envelope> {
        let mut rng = rand::thread_rng();
        Box::new(Envelope {
            id: rng.gen(),
            slip: Slip::new(),
            man_con: ManCon::UNKNOWN,
            delay_until: 0,
            min_delay: 0,
            max_delay: 0,
            headers: HashMap::new(),
            payload: None
        })
    }
}

#[derive(Debug)]
pub struct Route {
    pub _service: String,
    pub _op: String,
    pub _orig: u64,
    pub _dest: u64,
    pub _from: u64,
    pub _to: u64,
    pub _routed: bool
}

impl Route {
    pub fn new_msg_route_no_relay(orig: u64, dest: u64) -> Route {
        Route {
            _service: String::new(),
            _op: String::new(),
            _orig: orig,
            _dest: dest,
            _from: 0,
            _to: 0,
            _routed: false
        }
    }
    pub fn new_msg_route_with_relay(orig: u64, dest: u64, from: u64, to: u64) -> Route {
        Route {
            _service: String::new(),
            _op: String::new(),
            _orig: orig,
            _dest: dest,
            _from: from,
            _to: to,
            _routed: false
        }
    }
    pub fn new_srv_route_no_relay(service: String, operation: String, orig: u64, dest: u64) -> Route {
        Route {
            _service: service,
            _op: operation,
            _orig: orig,
            _dest: dest,
            _from: 0,
            _to: 0,
            _routed: false
        }
    }
    pub fn new_srv_route_with_relay(service: String, operation: String, orig: u64, dest: u64, from: u64, to: u64) -> Route {
        Route {
            _service: service,
            _op: operation,
            _orig: orig,
            _dest: dest,
            _from: from,
            _to: to,
            _routed: false
        }
    }
}

pub trait Router {
    fn route(&self, env: Box<Envelope>) -> Option<Route>;
}

#[derive(Debug)]
pub struct Slip {
    routes: Vec<Route>,
    in_progress: bool
}

impl Slip {
    fn new() -> Slip {
        Slip {
            routes: Vec::with_capacity(2),
            in_progress: false
        }
    }
    fn with_capacity(capacity: usize) -> Slip {
        Slip {
            routes: Vec::with_capacity(capacity),
            in_progress: false
        }
    }
    pub fn add_route(&mut self, r: Route) {
        self.routes.push(r);
    }
    pub fn current_route(&self) -> Option<&Route> {
        self.routes.last()
    }
    pub fn end_route(&mut self) -> Option<Route> {
        self.routes.pop()
    }
    pub fn number_remaining_routes(&self) -> usize {
        self.routes.len()
    }
}

pub mod util {
    pub mod wait {
        use std::{thread, time};
        fn wait_a_day(days: u64) {
            thread::sleep(time::Duration::from_secs(days * 24 * 60 * 60));
        }
        fn wait_a_hour(hours: u64) {
            thread::sleep(time::Duration::from_secs(hours * 60 * 60));
        }
        fn wait_a_minute(minutes: u64) {
            thread::sleep(time::Duration::from_secs(minutes * 60));
        }
        fn wait_a_sec(seconds: u64) {
            thread::sleep(time::Duration::from_secs(seconds));
        }
        fn wait_a_ms(millis: u64) {
            thread::sleep(time::Duration::from_millis(millis));
        }
        fn wait_a_mic(mics: u64) {
            thread::sleep(time::Duration::from_micros(mics));
        }
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}