From d592c2bbeb63f2cde1f9593faab0317f65385123 Mon Sep 17 00:00:00 2001 From: sophie Date: Thu, 15 Feb 2024 12:48:07 +0900 Subject: [PATCH] Change struct `Configuration` to enum `Config` --- CHANGELOG.md | 8 +++++ src/lib.rs | 2 +- src/request.rs | 84 +++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 82 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a75f17..10894d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ file is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Changed + +- Changed struct `Configuration` to enum `Configs` and defined configuration by + module. + ## [0.9.3] - 2024-02-16 ### Added @@ -248,6 +255,7 @@ without relying on the content of the response. - `send_frame` and `recv_frame` to send and receive length-delimited frames. +[Unreleased]: https://github.com/petabi/oinq/compare/0.9.3...main [0.9.3]: https://github.com/petabi/oinq/compare/0.9.2...0.9.3 [0.9.2]: https://github.com/petabi/oinq/compare/0.9.1...0.9.2 [0.9.1]: https://github.com/petabi/oinq/compare/0.9.0...0.9.1 diff --git a/src/lib.rs b/src/lib.rs index d0f8bee..5ef4ec6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ pub mod request; mod test; use num_enum::{FromPrimitive, IntoPrimitive}; -pub use request::{Configuration, Process, ResourceUsage}; +pub use request::{Config, Process, ResourceUsage}; /// Numeric representation of the message types. #[derive(Clone, Copy, Debug, Eq, FromPrimitive, IntoPrimitive, PartialEq)] diff --git a/src/request.rs b/src/request.rs index eae69b4..572941f 100644 --- a/src/request.rs +++ b/src/request.rs @@ -42,15 +42,40 @@ pub struct Process { } #[derive(Debug, Deserialize, Serialize)] -pub struct Configuration { - pub hog_sources: Option>, - pub hog_protocols: Option>, - pub publish_address: Option, - pub ingest_address: Option, - pub giganto_name: Option, - pub dump_file: Option, - pub dump_size: Option, +pub enum Config { + Hog(HogConfig), + Piglet(PigletConfig), + Reconverge(ReconvergeConfig), + Crusher(CrusherConfig), +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct HogConfig { + pub review_address: SocketAddr, + pub giganto_address: Option, + pub active_protocols: Option>, + pub active_sources: Option>, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct PigletConfig { + pub review_address: SocketAddr, + pub giganto_address: Option, pub log_options: Option>, + pub http_file_types: Option>, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct ReconvergeConfig { + pub review_address: SocketAddr, + pub giganto_address: Option, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct CrusherConfig { + pub review_address: SocketAddr, + pub giganto_ingest_address: Option, + pub giganto_publish_address: Option, } #[derive(Clone, Default, Deserialize, Eq, PartialEq, Serialize)] @@ -120,11 +145,11 @@ pub trait Handler: Send { return Err("not supported".to_string()); } - async fn get_config(&mut self) -> Result { + async fn get_config(&mut self) -> Result { return Err("not supported".to_string()); } - async fn set_config(&mut self, _config: Configuration) -> Result<(), String> { + async fn set_config(&mut self, _config: Config) -> Result<(), String> { return Err("not supported".to_string()); } @@ -295,7 +320,7 @@ pub async fn handle( } RequestCode::SetConfig => { let conf = codec - .deserialize::(body) + .deserialize::(body) .map_err(frame::RecvError::DeserializationFailure)?; let result = handler.set_config(conf).await; send_response(send, &mut buf, result).await?; @@ -360,6 +385,7 @@ mod tests { trusted_user_agents: usize, process_list_cnt: usize, shutdown_count: usize, + set_config_cnt: usize, } #[async_trait] @@ -418,6 +444,11 @@ mod tests { self.shutdown_count += 1; Ok(()) } + + async fn set_config(&mut self, _config: super::Config) -> Result<(), String> { + self.set_config_cnt += 1; + Ok(()) + } } let mut handler = TestHandler::default(); @@ -567,6 +598,35 @@ mod tests { ) .await; assert!(res.is_ok()); + let hog_config = super::Config::Hog(super::HogConfig { + review_address: "127.0.0.1:1234".parse().unwrap(), + giganto_address: None, + active_protocols: None, + active_sources: None, + }); + + let res = message::send_request( + &mut channel.client.send, + &mut buf, + RequestCode::SetConfig, + hog_config, + ) + .await; + assert!(res.is_ok()); + + let reconverge_config = super::Config::Reconverge(super::ReconvergeConfig { + review_address: "127.0.0.1:1234".parse().unwrap(), + giganto_address: "127.0.0.1:2345".parse().ok(), + }); + + let res = message::send_request( + &mut channel.client.send, + &mut buf, + RequestCode::SetConfig, + reconverge_config, + ) + .await; + assert!(res.is_ok()); channel.client.send.finish().await.unwrap(); @@ -579,6 +639,7 @@ mod tests { assert_eq!(handler.trusted_user_agents, 0); assert_eq!(handler.process_list_cnt, 0); assert_eq!(handler.shutdown_count, 0); + assert_eq!(handler.set_config_cnt, 0); let res = super::handle( &mut handler, &mut channel.server.send, @@ -595,6 +656,7 @@ mod tests { assert_eq!(handler.trusted_user_agents, 4); assert_eq!(handler.process_list_cnt, 1); assert_eq!(handler.shutdown_count, 1); + assert_eq!(handler.set_config_cnt, 2); frame::recv_raw(&mut channel.client.recv, &mut buf) .await