Struct EtherSimulator

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

Implementations§

Source§

impl EtherSimulator

Source

pub fn new(name: &str) -> Self

Source

pub fn get_name(&self) -> &str

Gets the name of the ether

use network_simulator::EtherSimulator;
let mut ether = EtherSimulator::new("my_ether");
assert_eq!(ether.get_name(), "my_ether");
Source

pub fn register_driver(&mut self, driver: WirelessModemFake)

Registers a new device (driver / modem)

use network_simulator::EtherSimulator;
use network_simulator::WirelessModemFake;
use network_simulator::IODriverSimulator;

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

pub fn unregister_driver(&mut self, name: &str)

Unregisters a device

use network_simulator::EtherSimulator;
use network_simulator::WirelessModemFake;
use network_simulator::IODriverSimulator;

let mut ether = EtherSimulator::new("my_ether");
ether.register_driver(WirelessModemFake::new("my_modem"));
ether.unregister_driver("my_modem");
assert!(ether.get_driver("my_modem").is_none());
Source

pub fn get_driver(&self, name: &str) -> Option<WirelessModemFake>

Gets a registered device

use network_simulator::EtherSimulator;
use network_simulator::WirelessModemFake;
use network_simulator::IODriverSimulator;

let mut ether = EtherSimulator::new("my_ether");
assert!(ether.get_driver("my_modem").is_none());
ether.register_driver(WirelessModemFake::new("my_modem"));
assert_eq!(ether.get_driver("my_modem").unwrap().get_name(), "my_modem");
Source

pub fn start_tick(&self)

Prepares all the registered devices for starting of simulation during tick.

Source

pub fn end_tick(&self)

Prepares all the registered devices for ending of simulation during tick.

Source

pub fn simulate(&self)

This operation shall be called only during tick is active.

Source

pub fn clone(&self) -> EtherSimulator

Clones itself. Also makes all internal data shared to be able to use from multiple threads.

use network_simulator::EtherSimulator;
use network_simulator::WirelessModemFake;
use network_simulator::IODriverSimulator;

let mut ether = EtherSimulator::new("my_ether");
let ether_clone = ether.clone();

assert_eq!(ether.get_name(), ether_clone.get_name());

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.