Skip to main content

leodos_protocols/network/isl/routing/algorithm/
manhattan.rs

1//! Manhattan (Pure Topology)
2
3use crate::network::isl::address::Address;
4use crate::network::isl::routing::algorithm::RoutingAlgorithm;
5use crate::network::isl::routing::algorithm::gateway::GatewayTable;
6use crate::network::isl::shell::Shell;
7use crate::network::isl::torus::{Direction, Hop};
8use crate::network::isl::torus::Point;
9
10/// Shortest-hop routing using Manhattan distance on the toroidal grid.
11pub struct Manhattan<const N: usize> {
12    shell: Shell,
13    gateway_table: GatewayTable<N>,
14}
15
16impl<const N: usize> Manhattan<N> {
17    /// Creates a new Manhattan router.
18    pub fn new(shell: Shell, gateway_table: GatewayTable<N>) -> Self {
19        Self { shell, gateway_table }
20    }
21
22    fn route_to_point(
23        &self,
24        current: Point,
25        target: Point,
26    ) -> Direction {
27        let torus = &self.shell.torus;
28        if current.orb != target.orb {
29            return torus.direction_to_orb(
30                current, target,
31            );
32        }
33        torus.direction_to_sat(current, target)
34    }
35}
36
37impl<const N: usize> RoutingAlgorithm for Manhattan<N> {
38    fn route(
39        &self,
40        current: Point,
41        target: Address,
42        time_s: u32,
43    ) -> Hop {
44        let (target_point, local_hop) = match target {
45            Address::Satellite(p) => (p, Hop::Local),
46            Address::Ground { station } => {
47                let gw = self
48                    .gateway_table
49                    .gateway(&self.shell, station, time_s)
50                    .unwrap_or(Point {
51                        orb: 0,
52                        sat: station,
53                    });
54                (gw, Hop::Ground)
55            }
56            Address::ServiceArea { orb } => (
57                Point { orb: current.orb, sat: orb },
58                Hop::Local,
59            ),
60        };
61        if current == target_point {
62            return local_hop;
63        }
64        Hop::Isl(self.route_to_point(current, target_point))
65    }
66}