Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

NS-3 Tutorial

Gustavo J. A. M. Carneiro
INESC Porto / Faculdade de Engenharia / Universidade do Porto
Rede Temtica de Comunicaes Mveis 2009-02- 3

RTCM

2009-02-13

!utline

"ntroduction NS-3 module stac# $ sim%le e&am%le 'o(er level


"nterestin, NS-3 .eatures NS-3 -er.ormance Conclusion


RTCM 2009-02-13 2

Sc)eduler Random varia*les Memor+ mana,ement -ac#ets Nodes Call*ac#s Tracin,

"ntroduction/ NS-2

T)e most used simulator .or net(or# researc)

5ent unmaintained .or a lon, %eriod o. time !utdated code desi,n

0!ver 102 o. $CM and "333 net(or# simulation %a%ers .rom 2000-2004 cite t)e use o. ns-2

6oes not ta#e into account modern %ro,rammin,


Smart %ointers7 6esi,n %atterns7 8e9,9 :TNetS;

6oes not scale as (ell as some alternatives


Tracin, s+stem is di..icult to use


Need to %arse trace .iles to e&tract results Trace .iles end u% eit)er

<avin, in.ormation researc)ers do not need= or Missin, in.ormation ">ts usual %ractice to add %rint.>s in t)e ns-2 code
RTCM 2009-02-13 3

"ntroduction/ NS-3

NS-3 is a ne( simulator= (ritten .rom scratc)


Not reall+ an evolution o. NS-2

-ro,rammin, lan,ua,es/ C??= -+t)on


@nli#e NS-2= ever+t)in, desi,ned .or C?? !%tional -+t)on scri%tin, Still under )eav+ develo%ment @niversit+ o. 5as)in,ton

-roAect started around mid 200B


!..icial .unded %artners/

"NR"$= So%)ia $nti%olis


8Tom <enderson= Crai, 6o(ell; 8Mat)ieu 'aca,e;

:eor,ia Tec) @niversit+ 8$tlanta;

:eor,e Rile+ 8main aut)or o. GTNetS; RaA C)attac)arAea


RTCM 2009-02-13 4

NS-3 Modules
.ode class .etDevice A/C Address types &+ v-, %AC, etc'( 0ueues Socket A/C + v- A/Cs acket Sockets

!igh,level $rappers #or everything else' .o smart pointers used' Aimed at scripting'

helper Routing) +nternet,stack olsr,global,routing &ipv- impl'( node common core


ackets acket Tags acket !eaders cap"ascii #ile $riting RTCM 2009-02-13

Devices) csma * $i#i * ''' mobility simulator

Events Scheduler Time arithmetic

Smart pointers Dynamic type system Attributes Callbacks, Tracing Logging Random Variables

%obility %odels &static, random $alk, etc'( 5

e&am%lesD.irst9cc 8 D B;
int main (int argc, char *argv[]) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_I !O)" LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_I !O)" #an$omVariable%%&'eGlobal(ee$ (), ), *, +, ,, -)" o$eContainer nodes" nodes.Create (2)"
nodes: NodeContainer .ode 1 .ode 2

RTCM

2009-02-13

e&am%lesD.irst9cc 82 D B;
/oint0o/oint1elper pointToPoint" pointToPoint.(et2evice3ttrib4te (5DataRate5, (tringVal4e (55Mbps5))" pointToPoint.(etChannel3ttrib4te (5Delay5, (tringVal4e (52ms5))" et2eviceContainer devices" devices 6 pointToPoint.Install (nodes)"
devices: NetDeviceContainer

.ode 1

5Mbit/ ! 2" PointToPointNetDevice RTCM 2009-02-13

.ode 2 7

e&am%lesD.irst9cc 83 D B;
Internet(tac71elper stack" stack.In'tall (nodes)" Ipv83$$re''1elper address" address.(et9a'e (510.1.1.05, 5255.255.255.05)" Ipv8Inter:aceContainer interfaces 6 address.Assign (devices)"
inter#aces: $%v4$nter&aceContainer

21'2'2'2 ArpIpv4Interface .ode 1 PointToPointNetDevice RTCM 2009-02-13

21'2'2'3 .ode 2

e&am%lesD.irst9cc 84 D B;
&$pEcho(erver1elper echoServer (;)" 3pplicationContainer serverApps 6 echoServer.In'tall (nodes.Get ()))" serverApps.Start ((econ$' ().<))" serverApps.Stop ((econ$' ()<.<))"

serverApps: '%%(icationContainer

)d%*c+o,erver .ode 1 .ode 2

RTCM

2009-02-13

e&am%lesD.irst9cc 81 D B;
&$pEchoClient1elper echoClient (interfaces.Get3$$re'' ()), ;)" echoClient.(et3ttrib4te (5=a>/ac7et'5, &integerVal4e ()))" echoClient.(et3ttrib4te (5Interval5, 0imeVal4e ((econ$' ().)))" echoClient.(et3ttrib4te (5/ac7et(i?e5, &integerVal4e ()<*8))" 3pplicationContainer clientApps 6 echoClient.Install (nodes.Get (<))" clientApps.(tart ((econ$' (*.<))" clientApps.(top ((econ$' ()<.<))"
clientApps: '%%(icationContainer De t: 10-1-1-2! %ort 9 )d%*c+oC(ient .ode 1 1 %ac.et! 1024 b/te )d%*c+o,erver .ode 2

RTCM

2009-02-13

10

e&am%lesD.irst9cc 8B D B;
[@] Simulator Simulator ret4rn <" A Run ()" Destroy ()"
)d%*c+oC(ient .ode 1 &21'2'2'2( )d%*c+o,erver .ode 2 &21'2'2'3(

)DP Pac.et

3 4
)DP Pac.et

B .CDa: EEr4n :ir't [@] (ent )<*8 bFte' to )<.).).* 2 #eceive$ )<*8 bFte' :rom )<.).).) 3 #eceive$ )<*8 bFte' :rom )<.).).* 4
RTCM 2009-02-13 11

import n'+

e&am%lesD.irst9cc in -+t)on

n'+.LogComponentEnable(5&$pEchoClient3pplication5, n'+.LOG_LEVEL_I !O) n'+.LogComponentEnable(5&$pEcho(erver3pplication5, n'+.LOG_LEVEL_I !O) n'+.#an$omVariable.&'eGlobal(ee$(), ), *, +, ,, -) no$e' 6 n'+. o$eContainer() no$e'.Create(*) point0o/oint 6 n'+./oint0o/oint1elper() point0o/oint.(et2evice3ttrib4te(52ata#ate5, n'+.(tringVal4e(5,=bp'5)) point0o/oint.(etChannel3ttrib4te(52elaF5, n'+.(tringVal4e(5*m'5)) $evice' 6 point0o/oint.In'tall(no$e') 'tac7 6 n'+.Internet(tac71elper() 'tac7.In'tall(no$e') a$$re'' 6 n'+.Ipv83$$re''1elper() a$$re''.(et9a'e(n'+.Ipv83$$re''(5)<.).).<5), n'+.Ipv8=a'7(5*,,.*,,.*,,.<5)) inter:ace' 6 a$$re''.3''ign($evice') echo(erver 6 n'+.&$pEcho(erver1elper(;) 'erver3pp' 6 echo(erver.In'tall(no$e'.Get())) 'erver3pp'.(tart(n'+.(econ$'().<)) 'erver3pp'.(top(n'+.(econ$'()<.<)) echoClient 6 n'+.&$pEchoClient1elper(inter:ace'.Get3$$re''()), ;) echoClient.(et3ttrib4te(5=a>/ac7et'5, n'+.&integerVal4e())) echoClient.(et3ttrib4te(5Interval5, n'+.0imeVal4e(n'+.(econ$' ().<))) echoClient.(et3ttrib4te(5/ac7et(i?e5, n'+.&integerVal4e()<*8)) client3pp' 6 echoClient.In'tall(no$e'.Get(<)) client3pp'.(tart(n'+.(econ$'(*.<)) client3pp'.(top(n'+.(econ$'()<.<)) n'+.(im4lator.#4n() n'+.(im4lator.2e'troF()

RTCM

2009-02-13

12

'o(er 'evel

RTCM

2009-02-13

13

Simulator Core

Time is not mani%ulated directl+/ t)e Time class

Time class su%%orts )i,) %recision 2E *it time values 8nanosecond %recision;
0ime t) 6 (econ$' ()<)" 0ime t* 6 t) G =illi(econ$' ()<<)" 't$%%co4t HH t*.Get(econ$' () HH 't$%%en$l" CC t* 6 )<.)

:et current time/

0ime noD 6 (im4lator%% oD ()"

Sc)edule an event to )a%%en in 3 seconds/

voi$ =FCallbac7 (0) param), 0* param*) {...A [...] (im4lator%%(che$4le ((econ$' (+), =FCallbac7, param), param*)" Falues para ! and para 2 %assed as call*ac# %arameters

$lso (or#s (it) instance met)ods/

(im4lator%%(che$4le ((econ$' (+), I=FCla''%%=etho$, in'tance/tr, RTCM 2009-02-13param), param*)"

14

Currentl+ im%lemented distri*utions


Random Faria*les

@ni.orm/ values uni.orml+ distri*uted in an interval Constant/ value is al(a+s t)e same 8not reall+ random; SeGuential/ return a seGuential list o. %rede.ined values 3&%onential/ e&%onential distri*ution 8%oisson %rocess; Normal 8,aussian; 'o,-normal %areto= (ei*ull= trian,ular= H
012 032

import pFlab import n'+

rng 6 n'+.!ormal"ariable()<.<, ,.<) > 6 [rng.#et"alue() :or t in range()<<<<<)] pFlab.hi't(>, )<<) pFlab.'hoD() RTCM 2009-02-13 15

Memor+ Mana,ement

Man+ NS-3 o*Aects use automatic ,ar*a,e collection Re.erence countin,

Smart %ointers

/ac7et *p 6 neD /ac7et" # refcount starts as 1 pEJ#e: ()" # refcount becomes pEJ&nre: ()" # refcount becomes 1 pEJ&nre: ()" # refcount becomes !" packet is freed

Manual re.erence countin, is error %rone Smart %ointers 3&am%le/


Can easil+ lead to memor+ errors Ta#e care o. all t)e re.erence countin, (or# !t)er(ise t)e+ *e)ave li#e normal %ointers

voi$ =F!4nction () { $trH/ac7etJ p 6 CreateH/ac7etJ ()<)" 't$%%cerr HH K/ac7et 'i?e% K HH pEJGet(i?e () HH 't$%%en$l" A # Packet is released #smart pointer goes out of scope$ RTCM 2009-02-13 16

-ac#ets

-ac#et o*Aects used verticall" in NS-3 to re%resent/


@nits o. in.ormation sent and received *+ a%%lications "n.ormation c)un#s o. ()at (ill *ecome a real %ac#et 8similar s#I*u.. in 'inu& #ernel; Simulated %ac#ets and '2D' .rames *ein, transmitted Create em%t+ %ac#et

Casic @sa,e

/trH/ac7etJ pac7et 6 CreateH/ac7etJ ()" /trH/ac7etJ pac7et 6 CreateH/ac7etJ ()<)"

Create %ac#et (it) 0 Jdumm+J *+tes

Create %ac#et (it) user data

J6umm+J *+tes are simulated as *ein, t)ere= *ut do not actuall+ occu%+ an+ memor+ 8reduces memor+ .oot%rint;
/trH/ac7etJ pac7et 6 CreateH/ac7etJ (KhelloK, ,)" /trH/ac7etJ pac7et* 6 pac7et)EJCopF ()"

Co%+ a %ac#et

Note/ %ac#et co%+RTCM is usuall+ c)ea% 8co%+-on-(rite; 2009-02-13

17

Nodes

Node class

Re%resents a net(or# element Ma+ )ave an IPv4 stack o*Aect Ma+ )ave a mobility model
Cut it is com%letel+ o%tionalK
'%%(ication
0--5

Node4i t
1

Contains a list o. NetDevices Contains a list o. Applications

Cut it is o%tional= e9,9 CsmaNet6evice needs no mo*ilit+ model

0--5

$%v4

Node
1 0--5

Mobi(it/Mode(

NetDevice

Node'ist class 8sin,leton;


Trac#s all nodes ever created Node inde& LMN -tr conversions
RTCM 2009-02-13 1#

Call*ac# !*Aects

NS-3 Call*ac# class im%lements function objects

T+%e sa.e call*ac#s= mani%ulated *+ value

3&am%le

@sed .or e&am%le in soc#ets and tracin,

$o4ble =F!4nc (int >, :loat F) { ret4rn $o4ble (> G F) C *" A [...] Callbac%H$o4ble, int, :loatJ cb)" cb) 6 Ma%eCallbac% (=F!4nc)" $o4ble re'4lt 6 cb) &*, +'" %% result receives &' (&&&) cla'' =FCla'' { p4blic% $o4ble =F=etho$ (int >, :loat F) { ret4rn $o4ble (> G F) C *" A" (&&&) Callbac%H$o4ble, int, :loatJ cb)" =FCla'' mFobL" cb) 6 Ma%eCallbac% (I=FCla''%%=F=etho$, (myob))" $o4ble re'4lt 6 cb) &*, +'" %% result receives &' RTCM 2009-02-13 19

Tracin, 8*+ e&am%le;


4intN8_t g_pac7et2rop' 6 <" 4intN8_t g_pac7et2rop9Fte' 6 <" voi$ 0race2evM4e4e2rop ('t$%%'tring conte>t, /trHcon't /ac7etJ $roppe$/ac7et) { g_pac7et2rop' G6 )" g_pac7et2rop9Fte' G6 $roppe$/ac7etEJGet(i?e ()" A Node4i t
0--5 Node4i t

Node

0--5

Device4i t

C "aNetDevice

int main (int argc, char *argv[]) { [@] Con:ig%%Connect (5C o$eLi'tC*C2eviceLi'tC*C0>M4e4eC2rop5, =a7eCallbac7 (I0race2evM4e4e2rop))" [@] A

T867e7e

67e7e

RTCM

2009-02-13

20

"nterestin, NS-3 Oeatures

Scalability .eatures

-ac#ets can )ave Jvirtual #ero $"tesJ 8or du

Nodes )ave o%tional .eatures



Oor du " a%%lication data t)at (e don>t care a*out No memor+ is allocated .or virtual Pero *+tes Reduces t)e memor+ .oot%rint o. t)e simulation

" $"tes;

No memor+ (aste in "-v4 stac# .or nodes t)at don>t need it Mo*ilit+ model ma+ not *e needed Ne( .eatures can *e easil+ added in t)e .uture
Oor e&am%le= ener,+ models 39,9 (ired netdevices do not need to #no( t)e node %osition at all

Cross-layer .eatures

-ac#et Ta,s

Tracin,

Small units o. in.ormation attac)ed to %ac#ets $llo( to re%ort events across non-conti,uous la+ers
RTCM 2009-02-13 21

"nterestin, NS-3 Oeatures


Real (orld integration .eatures

-ac#ets can *e saved to -C$- .iles= in a real .ormat Real-time sc)eduler


Man+ tools can read -C$- .iles= e9,9 5ires)ar# Simulation events s+nc)roniPed to (all cloc# time Run 'inu& Qernel TC-D"- stac# under simulation

Net(or# Simulation Cradle


'inu& 29B9 E= 'inu& 29B92B

-!S"R 3mulation 8e&%erimental;


Run unmodi.ied -!S"R %ro,rams under simulation Runnin, routin, daemons on NS-3 8%lanned;
RTCM 2009-02-13

S%ecial 3'O loader converts -!S"R $-" calls into NS-3 calls

22

"nterestin, NS-3 Oeatures/ 3muNet6evice

RTCM

2009-02-13

23

FisualiPation

More "nterestin, Oeatures

Still e&%erimental Several on,oin, attem%ts= none +et inte,rated


3&am%le/ ns-3-%+viP

8demoed in S":C!MM (or#s)o%= $u,9 200E;

RTCM

2009-02-13

24

NS 3/ -er.ormance

Source/ 39 5ein,Srtner= <9 'e)n= and Q9 5e)rle= + per'or ance co parison o' recent net)or& si ulators= "333 "nternational Con.erence on Communications 20099

RTCM

2009-02-13

%ne sending node generates one pac&et ever" second and $roadcasts it to its neigh$ors The neigh$oring nodes rela" unseen essages a'ter a dela" o' one second( thus 'looding the entire net)or&* 25

NS-3 -er.ormance/ Time

Source/ 39 5ein,Srtner= <9 'e)n= and Q9 5e)rle= + per'or ance co parison o' recent net)or& si ulators= "333 "nternational Con.erence on Communications 20099 26 RTCM 2009-02-13

NS-3 -er.ormance/ Memor+

Source/ 39 5ein,Srtner= <9 'e)n= and Q9 5e)rle= + per'or ance co parison o' recent net)or& si ulators= "333 "nternational Con.erence on Communications 20099 27 RTCM 2009-02-13

Conclusion

NS-3 contains inovative and use.ul .eatures


NS-3 )as ,ood %er.ormance


Scala*ilit+ Cross-la+er Clean desi,n Real-(orld 8e9,9 test*ed; inte,ration

!ne o. t)e .astest simulators around T)e most memor+ e..icient simulator around Not man+ models availa*le .or NS-3 +et No :@" to *uild to%olo,+ FisualiPation still e&%erimental
RTCM 2009-02-13 2#

<o(ever

Tuestions7

RTCM

2009-02-13

29

3&am%le 2/ (i.i ? olsr 8 D4;


Di:i 6 n'+.*ifi+elper.2e:a4lt() Di:i.SetMac(5n'+%%Ad,ocOi:i=ac5) Di:i/hF 6 n'+.Pan'Oi:i/hF1elper.2e:a4lt() Di:i/hF.SetC,annel( n'+.Pan'Oi:iChannel1elper.2e:a4lt().Create()) internet 6 n'+.InternetStac%+elper() ipv83$$re''e' 6 n'+.Ipv83$$re''1elper() ipv83$$re''e'.Set-ase( n'+.Ipv83$$re''(5)<.<.<.<5), n'+.Ipv8=a'7(5*,,.*,,.*,,.*,,5))

RTCM

2009-02-13

30

3&am%le 2/ (i.i ? olsr 82D4;


port 6 ; Q 2i'car$ port(#!C -N+) onO::1elper 6 n'+.OnO::1elper(5n'+%%.dp(oc7et!actorF5, n'+.3$$re''(n'+.Inet(oc7et3$$re''( n'+.Ipv83$$re''(5)<.<.<.)5), port))) onO::1elper.(et3ttrib4te(5/n0ime5, n'+.#an$omVariableVal4e(n'+.ConstantVariable()))) onO::1elper.(et3ttrib4te(5/ff0ime5, n'+.#an$omVariableVal4e(n'+.ConstantVariable(<)))

RTCM

2009-02-13

31

3&am%le 2/ (i.i ? olsr 83D4;


for >i in range( &=_ O2E(_(I2E)% for Fi in range( &=_ O2E(_(I2E)% no$e 6 n'+. o$e() internet.In'tall(no$e) mobilitF 6 n'+.(tatic=obilitF=o$el() mobilitF.(et/o'ition( n'+.Vector(>i*2I(03 CE, Fi*2I(03 CE, <)) no$e.3ggregateObLect(mobilitF) $evice' 6 Di:i.In'tall(Di:i/hF, no$e) ipv83$$re''e'.3''ign($evice') ol'r1elper.In'tall(no$e) app 6 onO::1elper.In'tall(no$e) app.(tart(n'+.(econ$'( n'+.&ni:ormVariable(-, ))).GetVal4e()))
RTCM 2009-02-13 32

3&am%le 2/ (i.i ? olsr 84D4;


internet.1nable$capAll(5Di:iEol'r5) n'+.(im4lator.Stop(n'+.(econ$'(),.<)) n'+.(im4lator.Run()
gLcR$ar7EtoDer%n'E+E$evB l' E' Di:iEol'rE* ,)N Di:iEol'rE<E<.pcap ))*< Di:iEol'rE<E).pcap 8 Di:iEol'rE)E<.pcap S*- Di:iEol'rE)E).pcap 8 Di:iEol'rE*E<.pcap 8<< Di:iEol'rE*E).pcap 8 Di:iEol'rE+E<.pcap ),<< Di:iEol'rE+E).pcap 8 Di:iEol'rE8E<.pcap )<88 Di:iEol'rE8E).pcap 8 Di:iEol'rE,E<.pcap *SN Di:iEol'rE,E).pcap 8 Di:iEol'rENE<.pcap +,N Di:iEol'rENE).pcap 8 Di:iEol'rESE<.pcap ,,* Di:iEol'rESE).pcap 8 Di:iEol'rE-E<.pcap +<8 Di:iEol'rE-E).pcap

RTCM

2009-02-13

33

-ac#et/ <eaders and Trailers

-ac#ets su%%ort headers and trailers

<eaders an trailers are im%lemented as classes t)at

"m%lement a Serialize met)od/

"m%lement a Deserialize met)od/

5rites t)e )eader in.ormation as a *+te streamU Reads t)e )eader in.ormation .rom a *+te streamU

<eaders and trailers used to im%lement %rotocols -ac#ets contain e&act %rotocol *+te contents

T)e+ are not Aust structures li#e in NS2 $llo(s (ritin, %ca% trace .iles= reada*le .rom (ires)ar#
RTCM

2009-02-13

34

''CDSN$- e&am%le 8.rom ns-3-dev;/

4int+*_t Llc(nap1ea$er%%Get(eriali?e$(i?e (voi$) con't { ret4rn ) G ) G ) G + G *" A voi$ Llc(nap1ea$er%%(eriali?e (94::er%%Iterator 'tart) con't { 94::er%%Iterator i 6 'tart" 4int-_t b4:[] 6 {<>aa, <>aa, <><+, <, <, <A" i.*rite (b4:, N)" i.Orite+ton&)N (m_ether0Fpe)" A 4int+*_t Llc(nap1ea$er%%2e'eriali?e (94::er%%Iterator 'tart) { 94::er%%Iterator i 6 'tart" i.!e2t (,G))" %% skip * b+tes" don,t care about content m_ether0Fpe 6 i.#ea$!to,&)N ()" ret4rn #etSeriali3edSi3e ()" A

$ddin, a )eader/

Llc(nap1ea$er llc'nap" llc'nap.(et0Fpe (<><-<<)" Q Ipv8 pac7etEJAdd+eader (llc'nap)" Llc(nap1ea$er llc'nap" i: (pac7etEJRemove+eader (llc'nap) { 't$%%co4t HH llc'nap.Get0Fpe () HH 't$%%en$l" A

Removin, a )eader/

RTCM

2009-02-13

35

-ac#et/ Ta,s

Ta,s

Small c)un#s o. in.ormation $n+ num*er o. ta,s can *e attac)ed a %ac#et Ta,s are #e+ed *+ t)e a structure t+%e itsel.

Ne( ta, t+%es are de.ined similarl+ to )eader t+%es $ttac) conte&t in.ormation to a %ac#et

-trL-ac#etN %U M+Ta, ta,U %-N$ddTa, 8ta,; %-N-ee#Ta, 8ta,;

Ta,s can *e used to/

3&am%le/ Net6evice attac)es destination M$C address ()en Gueuein,= retrieves it ()en deGueuin, .or transmission

Conve+ additional in.ormation across la+ers


RTCM 2009-02-13 36

class !*Aect

Object is t)e *ase class .or man+ im%ortant classes/


Node= Net6evice= $%%lication= Soc#et= 999

class !*Aect %rovides man+ use.ul .eatures


Casic memor+ mana,ement 8re.erence countin,; $dvanced memor+ mana,ement 8t)e 6is%ose met)od;

6is%oseD6o6is%ose/ used to *rea# re.erence countin, loo%s

!*Aect a,,re,ation

Node MN list8$%%lication;U $%%lication MN Node

C!M-li#e inter.ace Guer+ mec)anism "nstead o. a )u,e class= s%lit class into several o*Aects/
Node= "%v4= V@d%DTc%WSoc#etOactor+= Mo*ilit+=999

3&am%le/ .rom a Node o*Aect= see i. it su%%orts "%v4 voi$ =F!4nction (/trH o$eJ no$e) { /trHIpv8J ipv8 6 no$eEJGetObLectHIpv8J ()" i: (ipv8 T6 &LL) 't$%%cerr HH K o$e ha' K HH ipv8EJGet #o4te' () HH Kro4te'.K HH 't$%%en$l" A

Tracin, )oo#s

RTCM

2009-02-13

37

!*Aect and T+%e"d


T+%e"d/ (or#in, around C?? limitations


"n C??= classes are not 'irst-class o$,ects T+%e name 'ist o. attri$utes or trace sources 3&am%le/ to create an o*Aect .rom t+%e name/

T+%e"d is an o*Aect t)at descri*es a class t+%e/ T+%e"d im%lements t)e Factor" 6esi,n -attern

0FpeI$ obL0Fpe 6 0FpeI$%%Loo74p9F ame (K(tatic=obilitF=o$elK) /trHObLectJ mobilitF=o$el 6 obL0Fpe.CreateObLect ()

RTCM

2009-02-13

3#

!*Aect and T+%e"d 8cont9;

Cecause o. t)e T+%e"d s+stem= creatin, !*Aect instances s)ould *e done (it)/

6e.inin, ne( !*Aect su*classes needs s%ecial care/

/trHCla'' ameJ obL 6 Create/b)ectHCla'' ameJ (...parameter')

Must de.ine a :etT+%e"d static met)od= li#e t)is/


cla'' =FCla'' % p4blic =F/arent { p4blic% =FCla'' (/aram0Fpe) p), ...)" 'tatic 0FpeI$ #et0ypeId (voi$)" (&&&) A" 0FpeI$ =FCla''%%#et0ypeId (voi$) { 'tatic 0FpeI$ ti$ 6 0FpeI$ (5=FCla''5) .(et/arentH=F/arentJ () .3$$Con'tr4ctorH=FCla'', /aram0Fpe), ... J ()" ret4rn ti$" A RTCM 2009-02-13

39

6e*u,,in, Su%%ort

$ssertions/ NSI$SS3RT 8e&%ression;U


$*orts t)e %ro,ram i. e&%ression evaluates to .alse "ncludes source .ile name and line num*er Oorces an unconditional *rea#%oint= com%iled in -ur%ose

@nconditional Crea#%oints/ NSICR3$Q-!"NT 8;U


6e*u, 'o,,in, -not to $e con'used )ith tracing./


@sed to trace code e&ecution lo,ic Oor de*u,,in,= not to e&tract resultsK NSI'!:X macros (or# (it) C?? "! streams

-ro%erties

39,9/

NSI'!: macros evaluate to not)in, in o%timiPed *uilds

(_LOG_& CO 2 (KI have receive$ K HH pEJGet(i?e () HH K bFte'K)"

5)en de*u,,in, is done= lo,,in, does not ,et in t)e (a+ o. e&ecution %er.ormance
RTCM 2009-02-13 40

6e*u,,in, Su%%ort 8cont9;

'o,,in, levels/

'o,,in, Jcom%onentsJ

NSI'!:I3RR!R 8999;/ serious error essages onl" NSI'!:I5$RN 8999;/ )arning essages NSI'!:I63C@: 8999;/ rare ad-hoc de$ug essages NSI'!:I"NO! 8999;/ in'or ational essages -eg* $anners/ NSI'!:IO@NCT"!N 8999;/ 'unction tracing NSI'!:I-$R$M 8999;/ para eters to 'unctions NSI'!:I'!:"C 8999;/ control 'lo) tracing )ithin 'unctions

'o,,in, messa,es or,aniPed *+ com%onents


@suall+ one com%onent is one 9cc source .ile
NSI'!:IC!M-!N3NTI63O"N3 8Y!lsr$,entY;U

6is%la+in, lo, messa,es9 T(o (a+s/


-ro,ramaticall+/

Orom t)e environment/


RTCM

'o,Com%onent3na*le8Y!lsr$,entY= '!:I'3F3'I$'';U
(_LOG65Ol'r3gent5 .CmFEprogram 2009-02-13 41

$%%lications and Soc#ets


3ac) Node contains a list o. $%%lications


$%%lications are li#e processes in a normal s+stem Soc#ets re%resent communication end %oints NS-3 soc#ets modelled a.ter t)e CS6 soc#et $-" Tra..ic ,enerators 8e9,9 !n!..$%%lication; Tra..ic sin#s 8e9,9 to res%ond to connection reGuests; Routin, a,ents= )i,)er level %rotocols

$%%lications contain a num*er o. Soc#ets 3&am%le uses o. $%%lications


Soc#ets creation/ a soc&et 'actor" Node inter.ace/


/trH(oc7et!actorFJ udp4actory 6 no$eEJ#et/b)ectH(oc7et!actorFJ (0FpeI$%%Loo74p9F ame (5.dp5))" /trH(oc7etJ 'oc7et 6 4$p!actorFEJCreateSoc%et ()" RTCM 2009-02-13

5)atever normall+ runs in users%ace in a @N"R s+stem

42

NS-3 Soc#ets

-lain C soc#ets

NS-3 soc#ets

int '7" '7 6 soc%et(/!_I E0, (OCU_2G#3=, <)" 'tr4ct soc%addr5in 'rc" inet_pton(3!_I E0,K<.<.<.<K,I'rc.'in_ a$$r)" 'rc.'in_port 6 hton'(-<)" bind('7, ('tr4ct 'oc7a$$r *) I'rc, 'i?eo:('rc))" 'tr4ct 'oc7a$$r_in $e't" inet_pton(3!_I E0,K)<.<.<.)K,I$e't.'i n_a$$r)" $e't.'in_port 6 hton'(-<)" sendto('7, KhelloK, N, <, ('tr4ct 'oc7a$$r *) I$e't, 'i?eo:($e't))" char b4:[N]" recv('7, b4:, N, <)"

/trH(oc7etJ '7 6 4$p!actorFEJCreateSoc%et ()"

'7EJ-ind (InetSoc%etAddress (-<))"

'7EJSend0o (Inet(oc7et3$$re'' (Ipv83$$re'' (K)<.<.<.)K), -<), CreateH$ac%etJ (KhelloK, N))"

'7EJSetReceiveCallbac% (=a7eCallbac7 (-+Socket.eceive))" (/) #Simulator00.un #$$ voi$ -+Socket.eceive (/trH(oc7etJ '7, /trH/ac7etJ pac%et) { ... A

RTCM

2009-02-13

43

T)e Mo*ilit+Model inter.ace/


Mo*ilit+ Models

void Set-osition 8Fector %os; Fector :et-osition 8; Node is at a .i&ed locationU does not move on its o(n 8(or#s inside a rectan,ular *ounded area; Node %auses .or a certain random time Node selects a random (a+%oint and s%eed Node starts (al#in, to(ards t)e (a+%oint 5)en (a+%oint is reac)ed= ,oto .irst state 8(or#s inside a rectan,ular *ounded area; Node selects a random direction and s%eed Node (al#s in t)at direction until t)e ed,e Node %auses .or random time Re%eat RTCM 2009-02-13

StaticMo*ilit+Model

Random5a+%ointMo*ilit+Model

Random6irectionMo*ilit+Model

44

:ettin, Started/ 'inu&

Cuildin, it
)) '4$o aptEget in'tall b4il$Ee''ential gGG pFthon merc4rial Q (&b4nt4) *) hg clone http%CCco$e.n'nam.orgCn'E+EallinoneC +) c$ n'E+Eallinone 8) .C$oDnloa$.pF # 1ill do1nload components ,) .Cb4il$.pF # 1ill build 2S34 N) c$ n'E+E$ev

Runnin, e&am%le %ro,rams

-ro,rams are *uilt as @sin, (a. --s)ell @sin, (a. run

b4il$C5variant6CpathCprogram7name 5variant6 i' either debug or optimi8ed

)) .CDa: V'hell *) .Cb4il$C$eb4gCe>ample'C'impleEpointEtoEpoint

)) .CDa: Vr4n 'impleEpointEtoEpoint


RTCM 2009-02-13 45

:ettin, Started/ 5indo(s

Cuildin, it
)) In'tall b4il$ tool' )) CFgDin or =ingD GCC (gGG) *) /Fthon% http%CCDDD.pFthon.org +) =erc4rial% http%CCmerc4rial.ber7Doo$.comC *) hg clone http%CCco$e.n'nam.orgCn'E+.<.))C +) c$ n'E+.<.)) 8) Da: con:ig4re Q optional% E$ optimi?e$ ,) Da: chec7 Q r4n' 4nit te't'

Rest o. instructions t)e same as in 'inu&999

RTCM

2009-02-13

46

You might also like