# nivx18818/routing architecture

> Architecture diagram of the GitHub repository nivx18818/routing, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nivx18818/routing
- Repository: https://github.com/nivx18818/routing

## Overview

##### Project type and purpose
- This is a Python-based network simulator for intra-domain routing algorithms.
- It lets you plug in two routing-algorithm implementations (distance-vector in DVrouter.py or link-state in LSrouter.py) and then simulates a small autonomous system to test correctness.
- Key use case: students implement routing logic and then run the provided simulator (network.py or visualize_network.py) on JSON topologies, observe packet forwarding and convergence.

##### File structure
- Top-level files:
- DVrouter.py, LSrouter.py    ← your algorithm implementations
- router.py                   ← abstract base class for routers
- client.py, link.py, packet.py, network.py, visualize_network.py
- JSON files (01_small_net.json, … _events.json) hold topology + event specs
- test_scripts/test_dv_ls.sh  ← batch runner for all scenarios
- apps/ directory (possibly helper scripts or sample apps)
- Patterns:
- router.py defines the Router interface (hook methods: handle_packet, handle_time, etc.)
- DVrouter & LSrouter subclass Router and override its methods
- network.py / visualize_network.py instantiate a Simulator, load a JSON, spawn Router threads + Clients/Links, schedule events

##### README for additional insights
- The simulator is event-driven: routers receive calls for packet arrival, link up/down, periodic time ticks.
- Packets are Python objects (Packet.ROUTING for control, traceroute-style for data) passed via send(port, packet).
- JSONs define nodes, links, link costs, and scheduled link-failure/add events.
- visualize_network.py adds a GUI showing clients (blue), routers (red), packet flows, link costs.

- Main components:
A. Simulator Core
- JSON Config Loader
- Event Scheduler (link events, heartbeats)
- Topology Manager (creates Link/Client/Router objects)
B. Router Abstraction
- Base class (router.py) exposing hooks
- Subclasses: DVrouter, LSrouter
C. Packet Engine
- Packet class (packet.py)
- send/receive API hiding link latencies
D. Network Entities
- Clients (client.py) – generate traceroute traffic
- Links (link.py) – model cost/latency, reliability
E. UI & CLI
- visualize_network.py – GUI front end
- network.py – headless run + final-routes output
F. Test Harness
- test_scripts/test_dv_ls.sh – automated batch runs
- Relationships & data flows:
1. User launches network.py or visualize_network.py, passes JSON + “DV” or “LS”
2. Simulator Core loads JSON → instantiates Link, Client, Router base-class instances
3. Depending on argument, Router instances are actual DVrouter or LSrouter objects
4. Clients periodically send data packets into the topology (via Router send API)
5. Routers exchange control packets (distance-vector or link-state messages) via Packet Engine
6. Event Scheduler invokes handle_time, handle_new_link, handle_remove_link, handle_packet on each Router
7. At simulation end, network.py collects traceroute results and prints correctness
8. visualize_network.py additionally subscribes to packet events to animate flows
- Architectural patterns & principles:
- Plugin / Strategy Pattern: routing algorithm is pluggable via subclassing Router
- Event-driven simulation: discrete events trigger router callbacks
- Separation of concerns: topology & event scheduling (network.py) vs. routing logic (DVrouter/LSrouter) vs. packet format (packet.py)
- Dependency injection via JSON configs – topology is data, not hard-coded

##### Essential details to capture
- The hook methods in Router (handle_packet, handle_time, handle_new_link/remove_link) are critical extension points
- The periodic heartbeat mechanism: Simulator Core triggers handle_time every heartbeat_time
- The bidirectional nature of Links (latency, cost) on which Packet objects travel
- The final route-validation step in network.py

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Infrastructure"
        SC["Simulator Core"]:::infra
        PE["Packet Engine"]:::infra
    end
    subgraph "Routing Algorithm Extensions"
        RB["Router Base Class"]:::algo
        DV["Distance-Vector Router"]:::algo
        LS["Link-State Router"]:::algo
    end
    subgraph "Entities"
        CL["Client"]:::entity
        LK["Link"]:::entity
    end
    subgraph "Interfaces"
        CLI["Headless CLI"]:::infra
        GUI["GUI Frontend"]:::infra
    end
    subgraph "Configurations & Scripts"
        JSON["JSON Config Files"]:::doc
        TH["Test Harness"]:::doc
        AP["Sample Apps Directory"]:::doc
        RD["Project README"]:::doc
    end

    JSON --> SC
    SC --> CL
    SC --> LK
    SC --> RB
    RB --> DV
    RB --> LS
    CL --> PE
    RB -.-> PE
    PE -->|"sends packets over"| LK
    LK -->|"delivers packets"| RB
    SC -->|"schedules events"| RB
    CLI --> SC
    GUI --> SC
    GUI --> PE
    CLI --> TH

    click SC "https://github.com/nivx18818/routing/blob/main/network.py"
    click GUI "https://github.com/nivx18818/routing/blob/main/visualize_network.py"
    click RB "https://github.com/nivx18818/routing/blob/main/router.py"
    click DV "https://github.com/nivx18818/routing/blob/main/DVrouter.py"
    click LS "https://github.com/nivx18818/routing/blob/main/LSrouter.py"
    click PE "https://github.com/nivx18818/routing/blob/main/packet.py"
    click CL "https://github.com/nivx18818/routing/blob/main/client.py"
    click LK "https://github.com/nivx18818/routing/blob/main/link.py"
    click JSON "https://github.com/nivx18818/routing/blob/main/01_small_net.json"
    click JSON "https://github.com/nivx18818/routing/blob/main/02_small_net_events.json"
    click JSON "https://github.com/nivx18818/routing/blob/main/03_pg244_net.json"
    click JSON "https://github.com/nivx18818/routing/blob/main/04_pg244_net_events.json"
    click JSON "https://github.com/nivx18818/routing/blob/main/05_pg242_net.json"
    click JSON "https://github.com/nivx18818/routing/blob/main/06_pg242_net_events.json"
    click TH "https://github.com/nivx18818/routing/blob/main/test_scripts/test_dv_ls.sh"
    click AP "https://github.com/nivx18818/routing/tree/main/apps/"
    click RD "https://github.com/nivx18818/routing/blob/main/README.md"

    classDef infra fill:#D0E7FF,stroke:#333,stroke-width:1px
    classDef algo fill:#FFD0D0,stroke:#333,stroke-width:1px
    classDef entity fill:#D0FFD0,stroke:#333,stroke-width:1px
    classDef doc fill:#FFF5B1,stroke:#333,stroke-width:1px
```
