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
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations
impl RefUnwindSafe for NodeNetworkItem
impl Send for NodeNetworkItem
impl Sync for NodeNetworkItem
impl Unpin for NodeNetworkItem
impl UnwindSafe for NodeNetworkItem
Blanket Implementations
Mutably borrows from an owned value. Read more