From 27a8ce0dd49a89e380cebbf44e6b4e765724a536 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Wed, 2 Oct 2019 22:54:44 -0400 Subject: [PATCH] Start reading the CnC file --- Cargo.toml | 1 + src/client/cnc_descriptor.rs | 92 ++++++++++++++++++++++++++++ src/{client.rs => client/context.rs} | 4 +- src/client/mod.rs | 5 ++ 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 src/client/cnc_descriptor.rs rename src/{client.rs => client/context.rs} (92%) create mode 100644 src/client/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 492aaf4..a8ab2f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ maintenance = { status = "actively-developed" } [dependencies] aeron_driver-sys = { path = "./aeron_driver-sys" } +memmap = "0.7" [dev-dependencies] clap = "2.33" diff --git a/src/client/cnc_descriptor.rs b/src/client/cnc_descriptor.rs new file mode 100644 index 0000000..42ea7b0 --- /dev/null +++ b/src/client/cnc_descriptor.rs @@ -0,0 +1,92 @@ +//! Description of the command and control file used to communicate between the Media Driver +//! and its clients. +//! +//! File layout: +//! +//! ```text +//! +-----------------------------+ +//! | Meta Data | +//! +-----------------------------+ +//! | to-driver Buffer | +//! +-----------------------------+ +//! | to-clients Buffer | +//! +-----------------------------+ +//! | Counters Metadata Buffer | +//! +-----------------------------+ +//! | Counters Values Buffer | +//! +-----------------------------+ +//! | Error Log | +//! +-----------------------------+ +//! ``` + +/// The CnC file metadata header. Layout: +/// +/// ```text +/// 0 1 2 3 +/// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +/// | Aeron CnC Version | +/// +---------------------------------------------------------------+ +/// | to-driver buffer length | +/// +---------------------------------------------------------------+ +/// | to-clients buffer length | +/// +---------------------------------------------------------------+ +/// | Counters Metadata buffer length | +/// +---------------------------------------------------------------+ +/// | Counters Values buffer length | +/// +---------------------------------------------------------------+ +/// | Error Log buffer length | +/// +---------------------------------------------------------------+ +/// | Client Liveness Timeout | +/// | | +/// +---------------------------------------------------------------+ +/// | Driver Start Timestamp | +/// | | +/// +---------------------------------------------------------------+ +/// | Driver PID | +/// | | +/// +---------------------------------------------------------------+ +/// ``` +#[repr(C, align(4))] +pub struct MetaDataDefinition { + cnc_version: i32, + _to_driver_buffer_length: i32, + _to_client_buffer_length: i32, + _counter_metadata_buffer_length: i32, + _counter_values_buffer_length: i32, + _error_log_buffer_length: i32, + _client_liveness_timeout: i64, + _start_timestamp: i64, + _pid: i64, +} + +#[cfg(test)] +mod tests { + use crate::client::cnc_descriptor::MetaDataDefinition; + use crate::driver::{DriverContext, MediaDriver}; + use memmap::MmapOptions; + use std::fs::File; + use tempfile::tempdir; + + #[test] + fn read_cnc_version() { + let dir = tempdir().unwrap(); + let dir_path = dir.as_ref().to_path_buf(); + dir.close().unwrap(); + + let context = DriverContext::default().set_aeron_dir(&dir_path); + let _driver = MediaDriver::with_context(context).unwrap(); + + // Open the CnC location + let cnc_path = dir_path.join("cnc.dat"); + let cnc_file = File::open(&cnc_path).expect("Unable to open CnC file"); + let mmap = unsafe { + MmapOptions::default() + .map(&cnc_file) + .expect("Unable to memory map CnC file") + }; + + let metadata: &MetaDataDefinition = unsafe { &*(mmap.as_ptr().cast()) }; + assert_eq!(metadata.cnc_version, 16); + } +} diff --git a/src/client.rs b/src/client/context.rs similarity index 92% rename from src/client.rs rename to src/client/context.rs index 0702c24..d011493 100644 --- a/src/client.rs +++ b/src/client/context.rs @@ -5,7 +5,7 @@ use std::path::PathBuf; /// Context used to initialize the Aeron client pub struct ClientContext { - aeron_dir: PathBuf, + _aeron_dir: PathBuf, } impl ClientContext { @@ -31,7 +31,7 @@ impl ClientContext { impl Default for ClientContext { fn default() -> Self { ClientContext { - aeron_dir: ClientContext::default_aeron_path(), + _aeron_dir: ClientContext::default_aeron_path(), } } } diff --git a/src/client/mod.rs b/src/client/mod.rs new file mode 100644 index 0000000..969153c --- /dev/null +++ b/src/client/mod.rs @@ -0,0 +1,5 @@ +//! Aeron client +//! +//! These are the modules necessary to construct a functioning Aeron client +pub mod cnc_descriptor; +pub mod context;