Skip to content

Commit

Permalink
Merge pull request #25 from N8BWert/no-std-serial
Browse files Browse the repository at this point in the history
Add Serial Publisher/Subscriber + Client/Server
  • Loading branch information
N8BWert authored Sep 14, 2024
2 parents ff0fd93 + d5d2f04 commit 0590fcf
Show file tree
Hide file tree
Showing 28 changed files with 1,073 additions and 43 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ categories = ["science::robotics"]

[workspace.dependencies]
# NComm
ncomm = { path = "ncomm", version = "1.0.0" }
ncomm-core = { path = "ncomm-core", version = "1.0.0" }
ncomm-utils = { path = "ncomm-utils", version = "1.0.0" }
ncomm-executors = { path = "ncomm-executors", version = "1.0.0" }
ncomm-publishers-and-subscribers = { path = "ncomm-publishers-and-subscribers", version = "1.0.0" }
ncomm-clients-and-servers = { path = "ncomm-clients-and-servers", version = "1.0.0" }
ncomm-update-clients-and-servers = { path = "ncomm-update-clients-and-servers", version = "1.0.0" }
ncomm-nodes = { path = "ncomm-nodes", version = "1.0.0" }
ncomm = { path = "ncomm", version = "1.0.0", default-features = false }
ncomm-core = { path = "ncomm-core", version = "1.0.0", default-features = false }
ncomm-utils = { path = "ncomm-utils", version = "1.0.0", default-features = false }
ncomm-executors = { path = "ncomm-executors", version = "1.0.0", default-features = false }
ncomm-publishers-and-subscribers = { path = "ncomm-publishers-and-subscribers", version = "1.0.0", default-features = false }
ncomm-clients-and-servers = { path = "ncomm-clients-and-servers", version = "1.0.0", default-features = false }
ncomm-update-clients-and-servers = { path = "ncomm-update-clients-and-servers", version = "1.0.0", default-features = false }
ncomm-nodes = { path = "ncomm-nodes", version = "1.0.0", default-features = false }

# Outside Dependencies
crossbeam = "0.8.4"
Expand All @@ -59,3 +59,4 @@ rand_distr = "0.4.3"
rerun = "0.18.2"
re_web_viewer_server = "0.18.2"
re_ws_comms = "0.18.2"
embedded-io = "0.6.1"
13 changes: 10 additions & 3 deletions ncomm-clients-and-servers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ homepage.workspace = true
repository.workspace = true

[dependencies]
crossbeam = { workspace = true }
ncomm-core = { workspace = true }
ncomm-utils = { workspace = true }
crossbeam = { workspace = true, optional = true }
ncomm-core = { workspace = true, default-features = false }
ncomm-utils = { workspace = true, default-features = false }
embedded-io = { workspace = true }

[dev-dependencies]
rand = { workspace = true }

[features]
default = ["std"]
nostd = ["ncomm-core/nostd", "ncomm-utils/nostd"]
alloc = ["nostd", "ncomm-core/alloc", "ncomm-utils/alloc"]
std = ["dep:crossbeam", "ncomm-core/std", "ncomm-utils/std"]
7 changes: 7 additions & 0 deletions ncomm-clients-and-servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
//!

#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "std")]
pub mod local;

#[cfg(feature = "std")]
pub mod udp;

pub mod serial;
42 changes: 42 additions & 0 deletions ncomm-clients-and-servers/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ impl<Req, Res> Client for LocalClient<Req, Res> {
Ok(())
}

fn poll_for_response(
&mut self,
) -> Result<Option<(Self::Request, Self::Response)>, Self::Error> {
match self.rx.try_recv() {
Ok(response) => Ok(Some(response)),
Err(_) => Ok(None),
}
}

fn poll_for_responses(&mut self) -> Vec<Result<(Self::Request, Self::Response), Self::Error>> {
let mut responses = Vec::new();
for response in self.rx.try_iter() {
Expand Down Expand Up @@ -91,6 +100,15 @@ impl<Req, Res, K: Hash + Eq + Clone> Server for LocalServer<Req, Res, K> {
requests
}

fn poll_for_request(&mut self) -> Result<Option<(Self::Key, Self::Request)>, Self::Error> {
for (k, (rx, _)) in self.client_map.iter() {
if let Ok(request) = rx.try_recv() {
return Ok(Some((k.clone(), request)));
}
}
Ok(None)
}

fn send_response(
&mut self,
client_key: Self::Key,
Expand Down Expand Up @@ -155,4 +173,28 @@ mod tests {
assert_eq!(response, original_response);
}
}

#[test]
fn test_local_client_server_single() {
let mut server = LocalServer::new();
let mut client = server.create_client(0u8);

let original_request = Request::new();
let original_response = Response::new(original_request);
client.send_request(original_request).unwrap();
if let Ok(Some((client, request))) = server.poll_for_request() {
assert_eq!(request, original_request);
server
.send_response(client, request, Response::new(request.clone()))
.unwrap()
} else {
assert!(false, "Expected to receive request");
}
if let Ok(Some((request, response))) = client.poll_for_response() {
assert_eq!(request, original_request);
assert_eq!(response, original_response);
} else {
assert!(false, "Expected to receive response");
}
}
}
Loading

0 comments on commit 0590fcf

Please sign in to comment.