Omnet++ Lessons Part One

Omnet++ is one simulator which I liked a lot. It gave me enough confidence that I can build a network from scratch (like Steven's book building a complete TCP/IP stack). These files were written while I was at IIT Madras and reproduced here for learning purpose.
In this post, I start with a packet generator which simply generate a packet per given period of time.
This will introduce minimum number of files needed to write an Omnet application (cc c++ source file, the network descriptor : ned and the launcher : omnetpp.ini)



Tool: Omnetpp 4.xx (which is built over eclipse)

Not too much speech, let's get our hands dirty.














Periodic packet generator
input : number of packets to generate

Steps: Define a cpp file generator.cc




#include <omnetpp.h>
class generator : public cSimpleModule
{
private:
int nbPackets, packetCount;
cMessage*generateMsg;
protected:
virtual void initialize()
{
nbPackets=par("totalPackets");
packetCount=0;
generateMsg=new cMessage("selfMsg");
scheduleAt(simTime()+1,generateMsg);
}
virtual void handleMessage(cMessage *msg)
{
EV<<"generated the msg"<packetCount++;
if(packetCount<nbpackets)
scheduleAt(simTime()+1,generateMsg);
}
virtual void finish()
{
if (generateMsg!=NULL)
delete generateMsg;
}
};
Define_Module(generator);

The last line instructs the omnet tool to use this source as "generator"

Now let's build the network

generator.ned

package1;
simple generator
{
parameters:
int totalPackets = default(100);
@display("i=msg/job");
}
network one
{
submodules:
gen:generator;
}

Now let's write the launcher file: omnetpp.ini
network = one
**.gen.totalPackets = 10

Build and run
Just right click "one" project and from build menu, build it.

Right click on omnetpp.ini and run the same.

Output is shown below.

3 comments:

Anonymous said...

Hi, i'm trying to do your example.
I noticed that in generator.cc there is an error:
in the line with

if(packetCount<nbpackets)

the p of packets has to be P

Carlo said...

do you know why running the .ini file i have this error


Error in module (cCompoundModule) one (id=1) during network setup: Class "generator" not found -- perhaps its code was not linked in, or the class wasn't registered with Register_Class(), or in the case of modules and channels, with Define_Module()/Define_Channel().


?

Anonymous said...

probably you forgot tou put this line of code (Define_Module(generator);) in the end of your generator.cc file.