You are on page 1of 17

Hc vin Cng ngh Bu chnh Vin thng

B mn Mng Vin Thng

OMNET++ Tutorial
Nhm NCKH Wireless Sensor Network

GVHD:

Nguyn Th Thu Hng

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

Step 1: Getting started


Txc1 Getting started
File Topo: *.ned
tic v toc: thuc cng mt kiu module gi l Txc1. Txc1 l mt loi module n gin (n l thnh phn mc nguyn t
trong NED, v c coding bng C++).

txc1.cc:
#include <string.h>
#include <omnetpp.h>

class Txc1 : public cSimpleModule


{
protected:
// The following redefined virtual function holds the algorithm.
virtual void initialize();
virtual void handleMessage(cMessage *msg);
};
// The module class needs to be registered with OMNeT++
Define_Module(Txc1);
void Txc1::initialize()
{
// Initialize is called at the beginning of the simulation.
// Khi to: bt u qu trnh tic-toc-tic-toc,
// mt trong cc m-un cn gi message u tin. GS l 'tic'.
// Ti l tic hay toc?
if (strcmp("tic", getName()) == 0)
{
// To v gi message u tin trn cng "out".
// "tictocMsg" l mt chui ty v s l tn ca i tng message.
cMessage *msg = new cMessage("tictocMsg");
send(msg, "out");
}
}
void Txc1::handleMessage(cMessage *msg)
{
// Quy trnh handleMessage() c gi ra bt c khi no mt message n module.
// y, chng ti ch cn gi ra n cc module khc, thng qua cng out.
// V c tic v toc u khng gi ra ng thi,
// nn message s gi qua gi li gia hai thc th.
send(msg, "out");
}

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().

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

Page 2

Omnetpp tutorial

Ta nh ngha li 2 methods t cSimpleModule:


initialize() gi ra khi khi to ra bn tin
handleMessage() c gi khi bn tin n module.
initialize() to mt i tng bn tin (cMessage), v gi n i ra cng out. Cng ny c kt ni vi cng
input ca cc module khc, sau 100ms delay do truyn bn tin (tham s ca link trong file NED), nhn m
phng s chuyn bn tin ny n cc module khc bng i s ti hm handleMessage (). Cc module khc
ch cn gi n tr li (delay 100ms na), ping-pong!
Messages (packets, frames, jobs, etc) v cc events (timers, timeouts) tt c c i din bi cc i tng
cMessage (hoc cc lp con ca n) trong OMNeT + +.

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

=================================================================================

Step 2: Refining the graphics, and adding debugging output


Tang cuong mo hinh Tic-Toc 2 node
// "block/routing" icon to the simple module. All submodules of type
// Txc2 will use this icon by default
simple Txc2
{
parameters:
@display("i=block/routing"); // chn kiu icon mc nh
gates:
input in;
output out;
}
// Make the two module look a bit different with colorization effect.
// Use cyan for `tic', and yellow for `toc'.
//
network Tictoc2
{
submodules:
tic: Txc2 {
parameters:
@display("i=,cyan"); // khng thay i hnh dng icon (i s u tin
// ca i=) m ch dnh mu cho n
}
toc: Txc2 {
parameters:
@display("i=,gold"); // here too
}
connections:

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

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)
{

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

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");
}

Sources: tictoc2.ned, txc2.cc, omnetpp.ini

=================================================================================

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

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");

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

Page 7

Omnetpp tutorial
}
}

Sources: tictoc3.ned, txc3.cc, omnetpp.ini

=================================================================================

Step 4: Adding parameters


dem bien ra ngoai de config dc o omnetpp.ini, ko can strcmp
Mc ch: thm tham s vo trnh m phng:
+ Chuyn "magic number" 10 vo tham s no
+ Thm 1 tham s kiu boolean quyt nh xem khi no cc module nn gi message u tin trong m khi to ca n
(d y l module tic hay toc).
Tham bin ca module phi c khai bo trong file NED. Kiu dl c th l numeric, string, bool, hay xml.
simple Txc4
{
parameters:
bool sendMsgOnInit = default(false); // whether the module should send out a
// message on initialization
int limit = default(2); // another parameter with a default value
@display("i=block/routing");
gates:

Ta thay i code C++ trong Txc1.ned n c tham s trong initialize() v gn n vo counter.


counter = par("limit");
Ta dng tham s cn li quyt nh xem khi no cc module no c php gi message u tin:

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

Page 8

Omnetpp tutorial
if (par("sendMsgOnInit").boolValue() == true)

M t ca Txc4 trong Txc4.cc nh sau


Define_Module(Txc4);
void Txc4::initialize()
{
// counter c gn tham s module "limit",
// c khai bo trong file NED (tictoc4.ned).
counter = par("limit");
// chng ta khng cn da vo tn ca module quyt nh
// khi no cho php n gi bn u tin ra na
if (par("sendMsgOnInit").boolValue() == true)
{
EV << "Sending initial message\n";
cMessage *msg = new cMessage("tictocMsg");
send(msg, "out");
}
}
By gi ta c 2 cch s dng cc tham s, l gn chng vo trong file NED hoc t omnetpp.ini. u tin cch gn trc tip
vo file NED. C th t gi tr mc nh cho cc tham s bng cch dng c php default(...)trong file NED. V y l cch gn cc
tham s trong file NED:
network Tictoc4
{
submodules:
tic: Txc4 {
parameters:
sendMsgOnInit = true;
@display("i=,cyan");
}
toc: Txc4 {
parameters:
sendMsgOnInit = false;
@display("i=,gold");
}
connections:
v trong omnetpp.ini:

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

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

=================================================================================

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

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

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

Page 11

Omnetpp tutorial
=================================================================================

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

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:

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

Page 13

Omnetpp tutorial

Sources: tictoc6.ned, txc6.cc, omnetpp.ini

=================================================================================

Step 7: Random numbers and parameters


delay la bien cua ham xac suat, hien tuong mat goi
Trong bc ny, ta s dng n cc s ngu nhin. Ta thay i delay t 1 giy thnh mt gi tr ngu nhin no m c th c
thit lp t file NED hoc t omnetpp.ini. Thng s module c th tr v cc bin ngu nhin, tuy nhin, s dng tnh nng ny
chng ta phi c cc tham s trong handleMessage()mi khi chng ta s dng n.
// Tham s module "delayTime" c th c thit lp gi tr nh
// "exponential(5)" (tictoc7.ned, omnetpp.ini), v do y
// chng ta s nhn c gi tr delay khc nhau mi ln.
simtime_t delay = par("delayTime");
EV << "Message arrived, starting to wait " << delay << " secs...\n";
tictocMsg = msg;
scheduleAt(simTime()+delay, event);
Ngoi ra, ta s "lose" (delete) cc gi tin vi mt xc sut (hardcoded) nh.
if (uniform(0,1) < 0.1)
{
EV << "\"Losing\" message\n";

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

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

Step 8: Timeout, cancelling timers


stop-and-wait. Doi Timeout het han, sau do truyen lai va restarting timer
c c mt bc gn hn vi cc mu giao thc mng, ta hy chuyn i m hnh ca mnh vo m phng stop-and-wait. By
gi ta s c cc class ring cho tic v toc. Kch bn v c bn l ging nh nhng phn trc: tic v toc s truyn bn tin qua li vi
nhau. Tuy nhin, toc s " lose" bn tin vi mt xc sut khc khng, v trong trng hp ny tic s phi gi li n.
Di y l code ca toc:
void Toc8::handleMessage(cMessage *msg)
{
if (uniform(0,1) < 0.1)
{
EV << "\"Losing\" message.\n";
bubble("message lost"); // making animation more informative...
delete msg;
}
else
Nh c hm bubble() gi trong m, toc s hin th thng bo bt c khi no n mt bn tin.

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

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

=================================================================================

Step 9: Retransmitting the same message


cac ban tin co ten khac nhau, khi mat goi se truyen lai ban sao cua ban tin
Trong bc ny, ta thay i cc m hnh trc. Hin ta ch to ra gi tin khc khi cn phi truyn li. iu ny l chp nhn c v
cc gi tin khng cha nhiu thng tin, nhng trong thc t, thng l thit thc hn khi gi mt bn sao ca gi ban u c th
truyn li m khng cn phi to li n mt ln na.
Nhng g chng ta lm y l gi li gi d liu gc v ch gi i bn sao ca n. Chng ta xa gi tin gc khi nhn c bn tin
acknowledgement t toc. d dng hn khi kim tra trc quan m hnh, ta s a sequence number vo tn bn tin.
trnh handleMessage() pht trin qu ln, chng ta s a code tng ng vo hai chc nng mi, generateNewMessage() v
sendCopyOf() v gi chng t handleMessage().
The functions:

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

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!

Nhm NCKH- B mn mng Hc vin CN Bu Chnh Vin Thng PTIT 2011

Page 17

You might also like