Trait fbp::fbp_node_trait::FBPNodeTrait[][src]

pub trait FBPNodeTrait: Sync + Send + 'static {
    fn node_data_clone(&self) -> FBPNodeContext;
fn node_data(&self) -> &FBPNodeContext;
fn node_data_mut(&mut self) -> &mut FBPNodeContext;
fn process_message(
        &mut self,
        msg: IIDMessage
    ) -> Result<IIDMessage, NodeError>; fn node_is_configured(&self) -> bool { ... }
fn wait_for_node_to_be_configured<'life0, 'async_trait>(
        &'life0 self
    ) -> Pin<Box<dyn Future<Output = ()> + 'async_trait>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
, { ... }
fn do_process_message(
        &mut self,
        msg_to_process: IIDMessage
    ) -> Result<(), NodeError> { ... }
fn process_config(
        &mut self,
        msg: IIDMessage
    ) -> Result<IIDMessage, NodeError> { ... }
fn process_process_message(
        &mut self,
        msg: IIDMessage
    ) -> Result<IIDMessage, NodeError> { ... }
fn start(self)
    where
        Self: Sized + Send + Sync + Clone + 'static
, { ... }
fn stop(&mut self) { ... } }

Required methods

Return a reference to the FBPNodeContext

This must be implemented by the struct adhering to the FBPNode trait

Example
use serde::{Deserialize, Serialize};
use async_trait::async_trait;
use std::any::Any;
use std::ops::Deref;

use fbp::fbp_node_trait::*;
use fbp::fbp_node_context::*;
use fbp::fbp_iidmessage::*;
use fbp::fbp_node_error::*;
use fbp::fbp_threadsafe_wrapper::ThreadSafeType;

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

impl ExampleFBPNode {
    pub fn new() -> Self {
        ExampleFBPNode {
            data: Box::new(FBPNodeContext::new("ExampleFBPNode")),
        }
    }
}

#[async_trait]
impl FBPNodeTrait for ExampleFBPNode {

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

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

    fn node_data_clone(&self) -> FBPNodeContext {
        self.data.deref().clone()
    }

    // This is where an FBP node processes IIDMessages.  In this example
    // No processing is done and the original message is sent along to all of
    // the FBP nodes that have registered to receive the output of this node.
    fn process_message(&mut self,
        msg: IIDMessage) -> std::result::Result<IIDMessage, NodeError> {
        Ok(msg.clone())
    }
}

Return a mutable reference to an FBPNodeContext

This must be implemented by the struct adhering to the FBPNode trait Please see the example for the node_data method for details

Provide the processing for this node.

This is where a specific node does its specific work. In this example, all that is done to to forward the incoming IIDMessage onto any nodes that have registered to receive the output of this node. Please see the example for the node_data method for details

Provided methods

Return is an FBP node is fully configured and can process IIDMessages

This must be implemented by the struct adhering to the FBPNode trait Please see the example for the node_data method for details

Block waiting on node to be configured

This method will block the caller until the node is fully configured

Example
use serde::{Deserialize, Serialize};
use async_trait::async_trait;
use std::any::Any;
use std::ops::{Deref, DerefMut};

use fbp::fbp_node_trait::*;
use fbp::fbp_node_context::*;
use fbp::fbp_iidmessage::*;
use fbp::fbp_node_error::*;
use fbp::fbp_threadsafe_wrapper::ThreadSafeType;

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

impl ExampleFBPNode {
    pub fn new() -> Self {
        ExampleFBPNode {
            data: Box::new(FBPNodeContext::new("ExampleFBPNode")),
        }
    }
}

#[async_trait]
impl FBPNodeTrait for ExampleFBPNode {

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

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

    fn node_data_clone(&self) -> FBPNodeContext {
        self.data.deref().clone()
    }

    // This is where an FBP node processes IIDMessages.  In this example
    // No processing is done and the original message is sent along to all of
    // the FBP nodes that have registered to receive the output of this node.
    fn process_message(&mut self,
        msg: IIDMessage) -> std::result::Result<IIDMessage, NodeError> {
        Ok(msg.clone())
    }
}

let example_node = ExampleFBPNode::new();

async fn do_wait(node: &ExampleFBPNode) {
    node.wait_for_node_to_be_configured().await;
}

do_wait(&example_node);

Process an incoming IIDMessage

When a IIDMessage is sent to an FBP node, it is enqueue onto the input queue of the node. The node runs a thread with a loop. At the top of the loop, the queue is checked and if there are no items in the queue, then the node thread blocks waiting for an IIDMessage to be posted to the input queue. If there is at least one message in the input queue, then it will be dequeued and will be processed by this method. If the message is a Data message, then the process_message method will be called. If the message is a Process message then the process_process_message method will be called. If the message is a Config message, then the process_config method will be called. If any errors occur in the processing of the IIDMessage, then a NodeError will be returned.

Process an IIDMessage that is an Config message

This method is called from the do_process_message method to handled incoming config message. If an FBP node needs to receive an Config message to setup its fields so that it has all of the information needed to process messages, then the node will need to implement this method.

Process an IIDMessage that is a Process message

This method is called from the do_process_message method to handled incoming process message. It is async as it will call the stop method which is async.

Run the message loop for an FBP node.

The start method runs a thread that will block waiting on IIDMessages to be enqueued on to the input of the FBP node. Once enqueued the loop will dequeue a IIDMessage and then call do_process_message to determine what the correct processing is needed for the IIDMessage

Tell the FBP Node to stop its processing loop

All good things come to an end. When a node receives a Process IIDMessage with the stop message, this method will be called and it will stop running the nodes processing loop

Implementors