Struct instance_chart::Chart
source · pub struct Chart<const N: usize, T: Debug + Clone + Serialize> { /* private fields */ }
Expand description
The chart keeping track of the discoverd nodes. That a node appears in the chart is no guarentee that it is reachable at this moment.
Implementations§
source§impl<const N: usize> Chart<N, u16>
impl<const N: usize> Chart<N, u16>
sourcepub fn get_addr_list(&self, id: Id) -> Option<[SocketAddr; N]>
pub fn get_addr_list(&self, id: Id) -> Option<[SocketAddr; N]>
Get all the SocketAddr
’s for a given node’s Id
Note
returns None if the node was not in the Chart
Performance
This locks the map. if you need adresses for many nodes
is faster to get a vector of them at once Self::addr_lists_vec()
instead of calling this repeatedly
Panics
This function panics when called with the Id
of the chart instance
it is called on
Examples
let service_ports = [8042, 8043, 8044];
let chart = ChartBuilder::new()
.with_id(1)
.with_service_ports(service_ports)
.finish()?;
let maintain = discovery::maintain(chart.clone());
let _ = tokio::spawn(maintain); // maintain task will run forever
let from_chart = chart.get_addr_list(2);
assert_eq!(None, from_chart);
source§impl<const N: usize> Chart<N, u16>
impl<const N: usize> Chart<N, u16>
sourcepub fn get_nth_addr<const IDX: usize>(&self, id: Id) -> Option<SocketAddr>
pub fn get_nth_addr<const IDX: usize>(&self, id: Id) -> Option<SocketAddr>
Get a nodes nth SocketAddr
’s given its Id
Note
returns None if the node was not in the Chart
Panics
This function panics when called with the Id
of the chart instance
it is called on
Performance
This locks the map. if you need adresses for many nodes
is faster to get a vector of them at once Self::addr_lists_vec()
instead of calling this repeatedly
Examples
let port = 8043;
let chart = ChartBuilder::new()
.with_id(1)
.with_service_ports([8042, port, 8044])
.finish()?;
let maintain = discovery::maintain(chart.clone());
let _ = tokio::spawn(maintain); // maintain task will run forever
let from_chart = chart.get_nth_addr::<2>(2);
assert_eq!(None, from_chart);
source§impl Chart<1, u16>
impl Chart<1, u16>
sourcepub fn get_addr(&self, id: Id) -> Option<SocketAddr>
pub fn get_addr(&self, id: Id) -> Option<SocketAddr>
Get a nodes SocketAddr
’s given its Id
Note
returns None if the node was not in the Chart
Panics
This function panics when called with the Id
of the chart instance
it is called on
Performance
This locks the map. if you need adresses for many nodes
is faster to get a vector of them at once Self::addr_lists_vec()
instead of calling this repeatedly
Examples
let port = 8043;
let chart = ChartBuilder::new()
.with_id(1)
.with_service_ports([port])
.finish()?;
let maintain = discovery::maintain(chart.clone());
let _ = tokio::spawn(maintain); // maintain task will run forever
let from_chart = chart.get_addr(2);
assert_eq!(None, from_chart);
source§impl<const N: usize> Chart<N, u16>
impl<const N: usize> Chart<N, u16>
sourcepub fn addr_lists_vec(&self) -> Vec<(Id, [SocketAddr; N])>
pub fn addr_lists_vec(&self) -> Vec<(Id, [SocketAddr; N])>
Returns an vector with each discovered node’s socketadresses.
Note
- vector order is random
- only availible for Chart configured with
ChartBuilder::with_service_ports
and build usingChartBuilder::finish
.
let chart = ChartBuilder::new()
.with_id(1)
.with_service_ports([8042, 8043, 8044])
.finish()?;
let maintain = discovery::maintain(chart.clone());
let _ = tokio::spawn(maintain); // maintain task will run forever
let port_lists = chart.addr_lists_vec();
source§impl<const N: usize> Chart<N, u16>
impl<const N: usize> Chart<N, u16>
sourcepub fn nth_addr_vec<const IDX: usize>(&self) -> Vec<(Id, SocketAddr)>
pub fn nth_addr_vec<const IDX: usize>(&self) -> Vec<(Id, SocketAddr)>
Returns a vector over each discoverd node’s nth-socketadress
Note
- vector order is random
- only availible for Chart configured with
ChartBuilder::with_service_ports
and build usingChartBuilder::finish
.
Examples
let web_server_port = 8043;
let chart = ChartBuilder::new()
.with_id(1)
.with_service_ports([8042, web_server_port, 8044])
.finish()?;
let maintain = discovery::maintain(chart.clone());
let _ = tokio::spawn(maintain); // maintain task will run forever
let web_server_ports = chart.nth_addr_vec::<2>();
source§impl<'a> Chart<1, u16>
impl<'a> Chart<1, u16>
sourcepub fn addr_vec(&'a self) -> Vec<(Id, SocketAddr)>
pub fn addr_vec(&'a self) -> Vec<(Id, SocketAddr)>
Returns a vector over each discoverd nodes’s socketadress
Note
- vector order is random
- only availible for Chart configured with
ChartBuilder::with_service_port
and build usingChartBuilder::finish
.
let chart = ChartBuilder::new()
.with_id(1)
.with_service_port(8042)
.finish()?;
let maintain = discovery::maintain(chart.clone());
let _ = tokio::spawn(maintain); // maintain task will run forever
let ports = chart.addr_vec();
source§impl<const N: usize> Chart<N, u16>
impl<const N: usize> Chart<N, u16>
The array of ports set for this chart instance, set in ChartBuilder::with_service_ports
.
pub fn our_service_ports(&self) -> &[u16]
source§impl Chart<1, u16>
impl Chart<1, u16>
The port set for this chart instance, set in ChartBuilder::with_service_port
.
pub fn our_service_port(&self) -> u16
source§impl<T: Debug + Clone + Serialize> Chart<1, T>
impl<T: Debug + Clone + Serialize> Chart<1, T>
The msg struct for this chart instance, set in ChartBuilder::custom_msg
.
source§impl<const N: usize, T: Debug + Clone + Serialize + DeserializeOwned> Chart<N, T>
impl<const N: usize, T: Debug + Clone + Serialize + DeserializeOwned> Chart<N, T>
sourcepub fn notify(&self) -> Notify<N, T>
pub fn notify(&self) -> Notify<N, T>
Wait for new discoveries. Use one of the methods on the notify object
to await a new discovery and get the data.
Examples
let chart = ChartBuilder::new()
.with_id(1)
.with_service_port(8042)
.local_discovery(true)
.finish()?;
let mut node_discoverd = chart.notify();
let maintain = discovery::maintain(chart.clone());
let _ = tokio::spawn(maintain); // maintain task will run forever
while chart.size() < full_size as usize {
let new = node_discoverd.recv().await.unwrap();
println!("discoverd new node: {:?}", new);
}
sourcepub fn forget(&self, id: Id)
pub fn forget(&self, id: Id)
forget a node removing it from the map. If it is discovered again notify subscribers will get a notification (again)
Note
This has no effect if the node has not yet been discoverd
sourcepub fn discovery_port(&self) -> u16
pub fn discovery_port(&self) -> u16
The port this instance is using for discovery