Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
N8BWert committed Sep 6, 2024
1 parent 7749ed6 commit e2a732d
Show file tree
Hide file tree
Showing 31 changed files with 653 additions and 424 deletions.
22 changes: 7 additions & 15 deletions examples/minimal-client-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//!
//! This example shows a minimal client and server based on the
//! This example shows a minimal client and server based on the
//! [Ros 2 Foxy Tutorial]()
//!
//!
//! The premise of this example is that the client requests the server to
//! add two integers together and the server responds with the sum of the
//! two incoming integers.
//!
//!

#[deny(missing_docs)]

use ncomm_core::Executor;
use ncomm_executors::SimpleExecutor;

Expand All @@ -35,21 +34,14 @@ pub struct AddTwoIntsResponse {

fn main() {
let mut server_node = MinimalServer::new();
let client_node = MinimalClient::new(
server_node.create_client(String::from("Minimal Client")),
);
let client_node = MinimalClient::new(server_node.create_client(String::from("Minimal Client")));

let (tx, rx) = unbounded();
ctrlc::set_handler(move || tx.send(true).expect("Could not send data"))
.expect("Error setting Ctrl-C handler");

let mut executor = SimpleExecutor::new_with(
rx,
vec![
Box::new(client_node),
Box::new(server_node),
]
);
let mut executor =
SimpleExecutor::new_with(rx, vec![Box::new(client_node), Box::new(server_node)]);

executor.update_loop();
}
}
15 changes: 9 additions & 6 deletions examples/minimal-client-server/src/minimal_client.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//!
//! The Minimal Client node sends requests to add two `u64`s and receives a response
//! containing the sum of the two `u64`s.
//!
//!

use super::{AddTwoIntsRequest, AddTwoIntsResponse};

use ncomm_core::{Node, Client};
use ncomm_clients_and_servers::local::LocalClient;
use ncomm_core::{Client, Node};

use rand::random;

Expand All @@ -24,9 +24,12 @@ impl MinimalClient {

/// Generate two random numbers as a and b and send a request for their sum
fn send_request(&mut self) {
self.client.send_request(
AddTwoIntsRequest { a: random(), b: random() }
).unwrap();
self.client
.send_request(AddTwoIntsRequest {
a: random(),
b: random(),
})
.unwrap();
}

/// Poll for incoming responses and print the two input numbers and their sum.
Expand All @@ -47,4 +50,4 @@ impl Node for MinimalClient {
self.send_request();
self.handle_responses();
}
}
}
23 changes: 15 additions & 8 deletions examples/minimal-client-server/src/minimal_server.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//!
//! The Minimal Server responds to requests seeking to add two u64s with
//! the wrapping sum of the two u64s.
//!
//!

use super::{AddTwoIntsRequest, AddTwoIntsResponse};

use ncomm_core::{Node, Server};
use ncomm_clients_and_servers::local::{LocalClient, LocalServer};
use ncomm_core::{Node, Server};

/// A minimal server node example that receives requests to add two ints and
/// responds with the wrapping sum of the two integers.
Expand All @@ -23,7 +23,10 @@ impl MinimalServer {
}

/// Creates a client for the local server the node has
pub fn create_client(&mut self, client_name: String) -> LocalClient<AddTwoIntsRequest, AddTwoIntsResponse>{
pub fn create_client(
&mut self,
client_name: String,
) -> LocalClient<AddTwoIntsRequest, AddTwoIntsResponse> {
self.server.create_client(client_name)
}

Expand All @@ -32,11 +35,15 @@ impl MinimalServer {
fn handle_requests(&mut self) {
for request in self.server.poll_for_requests() {
let Ok((client_name, request)) = request;
self.server.send_response(
client_name,
request,
AddTwoIntsResponse { sum: request.a.wrapping_add(request.b) }
).unwrap();
self.server
.send_response(
client_name,
request,
AddTwoIntsResponse {
sum: request.a.wrapping_add(request.b),
},
)
.unwrap();
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions examples/minimal-publisher-subscriber/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//!
//! This example shows a minimal publisher based on the
//! [Ros 2 Foxy Tutorial](https://docs.ros.org/en/foxy/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html).
//!
//!
//! The premise of this example is that the publisher will publish the
//! string "Hello World! <NUM>", where NUM is incremented with each incremental
//! publish.
//!
//!

#![deny(missing_docs)]

Expand All @@ -23,21 +23,16 @@ use minimal_subscriber::MinimalSubscriber;

fn main() {
let mut publisher_node = MinimalPublisher::new();
let subscriber_node = MinimalSubscriber::new(
publisher_node.create_subscriber()
);
let subscriber_node = MinimalSubscriber::new(publisher_node.create_subscriber());

let (tx, rx) = unbounded();
ctrlc::set_handler(move || tx.send(true).expect("Could not send interrupt"))
.expect("Error setting Ctrl-C handler");

let mut executor = SimpleExecutor::new_with(
rx,
vec![
Box::new(publisher_node),
Box::new(subscriber_node),
]
vec![Box::new(publisher_node), Box::new(subscriber_node)],
);

executor.update_loop();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//!
//! The minimal publisher keeps track of a count and publishes
//! the message "Hello, World! {count}" to the subscriber.
//!
//!

use ncomm_core::{Node, Publisher};
use ncomm_publishers_and_subscribers::local::{LocalPublisher, LocalSubscriber};
Expand Down Expand Up @@ -44,4 +44,4 @@ impl Node for MinimalPublisher {
fn update(&mut self) {
self.publish_message()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//!
//! Minimal Subscriber Node that receives the message "Hello World! {count}" from
//! the publisher node.
//!
//!

use ncomm_core::{Node, Subscriber};
use ncomm_publishers_and_subscribers::local::LocalSubscriber;
Expand All @@ -15,9 +15,7 @@ pub struct MinimalSubscriber {
impl MinimalSubscriber {
/// Creates a new MinimalSubscriber Node with a given subscriber
pub fn new(subscriber: LocalSubscriber<String>) -> Self {
Self {
subscriber,
}
Self { subscriber }
}

/// Print "I heard: {data}" with the data published to it by the minimal
Expand All @@ -37,4 +35,4 @@ impl Node for MinimalSubscriber {
fn update(&mut self) {
self.print_subscriber_data();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//!
//! A Simple Update Client Example that sends a request for the nth term in the
//! fibonacci series.
//!
//!

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

use ncomm_core::{Node, UpdateClient};
use ncomm_update_clients_and_servers::local::LocalUpdateClient;
Expand All @@ -17,18 +17,21 @@ pub struct FibonacciUpdateClient {

impl FibonacciUpdateClient {
/// Create a new Fibonacci Update Client
pub fn new(update_client: LocalUpdateClient<FibonacciRequest, FibonacciUpdate, FibonacciResponse>) -> Self {
Self {
update_client,
}
pub fn new(
update_client: LocalUpdateClient<FibonacciRequest, FibonacciUpdate, FibonacciResponse>,
) -> Self {
Self { update_client }
}

/// Print the update messages received
pub fn handle_updates(&mut self) {
let updates = self.update_client.poll_for_updates();
for update in updates.iter() {
if let Ok((_request, update)) = update {
println!("Last Num: {} --- Current Num: {}", update.last_num, update.current_num);
println!(
"Last Num: {} --- Current Num: {}",
update.last_num, update.current_num
);
}
}
}
Expand All @@ -52,11 +55,13 @@ impl Node for FibonacciUpdateClient {
}

fn start(&mut self) {
self.update_client.send_request(FibonacciRequest { order: 10 }).unwrap();
self.update_client
.send_request(FibonacciRequest { order: 10 })
.unwrap();
}

fn update(&mut self) {
self.handle_updates();
self.handle_response();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
//! An update server example that calculates the nth term in the
//! fibonacci sequence, updating a client with each sequence of terms
//! into the series
//!
//!

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

use ncomm_core::{Node, UpdateServer};
use ncomm_update_clients_and_servers::local::{LocalUpdateClient, LocalUpdateServer};

/// An update server node that calculates the nth term in the fibonacci
/// sequence
///
///
/// Note: for this example, the update server only expects a single client but
/// it would be trivial to make this update server capable of handling multiple
/// clients.
Expand All @@ -38,7 +38,10 @@ impl FibonacciUpdateServer {
}

/// Create a client for the fibonacci update server node
pub fn create_client(&mut self, client_name: String) -> LocalUpdateClient<FibonacciRequest, FibonacciUpdate, FibonacciResponse> {
pub fn create_client(
&mut self,
client_name: String,
) -> LocalUpdateClient<FibonacciRequest, FibonacciUpdate, FibonacciResponse> {
self.update_server.create_update_client(client_name)
}

Expand All @@ -59,11 +62,16 @@ impl FibonacciUpdateServer {
let next_num = self.last_num + self.current_num;
self.last_num = self.current_num;
self.current_num = next_num;
self.update_server.send_update(
client_id.clone(),
&request,
FibonacciUpdate { last_num: self.last_num, current_num: self.current_num }
).unwrap();
self.update_server
.send_update(
client_id.clone(),
&request,
FibonacciUpdate {
last_num: self.last_num,
current_num: self.current_num,
},
)
.unwrap();
self.current_order += 1;
}
}
Expand All @@ -72,11 +80,15 @@ impl FibonacciUpdateServer {
fn send_response(&mut self) {
if self.current_request.is_some() && self.current_order == self.order {
let (client_id, request) = self.current_request.take().unwrap();
self.update_server.send_response(
client_id,
request,
FibonacciResponse { num: self.current_num }
).unwrap();
self.update_server
.send_response(
client_id,
request,
FibonacciResponse {
num: self.current_num,
},
)
.unwrap();
}
}
}
Expand All @@ -91,4 +103,4 @@ impl Node for FibonacciUpdateServer {
self.send_updates();
self.send_response();
}
}
}
9 changes: 3 additions & 6 deletions examples/minimal-update-client-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! the server responds with updates containing the last and current
//! value in a fibonacci series before it finds the fibonacci value of
//! given degree which is returned as a response
//!
//!

#![deny(missing_docs)]

Expand Down Expand Up @@ -47,7 +47,7 @@ pub struct FibonacciResponse {
fn main() {
let mut update_server_node = FibonacciUpdateServer::new();
let update_client_node = FibonacciUpdateClient::new(
update_server_node.create_client(String::from("Fibonacci Update Client"))
update_server_node.create_client(String::from("Fibonacci Update Client")),
);

let (tx, rx) = unbounded();
Expand All @@ -56,10 +56,7 @@ fn main() {

let mut executor = SimpleExecutor::new_with(
rx,
vec![
Box::new(update_client_node),
Box::new(update_server_node),
]
vec![Box::new(update_client_node), Box::new(update_server_node)],
);

executor.update_loop();
Expand Down
6 changes: 3 additions & 3 deletions ncomm-clients-and-servers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//!
//! NComm Clients and Servers
//!
//!
//! This crate contains a set of commonly used servers and their
//! respective clients to enable the sharing of data between
//! Nodes.
//!
//!

#![deny(missing_docs)]

pub mod local;

pub mod udp;
pub mod udp;
Loading

0 comments on commit e2a732d

Please sign in to comment.