<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta name="generator" content="pdf2htmlEX">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="https://static.pudn.com/base/css/base.min.css">
<link rel="stylesheet" href="https://static.pudn.com/base/css/fancy.min.css">
<link rel="stylesheet" href="https://static.pudn.com/prod/directory_preview_static/622b839715da9b288bad2d0b/raw.css">
<script src="https://static.pudn.com/base/js/compatibility.min.js"></script>
<script src="https://static.pudn.com/base/js/pdf2htmlEX.min.js"></script>
<script>
try{
pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({});
}catch(e){}
</script>
<title></title>
</head>
<body>
<div id="sidebar" style="display: none">
<div id="outline">
</div>
</div>
<div id="pf1" class="pf w0 h0" data-page-no="1"><div class="pc pc1 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://static.pudn.com/prod/directory_preview_static/622b839715da9b288bad2d0b/bg1.jpg"><div class="t m0 x1 h2 y1 ff1 fs0 fc0 sc0 ls0 ws0">A<span class="_ _0"></span> T<span class="_ _1"></span>utorial <span class="_ _2"></span>of W<span class="_ _3"></span>ir<span class="_ _3"></span>eless <span class="_ _2"></span>Simulation <span class="_ _2"></span>in <span class="_ _2"></span>NS-2 </div><div class="t m0 x1 h3 y2 ff2 fs1 fc0 sc0 ls1 ws1">Y<span class="_ _0"></span>ue W<span class="_ _4"></span>ang </div><div class="t m0 x1 h3 y3 ff2 fs1 fc0 sc0 ls2 ws2">Department of CSE, CUHK </div><div class="t m0 x1 h3 y4 ff2 fs1 fc1 sc0 ls3 ws3">ywang@cse.cuhk.edu.<span class="_ _3"></span>hk</div><div class="t m0 x2 h3 y5 ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h3 y6 ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h3 y7 ff2 fs1 fc0 sc0 ls5 ws4">(V<span class="_ _0"></span>ersion 2, revised at July<span class="_ _3"></span> 20, 2007) </div><div class="t m0 x1 h4 y8 ff1 fs2 fc0 sc0 ls6 ws5">1. Intr<span class="_ _3"></span>oduction to ns-2<span class="ff2 ls4 ws3"> </span></div><div class="t m0 x1 h5 y9 ff1 fs3 fc0 sc0 ls7 ws6">1.1 ns-2 </div><div class="t m0 x1 h3 ya ff2 fs1 fc0 sc0 ls8 ws7">Ns-2 [1] is a packet-level s<span class="_ _3"></span>imulator which is essentially a <span class="ff3 ls9 ws3">centraliz<span class="_ _3"></span>ed<span class="ff2 ls5 ws8"> discrete event scheduler t<span class="_ _3"></span>o </span></span></div><div class="t m0 x1 h3 yb ff2 fs1 fc0 sc0 lsa ws9">schedule the eve<span class="_ _3"></span>nts such as packet and timer e<span class="lsb wsa">xpirat<span class="_ _3"></span>ion. The centralized event scheduler cannot </span></div><div class="t m0 x1 h3 yc ff2 fs1 fc0 sc0 ls5 wsb">accurately emula<span class="_ _3"></span>te “events occurred at the sam<span class="_ _3"></span>e time”, instead, it ca<span class="_ _3"></span>n only handle events occ<span class="_ _3"></span>urred </div><div class="t m0 x1 h3 yd ff2 fs1 fc0 sc0 ls3 wsc">one by one <span class="_ _3"></span>in time. How<span class="_ _3"></span>ever<span class="_ _3"></span>, this is not<span class="_ _3"></span> a serious problem<span class="_ _3"></span> in most network sim<span class="_ _3"></span>ulations, because </div><div class="t m0 x1 h3 ye ff2 fs1 fc0 sc0 lsa wsd">the events her<span class="_ _3"></span>e are often transitory<span class="_ _4"></span>. Besides, ns-2 im<span class="_ _3"></span>plements a variety<span class="_ _3"></span> of network com<span class="_ _3"></span>ponents </div><div class="t m0 x1 h3 yf ff2 fs1 fc0 sc0 lsc wse">and protocols. Not<span class="_ _2"></span>ably<span class="_ _4"></span>, the wirele<span class="_ _2"></span>ss extension,<span class="lsd wsf"> derived from CMU Monarc<span class="_ _2"></span><span class="lsb ws10">h Project [2], <span class="_ _3"></span>has 2 </span></span></div><div class="t m0 x1 h3 y10 ff2 fs1 fc0 sc0 ls3 ws11">assumptions sim<span class="_ _3"></span>plifying the physical w<span class="_ _3"></span>orld: </div><div class="t m0 x3 h3 y11 ff2 fs1 fc0 sc0 ls8 ws12">(1) Nodes do not m<span class="_ _3"></span>ove significan<span class="lse ws13">tly over the length of time th<span class="ws14">ey transmit or receive<span class="_ _3"></span> a packet. </span></span></div><div class="t m0 x1 h3 y12 ff2 fs1 fc0 sc0 lsf ws15">This assumption <span class="_ _3"></span>holds only for mobile nodes of <span class="_ _3"></span>high-rate and low-spe<span class="_ _3"></span>ed. Consider a node with <span class="_ _3"></span>the </div><div class="t m0 x1 h3 y13 ff2 fs1 fc0 sc0 ls3 ws16">sending rate of 10K<span class="_ _3"></span>bps and moving spee<span class="_ _3"></span>d of 10m/s, during its receivin<span class="_ _3"></span>g a packet of 1500B, the </div><div class="t m0 x1 h3 y14 ff2 fs1 fc0 sc0 lsf ws15">node moves 12m<span class="_ _3"></span>. Thus, the <span class="_ _3"></span>surrounding can change sig<span class="_ _3"></span>nificantly and cause rece<span class="_ _3"></span>ption failure. </div><div class="t m0 x3 h3 y15 ff2 fs1 fc0 sc0 lsf ws15">(2) Node velocity is <span class="_ _3"></span>insignificant com<span class="_ _3"></span>pared to the speed of ligh<span class="_ _3"></span>t. In particular<span class="_ _3"></span>, none<span class="_ _3"></span> of the </div><div class="t m0 x1 h3 y16 ff2 fs1 fc0 sc0 lsf ws15">provided propaga<span class="_ _3"></span>tion models include D<span class="_ _3"></span>oppler effects,<span class="_ _3"></span> although they could. </div><div class="t m0 x3 h3 y17 ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h5 y18 ff1 fs3 fc0 sc0 ls10 ws17">1.2 GloMoSim and OPNET<span class="ff2 fs1 ls4 ws3"> </span></div><div class="t m0 x1 h3 y19 ff2 fs1 fc0 sc0 ls11 ws18">GloMoSim [3] is a<span class="_ _3"></span>nother open-source netw<span class="_ _3"></span>ork simulator which is bas<span class="_ _3"></span>ed on parallel programm<span class="_ _3"></span>ing. </div><div class="t m0 x1 h3 y1a ff2 fs1 fc0 sc0 ls12 ws19">Hopefully<span class="_ _4"></span>, it can emulate the real<span class="_ _3"></span> world more <span class="ls13 ws1a">accurately<span class="_ _4"></span>. However<span class="_ _3"></span>, it may be <span class="_ _3"></span>hard to debug </span></div><div class="t m0 x1 h3 y1b ff2 fs1 fc0 sc0 ls14 ws1b">parallel program<span class="_ _3"></span>s. Although GloMoSim<span class="_ _3"></span> currently <span class="ls15 ws1c">solely supports wireless networ<span class="_ _3"></span>ks, it provides </span></div><div class="t m0 x1 h3 y1c ff2 fs1 fc0 sc0 ls5 ws1d">more physical-lay<span class="_ _3"></span>er models than ns-2. Th<span class="_ _3"></span>ere is another simulator<span class="_ _3"></span> OPNET which requires <span class="_ _3"></span>licence. </div><div class="t m0 x1 h3 y1d ff2 fs1 fc0 sc0 ls5 ws14">T<span class="_ _4"></span>able 1 [4] compares the wireless p<span class="_ _3"></span>hysical models used in the<span class="_ _3"></span> three simulators. <span class="_"> </span> </div><div class="t m0 x1 h3 y1e ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h3 y1f ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h3 y20 ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h3 y21 ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h3 y22 ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h3 y23 ff2 fs1 fc0 sc0 ls4 ws3"> </div></div><div class="pi" data-data='{"ctm":[1.611639,0.000000,0.000000,1.611639,0.000000,0.000000]}'></div></div>
</body>
</html>
<div id="pf2" class="pf w0 h0" data-page-no="2"><div class="pc pc2 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://static.pudn.com/prod/directory_preview_static/622b839715da9b288bad2d0b/bg2.jpg"><div class="t m0 x1 h3 y24 ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x4 h6 y25 ff1 fs4 fc0 sc0 ls16 ws6">T<span class="_ _4"></span>able 1. Physical layer and propagation mode<span class="ls17">ls available in GloMoSim, ns-2 and OPNET </span></div><div class="t m0 x5 h3 y26 ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x6 h3 y27 ff2 fs1 fc0 sc0 ls18 ws3"> </div><div class="t m0 x1 h5 y28 ff1 fs3 fc0 sc0 ls10 ws1e">1.3 Ns-2 Basics </div><div class="t m0 x1 h7 y29 ff1 fs5 fc0 sc0 ls19 ws1f">Ns-2 directory structure </div><div class="t m0 x1 h3 y2a ff2 fs1 fc0 sc0 ls3 ws20">As shown in Figur<span class="_ _3"></span>e 1, the C++ clas<span class="_ _3"></span>ses of ns-2 network com<span class="_ _3"></span>ponents and protoco<span class="_ _3"></span>ls are </div><div class="t m0 x1 h3 y2b ff2 fs1 fc0 sc0 ls1a ws21">implem<span class="_ _3"></span>ented in the subdirectory<span class="_ _3"></span> “ns-2.*”, and the <span class="ls1b ws22">T<span class="_ _3"></span>CL<span class="_ _3"></span> library (corresponding to conf<span class="_ _3"></span>igurations of </span></div><div class="t m0 x1 h3 y2c ff2 fs1 fc0 sc0 ls8 ws12">these C++ instances) in the s<span class="_ _3"></span>ubdirectory of “tcl”. </div><div class="t m0 x7 h3 y2d ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x8 h6 y2e ff1 fs4 fc0 sc0 ls1c ws23">Figure 1<span class="_ _3"></span>. Ns-2 directo<span class="_ _3"></span>ry structur<span class="_ _3"></span>e </div><div class="t m0 x1 h3 y2f ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h7 y30 ff1 fs5 fc0 sc0 ls1d ws24">Network Components </div><div class="t m0 x1 h3 y31 ff2 fs1 fc0 sc0 ls1b ws25">Network compo<span class="_ _3"></span>nents are Node, Link,<span class="_ _3"></span> Queue, etc. Some of<span class="_ _3"></span> them are sim<span class="_ _3"></span>ple components, that is, </div><div class="t m0 x1 h3 y32 ff2 fs1 fc0 sc0 ls1b ws26">they corresponds <span class="_ _3"></span>to a single C++ objec<span class="_ _3"></span>t; The others are com<span class="_ _3"></span>pound components, t<span class="_ _3"></span>hat is, they </div><div class="t m0 x1 h3 y33 ff2 fs1 fc0 sc0 ls1e ws27">combine m<span class="_ _3"></span>ultiple simple com<span class="_ _3"></span>ponents, e.g. a Link com<span class="_ _3"></span>ponent is composed<span class="_ _3"></span> of a Delay componen<span class="_ _3"></span>t </div><div class="t m0 x1 h3 y34 ff2 fs1 fc0 sc0 ls1f ws28">(emulating pr<span class="_ _3"></span>opagation delay<span class="_ _3"></span>) and a Queue compone<span class="_ _3"></span>nt. In general, all network c<span class="_ _3"></span>omponents are </div><div class="t m0 x1 h3 y35 ff2 fs1 fc0 sc0 ls15 ws29">created, plugged a<span class="_ _3"></span>nd configured by som<span class="_ _3"></span>e TCL s<span class="_ _3"></span>cripts (./ns-allinone-2.*/<span class="_ _3"></span>tcl/) when ns-2 is </div><div class="t m0 x1 h3 y36 ff2 fs1 fc0 sc0 ls14 ws3">initialized. </div><div class="t m0 x1 h3 y37 ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h3 y38 ff2 fs1 fc0 sc0 ls4 ws3"> </div></div><div class="pi" data-data='{"ctm":[1.611639,0.000000,0.000000,1.611639,0.000000,0.000000]}'></div></div>
<div id="pf3" class="pf w0 h0" data-page-no="3"><div class="pc pc3 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://static.pudn.com/prod/directory_preview_static/622b839715da9b288bad2d0b/bg3.jpg"><div class="t m0 x1 h3 y24 ff2 fs1 fc0 sc0 ls5 ws4">Example: Plug MAC into <span class="lsd ws2a">N<span class="_ _3"></span>etIF (Network Interface) </span></div><div class="t m0 x1 h3 y39 ff2 fs1 fc2 sc0 ls15 ws3">class<span class="fc3 ls4"> <span class="fc4 ls20">MAC</span><span class="ls21 ws2b"> { </span></span></div><div class="t m0 x3 h3 y3a ff2 fs1 fc3 sc0 lsf ws15">void send (Packet* <span class="_ _3"></span>p); </div><div class="t m0 x3 h3 y3b ff2 fs1 fc3 sc0 ls11 ws2c">void recv(Packet*, Ha<span class="_ _3"></span>ndler* h); </div><div class="t m0 x3 h3 y3c ff2 fs1 fc3 sc0 ls22 ws3">NsObject* <span class="fc4 ls23">target_ <span class="fc5 ls24 ws2d">//<span class="_ _2"></span>pointing to an in<span class="_ _2"></span>stance of NetIF</span></span><span class="ls4"> </span></div><div class="t m0 x1 h3 y3d ff2 fs1 fc3 sc0 ls4 ws3">} </div><div class="t m0 x1 h3 y3e ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h7 y3f ff1 fs5 fc0 sc0 ls25 ws2e">Event Scheduling </div><div class="t m0 x1 h3 y40 ff2 fs1 fc0 sc0 lsb wsa">Events are something asso<span class="_ _3"></span>ciated with time. <span class="fc2 ls26 ws3">class<span class="fc0 ls4"> <span class="_ _5"></span><span class="fc4 ls27">Event</span><span class="ls28 ws2f"> is defined b<span class="_ _2"></span>y <span class="fc3 ls29 ws30">{time, uid, next, handler}</span></span><span class="ls2a">, </span></span></span></div><div class="t m0 x1 h3 y41 ff2 fs1 fc0 sc0 ls2b ws3">where <span class="_ _6"> </span><span class="fc3 ls2c">time</span><span class="ls1e ws31"> is the sched<span class="_ _3"></span>uling time of the event, <span class="fc3 ls2d ws3">ui<span class="_ _3"></span>d<span class="fc0 lsf ws32"> is the event’<span class="_ _4"></span>s id , <span class="fc3 ls2e ws3">next</span><span class="ls2c ws33"> points to the next </span></span></span></span></div><div class="t m0 x1 h3 y42 ff2 fs1 fc0 sc0 ls1f ws34">scheduling event in th<span class="_ _3"></span>e event queue, and <span class="fc3 ws3">handler<span class="_ _3"></span><span class="fc0 ls1e ws35"> points to the functio<span class="_ _3"></span>n to handle the event at<span class="_ _3"></span> </span></span></div><div class="t m0 x1 h3 y43 ff2 fs1 fc0 sc0 ls1b ws36">scheduling ti<span class="_ _3"></span>me. Events are put into<span class="_ _3"></span> the event queue sorted<span class="_ _3"></span> by their <span class="fc3 ls2c ws3">tim<span class="_ _3"></span>e<span class="fc0 ls1 ws37">, and scheduled <span class="_ _3"></span>one by </span></span></div><div class="t m0 x1 h3 y44 ff2 fs1 fc0 sc0 ls5 ws38">one by the event scheduler<span class="_ _4"></span>. Note that <span class="fc2 ls26 ws3">class</span><span class="fc4"> Packet</span><span class="lsa ws39"> is a subc<span class="_ _3"></span>lass of <span class="fc2 ls3 ws3">class<span class="fc4 ls5 ws38"> Event </span></span><span class="lsf ws3a">to s<span class="_ _3"></span>imulate packets </span></span></div><div class="t m0 x1 h3 y45 ff2 fs1 fc0 sc0 ls11 ws3b">transmitted or recei<span class="_ _3"></span>ved at some tim<span class="_ _3"></span>e. A<span class="ls8 ws3c">ll network components are subclasses of<span class="_ _3"></span> <span class="fc2 lse ws3">class<span class="fc0 ls4"> <span class="_ _7"></span><span class="fc4 ls20">Handler <span class="_ _7"></span></span><span class="ls2f">as </span></span></span></span></div><div class="t m0 x1 h3 y46 ff2 fs1 fc0 sc0 ls8 ws12">they need to handle events s<span class="_ _3"></span>uch as packets. </div><div class="t m0 x1 h3 y47 ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h3 y48 ff2 fs1 fc0 sc0 ls3 ws3d">The scheduling <span class="_ _3"></span>procedure (<span class="fc3 lse ws3e">void Sche<span class="_ _3"></span>duler::<span class="fc4 ls30 ws3">schedule</span><span class="ls1e ws3f">(Handler* h, Event* e,<span class="_ _3"></span> double delay)<span class="_ _3"></span><span class="fc0 ls31 ws40">) is </span></span></span></div><div class="t m0 x1 h3 y49 ff2 fs1 fc0 sc0 lsa ws41">shown in Figure <span class="_ _3"></span>2. The event at th<span class="_ _3"></span>e head of the e<span class="_ _3"></span>vent queue is delivered<span class="_ _3"></span> to its hander of som<span class="_ _3"></span>e </div><div class="t m0 x1 h3 y4a ff2 fs1 fc0 sc0 ls3 ws42">network com<span class="_ _3"></span>ponent (object). Then, this n<span class="_ _3"></span>etwork object may call oth<span class="_ _3"></span>er network objects to further </div><div class="t m0 x1 h3 y4b ff2 fs1 fc0 sc0 ls32 ws43">handle this event, and<span class="_ _2"></span> may generate new even<span class="ls33 ws44">ts which are inserted into <span class="_ _3"></span>the event queue. </span></div><div class="t m0 x9 h3 y4c ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 xa h6 y4d ff1 fs4 fc0 sc0 ls34 ws45">Figure 2. Discrete Event Scheduler </div><div class="t m0 x1 h3 y4e ff2 fs1 fc0 sc0 ls4 ws3"> </div><div class="t m0 x1 h3 y4f ff2 fs1 fc0 sc0 lsb ws46">Example 1: <span class="_ _4"></span>A<span class="_ _3"></span> and B are two <span class="_ _3"></span>mobile nodes within the tx range. <span class="_ _4"></span>A<span class="_ _3"></span> sends pac<span class="_ _3"></span>ket <span class="ff3 ls4 ws3">p</span><span class="ls1b ws47"> to B. </span></div><div class="t m0 x3 h3 y50 ff2 fs1 fc3 sc0 ls4 ws3">A::<span class="fc4 ls32">send</span><span class="fc1"> </span><span class="ls35 ws48">(Packet* p) {target_-></span><span class="fc4 ls8">recv</span><span class="ls2">(p)} <span class="fc5 lsf ws15">//t<span class="_ _3"></span>arget_ poi<span class="_ _3"></span>nts to B and will call B::rec<span class="_ _3"></span>v </span></span></div><div class="t m0 x3 h3 y51 ff2 fs1 fc3 sc0 ls4 ws3"> </div><div class="t m0 x3 h3 y52 ff2 fs1 fc3 sc0 ls3 ws3">B::<span class="fc4 lsc">recv</span><span class="ls8 ws12">(Packet*, Handler* h = 0) { </span></div><div class="t m0 xb h3 y53 ff2 fs1 fc3 sc0 ls4 ws3">… </div><div class="t m0 xb h3 y54 ff2 fs1 fc5 sc0 ls1b ws49">//tar<span class="_ _3"></span>get_ is B; schedule <span class="_ _3"></span>p at the time of (current_tim<span class="_ _3"></span>e + tx_time) </div><div class="t m0 xb h3 y55 ff2 fs1 fc3 sc0 lsb ws3">Scheduler::instance().<span class="fc4 ls29">schedule</span><span class="lsf ws4a">(tar<span class="_ _3"></span>get_, p, tx_tim<span class="_ _3"></span>e) <span class="_"> </span> </span></div><div class="t m0 xb h3 y56 ff2 fs1 fc3 sc0 ls4 ws3">… </div><div class="t m0 xc h3 y57 ff2 fs1 fc3 sc0 ls4 ws3">} </div></div><div class="pi" data-data='{"ctm":[1.611639,0.000000,0.000000,1.611639,0.000000,0.000000]}'></div></div>