Skip to content

Commit

Permalink
simplify state load/dump functions
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieugouel committed Jan 4, 2025
1 parent ccae3b3 commit f7eb045
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/bmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub async fn unmarshal_bmp_packet(socket: &mut TcpStream) -> Result<BmpMessage>
}
}

async fn process(
async fn process_bmp_packet(
state: AsyncState,
tx: Sender<Vec<u8>>,
router_addr: IpAddr,
Expand Down Expand Up @@ -194,7 +194,7 @@ pub async fn handle(socket: &mut TcpStream, state: AsyncState, tx: Sender<Vec<u8
let process_state = state.clone();
let process_tx = tx.clone();
tokio::spawn(async move {
process(process_state, process_tx, router_ip, router_port, message).await;
process_bmp_packet(process_state, process_tx, router_ip, router_port, message).await;
});
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

// Load the state if enabled
if state_config.enable {
state::load(state.clone(), state_config.clone());
state::load(state.clone());
}

// MPSC channel to communicate between BMP tasks and producer task
Expand Down
14 changes: 7 additions & 7 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ use crate::update::{format_update, synthesize_withdraw_update, Update};

pub type AsyncState = Arc<Mutex<State>>;

pub fn new_state(_sc: &StateConfig) -> AsyncState {
Arc::new(Mutex::new(State::new(_sc)))
pub fn new_state(state_config: &StateConfig) -> AsyncState {
Arc::new(Mutex::new(State::new(state_config)))
}

pub fn dump(state: AsyncState, cfg: StateConfig) {
pub fn dump(state: AsyncState) {
let state = state.lock().unwrap();
let file = std::fs::File::create(cfg.path).unwrap();
let file = std::fs::File::create(state.config.path.clone()).unwrap();
let mut writer = std::io::BufWriter::new(file);
serde_json::to_writer(&mut writer, &state.store).unwrap();
}

pub fn load(state: AsyncState, cfg: StateConfig) {
pub fn load(state: AsyncState) {
let mut state = state.lock().unwrap();

let file = match std::fs::File::open(cfg.path) {
let file = match std::fs::File::open(state.config.path.clone()) {
Ok(file) => file,
Err(_) => return,
};
Expand Down Expand Up @@ -321,7 +321,7 @@ pub async fn dump_handler(state: AsyncState, cfg: StateConfig) {
tokio::time::sleep(Duration::from_secs(cfg.interval)).await;
if cfg.enable {
log::debug!("state - dump handler - dumping state to {}", cfg.path);
dump(state.clone(), cfg.clone());
dump(state.clone());
}
}
}

0 comments on commit f7eb045

Please sign in to comment.