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>

source

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>

source

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>

source

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>

source

pub fn addr_lists_vec(&self) -> Vec<(Id, [SocketAddr; N])>

Returns an vector with each discovered node’s socketadresses.

Note
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>

source

pub fn nth_addr_vec<const IDX: usize>(&self) -> Vec<(Id, SocketAddr)>

Returns a vector over each discoverd node’s nth-socketadress

Note
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>

source

pub fn addr_vec(&'a self) -> Vec<(Id, SocketAddr)>

Returns a vector over each discoverd nodes’s socketadress

Note
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>

The array of ports set for this chart instance, set in ChartBuilder::with_service_ports.

source

pub fn our_service_ports(&self) -> &[u16]

source§

impl Chart<1, u16>

The port set for this chart instance, set in ChartBuilder::with_service_port.

source

pub fn our_service_port(&self) -> u16

source§

impl<T: Debug + Clone + Serialize> Chart<1, T>

The msg struct for this chart instance, set in ChartBuilder::custom_msg.

source

pub fn our_msg(&self) -> &T

source§

impl<const N: usize, T: Debug + Clone + Serialize + DeserializeOwned> Chart<N, T>

source

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);
}
source

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

source

pub fn size(&self) -> usize

number of instances discoverd including self

source

pub fn our_id(&self) -> Id

The id set for this chart instance

source

pub fn discovery_port(&self) -> u16

The port this instance is using for discovery

Trait Implementations§

source§

impl<const N: usize, T: Clone + Debug + Clone + Serialize> Clone for Chart<N, T>

source§

fn clone(&self) -> Chart<N, T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<const N: usize, T: Debug + Debug + Clone + Serialize> Debug for Chart<N, T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const N: usize, T> !RefUnwindSafe for Chart<N, T>

§

impl<const N: usize, T> Send for Chart<N, T>where T: Send,

§

impl<const N: usize, T> Sync for Chart<N, T>where T: Send + Sync,

§

impl<const N: usize, T> Unpin for Chart<N, T>where T: Unpin,

§

impl<const N: usize, T> !UnwindSafe for Chart<N, T>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

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

const: unstable · 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more