use std::collections::HashMap;
use std::convert::{TryFrom};
pub trait Service {
fn operate(&mut self, operation: u8, env: Envelope);
}
#[derive(Debug, Copy, Clone)]
pub enum ServiceStatus {
Unregistered = 0,
NotInitialized = 1,
Initializing = 2,
Waiting = 3,
Starting = 4,
Running = 5,
Verified = 6,
PartiallyRunning = 7,
DegradedRunning = 8,
Blocked = 9,
Unstable = 10,
Pausing = 11,
Paused = 12,
Unpausing = 13,
ShuttingDown = 14,
GracefullyShuttingDown = 15,
Shutdown = 16,
GracefullyShutdown = 17,
Restarting = 18,
Unavailable = 19,
GenError = 20
}
impl ServiceStatus {
fn as_str(&self) -> &'static str {
match *self {
ServiceStatus::Unregistered => "Unregistered",
ServiceStatus::NotInitialized => "NotInitialized",
ServiceStatus::Initializing => "Initializing",
ServiceStatus::Waiting => "Waiting",
ServiceStatus::Starting => "Starting",
ServiceStatus::Running => "Running",
ServiceStatus::Verified => "Verified",
ServiceStatus::PartiallyRunning => "PartiallyRunning",
ServiceStatus::DegradedRunning => "DegradedRunning",
ServiceStatus::Blocked => "Blocked",
ServiceStatus::Unstable => "Unstable",
ServiceStatus::Pausing => "Pausing",
ServiceStatus::Paused => "Paused",
ServiceStatus::Unpausing => "Unpausing",
ServiceStatus::ShuttingDown => "ShuttingDown",
ServiceStatus::GracefullyShuttingDown => "GracefullyShuttingDown",
ServiceStatus::Shutdown => "Shutdown",
ServiceStatus::GracefullyShutdown => "GracefullyShutdown",
ServiceStatus::Restarting => "Restarting",
ServiceStatus::Unavailable => "Unavailable",
ServiceStatus::GenError => "GenError"
}
}
}
impl TryFrom<u8> for ServiceStatus {
type Error = ();
fn try_from(original: u8) -> Result<Self, Self::Error> {
match original {
0 => Ok(ServiceStatus::Unregistered),
1 => Ok(ServiceStatus::NotInitialized),
2 => Ok(ServiceStatus::Initializing),
3 => Ok(ServiceStatus::Waiting),
4 => Ok(ServiceStatus::Starting),
5 => Ok(ServiceStatus::Running),
6 => Ok(ServiceStatus::Verified),
7 => Ok(ServiceStatus::PartiallyRunning),
8 => Ok(ServiceStatus::DegradedRunning),
9 => Ok(ServiceStatus::Blocked),
10 => Ok(ServiceStatus::Unstable),
11 => Ok(ServiceStatus::Pausing),
12 => Ok(ServiceStatus::Paused),
13 => Ok(ServiceStatus::Unpausing),
14 => Ok(ServiceStatus::ShuttingDown),
15 => Ok(ServiceStatus::GracefullyShuttingDown),
16 => Ok(ServiceStatus::Shutdown),
17 => Ok(ServiceStatus::GracefullyShutdown),
18 => Ok(ServiceStatus::Restarting),
19 => Ok(ServiceStatus::Unavailable),
20 => Ok(ServiceStatus::GenError),
n => Err(())
}
}
}
pub trait Producer {
fn send(&mut self, env: Envelope);
}
pub trait Consumer {
fn receive(&mut self) -> Envelope;
}
#[derive(Debug, Copy, Clone)]
pub enum Action {
POST = 0,
PUT = 1,
DELETE = 2,
GET = 3
}
#[derive(Debug, Copy, Clone)]
pub enum NetworkId {
IMS = 0,
LiFi = 1,
Bluetooth = 2,
WiFiDirect = 3,
HTTPS = 4,
VPN = 5,
TOR = 6,
I2P = 7,
Satellite = 8,
FSRadio = 9
}
impl TryFrom<u8> for NetworkId {
type Error = ();
fn try_from(original: u8) -> Result<Self, Self::Error> {
match original {
0 => Ok(NetworkId::IMS),
1 => Ok(NetworkId::LiFi),
2 => Ok(NetworkId::Bluetooth),
3 => Ok(NetworkId::WiFiDirect),
4 => Ok(NetworkId::HTTPS),
5 => Ok(NetworkId::VPN),
6 => Ok(NetworkId::TOR),
7 => Ok(NetworkId::I2P),
8 => Ok(NetworkId::Satellite),
9 => Ok(NetworkId::FSRadio),
_ => Err(())
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum NetworkStatus {
Unregistered = 0,
NotInitialized = 1,
Initializing = 2,
Starting = 3,
Waiting = 4,
NetworkWarmup = 5,
NetworkPortConflict = 6,
NetworkConnecting = 7,
NetworkConnected = 8,
NetworkVerified = 9,
NetworkStopping = 10,
NetworkStopped = 11,
NetworkBlocked = 12,
NetworkUnavailable = 13,
NetworkError = 14,
Pausing = 15,
Paused = 16,
Unpausing = 17,
ShuttingDown = 18,
GracefullyShuttingDown = 19,
Shutdown = 20,
GracefullyShutdown = 21,
Restarting = 22,
GenError = 23
}
impl TryFrom<u8> for NetworkStatus {
type Error = ();
fn try_from(original: u8) -> Result<Self, Self::Error> {
match original {
0 => Ok(NetworkStatus::Unregistered),
1 => Ok(NetworkStatus::NotInitialized),
2 => Ok(NetworkStatus::Initializing),
3 => Ok(NetworkStatus::Starting),
4 => Ok(NetworkStatus::Waiting),
5 => Ok(NetworkStatus::NetworkWarmup),
6 => Ok(NetworkStatus::NetworkPortConflict),
7 => Ok(NetworkStatus::NetworkConnecting),
8 => Ok(NetworkStatus::NetworkConnected),
9 => Ok(NetworkStatus::NetworkVerified),
9 => Ok(NetworkStatus::NetworkStopping),
9 => Ok(NetworkStatus::NetworkStopped),
9 => Ok(NetworkStatus::NetworkBlocked),
9 => Ok(NetworkStatus::NetworkUnavailable),
9 => Ok(NetworkStatus::NetworkError),
9 => Ok(NetworkStatus::Pausing),
9 => Ok(NetworkStatus::Paused),
9 => Ok(NetworkStatus::Unpausing),
9 => Ok(NetworkStatus::ShuttingDown),
9 => Ok(NetworkStatus::GracefullyShuttingDown),
9 => Ok(NetworkStatus::Shutdown),
9 => Ok(NetworkStatus::GracefullyShutdown),
9 => Ok(NetworkStatus::Restarting),
9 => Ok(NetworkStatus::GenError),
_ => Err(())
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum PacketType {
Data = 0,
Fin = 1,
Ack = 2,
Reset = 3,
Syn = 4,
}
impl TryFrom<u8> for PacketType {
type Error = ();
fn try_from(original: u8) -> Result<Self, Self::Error> {
match original {
0 => Ok(PacketType::Data),
1 => Ok(PacketType::Fin),
2 => Ok(PacketType::Ack),
3 => Ok(PacketType::Reset),
4 => Ok(PacketType::Syn),
n => Err(())
}
}
}
#[derive(Debug, Clone)]
pub struct Packet {
pub packet_type: u8,
pub id: u8,
pub network_id: u8,
pub from_addr: String,
pub to_addr: String,
pub min_delay: u16,
pub max_delay: u16,
pub envelope: Option<Envelope>
}
impl Packet {
pub fn new(id: u8, packet_type: u8, network_id: u8, from_addr: String, to_addr: String, envelope: Option<Envelope>) -> Packet {
Packet { id, packet_type, network_id, from_addr, to_addr, min_delay: 0, max_delay: 0, envelope }
}
}
pub struct Node {
pub local_peers: HashMap<NetworkId, Peer>
}
#[derive(Debug)]
pub struct Peer {
pub network_id: NetworkId,
pub did: DID
}
#[derive(Debug)]
pub struct DID {
pub username: String,
pub passphrase: String,
pub passphrase2: String,
pub address: String,
pub algorithm: String
}
#[derive(Debug, Clone)]
pub struct Envelope {
pub from: u8,
pub to: u8,
pub msg: Vec<u8>,
pub slip: Slip
}
impl Envelope {
pub fn new(from: u8, to: u8, msg: Vec<u8>) -> Envelope {
Envelope { from, to, msg, slip: Slip::new()}
}
pub fn new_msg_only(msg: Vec<u8>) -> Envelope {
Envelope {from: 0, to: 0, msg, slip: Slip::new()}
}
}
#[derive(Debug, Copy, Clone)]
pub struct Route {
pub service: u8,
pub op: u8,
}
impl Route {
pub fn new(service: u8, op: u8) -> Route {
Route { service, op }
}
}
#[derive(Debug, Clone)]
pub struct Slip {
routes: Vec<Route>,
in_progress: bool
}
impl Slip {
fn new() -> Slip {
Slip {
routes: Vec::with_capacity(2),
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()
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}