Fixed how `router` implement `action`s. We now need to implement the trait `Action` for our actions.

I am wrapping `router` in an `Arc` to share it between the threads.
This commit is contained in:
Martin Slot 2023-11-27 16:36:17 +01:00
parent 7c05a961fb
commit 346bed321e
3 changed files with 15 additions and 14 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target
/.idea

View File

@ -32,7 +32,7 @@ pub struct Host {
options: HostOptions,
should_stop: Arc<AtomicBool>,
handle: Option<thread::JoinHandle<Result<(), HostError>>>,
router: Router,
router: Arc<Router>,
}
impl Host {
@ -41,7 +41,7 @@ impl Host {
options,
should_stop: Arc::new(AtomicBool::new(false)),
handle: None,
router: Router::new(),
router: Arc::new(Router::new()),
}
}
@ -53,6 +53,7 @@ impl Host {
return Ok(());
}
let router = self.router.clone();
self.handle = Some(thread::spawn(move || -> Result<(), HostError> {
let listener = match TcpListener::bind(format!("{}:{}", options.ip, options.port)) {
Ok(it) => it,
@ -82,13 +83,13 @@ impl Host {
Err(e) => return Err(HostError::ReadError),
};
let payload = Payload::decode(Cursor::new(bytes));
let payload = Payload::decode(Cursor::new(&bytes));
let payload = match payload {
Ok(p) => p,
Err(e) => return Err(HostError::DecodeError),
};
self.router.call(payload.route, bytes);
router.call(payload.route, &bytes);
}
Ok(())
}));

View File

@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::sync::Arc;
pub struct Router {
router_map: HashMap<String, Box<dyn Action>>,
@ -16,7 +15,7 @@ impl Router {
self.router_map.insert(route.to_string(), action);
}
pub fn call(&self, route: String, data: Vec<u8>) {
pub fn call(&self, route: String, data: &Vec<u8>) {
let action = self.router_map.get(&route);
let action = match action {
@ -24,12 +23,12 @@ impl Router {
Some(a) => a,
};
action.work(data);
action.work(&data);
}
}
pub trait Action {
fn work(&self, data: Vec<u8>);
pub trait Action: Send + Sync {
fn work(&self, data: &Vec<u8>);
}
mod tests {
use super::{Action, Router};
@ -37,7 +36,7 @@ mod tests {
struct TestAction;
impl Action for TestAction {
fn work(&self, data: Vec<u8>) {}
fn work(&self, data: &Vec<u8>) {}
}
#[test]
fn create_router_with_one_action() {
@ -52,7 +51,7 @@ mod tests {
let action = TestAction;
router.add_action("my/action", Box::new(action));
router.call("my/action".to_string(), vec![0u8, 1u8, 2u8]);
router.call("my/action".to_string(), &vec![0u8, 1u8, 2u8]);
}
#[test]
@ -61,7 +60,7 @@ mod tests {
let action = TestAction;
router.add_action("my/action", Box::new(action));
router.call("my/action/none".to_string(), vec![0u8, 1u8, 2u8]);
router.call("my/action/none".to_string(), &vec![0u8, 1u8, 2u8]);
}
#[test]
@ -70,7 +69,7 @@ mod tests {
let action = TestAction;
router.add_action("my/action", Box::new(action));
router.call("my/action".to_string(), vec![0u8, 1u8, 2u8]);
router.call("my/action".to_string(), vec![0u8, 1u8, 2u8]);
router.call("my/action".to_string(), &vec![0u8, 1u8, 2u8]);
router.call("my/action".to_string(), &vec![0u8, 1u8, 2u8]);
}
}