CSE247 on YouTube

Simulation of Two Node Network using the Network Simulator - OMNeT++

Computer Networks Lab · OMNeT++
Lab 1 — Two Node Network Simulation
Simulating a stop-and-wait data exchange between two nodes using OMNeT++
Task 1 — Setup
What is OMNeT++? It is a network simulator. You write code that describes nodes and connections, and it simulates how messages travel between them — no real hardware needed.
1
Open a terminal and type omnetpp to launch the IDE.
2
Choose a workspace folder. Create a folder named workspace inside your home directory and select it.
3
When it offers to install INET framework or samples, click No. We don't need them for this lab.
4
Go to File → New → OMNeT++ Project and name it lab1. All your files will go here.
Task 2 — Network Simulation
1. Network Design
Before writing any C++ logic, we first describe the structure of our network using OMNeT++'s special language called NED. Think of it as a blueprint — how many nodes exist and how they are connected.

a) Create a computing module called node. It has one input gate and one output gate — like a machine with an IN slot and an OUT slot.

b) Create a network called twoNode with two instances of the node connected to each other:

Node 1
⟵⟶
Node 2

Bidirectional — messages can travel both ways

c) Define a packet type called N_PDU with four fields:

pID — message number pType — "data" or "ack" src — sender address dest — receiver address
2. Behavioural Design — Data Transfer
Now we define what the nodes actually do. This is the C++ logic. The pattern is simple:
Node 1 sends Data → Node 2 replies with Ack → Node 1 sends next Data → repeat 10 times.

a) One full exchange looks like this:

Time Node 1 Channel Node 2
t1 sends DATA → (puts packet on wire) ─►
t2 ─► receives DATA (packet arrives)
t3 ◄─ ← sends ACK (confirms delivery)
t4 receives ACK (now sends next) ◄─

b) This repeats 10 times (packet id 0 to 9). Each ACK carries the same id as the DATA it confirms. Node 1 never sends the next DATA until the ACK arrives. This pattern is called stop-and-wait.


Solution — Source Code
Step 1 — node.ned

Define the shape of the Node module — its gates and parameters. No logic yet, just the blueprint.

simple = a basic module (not a container).
gates = physical ports. gIn receives, gOut sends.
id = parameter to tell each node whether it is Node 1 or 2.
Step 2 — twoNode.ned

Define the network — two Node instances wired together bidirectionally.

submodules = the nodes that exist in this network.
connections = the wires. N1.gOut --> N2.gIn means output of N1 goes into input of N2.
@display sets visual position in the GUI — no effect on logic.
Step 3 — N_PDU.msg

Define the packet format — every message exchanged will look like this.

This is like designing a form with 4 fields. OMNeT++ will auto-generate a C++ class from this file (called N_PDU_m.h) with getter and setter methods like setPID(), getPID(), etc.
Step 4 — Node.h

The header file — declares what variables and functions our Node class will have.

address — stores whether this instance is Node 1 or 2.
counter — counts how many full DATA+ACK exchanges have completed.
id_num — the next packet ID to assign.
initialize() — called once when simulation starts.
handleMessage() — called every time a message arrives.
Step 5 — Node.cc

The main logic — Node 1 starts the exchange, Node 2 replies with ACK, and after 10 rounds the simulation ends.

initialize(): Node 1 schedules a self-message at t=0 to trigger the first DATA send. Node 2 does nothing at start.

handleMessage(): Three cases:
  1. Self-message → Node 1 creates and sends a DATA packet.
  2. address == 2 → Node 2 got DATA, creates and sends ACK.
  3. address == 1 → Node 1 got ACK. If 10 done → stop. Else → send next DATA.
Source Files
NED node.ned
simple Node
{
    parameters:
        int id;          // 1 = Node1,  2 = Node2
    gates:
        input  gIn;      // incoming messages arrive here
        output gOut;     // outgoing messages leave from here
}

No comments:

Powered by Blogger.