processNeighborUpdate method

DistanceVectorRoutingTable<T>? processNeighborUpdate(
  1. DistanceVectorRoutingTable<T> currentTable,
  2. NeighborAdvertisement<T> advertisement,
  3. Map<T, Map<T, num>> network
)

Processes a single neighbor advertisement update

currentTable - Current routing table advertisement - Single neighbor advertisement network - Current network topology

Returns updated routing table if changes occurred

Implementation

DistanceVectorRoutingTable<T>? processNeighborUpdate(
  DistanceVectorRoutingTable<T> currentTable,
  NeighborAdvertisement<T> advertisement,
  Map<T, Map<T, num>> network,
) {
  final updatedAds = Map<T, NeighborAdvertisement<T>>.from(
    currentTable.neighborAdvertisements,
  );

  // Check if this is a new or updated advertisement
  if (!updatedAds.containsKey(advertisement.neighbor) ||
      advertisement.sequenceNumber >
          updatedAds[advertisement.neighbor]!.sequenceNumber) {
    updatedAds[advertisement.neighbor] = advertisement;

    // Recompute routes with updated neighbor information
    final recomputed = computeRoutes(
      network,
      currentTable.sourceNode,
      initialAdvertisements: updatedAds,
    );

    final mergedRoutes = Map<T, DistanceVectorRouteEntry<T>>.from(
      recomputed.routes,
    );
    for (final dest in advertisement.distanceVector.keys) {
      if (!mergedRoutes.containsKey(dest)) {
        mergedRoutes[dest] = DistanceVectorRouteEntry<T>(
          destination: dest,
          nextHop: advertisement.neighbor,
          cost: advertisement.distanceVector[dest]!,
          lastUpdate: DateTime.now(),
          isDirectlyConnected: false,
          advertisingNeighbor: advertisement.neighbor,
          hopCount: 1,
          attributes: {'timeout': routeTimeout},
        );
      }
    }

    return DistanceVectorRoutingTable<T>(
      sourceNode: recomputed.sourceNode,
      routes: Map.unmodifiable(mergedRoutes),
      neighborAdvertisements: Map.unmodifiable(updatedAds),
      lastUpdate: recomputed.lastUpdate,
      totalRoutes: mergedRoutes.length,
      totalNeighbors: updatedAds.length,
    );
  }

  return null;
}