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:
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.
networkTwoNode
{
submodules:
N1: Node { id = 1; }
N2: Node {
id = 2;
@display("p=120,110"); // position in visual editor only
}
connections:
N1.gIn <-- N2.gOut; // N2 can send to N1
N1.gOut --> N2.gIn; // N1 can send to N2
}
MSGN_PDU.msg
packetN_PDU
{
int pID; // packet number: 0, 1, 2 ... 9string pType; // "data" or "ack"int src; // who sent this (1 or 2)int dest; // who should receive this (1 or 2)
}
C++Node.h
#ifndef __FIRST_CN_LAB_NODE_H_#define __FIRST_CN_LAB_NODE_H_#include<omnetpp.h>#include"N_PDU_m.h"// auto-generated from N_PDU.msgusing namespaceomnetpp;
classNode : publiccSimpleModule
{
protected:int address; // this node's id: 1 or 2cGate *in; // pointer to gIn gatecGate *out; // pointer to gOut gateint counter; // how many exchanges completedint id_num; // next packet id to usevirtualvoidinitialize();
virtualvoidhandleMessage(cMessage *msg);
};
#endif
C++Node.cc
#include"Node.h"Define_Module(Node);
// Called once at the start of the simulationvoidNode::initialize()
{
address = par("id"); // read "id" parameter from NED
in = gate("gIn");
out = gate("gOut");
counter = 0;
id_num = 0;
if (address == 1)
{
// Only Node 1 starts things off.// Schedule a self-message at t=0 to trigger the first DATA send.cMessage *event = newcMessage();
scheduleAt(0, event);
}
}
// Called every time a message arrives (or a self-message fires)voidNode::handleMessage(cMessage *msg)
{
if (msg->isSelfMessage())
{
// Our own trigger fired. Delete it, then send the first DATA.delete msg;
N_PDU *p = newN_PDU();
p->setPID(id_num++); // assign id, then increment
p->setPType("data");
p->setSrc(1);
p->setDest(2);
EV << "Node 1: Sending DATA id=" << p->getPID() << "\n";
send(p, out);
}
else
{
// A real packet arrived from the other node.N_PDU *pkt = check_and_cast<N_PDU *>(msg);
if (address == 2)
{
// Node 2 received DATA → reply with ACK
EV << "Node 2: Received DATA id=" << pkt->getPID() << "\n";
delete pkt;
N_PDU *ack = newN_PDU();
ack->setPID(id_num++);
ack->setPType("ack");
ack->setSrc(2);
ack->setDest(1);
EV << "Node 2: Sending ACK id=" << ack->getPID() << "\n";
send(ack, out);
}
else if (address == 1)
{
// Node 1 received ACK → check if we're done, else send next DATA
EV << "Node 1: Received ACK id=" << pkt->getPID() << "\n";
delete pkt;
counter++; // one more exchange doneif (counter >= 10)
{
// All 10 exchanges complete — stop the simulation
EV << "Node 1: All 10 exchanges done. Stopping.\n";
endSimulation();
return;
}
// Send the next DATA packetN_PDU *p = newN_PDU();
p->setPID(id_num++);
p->setPType("data");
p->setSrc(1);
p->setDest(2);
EV << "Node 1: Sending DATA id=" << p->getPID() << "\n";
send(p, out);
}
}
}
Simulation of Two Node Network using the Network Simulator - OMNeT++
Reviewed by Shivam Maheshwari
on
March 01, 2019
Rating: 5
No comments: