Skip to main content

NetworkSimulator

Struct NetworkSimulator 

Source
pub struct NetworkSimulator { /* private fields */ }

Implementations§

Source§

impl NetworkSimulator

NetworkSimulator is designed to simulate the network which consist of 1+ ethers. Each ether is instance of EtherSimulator

Source

pub fn new(ms_per_tick: u64) -> Self

Examples found in repository?
examples/embedded_nano_mesh.rs (line 11)
9fn main() {
10    /* Create simulator, ether, and devices registered in that ether. */
11    let mut simulator = NetworkSimulator::new(1);
12
13    simulator.create_ether("1");
14
15    let mut driver_1 = WirelessModemFake::new("1");
16    let mut driver_2 = WirelessModemFake::new("2");
17
18    {
19        let mut ether_1 = simulator.get_ether("1").expect("Failed to find ether 1");
20        ether_1.register_driver(driver_1.clone());
21        ether_1.register_driver(driver_2.clone());
22    }
23
24    /* Create tested nodes. */
25    let mut mesh_node_1 = Node::new(NodeConfig {
26        device_address: ExactAddressType::try_from(1).expect("1 equals to 0"),
27        listen_period: NODE_1_LISTEN_PERIOD,
28    });
29
30    let mut mesh_node_2 = Node::new(NodeConfig {
31        device_address: ExactAddressType::try_from(2).expect("2 equals to 0"),
32        listen_period: NODE_2_LISTEN_PERIOD,
33    });
34
35    /* Do testing scenario */
36    let _ = mesh_node_1.send_to_exact(
37        NodeString::try_from("Message from node 1")
38            .expect("Fail to pack message")
39            .into_bytes(),
40        ExactAddressType::try_from(2).expect("2 is 0"),
41        2.into(),
42        false,
43    );
44
45    simulator.start_simulation_thread();
46
47    let start_time = Instant::now();
48    loop {
49        let current_time = Instant::now().duration_since(start_time).as_millis() as ms;
50
51        let _ = mesh_node_1.update(&mut driver_1, current_time);
52        let _ = mesh_node_2.update(&mut driver_2, current_time);
53
54        if current_time >= 200 as ms {
55            panic!("Simulation timeout");
56        }
57
58        if let Some(_packet) = mesh_node_2.receive() {
59            break;
60        }
61    }
62
63    simulator.stop_simulation_thread();
64
65    println!("Simulation done");
66}
Source

pub fn create_ether(&self, name: &str)

Examples found in repository?
examples/embedded_nano_mesh.rs (line 13)
9fn main() {
10    /* Create simulator, ether, and devices registered in that ether. */
11    let mut simulator = NetworkSimulator::new(1);
12
13    simulator.create_ether("1");
14
15    let mut driver_1 = WirelessModemFake::new("1");
16    let mut driver_2 = WirelessModemFake::new("2");
17
18    {
19        let mut ether_1 = simulator.get_ether("1").expect("Failed to find ether 1");
20        ether_1.register_driver(driver_1.clone());
21        ether_1.register_driver(driver_2.clone());
22    }
23
24    /* Create tested nodes. */
25    let mut mesh_node_1 = Node::new(NodeConfig {
26        device_address: ExactAddressType::try_from(1).expect("1 equals to 0"),
27        listen_period: NODE_1_LISTEN_PERIOD,
28    });
29
30    let mut mesh_node_2 = Node::new(NodeConfig {
31        device_address: ExactAddressType::try_from(2).expect("2 equals to 0"),
32        listen_period: NODE_2_LISTEN_PERIOD,
33    });
34
35    /* Do testing scenario */
36    let _ = mesh_node_1.send_to_exact(
37        NodeString::try_from("Message from node 1")
38            .expect("Fail to pack message")
39            .into_bytes(),
40        ExactAddressType::try_from(2).expect("2 is 0"),
41        2.into(),
42        false,
43    );
44
45    simulator.start_simulation_thread();
46
47    let start_time = Instant::now();
48    loop {
49        let current_time = Instant::now().duration_since(start_time).as_millis() as ms;
50
51        let _ = mesh_node_1.update(&mut driver_1, current_time);
52        let _ = mesh_node_2.update(&mut driver_2, current_time);
53
54        if current_time >= 200 as ms {
55            panic!("Simulation timeout");
56        }
57
58        if let Some(_packet) = mesh_node_2.receive() {
59            break;
60        }
61    }
62
63    simulator.stop_simulation_thread();
64
65    println!("Simulation done");
66}
Source

pub fn get_ether(&self, name: &str) -> Option<EtherSimulator>

Examples found in repository?
examples/embedded_nano_mesh.rs (line 19)
9fn main() {
10    /* Create simulator, ether, and devices registered in that ether. */
11    let mut simulator = NetworkSimulator::new(1);
12
13    simulator.create_ether("1");
14
15    let mut driver_1 = WirelessModemFake::new("1");
16    let mut driver_2 = WirelessModemFake::new("2");
17
18    {
19        let mut ether_1 = simulator.get_ether("1").expect("Failed to find ether 1");
20        ether_1.register_driver(driver_1.clone());
21        ether_1.register_driver(driver_2.clone());
22    }
23
24    /* Create tested nodes. */
25    let mut mesh_node_1 = Node::new(NodeConfig {
26        device_address: ExactAddressType::try_from(1).expect("1 equals to 0"),
27        listen_period: NODE_1_LISTEN_PERIOD,
28    });
29
30    let mut mesh_node_2 = Node::new(NodeConfig {
31        device_address: ExactAddressType::try_from(2).expect("2 equals to 0"),
32        listen_period: NODE_2_LISTEN_PERIOD,
33    });
34
35    /* Do testing scenario */
36    let _ = mesh_node_1.send_to_exact(
37        NodeString::try_from("Message from node 1")
38            .expect("Fail to pack message")
39            .into_bytes(),
40        ExactAddressType::try_from(2).expect("2 is 0"),
41        2.into(),
42        false,
43    );
44
45    simulator.start_simulation_thread();
46
47    let start_time = Instant::now();
48    loop {
49        let current_time = Instant::now().duration_since(start_time).as_millis() as ms;
50
51        let _ = mesh_node_1.update(&mut driver_1, current_time);
52        let _ = mesh_node_2.update(&mut driver_2, current_time);
53
54        if current_time >= 200 as ms {
55            panic!("Simulation timeout");
56        }
57
58        if let Some(_packet) = mesh_node_2.receive() {
59            break;
60        }
61    }
62
63    simulator.stop_simulation_thread();
64
65    println!("Simulation done");
66}
Source

pub fn start_tick(&self)

Source

pub fn end_tick(&self)

Source

pub fn simulate(&self)

Source

pub fn start_simulation_thread(&mut self)

Examples found in repository?
examples/embedded_nano_mesh.rs (line 45)
9fn main() {
10    /* Create simulator, ether, and devices registered in that ether. */
11    let mut simulator = NetworkSimulator::new(1);
12
13    simulator.create_ether("1");
14
15    let mut driver_1 = WirelessModemFake::new("1");
16    let mut driver_2 = WirelessModemFake::new("2");
17
18    {
19        let mut ether_1 = simulator.get_ether("1").expect("Failed to find ether 1");
20        ether_1.register_driver(driver_1.clone());
21        ether_1.register_driver(driver_2.clone());
22    }
23
24    /* Create tested nodes. */
25    let mut mesh_node_1 = Node::new(NodeConfig {
26        device_address: ExactAddressType::try_from(1).expect("1 equals to 0"),
27        listen_period: NODE_1_LISTEN_PERIOD,
28    });
29
30    let mut mesh_node_2 = Node::new(NodeConfig {
31        device_address: ExactAddressType::try_from(2).expect("2 equals to 0"),
32        listen_period: NODE_2_LISTEN_PERIOD,
33    });
34
35    /* Do testing scenario */
36    let _ = mesh_node_1.send_to_exact(
37        NodeString::try_from("Message from node 1")
38            .expect("Fail to pack message")
39            .into_bytes(),
40        ExactAddressType::try_from(2).expect("2 is 0"),
41        2.into(),
42        false,
43    );
44
45    simulator.start_simulation_thread();
46
47    let start_time = Instant::now();
48    loop {
49        let current_time = Instant::now().duration_since(start_time).as_millis() as ms;
50
51        let _ = mesh_node_1.update(&mut driver_1, current_time);
52        let _ = mesh_node_2.update(&mut driver_2, current_time);
53
54        if current_time >= 200 as ms {
55            panic!("Simulation timeout");
56        }
57
58        if let Some(_packet) = mesh_node_2.receive() {
59            break;
60        }
61    }
62
63    simulator.stop_simulation_thread();
64
65    println!("Simulation done");
66}
Source

pub fn stop_simulation_thread(&mut self)

Examples found in repository?
examples/embedded_nano_mesh.rs (line 63)
9fn main() {
10    /* Create simulator, ether, and devices registered in that ether. */
11    let mut simulator = NetworkSimulator::new(1);
12
13    simulator.create_ether("1");
14
15    let mut driver_1 = WirelessModemFake::new("1");
16    let mut driver_2 = WirelessModemFake::new("2");
17
18    {
19        let mut ether_1 = simulator.get_ether("1").expect("Failed to find ether 1");
20        ether_1.register_driver(driver_1.clone());
21        ether_1.register_driver(driver_2.clone());
22    }
23
24    /* Create tested nodes. */
25    let mut mesh_node_1 = Node::new(NodeConfig {
26        device_address: ExactAddressType::try_from(1).expect("1 equals to 0"),
27        listen_period: NODE_1_LISTEN_PERIOD,
28    });
29
30    let mut mesh_node_2 = Node::new(NodeConfig {
31        device_address: ExactAddressType::try_from(2).expect("2 equals to 0"),
32        listen_period: NODE_2_LISTEN_PERIOD,
33    });
34
35    /* Do testing scenario */
36    let _ = mesh_node_1.send_to_exact(
37        NodeString::try_from("Message from node 1")
38            .expect("Fail to pack message")
39            .into_bytes(),
40        ExactAddressType::try_from(2).expect("2 is 0"),
41        2.into(),
42        false,
43    );
44
45    simulator.start_simulation_thread();
46
47    let start_time = Instant::now();
48    loop {
49        let current_time = Instant::now().duration_since(start_time).as_millis() as ms;
50
51        let _ = mesh_node_1.update(&mut driver_1, current_time);
52        let _ = mesh_node_2.update(&mut driver_2, current_time);
53
54        if current_time >= 200 as ms {
55            panic!("Simulation timeout");
56        }
57
58        if let Some(_packet) = mesh_node_2.receive() {
59            break;
60        }
61    }
62
63    simulator.stop_simulation_thread();
64
65    println!("Simulation done");
66}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.