NetworkCoordinate

Struct NetworkCoordinate 

Source
pub struct NetworkCoordinate<const N: usize> { /* private fields */ }
Expand description

A NetworkCoordinate<N> is the main interface to a Vivaldi network coordinate.

§Generic Parameters

  • N: Const generic for number of dimensions. For example, NetworkCoordinate<3> is a 3-Dimentionsal Euclidean coordinate plus a height. Should be a positive number greater than zero.

Note: Dimensions other than 2D or 3D are usually not useful. If you want to use one of those dimensions, you can use type aliases (NetworkCoordinate2D or NetworkCoordinate3D) which are a little more ergonomic than using the generic here.

§Examples

For an explanation and examples of usage, please see the main crate documentation.

Implementations§

Source§

impl<const N: usize> NetworkCoordinate<N>

Source

pub fn new() -> Self

Creates a new random NetworkCoordinate

§Example
use vivaldi_nc::NetworkCoordinate;

// create a new 3-dimensional random NC
let a: NetworkCoordinate<3> = NetworkCoordinate::new();

// print the NC
println!("Our new NC is: {:#?}", a);
Source

pub fn estimated_rtt(&self, rhs: &Self) -> Duration

Given another Vivaldi NetworkCoordinate, estimate the round trip time (ie ping) between them.

This is done by computing the height vector distance between between the two coordinates. Vivaldi uses this distance as a representation of estimated round trip time.

§Parameters
  • rhs: the other coordinate
§Returns
  • the estimated round trip time as a Duration
§Example
use vivaldi_nc::NetworkCoordinate;

// create some 2-dimensional NCs for the sake of this example. These will just be random
// NCs. In a real usecase these would have meaningful values.
let a: NetworkCoordinate<2> = NetworkCoordinate::new();
let b: NetworkCoordinate<2> = NetworkCoordinate::new();

// get the estimated RTT, convert to milliseconds, and print
println!("Estimated RTT: {}", a.estimated_rtt(&b).as_millis());
Source

pub fn update(&mut self, rhs: &Self, rtt: Duration) -> &Self

Given another Vivaldi NetworkCoordinate, adjust our coordinateto better represent the actual round trip time (aka distance) between us.

§Parameters
  • rhs: the other coordinate
  • rtt: the measured round trip time between self and rhs
§Returns
  • a reference to self
§Example
use core::time::Duration;
use vivaldi_nc::NetworkCoordinate;

// We always have our own NC:
let mut local: NetworkCoordinate<2> = NetworkCoordinate::new();

// Assume we received a NC from a remote node:
let remote: NetworkCoordinate<2> = NetworkCoordinate::new();

// And we measured the RTT between us and the remote node:
let rtt = Duration::from_millis(100);

// Now we can update our NC to adjust our position relative to the remote node:
local.update(&remote, rtt);
§Algorithm

This is an implementation of Vivaldi NCs per the original paper. It implements the following alogirthm (quoting from paper):

// Incorporate new information: node j has been
// measured to be rtt ms away, has coordinates xj,
// and an error estimate of ej .
//
// Our own coordinates and error estimate are xi and ei.
//
// The constants ce and cc are tuning parameters.

vivaldi(rtt, xj, ej)
    // Sample weight balances local and remote error. (1)
    w = ei /(ei + ej)
    // Compute relative error of this sample. (2)
    es = ∣∣∣‖xi − xj‖ − rtt∣∣∣/rtt
    // Update weighted moving average of local error. (3)
    ei = es × ce × w + ei × (1 − ce × w)
    // Update local coordinates. (4)
    δ = cc × w
    xi = xi + δ × (rtt − ‖xi − xj ‖) × u(xi − xj)
Source

pub const fn error(&self) -> f64

getter for error value - useful for consumers to understand the estimated accuracty of this NetworkCoordinate

Trait Implementations§

Source§

impl<const N: usize> Clone for NetworkCoordinate<N>

Source§

fn clone(&self) -> NetworkCoordinate<N>

Returns a duplicate 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> Debug for NetworkCoordinate<N>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Default for NetworkCoordinate<N>

Source§

fn default() -> Self

A default NetworkCoordinate has a random position and DEFAULT_ERROR

Source§

impl<'de, const N: usize> Deserialize<'de> for NetworkCoordinate<N>

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<const N: usize> Serialize for NetworkCoordinate<N>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<const N: usize> Freeze for NetworkCoordinate<N>

§

impl<const N: usize> RefUnwindSafe for NetworkCoordinate<N>

§

impl<const N: usize> Send for NetworkCoordinate<N>

§

impl<const N: usize> Sync for NetworkCoordinate<N>

§

impl<const N: usize> Unpin for NetworkCoordinate<N>

§

impl<const N: usize> UnwindSafe for NetworkCoordinate<N>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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 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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,