Files
clan-master-thesis/Listings/mycelium_find_best_route.rs
2026-04-14 11:36:11 +02:00

30 lines
1.0 KiB
Rust

fn find_best_route<'a>(&self, routes: &'a RouteList)
-> Option<&'a RouteEntry>
{
let source_table = self.source_table.read().unwrap();
let current = routes.selected();
let best = routes
.iter()
.filter(|re| !re.metric().is_infinite()
&& source_table.route_feasible(re))
.min_by_key(|re|
re.metric() + Metric::from(re.neighbour().link_cost()));
if let (Some(best), Some(current)) = (best, current) {
// Only switch if the metric is significantly better
// OR if the route is directly connected (metric 0).
if (best.source() != current.source()
|| best.neighbour() != current.neighbour())
&& !(best.metric()
+ Metric::from(best.neighbour().link_cost())
< current.metric()
+ Metric::from(current.neighbour().link_cost())
- SIGNIFICANT_METRIC_IMPROVEMENT
|| best.metric().is_direct())
{
return Some(current); // keep existing route
}
}
best
}