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

use std::collections::HashMap;
use std::collections::VecDeque;

use rand::Rng;

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

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

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

pub trait Consumer {
    fn receive(&self, env: &Envelope);
}

pub struct LogConsumer {

}

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

impl Consumer for LogConsumer {
    fn receive(&self, env: &Envelope) {
        println!("Envelope received: {}", env.id);
    }
}

/// Maneuvering Condition
pub enum ManCon {
    NEO,
    EXTREME,
    VERYHIGH,
    HIGH,
    MEDIUM,
    LOW,
    NONE,
    UNKNOWN
}

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

pub struct Context {

}

pub enum Network {
    IMS,
    LiFi,
    Bluetooth,
    WiFiDirect,
    HTTPS,
    TOR,
    I2P,
    Satellite,
    FSRadio
}

pub struct Node {
    pub local_peers: HashMap<Network, Peer>
}

pub struct Peer {
    pub id: String,
    pub network: Network,
    pub did: DID,
    pub port: u32
}

pub struct DID {
    pub username: String,
    pub passphrase: String,
    pub passphrase2: String,
    pub address: String,
    pub algorithm: String
}

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: HashMap<String, String>
}

impl Envelope {
    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: HashMap::new()
        })
    }
}

pub struct Route {
    pub _service: String,
    pub _op: String,
    pub _orig: String,
    pub _dest: String,
    pub _from: String,
    pub _to: String,
    pub _routed: bool
}

impl Route {
    fn new(service: String, operation: String, orig: String, dest: String, from: String, to: String) -> Route {
        Route {
            _service: service,
            _op: operation,
            _orig: orig,
            _dest: dest,
            _from: from,
            _to: to,
            _routed: false
        }
    }
}

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

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()
    }
}

mod util {
    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);
    }
}