NodeNetworkItem

Struct NodeNetworkItem 

Source
pub struct NodeNetworkItem {
    pub connections: NodeNetwork,
    /* private fields */
}
Expand description

§NodeNetworkItem

A NodeNetworkItem define the string representation of an FBP node.

A JSON string can be created from this class to be used to create a node.

§Example

// Assuming FBP Nodes NodeA and NodeB and neither of these nodes
// requires configurations.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;
use async_trait::async_trait;
use std::sync::atomic::{Ordering, AtomicBool};
use futures::future::*;
use std::ops::{Deref};

use fbp::fbp_node_network::*;
use fbp::fbp_node_context::*;
use fbp::fbp_node_trait::*;
use fbp::fbp_iidmessage::*;
use fbp::fbp_node_error::*;


   
#[derive(Clone, Serialize, Deserialize)]
pub struct NodeA {
    data: Box<FBPNodeContext>,
}

impl NodeA {
    #[allow(dead_code)]
    pub fn new() -> Self {
        let result = NodeA {
            data: Box::new(FBPNodeContext::new("NodeA")),
        };

        result.node_data().set_node_is_configured(true);
        result.clone().start();
        result
    }
}

#[async_trait]
impl FBPNodeTrait for NodeA {
    fn node_data_clone(&self) -> FBPNodeContext {
        self.data.deref().clone()
    }

    fn node_data(&self) -> &FBPNodeContext { &self.data }

    fn node_data_mut(&mut self) -> &mut FBPNodeContext { &mut self.data }

    fn process_message(&mut self, msg: IIDMessage) -> std::result::Result<IIDMessage, NodeError> {
        Ok(msg.clone())
    }
}

   
#[derive(Clone, Serialize, Deserialize)]
pub struct NodeB {
    data: Box<FBPNodeContext>,
}

impl NodeB {
    #[allow(dead_code)]
    pub fn new() -> Self {
        let result = NodeB {
            data: Box::new(FBPNodeContext::new("NodeB")),
        };

        result.node_data().set_node_is_configured(true);
        result.clone().start();
        result
    }
}

#[async_trait]
impl FBPNodeTrait for NodeB {
    fn node_data_clone(&self) -> FBPNodeContext {
        self.data.deref().clone()
    }

    fn node_data(&self) -> &FBPNodeContext { &self.data }

    fn node_data_mut(&mut self) -> &mut FBPNodeContext { &mut self.data }

    fn process_message(&mut self, msg: IIDMessage) -> std::result::Result<IIDMessage, NodeError> {
        Ok(msg.clone())
    }
}

let mut nni = NodeNetworkItem::new("NodeA".to_string());
nni.add_connection(&"NodeB".to_string(), None);

let json_string = serde_json::to_string(&nni).unwrap();

// Subsequently, the NodeNetworkItem can be 'reconstructed' as follows:

let a_nni: NodeNetworkItem = serde_json::from_str(json_string.as_str()).unwrap();

// With a NodeNetworkItem, the node can be created as follows:

let net_config_op: Option<NetworkConfiguration> = a_nni.create_node();

// A NetworkConfiguration is a struct which contains all of the ndoes that
// were constructed.  Please the see the documentation for the NetworkConfiguration
// struct

Fields§

§connections: NodeNetwork

Implementations§

Source§

impl NodeNetworkItem

Source

pub fn new(node_name: String) -> Self

Source

pub fn name(&self) -> String

Source

pub fn add_configuration(&mut self, config_str: String)

Source

pub fn add_connection(&mut self, node_name: &String, key: Option<String>)

Source

pub fn add_node_connection( &mut self, node: NodeNetworkItem, key: Option<String>, )

Source

pub fn create_node(&self) -> Option<NetworkConfiguration>

Trait Implementations§

Source§

impl Clone for NodeNetworkItem

Source§

fn clone(&self) -> NodeNetworkItem

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 Debug for NodeNetworkItem

Source§

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

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

impl<'de> Deserialize<'de> for NodeNetworkItem

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 PartialEq for NodeNetworkItem

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for NodeNetworkItem

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

impl Eq for NodeNetworkItem

Auto Trait Implementations§

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,