diff --git a/src/contact_manager/legacy/mod.rs b/src/contact_manager/legacy/mod.rs index 631beae..7780676 100644 --- a/src/contact_manager/legacy/mod.rs +++ b/src/contact_manager/legacy/mod.rs @@ -337,15 +337,13 @@ macro_rules! generate_prio_volume_manager { at_time: crate::types::Date, bundle: &crate::bundle::Bundle, ) -> Option { - if let Some(data) = self.dry_run_tx(contact_data, at_time, bundle) { - // Conditionally update queue size based on $auto_update - // Can overflow with overbooking - if $auto_update { - self.enqueue(bundle); - } - return Some(data); + let data = self.dry_run_tx(contact_data, at_time, bundle)?; + // Conditionally update queue size based on $auto_update + // Can overflow with overbooking + if $auto_update { + self.enqueue(bundle); } - None + return Some(data); } /// Initializes the segmentation manager by checking that rate and delay intervals have no gaps. diff --git a/src/contact_plan/from_asabr_lexer.rs b/src/contact_plan/from_asabr_lexer.rs index 80a0980..1194a26 100644 --- a/src/contact_plan/from_asabr_lexer.rs +++ b/src/contact_plan/from_asabr_lexer.rs @@ -139,18 +139,18 @@ impl ASABRContactPlan { return Err(msg); } ParsingState::Finished((info, manager)) => { - if let Some(contact) = Contact::try_new(info, manager) { - Self::add_contact( - contact, - &mut contacts, - &mut max_node_id_in_contacts, - ); - } else { + let Some(contact) = Contact::try_new(info, manager) else { return Err(format!( "Malformed contact ({})", lexer.get_current_position() )); - } + }; + + Self::add_contact( + contact, + &mut contacts, + &mut max_node_id_in_contacts, + ); } } } @@ -164,25 +164,20 @@ impl ASABRContactPlan { return Err(msg); } ParsingState::Finished((info, manager)) => { - if let Some(node) = Node::try_new(info, manager) { - match Self::add_node( - node, - &mut nodes, - &mut max_node_in_in_nodes, - &mut known_node_ids, - &mut known_node_names, - ) { - Ok(_) => {} - Err(msg) => { - return Err(msg); - } - } - } else { + let Some(node) = Node::try_new(info, manager) else { return Err(format!( "Malformed node ({})", lexer.get_current_position() )); - } + }; + + Self::add_node( + node, + &mut nodes, + &mut max_node_in_in_nodes, + &mut known_node_ids, + &mut known_node_names, + )?; } } } diff --git a/src/contact_plan/from_ion_file.rs b/src/contact_plan/from_ion_file.rs index 72b7282..4520f12 100644 --- a/src/contact_plan/from_ion_file.rs +++ b/src/contact_plan/from_ion_file.rs @@ -242,16 +242,18 @@ impl IONContactPlan { } for range in &ranges { - if let Some(tx_map) = contact_info_map.get_mut(&range.tx_node) { - if let Some(contact_vec) = tx_map.get_mut(&range.rx_node) { - for contact in contact_vec.iter_mut() { - if range.tx_start <= contact.tx_start && contact.tx_end <= range.tx_end { - contact.delay = range.delay; - contacts.push(CM::ion_convert(contact).unwrap()); - } else { - panic!("This parser only supports one range per contact"); - } - } + let Some(tx_map) = contact_info_map.get_mut(&range.tx_node) else { + continue; + }; + let Some(contact_vec) = tx_map.get_mut(&range.rx_node) else { + continue; + }; + for contact in contact_vec.iter_mut() { + if range.tx_start <= contact.tx_start && contact.tx_end <= range.tx_end { + contact.delay = range.delay; + contacts.push(CM::ion_convert(contact).unwrap()); + } else { + panic!("This parser only supports one range per contact"); } } } diff --git a/src/parsing.rs b/src/parsing.rs index c414bd9..2d5dfe5 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -224,21 +224,21 @@ macro_rules! implement_manager { ParsingState::EOF => ParsingState::EOF, ParsingState::Error(msg) => ParsingState::Error(msg), ParsingState::Finished(marker) => { - if let Some(marker_map) = marker_map_opt { - if let Some(parse_fn) = marker_map.get(marker.as_str()) { - parse_fn(lexer) - } else { - ParsingState::Error(format!( - "Unrecognized marker ({})", - lexer.get_current_position() - )) - } - } else { - ParsingState::Error(format!( + let Some(marker_map) = marker_map_opt else { + return ParsingState::Error(format!( "Dynamic parsing requires a map ({})", lexer.get_current_position() - )) - } + )); + }; + + let Some(parse_fn) = marker_map.get(marker.as_str()) else { + return ParsingState::Error(format!( + "Unrecognized marker ({})", + lexer.get_current_position() + )); + }; + + parse_fn(lexer) } } } diff --git a/src/pathfinding/contact_parenting.rs b/src/pathfinding/contact_parenting.rs index 4804e79..3409213 100644 --- a/src/pathfinding/contact_parenting.rs +++ b/src/pathfinding/contact_parenting.rs @@ -190,8 +190,9 @@ macro_rules! define_contact_graph { let rx_node_id = receiver.node.borrow().info.id; if let Some(hop) = &route_proposition.via { - let route_proposition_ref = - Rc::new(RefCell::new(route_proposition.clone_work_area())); + let route_proposition_ref = Rc::new(RefCell::new( + route_proposition.clone_work_area(), + )); priority_queue.push(Reverse(DistanceWrapper::new( route_proposition_ref.clone(), ))); diff --git a/src/pathfinding/hybrid_parenting.rs b/src/pathfinding/hybrid_parenting.rs index 4b6b1d8..713f522 100644 --- a/src/pathfinding/hybrid_parenting.rs +++ b/src/pathfinding/hybrid_parenting.rs @@ -332,26 +332,30 @@ macro_rules! define_mpt { } } - if let Some(first_contact_index) = + let Some(first_contact_index) = receiver.lazy_prune_and_get_first_idx(current_time) - { - if let Some(route_proposition) = try_make_hop( - first_contact_index, - &from_route, - bundle, - &receiver.contacts_to_receiver, - &sender.node, - &receiver.node, - ) { - // This transforms a prop in the stack to a prop in the heap - if let Some(new_route) = - try_insert::(route_proposition, &mut tree) - { - priority_queue - .push(Reverse(DistanceWrapper::new(new_route.clone()))); - } - } - } + else { + continue; + }; + + let Some(route_proposition) = try_make_hop( + first_contact_index, + &from_route, + bundle, + &receiver.contacts_to_receiver, + &sender.node, + &receiver.node, + ) else { + continue; + }; + + // This transforms a prop in the stack to a prop in the heap + let Some(new_route) = try_insert::(route_proposition, &mut tree) + else { + continue; + }; + + priority_queue.push(Reverse(DistanceWrapper::new(new_route.clone()))); } } diff --git a/src/pathfinding/node_parenting.rs b/src/pathfinding/node_parenting.rs index b563de4..90a018b 100644 --- a/src/pathfinding/node_parenting.rs +++ b/src/pathfinding/node_parenting.rs @@ -126,37 +126,41 @@ macro_rules! define_node_graph { } } - if let Some(first_contact_index) = + let Some(first_contact_index) = receiver.lazy_prune_and_get_first_idx(current_time) - { - if let Some(route_proposition) = try_make_hop( - first_contact_index, - &from_route, - bundle, - &receiver.contacts_to_receiver, - &sender.node, - &receiver.node, - ) { - let mut push = false; - if let Some(know_route_ref) = tree.by_destination - [receiver.node.borrow().info.id as usize] - .clone() - { - let mut known_route = know_route_ref.borrow_mut(); - if D::cmp(&route_proposition, &known_route) == Ordering::Less { - known_route.is_disabled = true; - push = true; - } + else { + continue; + }; + + let Some(route_proposition) = try_make_hop( + first_contact_index, + &from_route, + bundle, + &receiver.contacts_to_receiver, + &sender.node, + &receiver.node, + ) else { + continue; + }; + + let idx = receiver.node.borrow().info.id as usize; + let push = match tree.by_destination[idx].as_ref() { + Some(known_route_ref) => { + let mut known_route = known_route_ref.borrow_mut(); + if D::cmp(&route_proposition, &known_route) == Ordering::Less { + known_route.is_disabled = true; + true } else { - push = true; - } - if push { - let route_ref = Rc::new(RefCell::new(route_proposition)); - tree.by_destination[receiver.node.borrow().info.id as usize] = - Some(route_ref.clone()); - priority_queue.push(Reverse(DistanceWrapper::new(route_ref))); + false } } + None => true, + }; + + if push { + let route_ref = Rc::new(RefCell::new(route_proposition)); + tree.by_destination[idx] = Some(route_ref.clone()); + priority_queue.push(Reverse(DistanceWrapper::new(route_ref))); } } } diff --git a/src/route_stage.rs b/src/route_stage.rs index 7d14960..e8d23a8 100644 --- a/src/route_stage.rs +++ b/src/route_stage.rs @@ -160,67 +160,68 @@ impl RouteStage { /// * `true` if the scheduling process was successful and the bundle is properly scheduled. /// * `false` if the scheduling process failed for any reason, such as a node being excluded, timing constraints, or invalid transmission conditions. pub fn schedule(&mut self, at_time: Date, bundle: &Bundle) -> bool { - if let Some(via) = &self.via { - let mut contact_borrowed = via.contact.borrow_mut(); - let info = contact_borrowed.info; + let Some(via) = &self.via else { + return false; + }; - // If bundle processing is enabled, a mutable bundle copy is required to be attached to the RouteStage. - #[cfg(feature = "node_proc")] - let mut bundle_to_consider = bundle.clone(); - #[cfg(not(feature = "node_proc"))] - let bundle_to_consider = bundle; + let mut contact_borrowed = via.contact.borrow_mut(); + let info = contact_borrowed.info; - #[allow(unused_mut)] - #[cfg(any(feature = "node_tx", feature = "node_proc"))] - let mut tx_node = via.tx_node.borrow_mut(); - #[cfg(feature = "node_rx")] - let mut rx_node = via.rx_node.borrow_mut(); + // If bundle processing is enabled, a mutable bundle copy is required to be attached to the RouteStage. + #[cfg(feature = "node_proc")] + let mut bundle_to_consider = bundle.clone(); + #[cfg(not(feature = "node_proc"))] + let bundle_to_consider = bundle; - #[cfg(feature = "node_proc")] - let sending_time = tx_node + #[allow(unused_mut)] + #[cfg(any(feature = "node_tx", feature = "node_proc"))] + let mut tx_node = via.tx_node.borrow_mut(); + #[cfg(feature = "node_rx")] + let mut rx_node = via.rx_node.borrow_mut(); + + #[cfg(feature = "node_proc")] + let sending_time = tx_node + .manager + .schedule_process(at_time, &mut bundle_to_consider); + #[cfg(not(feature = "node_proc"))] + let sending_time = at_time; + + let Some(res) = + contact_borrowed .manager - .schedule_process(at_time, &mut bundle_to_consider); - #[cfg(not(feature = "node_proc"))] - let sending_time = at_time; - - if let Some(res) = - contact_borrowed - .manager - .schedule_tx(&info, sending_time, &bundle_to_consider) - { - #[cfg(feature = "node_tx")] - if !tx_node.manager.schedule_tx( - sending_time, - res.tx_start, - res.tx_end, - &bundle_to_consider, - ) { - return false; - } + .schedule_tx(&info, sending_time, &bundle_to_consider) + else { + return false; + }; - let arrival_time = res.tx_end + res.delay; + #[cfg(feature = "node_tx")] + if !tx_node + .manager + .schedule_tx(sending_time, res.tx_start, res.tx_end, &bundle_to_consider) + { + return false; + } - if arrival_time > bundle_to_consider.expiration { - return false; - } - #[cfg(feature = "node_rx")] - if !rx_node.manager.schedule_rx( - res.tx_start + res.delay, - res.tx_end + res.delay, - &bundle_to_consider, - ) { - return false; - } + let arrival_time = res.tx_end + res.delay; - self.at_time = arrival_time; - #[cfg(feature = "node_proc")] - { - self.bundle = bundle_to_consider; - } - return true; - } + if arrival_time > bundle_to_consider.expiration { + return false; + } + #[cfg(feature = "node_rx")] + if !rx_node.manager.schedule_rx( + res.tx_start + res.delay, + res.tx_end + res.delay, + &bundle_to_consider, + ) { + return false; } - false + + self.at_time = arrival_time; + #[cfg(feature = "node_proc")] + { + self.bundle = bundle_to_consider; + } + true } /// Performs a dry run to simulate the transmission of a `bundle` through a network without actually @@ -243,75 +244,76 @@ impl RouteStage { /// * `true` if the dry run was successful and the bundle can be transmitted according to the simulation. /// * `false` if the dry run fails, such as due to an excluded node, invalid timing, or any other condition preventing transmission. pub fn dry_run(&mut self, at_time: Date, bundle: &Bundle, with_exclusions: bool) -> bool { - if let Some(via) = &self.via { - let contact_borrowed = via.contact.borrow_mut(); - let info = contact_borrowed.info; - - if with_exclusions { - { - let node = via.rx_node.borrow(); - if node.info.excluded { - return false; - } - } - } + let Some(via) = &self.via else { + return false; + }; - // If bundle processing is enabled, a mutable bundle copy is required to be attached to the RouteStage. - #[cfg(feature = "node_proc")] - let mut bundle_to_consider = bundle.clone(); - #[cfg(not(feature = "node_proc"))] - let bundle_to_consider = bundle; - - #[cfg(any(feature = "node_tx", feature = "node_proc"))] - let tx_node = via.tx_node.borrow_mut(); - #[cfg(feature = "node_rx")] - let rx_node = via.rx_node.borrow_mut(); - #[cfg(feature = "node_proc")] - let sending_time = tx_node - .manager - .dry_run_process(at_time, &mut bundle_to_consider); + let contact_borrowed = via.contact.borrow_mut(); + let info = contact_borrowed.info; - #[cfg(not(feature = "node_proc"))] - let sending_time = at_time; - - if let Some(res) = - contact_borrowed - .manager - .dry_run_tx(&info, sending_time, &bundle_to_consider) + if with_exclusions { { - #[cfg(feature = "node_tx")] - if !tx_node.manager.dry_run_tx( - sending_time, - res.tx_start, - res.tx_end, - &bundle_to_consider, - ) { + let node = via.rx_node.borrow(); + if node.info.excluded { return false; } + } + } - let arrival_time = res.tx_end + res.delay; + // If bundle processing is enabled, a mutable bundle copy is required to be attached to the RouteStage. + #[cfg(feature = "node_proc")] + let mut bundle_to_consider = bundle.clone(); + #[cfg(not(feature = "node_proc"))] + let bundle_to_consider = bundle; - if arrival_time > bundle_to_consider.expiration { - return false; - } - #[cfg(feature = "node_rx")] - if !rx_node.manager.dry_run_rx( - res.tx_start + res.delay, - res.tx_end + res.delay, - &bundle_to_consider, - ) { - return false; - } + #[cfg(any(feature = "node_tx", feature = "node_proc"))] + let tx_node = via.tx_node.borrow_mut(); + #[cfg(feature = "node_rx")] + let rx_node = via.rx_node.borrow_mut(); + #[cfg(feature = "node_proc")] + let sending_time = tx_node + .manager + .dry_run_process(at_time, &mut bundle_to_consider); - self.at_time = arrival_time; - #[cfg(feature = "node_proc")] - { - self.bundle = bundle_to_consider; - } - return true; - } + #[cfg(not(feature = "node_proc"))] + let sending_time = at_time; + + let Some(res) = + contact_borrowed + .manager + .dry_run_tx(&info, sending_time, &bundle_to_consider) + else { + return false; + }; + + #[cfg(feature = "node_tx")] + if !tx_node + .manager + .dry_run_tx(sending_time, res.tx_start, res.tx_end, &bundle_to_consider) + { + return false; + } + + let arrival_time = res.tx_end + res.delay; + + if arrival_time > bundle_to_consider.expiration { + return false; + } + #[cfg(feature = "node_rx")] + if !rx_node.manager.dry_run_rx( + res.tx_start + res.delay, + res.tx_end + res.delay, + &bundle_to_consider, + ) { + return false; + } + + self.at_time = arrival_time; + #[cfg(feature = "node_proc")] + { + self.bundle = bundle_to_consider; } - false + true } pub fn get_via_contact(&self) -> Option>>> { diff --git a/src/route_storage/mod.rs b/src/route_storage/mod.rs index f7d39e8..fc9370b 100644 --- a/src/route_storage/mod.rs +++ b/src/route_storage/mod.rs @@ -60,8 +60,8 @@ impl Route { pub fn from_tree(tree: Rc>>, dest: NodeID) -> Option { let tree_ref = tree.borrow(); let source_stage = tree_ref.get_source_route(); - let destination_stage= tree_ref.by_destination.get(dest as usize).cloned()??; - + let destination_stage = tree_ref.by_destination.get(dest as usize).cloned()??; + Some(Route { source_stage, destination_stage, diff --git a/src/routing/cgr.rs b/src/routing/cgr.rs index 94e4cee..40ee936 100644 --- a/src/routing/cgr.rs +++ b/src/routing/cgr.rs @@ -101,22 +101,19 @@ impl, NM: NodeManager, CM: ContactManager, P: Pathfindin .get_next(curr_time, source, &bundle_to_consider, excluded_nodes); let tree = Rc::new(RefCell::new(new_tree)); - if let Some(route) = Route::from_tree(tree, dest) { - RouteStage::init_route(route.destination_stage.clone()); - self.route_storage - .borrow_mut() - .store(bundle, route.clone()); - let dry_run = - dry_run_unicast_path(bundle, curr_time, route.source_stage.clone(), true); - if dry_run.is_some() { - return Some(schedule_unicast_path( - bundle, - curr_time, - route.source_stage.clone(), - )); - } - } else { + let Some(route) = Route::from_tree(tree, dest) else { break; + }; + + RouteStage::init_route(route.destination_stage.clone()); + self.route_storage.borrow_mut().store(bundle, route.clone()); + let dry_run = dry_run_unicast_path(bundle, curr_time, route.source_stage.clone(), true); + if dry_run.is_some() { + return Some(schedule_unicast_path( + bundle, + curr_time, + route.source_stage.clone(), + )); } } None diff --git a/src/routing/mod.rs b/src/routing/mod.rs index c0427d7..5e609e7 100644 --- a/src/routing/mod.rs +++ b/src/routing/mod.rs @@ -183,11 +183,11 @@ fn update_multicast( HashMap::new(); for dest in downstream_dests { if reached_node == dest { - if let Some(ptr) = first_hop_ptr { - if let Some((_, rts)) = first_hops_map.get_mut(&ptr) { - rts.push(current_route.clone()); - } - } + let Some(ptr) = first_hop_ptr else { continue }; + let Some((_, rts)) = first_hops_map.get_mut(&ptr) else { + continue; + }; + rts.push(current_route.clone()); } else if let Some(next_route) = route_borrowed.next_for_destination.get(&dest) { let ptr = Rc::as_ptr(next_route) as usize; if let Some((_, entry)) = next_routes.get_mut(&ptr) { @@ -203,7 +203,9 @@ fn update_multicast( if let Some(first_hop_contact) = first_hop_contact { let ptr = first_hop_contact.as_ptr() as usize; first_hop_ptr = Some(ptr); - first_hops_map.entry(ptr).or_insert_with(|| (first_hop_contact, Vec::new())); + first_hops_map + .entry(ptr) + .or_insert_with(|| (first_hop_contact, Vec::new())); } } accumulator.push((next_route, first_hop_ptr, time, next_downstream_dests)); @@ -313,7 +315,7 @@ pub fn dry_run_unicast_tree( let tree_ref = tree.borrow(); let dest_route = tree_ref.by_destination.get(dest as usize).cloned()??; let source_route = tree_ref.get_source_route(); - + RouteStage::init_route(dest_route); dry_run_unicast_path(bundle, at_time, source_route, with_exclusions) } diff --git a/src/routing/volcgr.rs b/src/routing/volcgr.rs index 1576483..34f8996 100644 --- a/src/routing/volcgr.rs +++ b/src/routing/volcgr.rs @@ -99,19 +99,16 @@ impl, NM: NodeManager, CM: ContactManager, P: Pathfindin .get_next(curr_time, source, bundle, excluded_nodes); let tree = Rc::new(RefCell::new(new_tree)); - if let Some(route) = Route::from_tree(tree, dest) { - RouteStage::init_route(route.destination_stage.clone()); - self.route_storage - .borrow_mut() - .store(bundle, route.clone()); - let dry_run = dry_run_unicast_path(bundle, curr_time, route.source_stage.clone(), true); - if dry_run.is_some() { - return Some(schedule_unicast_path( - bundle, - curr_time, - route.source_stage.clone(), - )); - } + let route = Route::from_tree(tree, dest)?; + RouteStage::init_route(route.destination_stage.clone()); + self.route_storage.borrow_mut().store(bundle, route.clone()); + let dry_run = dry_run_unicast_path(bundle, curr_time, route.source_stage.clone(), true); + if dry_run.is_some() { + return Some(schedule_unicast_path( + bundle, + curr_time, + route.source_stage.clone(), + )); } None }