Skip to content

AnaPaula04/network-routing-simulator

Repository files navigation

Dynamic Network Routing Simulator

A Java-based implementation of a Distance Vector routing protocol that simulates how routers dynamically build and maintain routing tables in a network. The simulator demonstrates fundamental concepts used in real-world routing protocols like RIP (Routing Information Protocol).

🚀 Features

  • Dynamic Routing Table Construction - Routers automatically discover optimal paths to all network destinations
  • Distributed Algorithm - Each router independently computes routes based on information from neighbors
  • Network Topology Changes - Simulates link cost changes and demonstrates automatic route recalculation
  • Multi-hop Path Selection - Correctly identifies shortest paths across multiple network hops
  • Convergence Simulation - Shows how routing information propagates through the network over multiple rounds

🛠️ Technical Implementation

Core Components

Router Class

The heart of the simulator, implementing:

  • Routing Table Management - Maintains destination-to-next-hop mappings with associated costs
  • Builder Function - Constructs and broadcasts routing information packets to neighbors
  • Listener Function - Processes incoming packets and updates routing tables using the Bellman-Ford algorithm
  • Change_Cost Function - Simulates network topology changes by modifying link costs
  • Display_Table Function - Outputs formatted routing tables for analysis

Initializer

Parses network configuration files to set up the simulation:

  • Reads network topology from text files
  • Creates router objects with specified connections
  • Initializes neighbor relationships and link costs

Main Engine

Orchestrates the simulation:

  • Loads network configuration
  • Runs multiple rounds of routing information exchange
  • Demonstrates convergence of routing tables
  • Tests dynamic adaptation to topology changes

📊 Test Scenarios

Scenario 1: Five-Router Network

Topology: Medium-complexity network with multiple path options

Network Configuration:
- 5 routers (R1, R2, R3, R4, R5)
- R1 ↔ R2 (140ms), R1 ↔ R4 (180ms)
- R2 ↔ R3 (300ms), R2 ↔ R5 (450ms)
- R3 ↔ R4 (200ms)
- R4 ↔ R5 (350ms)

Key Observations:

  • R1 routes to R5 via R4 (total: 530ms) rather than through R2 (590ms)
  • After increasing R1-R4 cost to 400ms, routing tables update correctly
  • Demonstrates intelligent path selection based on total cost

Initial Routing (R1):

Destination | Next Hop | Cost
------------|----------|------
R2          | R2       | 140
R3          | R4       | 380
R4          | R4       | 180
R5          | R4       | 530

After Cost Change (R1-R4: 180ms → 400ms):

Destination | Next Hop | Cost
------------|----------|------
R2          | R2       | 140
R3          | R2       | 440
R4          | R4       | 400
R5          | R2       | 590

Scenario 2: Four-Router Square Network

Topology: Simplified square configuration for baseline testing

Network Configuration:
- 4 routers arranged in a square pattern
- R1 ↔ R2 (100ms), R1 ↔ R3 (200ms)
- R2 ↔ R4 (150ms)
- R3 ↔ R4 (100ms)

Key Observations:

  • R1 reaches R4 through R2 (250ms) instead of through R3 (300ms)
  • After increasing R1-R3 to 400ms, R1 reroutes to R3 via R2-R4-R3 (350ms)
  • Shows dynamic adaptation to network changes

Initial Routing (R1):

Destination | Next Hop | Cost
------------|----------|------
R2          | R2       | 100
R3          | R3       | 200
R4          | R2       | 250

After Cost Change (R1-R3: 200ms → 400ms):

Destination | Next Hop | Cost
------------|----------|------
R2          | R2       | 100
R3          | R2       | 350
R4          | R2       | 250

Scenario 3: Six-Router Complex Network

Topology: Advanced mesh configuration with multiple routing options

Network Configuration:
- 6 routers in a complex mesh topology
- R1 ↔ R2 (50ms), R1 ↔ R3 (100ms)
- R2 ↔ R4 (75ms)
- R3 ↔ R5 (80ms)
- R4 ↔ R6 (90ms)
- R5 ↔ R6 (60ms)

Key Observations:

  • R1 reaches R6 through R2→R4 path (215ms) - optimal among multiple options
  • R5 routes to R2 through R6→R4 path (225ms)
  • Multi-hop routing demonstrates shortest path algorithm working correctly across complex topologies
  • After cost change, all six routers maintain consistent routing tables

Initial Routing (R1):

Destination | Next Hop | Cost
------------|----------|------
R2          | R2       | 50
R3          | R3       | 100
R4          | R2       | 125
R5          | R3       | 180
R6          | R2       | 215

After Cost Change (R1-R3: 100ms → 400ms):

Destination | Next Hop | Cost
------------|----------|------
R2          | R2       | 50
R3          | R2       | 200
R4          | R2       | 125
R5          | R3       | 180
R6          | R2       | 215

🎯 Algorithm Details

The simulator implements a Distance Vector routing algorithm:

  1. Initialization:

    • Each router knows only its directly connected neighbors and associated costs
    • All other destinations initialized with infinite cost (-1)
  2. Information Exchange:

    • Each router broadcasts its routing table to all neighbors
    • Routers update their tables based on received information
  3. Route Calculation:

    • For each destination, router calculates: cost_to_neighbor + neighbor_cost_to_destination
    • Selects minimum cost path and updates next-hop accordingly
  4. Convergence:

    • Process repeats for multiple rounds until routing tables stabilize
    • Typically converges in 5 rounds for small to medium networks
  5. Dynamic Updates:

    • When link costs change, affected router updates its table
    • Change propagates through network via the same exchange mechanism

🚀 How to Run

Prerequisites

  • Java 21 or higher
  • Any Java IDE (VS Code, IntelliJ IDEA, Eclipse) or command line

Compilation

javac *.java

Execution

java MainEngine

Configuration Files

The simulator reads network topology from text files in the following format:

<number_of_routers>
<router_name>: (<neighbor_name>, <cost>), (<neighbor_name>, <cost>), ...
<router_name>: (<neighbor_name>, <cost>), (<neighbor_name>, <cost>), ...
...

Example (config.txt):

5
R1: (R2, 140), (R4, 180)
R2: (R1, 140), (R3, 300), (R5, 450)
R3: (R2, 300), (R4, 200)
R4: (R1, 180), (R3, 200), (R5, 350)
R5: (R2, 450), (R4, 350)

Testing Different Topologies

Modify MainEngine.java to load different configuration files:

String configFile = "config.txt";  // Change to config2.txt or config3.txt

📁 Project Structure

Routing_Simulator/
├── Router.java          # Router implementation with routing logic
├── Initializer.java     # Configuration file parser
├── MainEngine.java      # Main simulation orchestrator
├── config.txt           # Test scenario 1 (5 routers)
├── config2.txt          # Test scenario 2 (4 routers)
├── config3.txt          # Test scenario 3 (6 routers)
└── README.md            # This file

💡 Key Concepts Demonstrated

  • Distributed Systems - No central authority; each router acts independently
  • Graph Algorithms - Shortest path computation using Bellman-Ford principles
  • Network Protocols - Packet-based information exchange between routers
  • Dynamic Adaptation - Automatic reconfiguration when network topology changes
  • Object-Oriented Design - Clean separation of concerns across classes

🔍 Technical Highlights

Router Class Design

  • Uses HashMap for O(1) neighbor and routing table lookups
  • Inner RouteInfo class encapsulates next-hop and cost data
  • Packet format: "RouterName:Dest1,Cost1;Dest2,Cost2;..."

Routing Algorithm

  • Implements distance vector with split horizon
  • Converges after diameter of network + 1 rounds
  • Handles cost changes through re-broadcast mechanism

Configuration Parser

  • Robust parsing with error handling
  • Supports flexible network topologies
  • Automatically initializes bidirectional links

🎓 Learning Outcomes

This project demonstrates understanding of:

  • Network layer routing protocols
  • Distributed algorithms and convergence
  • Java collections and data structures
  • File I/O and string parsing
  • Software design patterns

📈 Future Enhancements

Potential improvements to explore:

  • Implement link-state routing (OSPF-style) for comparison
  • Add GUI visualization of network topology and routing updates
  • Simulate packet routing through the network using computed tables
  • Implement split horizon and poison reverse optimizations
  • Add support for router failures and network partitioning
  • Create performance metrics (convergence time, message overhead)
  • Support for weighted routing policies (not just shortest path)

📚 References

Based on concepts from:

  • Distance Vector Routing (RFC 1058 - RIP)
  • Bellman-Ford shortest path algorithm
  • Computer networking fundamentals

📝 License

This project is available for educational and portfolio purposes.


Built with Java ☕ | December 2025

About

Java implementation of Distance Vector routing protocol

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages