Professional Documents
Culture Documents
OMNET++ Tutorial
Nhm NCKH Wireless Sensor Network
GVHD:
SVTH:
Nguyn Hong Sn
hson7246@gmail.com
Nguyn nh Quang
Quangnd.na@gmail.com
Phan L Hi
plh.hut4ever@gmail.com
L Minh Ngc
Lu Vn Huy
Omnetpp tutorial
CAC MO HINH TICTOC DON GIAN
txc1.cc:
#include <string.h>
#include <omnetpp.h>
Simple module Txc1 c i din bi class Txc1 trong C++, c phn lp t cSimpleModule, v ng k
trong OMNeT++ vi macro Define_Module().
Page 2
Omnetpp tutorial
Sau khi bn gi hoc schedule cc bn tin, chng s c t chc bi nhn m phng trong danh sch
"scheduled events" hay "future events" cho n thi im thi hnh bn tin v chng c giao cho cc module
thng qua handleMessage ().
To file cu hnh omnetpp.ini
Run: gp li cha lin kt vi file Txc1.cc Sa bng chut phi vo Project > Clean Project
Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011
Page 3
Omnetpp tutorial
Sources: tictoc1.ned, txc1.cc, omnetpp.ini
=================================================================================
Page 4
Omnetpp tutorial
Ta cng c th thay i file C++ thm cc debug messages vo Txc1 bng cch vit OMNeT++ object EV nh sau:
EV << "Sending initial message\n";
hay
EV << "Received message `" << msg->getName() << "', sending it out again\n";
#include <string.h>
#include <omnetpp.h>
class Txc2 : public cSimpleModule
{
protected:
virtual void initialize();
virtual void handleMessage(cMessage *msg);
};
Define_Module(Txc2);
void Txc2::initialize()
{
if (strcmp("tic", getName()) == 0)
{
// ev object hot ng nh hm 'cout' trong C++.
EV << "Sending initial message\n";
cMessage *msg = new cMessage("tictocMsg");
send(msg, "out");
}
}
void Txc2::handleMessage(cMessage *msg)
{
Page 5
Omnetpp tutorial
// msg->getName() l tn ca msg object, trng hp ny l "tictocMsg".
EV << "Received message '" << msg->getName() << "', sending it out again\n";
send(msg, "out");
}
=================================================================================
Page 6
Omnetpp tutorial
Step 3: Adding state variables
chuc nang huy ban tin
Mc ch: thm 1 b m (nh mt class member) vo module, xa bn tin sau 10 ln trao i.
Ta thm bin 10 vo hm initialize() v gim dn trong hm handleMessage() khi cc bn tin ti module. Sau khi gim ti 0,
simulation s kt thc events v hy n.
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
class Txc3 : public cSimpleModule
{
private:
int counter; // Note the counter here
protected:
virtual void initialize();
virtual void handleMessage(cMessage *msg);
};
Define_Module(Txc3);
void Txc3::initialize()
{
counter = 10;
WATCH(counter); // WATCH()cho php quan st gi tr ca counter trong Tkenv.
//(Double-click on tic's icon > Contents).
if (strcmp("tic", getName()) == 0)
{
EV << "Sending initial message\n";
cMessage *msg = new cMessage("tictocMsg");
send(msg, "out");
}
}
void Txc3::handleMessage(cMessage *msg)
{
// Increment counter and check value.
counter--;
if (counter==0)
{
// If counter is zero, delete message. If you run the model, you'll
// find that the simulation will stop at this point with the message
// "no more events".
EV << getName() << "'s counter reached zero, deleting message\n";
delete msg;
}
else
{
EV << getName() << "'s counter is " << counter << ", sending back message\n";
send(msg, "out");
Page 7
Omnetpp tutorial
}
}
=================================================================================
Page 8
Omnetpp tutorial
if (par("sendMsgOnInit").boolValue() == true)
Page 9
Omnetpp tutorial
Tictoc4.toc.limit = 5
S dng wildcards thay th:
Tictoc4.t*c.limit=5
or
Tictoc4.*.limit=5
or even
**.limit=5
Trong Tkenv, c th kim tra cc thng s module hoc trong object tree pha bn tay tri ca ca s chnh, hoc trong trang
Parameters ca module inspector (m bng cch nhn i vo biu tng module).
Sources: tictoc4.ned, txc4.cc, omnetpp.ini
=================================================================================
Page 10
Omnetpp tutorial
Step 5: Using inheritance
khai bao tic va toc theo 2 kieu mo rong cua simple module txc5
Nu ta quan st k cc file NED ta s nhn ra rng tic v toc v ch khc gi tr tham s v tn hin th ca chng . Chng ta c
th to ra mt loi module n gin bng cch tha k t mt module khc v ch r hoc thay th cc thng s ca n.
y l module c bn:
simple Txc5
{
parameters:
bool sendMsgOnInit = default(false);
int limit = default(2);
@display("i=block/routing");
gates:
input in;
output out;
}
V y l module tic c dn xut t Txc5. Chng ta ch n gin l xc nh gi tr tham s v thm mt s thuc tnh hin th.
simple Tic5 extends Txc5
{
parameters:
@display("i=,cyan");
sendMsgOnInit = true; // Tic modules should send a message on init
}
Module toc tng t nhng vi vi tham s khc.
simple Toc5 extends Txc5
{
parameters:
@display("i=,gold");
sendMsgOnInit = false; // Toc modules should NOT send a message on init
}
Note: Code C++ trong file Txc5 gi nguyn nh trong base simple module (Txc4).
Khi to ra simple modules mi, ta phi s dng chng nh kiu submodule trong network:
network Tictoc5
{
submodules:
tic: Tic5; // Tham s limit vn cha c lin kt y.
// Ta s gn gi tr cho n trong file ini
toc: Toc5;
connections:
Nh bn thy, phn nh ngha mng by gi ngn v n gin hn nhiu. Cch tha k cho php bn s dng mt kiu no ph
bin trong network v trnh s nh ngha cng nh thit lp thng s d tha
Page 11
Omnetpp tutorial
=================================================================================
Page 12
Omnetpp tutorial
Step 6: Modeling processing delay
khi nhan ban tin thi giu lai tuong duong voi viec gui ban tin cho chinh minh
Trong cc m hnh trc, tic v toc ngay lp tc gi tr li bn tin nhn c. y chng ta s thm vo thi gian tr: tic v toc s
gi bn tin trong 1s trc khi gi n tr li. Trong OMNeT + + thi gian tr nh vy t c bng cch cho cc module gi bn tin
n chnh n. Nhng bn tin ny c gi l self-messages (theo cch chng c s dng, cn khng chng vn l l i tng
bn tin thng thng).
Ta thm hai bin cMessage *, event v tictocMsg vo class, phn bit bn tin chng ta s dng lm tr thi gian v bn tin b tr
do c x l trong m phng.
class Txc6 : public cSimpleModule
{
private:
cMessage *event; // pointer to the event object which we'll use for timing
cMessage *tictocMsg; // variable to remember the message until we send it back
public:
Ta "gi" self-messages vi hm scheduleAt(),c quy nh c th khi n gi tr li module.
scheduleAt(simTime()+1.0, event);
Trong handleMessage() now we have to differentiate whether a new message has arrived via the input gate or the self-message
came back (timer expired). Here we are using
gi chng ta phi phn bit xem mt tin nhn mi n thng qua input gate hay l self-message tr li (gi ht hn). Ta ang
s dng
if (msg==event)
Nhng chng ta cng c th vit nh sau vn c
if (msg->isSelfMessage())
Ta b li li counter, gi cho kch thc ca m ngun nh. Kt qu cc m phng:
Page 13
Omnetpp tutorial
=================================================================================
Page 14
Omnetpp tutorial
delete msg;
}
Ta gn tham bin trong omnetpp.ini:
Tictoc7.tic.delayTime = exponential(3s)
Tictoc7.toc.delayTime = truncnormal(3s,1s)
Bn c th th m khng cn quan tm l chy li m phng bao nhiu ln (hoc khi ng li, Simulate|Rebuild network menu
item), bn s c c kt qu chnh xc tng t.
iu ny l do OMNeT++ s dng mt thut ton tt nh (theo mc nh l Mersenne Twister RNG) to ra cc s ngu nhin,
v khi to n vi cng khi u. iu ny quan trng cho vic m phng lp li. Bn c th th nghim vi cc iu kin u khc
nhau nu bn thm nhng dng sau vo omnetpp.ini:
[General]
seed-0-mt=532569 # or any other 32-bit value
T c php, ta c th on rng OMNeT++ h tr nhiu hn mt RNG. iu l chnh xc, tuy nhin, tt c cc m hnh trong
hng dn s dng RNG 0.
Exercise: Try other distributions as well.
Sources: tictoc8.ned, txc8.cc, omnetpp.ini
Page 15
Omnetpp tutorial
V vy, tic s khi ng b nh thi bt c khi no n gi ra bn tin. Khi b nh thi ht hn, ta s cho rng bn tin b mt v s
gi li bn tin khc. Nu nhn c reply ca toc, b nh thi s c hy b. Cc b nh thi s l mt self-message.
scheduleAt(simTime()+timeout, timeoutEvent);
Vic hy b b nh thi c thc hin bng cch gi hm cancelEvent(). Lu rng iu ny khng ngn cn chng ta ti s
dng bn tin c cng thi gian timeout lp i lp li.
cancelEvent(timeoutEvent);
You can read Tic's full source in txc8.cc.
Sources: tictoc8.ned, txc8.cc, omnetpp.ini
=================================================================================
Page 16
Omnetpp tutorial
cMessage *Tic9::generateNewMessage()
{
// Generate a message with a different name every time.
char msgname[20];
sprintf(msgname, "tic-%d", ++seq);
cMessage *msg = new cMessage(msgname);
return msg;
}
void Tic9::sendCopyOf(cMessage *msg)
{
// Duplicate message and send the copy.
cMessage *copy = (cMessage *) msg->dup();
send(copy, "out");
}
Sources: tictoc9.ned, txc9.cc, omnetpp.ini
=================================================================================
y ch l ti liu nhm NCKH t son tho trong qu trnh lm vic. Mong cc bn i sau s c nhiu ng gp ti
liu hon thin hn.
Cm n cc bn quan tm!
Page 17