cleanupStaleRoutes method

DistanceVectorRoutingTable<T> cleanupStaleRoutes(
  1. DistanceVectorRoutingTable<T> routingTable,
  2. DateTime currentTime
)

Removes stale routes and performs garbage collection

routingTable - Current routing table currentTime - Current time for comparison

Returns cleaned routing table with stale routes removed

Implementation

DistanceVectorRoutingTable<T> cleanupStaleRoutes(
  DistanceVectorRoutingTable<T> routingTable,
  DateTime currentTime,
) {
  final validRoutes = <T, DistanceVectorRouteEntry<T>>{};
  final validNeighborAds = <T, NeighborAdvertisement<T>>{};

  // Keep valid routes
  for (final entry in routingTable.routes.entries) {
    final dest = entry.key;
    final route = entry.value;

    if (route.isValid) {
      validRoutes[dest] = route;
    }
  }

  // Keep recent neighbor advertisements
  for (final entry in routingTable.neighborAdvertisements.entries) {
    final neighbor = entry.key;
    final ad = entry.value;

    if (ad.isRecent) {
      validNeighborAds[neighbor] = ad;
    }
  }

  return DistanceVectorRoutingTable<T>(
    sourceNode: routingTable.sourceNode,
    routes: Map.unmodifiable(validRoutes),
    neighborAdvertisements: Map.unmodifiable(validNeighborAds),
    lastUpdate: currentTime,
    totalRoutes: validRoutes.length,
    totalNeighbors: validNeighborAds.length,
  );
}