pub struct EtherSimulator { /* private fields */ }
Implementations§
Source§impl EtherSimulator
impl EtherSimulator
pub fn new(name: &str) -> Self
Sourcepub fn get_name(&self) -> &str
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");
Sourcepub fn register_driver(&mut self, driver: WirelessModemFake)
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 22)
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 simulator.create_ether("2");
15
16 let mut driver_1 = WirelessModemFake::new("1");
17 let mut driver_2 = WirelessModemFake::new("2");
18 let mut driver_3 = WirelessModemFake::new("3");
19
20 {
21 let mut ether_1 = simulator.get_ether("1").expect("Failed to find ether 1");
22 ether_1.register_driver(driver_1.clone());
23 ether_1.register_driver(driver_2.clone());
24 }
25 {
26 let mut ether_2 = simulator.get_ether("2").expect("Failed to find ether 2");
27 ether_2.register_driver(driver_2.clone());
28 ether_2.register_driver(driver_3.clone());
29 }
30
31 /* Create tested nodes. */
32 let mut mesh_node_1 = Node::new(NodeConfig {
33 device_address: ExactAddressType::try_from(1).unwrap(),
34 listen_period: NODE_LISTEN_PERIOD,
35 });
36
37 let mut mesh_node_2 = Node::new(NodeConfig {
38 device_address: ExactAddressType::try_from(2).unwrap(),
39 listen_period: NODE_LISTEN_PERIOD,
40 });
41
42 let mut mesh_node_3 = Node::new(NodeConfig {
43 device_address: ExactAddressType::try_from(3).unwrap(),
44 listen_period: NODE_LISTEN_PERIOD,
45 });
46
47 /* Do testing scenario */
48 let _ = mesh_node_1.send_to_exact(
49 NodeString::try_from(MESSAGE)
50 .expect("Fail to pack message")
51 .into_bytes(),
52 ExactAddressType::try_from(3).expect("3 is 0"),
53 2 as LifeTimeType,
54 false,
55 );
56
57 simulator.start_simulation_thread();
58
59 let start_time = Instant::now();
60
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 let got_message = NodeString::from_iter(packet.data.iter().map(|b| *b as char));
74 if got_message.starts_with(MESSAGE) {
75 break;
76 }
77 }
78 }
79
80 simulator.stop_simulation_thread();
81
82 println!("Simulation done");
83}
Sourcepub fn unregister_driver(&mut self, name: &str)
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());
Sourcepub fn get_driver(&self, name: &str) -> Option<WirelessModemFake>
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");
Sourcepub fn start_tick(&self)
pub fn start_tick(&self)
Prepares all the registered devices for starting of simulation during tick.
Sourcepub fn end_tick(&self)
pub fn end_tick(&self)
Prepares all the registered devices for ending of simulation during tick.
Sourcepub fn clone(&self) -> EtherSimulator
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§
impl Freeze for EtherSimulator
impl RefUnwindSafe for EtherSimulator
impl Send for EtherSimulator
impl Sync for EtherSimulator
impl Unpin for EtherSimulator
impl UnwindSafe for EtherSimulator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more