Skip to content

Commit

Permalink
Change struct Configuration to enum Configs
Browse files Browse the repository at this point in the history
  • Loading branch information
sophie-cluml committed Feb 21, 2024
1 parent 0bec4d0 commit d4bd2fc
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 12 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

- Change struct `Configuration` to enum `Configs` and define configuration by
module.

## [0.9.3] - 2024-02-16

### Added
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod request;
mod test;

use num_enum::{FromPrimitive, IntoPrimitive};
pub use request::{Configuration, Process, ResourceUsage};
pub use request::{Configs, Process, ResourceUsage};

/// Numeric representation of the message types.
#[derive(Clone, Copy, Debug, Eq, FromPrimitive, IntoPrimitive, PartialEq)]
Expand Down
83 changes: 72 additions & 11 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,40 @@ pub struct Process {
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Configuration {
pub hog_sources: Option<Vec<String>>,
pub hog_protocols: Option<Vec<String>>,
pub publish_address: Option<SocketAddr>,
pub ingest_address: Option<SocketAddr>,
pub giganto_name: Option<String>,
pub dump_file: Option<String>,
pub dump_size: Option<usize>,
pub enum Configs {
Hog(HogConfig),
Piglet(PigletConfig),
Reconverge(ReconvergeConfig),
Crusher(CrusherConfig),
}

#[derive(Debug, Deserialize, Serialize)]
pub struct HogConfig {
pub review_address: SocketAddr,
pub giganto_address: Option<SocketAddr>,
pub active_protocols: Option<Vec<String>>,
pub active_sources: Option<Vec<String>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct PigletConfig {
pub review_address: SocketAddr,
pub giganto_address: Option<SocketAddr>,
pub log_options: Option<Vec<String>>,
pub http_file_types: Option<Vec<String>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ReconvergeConfig {
pub review_address: SocketAddr,
pub giganto_address: Option<SocketAddr>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct CrusherConfig {
pub review_address: SocketAddr,
pub giganto_ingest_address: Option<SocketAddr>,
pub giganto_publish_address: Option<SocketAddr>,
}

#[derive(Clone, Default, Deserialize, Eq, PartialEq, Serialize)]
Expand Down Expand Up @@ -120,11 +145,11 @@ pub trait Handler: Send {
return Err("not supported".to_string());
}

async fn get_config(&mut self) -> Result<Configuration, String> {
async fn get_config(&mut self) -> Result<Configs, String> {
return Err("not supported".to_string());
}

async fn set_config(&mut self, _config: Configuration) -> Result<(), String> {
async fn set_config(&mut self, _config: Configs) -> Result<(), String> {
return Err("not supported".to_string());
}

Expand Down Expand Up @@ -295,7 +320,7 @@ pub async fn handle<H: Handler>(
}
RequestCode::SetConfig => {
let conf = codec
.deserialize::<Configuration>(body)
.deserialize::<Configs>(body)
.map_err(frame::RecvError::DeserializationFailure)?;
let result = handler.set_config(conf).await;
send_response(send, &mut buf, result).await?;
Expand Down Expand Up @@ -360,6 +385,7 @@ mod tests {
trusted_user_agents: usize,
process_list_cnt: usize,
shutdown_count: usize,
set_config_cnt: usize,
}

#[async_trait]
Expand Down Expand Up @@ -418,6 +444,11 @@ mod tests {
self.shutdown_count += 1;
Ok(())
}

async fn set_config(&mut self, _config: super::Configs) -> Result<(), String> {
self.set_config_cnt += 1;
Ok(())
}
}

let mut handler = TestHandler::default();
Expand Down Expand Up @@ -567,6 +598,34 @@ mod tests {
)
.await;
assert!(res.is_ok());
let hog_config = super::Configs::Hog(super::HogConfig {
review_address: "127.0.0.1:1234".parse().unwrap(),
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::Configs::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();

Expand All @@ -579,6 +638,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,
Expand All @@ -595,6 +655,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
Expand Down

0 comments on commit d4bd2fc

Please sign in to comment.