Struct fbp::fbp_node_network::NodeNetworkItem[][src]

pub struct NodeNetworkItem {
    pub connections: NodeNetwork,
    // some fields omitted
}
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

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.