Struct Network

Source
pub struct Network<T> { /* private fields */ }
Expand description

A network of processes. Each process has a mailbox and the set of addresses (senders) it needs to communicate with its neighbors.

Implementations§

Source§

impl<T> Network<T>
where T: Default + Send + 'static,

Source

pub fn new() -> Self

Examples found in repository?
examples/lattice.rs (line 10)
7fn main() {
8    env_logger::init();
9
10    let mut net = Network::new();
11
12    net.add_process("a", vec!["b", "c"], |senders, _| loop {
13        thread::sleep(Duration::from_secs(1));
14        for (adj, s) in senders.iter() {
15            info!("a is sending to {}", adj);
16            s.send(("a".to_string(), ()))
17                .expect("shouldn't encounter a closed channel");
18        }
19    });
20
21    net.add_process("b", vec!["d"], |senders, receiver| loop {
22        thread::sleep(Duration::from_secs(1));
23        let (sender, _) = receiver
24            .recv()
25            .expect("shouldn't encounter a closed channel");
26        info!("b received from {}", sender);
27        for s in senders.values() {
28            s.send(("b".to_string(), ()))
29                .expect("shouldn't encounter a closed channel");
30        }
31    });
32
33    net.add_process("c", vec!["d"], |senders, receiver| loop {
34        thread::sleep(Duration::from_secs(1));
35        let (sender, _) = receiver
36            .recv()
37            .expect("shouldn't encounter a closed channel");
38        info!("c received from {}", sender);
39        for s in senders.values() {
40            s.send(("c".to_string(), ()))
41                .expect("shouldn't encounter a closed channel");
42        }
43    });
44
45    net.add_process("d", vec![], |_, receiver| loop {
46        thread::sleep(Duration::from_secs(1));
47        let (sender, _) = receiver
48            .recv()
49            .expect("shouldn't encounter a closed channel");
50        info!("d received from {}", sender);
51    });
52
53    net.start_and_wait();
54}
Source

pub fn add_process<'a, F>( &'a mut self, name: &'static str, adj: Vec<&'static str>, body: F, )
where F: Fn(HashMap<String, Sender<(String, T)>>, Receiver<(String, T)>) + Send + 'static,

Adding a process to the network takes a name, its neighbors, and a closure which acts as the entire body of this process. I.e., if you want the process to run forever, that loop has to be inside the given closure.

The process’s neighbor’s addresses (the senders) and the process’s mailbox are passed into the closure. The only abstraction provided here is converting the given graph—described the process and its adjacencies—into mailboxes and addresses.

Examples found in repository?
examples/lattice.rs (lines 12-19)
7fn main() {
8    env_logger::init();
9
10    let mut net = Network::new();
11
12    net.add_process("a", vec!["b", "c"], |senders, _| loop {
13        thread::sleep(Duration::from_secs(1));
14        for (adj, s) in senders.iter() {
15            info!("a is sending to {}", adj);
16            s.send(("a".to_string(), ()))
17                .expect("shouldn't encounter a closed channel");
18        }
19    });
20
21    net.add_process("b", vec!["d"], |senders, receiver| loop {
22        thread::sleep(Duration::from_secs(1));
23        let (sender, _) = receiver
24            .recv()
25            .expect("shouldn't encounter a closed channel");
26        info!("b received from {}", sender);
27        for s in senders.values() {
28            s.send(("b".to_string(), ()))
29                .expect("shouldn't encounter a closed channel");
30        }
31    });
32
33    net.add_process("c", vec!["d"], |senders, receiver| loop {
34        thread::sleep(Duration::from_secs(1));
35        let (sender, _) = receiver
36            .recv()
37            .expect("shouldn't encounter a closed channel");
38        info!("c received from {}", sender);
39        for s in senders.values() {
40            s.send(("c".to_string(), ()))
41                .expect("shouldn't encounter a closed channel");
42        }
43    });
44
45    net.add_process("d", vec![], |_, receiver| loop {
46        thread::sleep(Duration::from_secs(1));
47        let (sender, _) = receiver
48            .recv()
49            .expect("shouldn't encounter a closed channel");
50        info!("d received from {}", sender);
51    });
52
53    net.start_and_wait();
54}
Source

pub fn start_and_wait(self)

Start the processes in this network. Blocks until all of the processes have finished.

Examples found in repository?
examples/lattice.rs (line 53)
7fn main() {
8    env_logger::init();
9
10    let mut net = Network::new();
11
12    net.add_process("a", vec!["b", "c"], |senders, _| loop {
13        thread::sleep(Duration::from_secs(1));
14        for (adj, s) in senders.iter() {
15            info!("a is sending to {}", adj);
16            s.send(("a".to_string(), ()))
17                .expect("shouldn't encounter a closed channel");
18        }
19    });
20
21    net.add_process("b", vec!["d"], |senders, receiver| loop {
22        thread::sleep(Duration::from_secs(1));
23        let (sender, _) = receiver
24            .recv()
25            .expect("shouldn't encounter a closed channel");
26        info!("b received from {}", sender);
27        for s in senders.values() {
28            s.send(("b".to_string(), ()))
29                .expect("shouldn't encounter a closed channel");
30        }
31    });
32
33    net.add_process("c", vec!["d"], |senders, receiver| loop {
34        thread::sleep(Duration::from_secs(1));
35        let (sender, _) = receiver
36            .recv()
37            .expect("shouldn't encounter a closed channel");
38        info!("c received from {}", sender);
39        for s in senders.values() {
40            s.send(("c".to_string(), ()))
41                .expect("shouldn't encounter a closed channel");
42        }
43    });
44
45    net.add_process("d", vec![], |_, receiver| loop {
46        thread::sleep(Duration::from_secs(1));
47        let (sender, _) = receiver
48            .recv()
49            .expect("shouldn't encounter a closed channel");
50        info!("d received from {}", sender);
51    });
52
53    net.start_and_wait();
54}
Source

pub fn start(self)

Start the processes in this network.

Trait Implementations§

Source§

impl<T: Default> Default for Network<T>

Source§

fn default() -> Network<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Network<T>

§

impl<T> !RefUnwindSafe for Network<T>

§

impl<T> Send for Network<T>
where T: Send,

§

impl<T> !Sync for Network<T>

§

impl<T> Unpin for Network<T>

§

impl<T> !UnwindSafe for Network<T>

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.