aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: fc1a26a762b702fadef8f5c6ed26800538ecbdc7 (plain) (blame)
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
#![feature(test)]

mod constants;
mod handshake;
mod router;
mod types;

use hjul::*;

use std::error::Error;
use std::fmt;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

use sodiumoxide;
use types::{Bind, KeyPair};

struct Test {}

impl Bind for Test {
    type Error = BindError;
    type Endpoint = SocketAddr;

    fn new() -> Test {
        Test {}
    }

    fn set_port(&self, port: u16) -> Result<(), Self::Error> {
        Ok(())
    }

    fn get_port(&self) -> Option<u16> {
        None
    }

    fn recv(&self, buf: &mut [u8]) -> Result<(usize, Self::Endpoint), Self::Error> {
        Ok((0, "127.0.0.1:8080".parse().unwrap()))
    }

    fn send(&self, buf: &[u8], dst: &Self::Endpoint) -> Result<(), Self::Error> {
        Ok(())
    }
}

#[derive(Debug)]
enum BindError {}

impl Error for BindError {
    fn description(&self) -> &str {
        "Generic Bind Error"
    }

    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }
}

impl fmt::Display for BindError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Not Possible")
    }
}

#[derive(Debug, Clone)]
struct PeerTimer {
    a: Timer,
    b: Timer,
}

fn main() {
    let runner = Runner::new(Duration::from_millis(100), 1000, 1024);

    // choose optimal crypto implementations for platform
    sodiumoxide::init().unwrap();
    {
        let router = router::Device::new(
            4,
            |t: &PeerTimer, data: bool, sent: bool| t.a.reset(Duration::from_millis(1000)),
            |t: &PeerTimer, data: bool, sent: bool| t.b.reset(Duration::from_millis(1000)),
            |t: &PeerTimer| println!("new key requested"),
        );

        let pt = PeerTimer {
            a: runner.timer(|| println!("timer-a fired for peer")),
            b: runner.timer(|| println!("timer-b fired for peer")),
        };

        let peer = router.new_peer(pt.clone());

        println!("{:?}", pt);
    }

    println!("joined");
}