Skip to content

Commit

Permalink
Merge pull request #20 from N8BWert/node-id
Browse files Browse the repository at this point in the history
Add IDs to Nodes
  • Loading branch information
N8BWert authored Sep 11, 2024
2 parents 933716c + 42a58ab commit 867e26e
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 67 deletions.
9 changes: 9 additions & 0 deletions examples/minimal-client-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ use minimal_server::MinimalServer;
pub mod minimal_client;
use minimal_client::MinimalClient;

/// An enum identifier or the two nodes
#[derive(PartialEq)]
pub enum NodeIdentifier {
/// The server node
ServerNode,
/// The client node
ClientNode,
}

/// A request that asks for the sum of two `u64`s.
#[derive(Clone, Copy)]
pub struct AddTwoIntsRequest {
Expand Down
8 changes: 6 additions & 2 deletions examples/minimal-client-server/src/minimal_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! containing the sum of the two `u64`s.
//!

use super::{AddTwoIntsRequest, AddTwoIntsResponse};
use super::{AddTwoIntsRequest, AddTwoIntsResponse, NodeIdentifier};

use ncomm_clients_and_servers::local::LocalClient;
use ncomm_core::{Client, Node};
Expand Down Expand Up @@ -41,7 +41,11 @@ impl MinimalClient {
}
}

impl Node for MinimalClient {
impl Node<NodeIdentifier> for MinimalClient {
fn get_id(&self) -> NodeIdentifier {
NodeIdentifier::ClientNode
}

fn get_update_delay_us(&self) -> u128 {
500_000
}
Expand Down
8 changes: 6 additions & 2 deletions examples/minimal-client-server/src/minimal_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! the wrapping sum of the two u64s.
//!

use super::{AddTwoIntsRequest, AddTwoIntsResponse};
use super::{AddTwoIntsRequest, AddTwoIntsResponse, NodeIdentifier};

use ncomm_clients_and_servers::local::{LocalClient, LocalServer};
use ncomm_core::{Node, Server};
Expand Down Expand Up @@ -48,7 +48,11 @@ impl MinimalServer {
}
}

impl Node for MinimalServer {
impl Node<NodeIdentifier> for MinimalServer {
fn get_id(&self) -> NodeIdentifier {
NodeIdentifier::ServerNode
}

fn get_update_delay_us(&self) -> u128 {
500_000
}
Expand Down
9 changes: 9 additions & 0 deletions examples/minimal-publisher-subscriber/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ use minimal_publisher::MinimalPublisher;
pub mod minimal_subscriber;
use minimal_subscriber::MinimalSubscriber;

/// Identifier for the two nodes.
#[derive(PartialEq)]
pub enum NodeIdentifier {
/// The publisher node
PublisherNode,
/// The subscriber node
SubscriberNode,
}

fn main() {
let mut publisher_node = MinimalPublisher::new();
let subscriber_node = MinimalSubscriber::new(publisher_node.create_subscriber());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//! the message "Hello, World! {count}" to the subscriber.
//!

use super::NodeIdentifier;

use ncomm_core::{Node, Publisher};
use ncomm_publishers_and_subscribers::local::{LocalPublisher, LocalSubscriber};

Expand Down Expand Up @@ -36,7 +38,11 @@ impl MinimalPublisher {
}
}

impl Node for MinimalPublisher {
impl Node<NodeIdentifier> for MinimalPublisher {
fn get_id(&self) -> NodeIdentifier {
NodeIdentifier::PublisherNode
}

fn get_update_delay_us(&self) -> u128 {
500_000u128
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//! the publisher node.
//!

use super::NodeIdentifier;

use ncomm_core::{Node, Subscriber};
use ncomm_publishers_and_subscribers::local::LocalSubscriber;

Expand All @@ -27,7 +29,11 @@ impl MinimalSubscriber {
}
}

impl Node for MinimalSubscriber {
impl Node<NodeIdentifier> for MinimalSubscriber {
fn get_id(&self) -> NodeIdentifier {
NodeIdentifier::SubscriberNode
}

fn get_update_delay_us(&self) -> u128 {
500_000
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! fibonacci series.
//!

use super::{FibonacciRequest, FibonacciResponse, FibonacciUpdate};
use super::{FibonacciRequest, FibonacciResponse, FibonacciUpdate, NodeIdentifier};

use ncomm_core::{Node, UpdateClient};
use ncomm_update_clients_and_servers::local::LocalUpdateClient;
Expand Down Expand Up @@ -49,7 +49,11 @@ impl FibonacciUpdateClient {
}
}

impl Node for FibonacciUpdateClient {
impl Node<NodeIdentifier> for FibonacciUpdateClient {
fn get_id(&self) -> NodeIdentifier {
NodeIdentifier::FibonacciClient
}

fn get_update_delay_us(&self) -> u128 {
100_000
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! into the series
//!

use super::{FibonacciRequest, FibonacciResponse, FibonacciUpdate};
use super::{FibonacciRequest, FibonacciResponse, FibonacciUpdate, NodeIdentifier};

use ncomm_core::{Node, UpdateServer};
use ncomm_update_clients_and_servers::local::{LocalUpdateClient, LocalUpdateServer};
Expand Down Expand Up @@ -93,7 +93,11 @@ impl FibonacciUpdateServer {
}
}

impl Node for FibonacciUpdateServer {
impl Node<NodeIdentifier> for FibonacciUpdateServer {
fn get_id(&self) -> NodeIdentifier {
NodeIdentifier::FibonacciServer
}

fn get_update_delay_us(&self) -> u128 {
100_000
}
Expand Down
9 changes: 9 additions & 0 deletions examples/minimal-update-client-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ use fibonacci_update_client::FibonacciUpdateClient;
pub mod fibonacci_update_server;
use fibonacci_update_server::FibonacciUpdateServer;

#[derive(PartialEq)]
/// An identifier for the fibonacci client and server
pub enum NodeIdentifier {
/// The fibonacci client
FibonacciClient,
/// The fibonacci server
FibonacciServer,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// A request asking for a the nth order of the fibonacci sequence.
pub struct FibonacciRequest {
Expand Down
12 changes: 9 additions & 3 deletions ncomm-core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ pub enum ExecutorState {
}

/// An executor handles the scheduling and execution of nodes
pub trait Executor {
///
/// All nodes should have some unique ID that makes them identifiable
/// as trait objects
pub trait Executor<ID: PartialEq> {
/// Starts the nodes contained by the executor
fn start(&mut self);

Expand All @@ -43,13 +46,16 @@ pub trait Executor {
fn check_interrupt(&mut self) -> bool;

/// Add a node to the executor.
fn add_node(&mut self, node: Box<dyn Node>);
fn add_node(&mut self, node: Box<dyn Node<ID>>);

/// Add a node to the executor with some given context.
///
/// Note: The context is mainly to allow for extra configuration when
/// adding nodes.
fn add_node_with_context<CTX>(&mut self, node: Box<dyn Node>, _ctx: CTX) {
fn add_node_with_context<CTX>(&mut self, node: Box<dyn Node<ID>>, _ctx: CTX) {
self.add_node(node);
}

/// Remove a node from the executor.
fn remove_node(&mut self, id: &ID) -> Option<Box<dyn Node<ID>>>;
}
8 changes: 7 additions & 1 deletion ncomm-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@

/// A Node represents a singular process that performs some singular
/// purpose
pub trait Node: Send {
///
/// Nodes should also all be given unique IDs so that they can be identified as
/// trait objects.
pub trait Node<ID: PartialEq>: Send {
/// Return the node's ID
fn get_id(&self) -> ID;

/// Return the node's update rate (in us)
fn get_update_delay_us(&self) -> u128;

Expand Down
21 changes: 14 additions & 7 deletions ncomm-executors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,47 @@ use std::cmp::{Ord, Ordering};
/// of their next update.
///
/// This ensures that nodes are updated at the correct time
pub(crate) struct NodeWrapper {
pub(crate) struct NodeWrapper<ID: PartialEq> {
/// The timestamp of the nodes next update
pub priority: u128,
/// The nde this NodeWrapper is wrapping around
pub node: Box<dyn Node>,
pub node: Box<dyn Node<ID>>,
}

impl Ord for NodeWrapper {
impl<ID: PartialEq> NodeWrapper<ID> {
/// Destroy the node wrapper returning the node it was wrapping.
pub fn destroy(self) -> Box<dyn Node<ID>> {
self.node
}
}

impl<ID: PartialEq> Ord for NodeWrapper<ID> {
fn cmp(&self, other: &Self) -> Ordering {
self.priority.cmp(&other.priority).reverse()
}
}

impl PartialOrd for NodeWrapper {
impl<ID: PartialEq> PartialOrd for NodeWrapper<ID> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl PartialEq for NodeWrapper {
impl<ID: PartialEq> PartialEq for NodeWrapper<ID> {
fn eq(&self, other: &Self) -> bool {
self.priority == other.priority
}
}

impl Eq for NodeWrapper {}
impl<ID: PartialEq> Eq for NodeWrapper<ID> {}

/// This method performs binary search insertion into the sorted vector
/// `vec` with the node `node`.
///
/// This is just a convenience method I found myself using a ton so I decided
/// to make it its own method.
#[inline(always)]
pub(crate) fn insert_into(vec: &mut Vec<NodeWrapper>, node: NodeWrapper) {
pub(crate) fn insert_into<ID: PartialEq>(vec: &mut Vec<NodeWrapper<ID>>, node: NodeWrapper<ID>) {
// If another node is found with the same priority, insert the node after that
// node. Otherwise, insert the node into the position it should be in in the
// sorted vector
Expand Down
Loading

0 comments on commit 867e26e

Please sign in to comment.