author | Tom Henderson <tomh@tomh.org> |
Sat, 16 Jan 2016 08:14:40 -0800 | |
changeset 11683 | 9142266fbb25 |
parent 11681 | d0b2d73051f7 |
permissions | -rw-r--r-- |
6754 | 1 |
.. include:: replace.txt |
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
2 |
.. highlight:: cpp |
6754 | 3 |
|
4 |
Conceptual Overview |
|
5 |
------------------- |
|
6 |
||
7 |
The first thing we need to do before actually starting to look at or write |
|
8 |
|ns3| code is to explain a few core concepts and abstractions in the |
|
9 |
system. Much of this may appear transparently obvious to some, but we |
|
10 |
recommend taking the time to read through this section just to ensure you |
|
11 |
are starting on a firm foundation. |
|
12 |
||
13 |
Key Abstractions |
|
14 |
**************** |
|
15 |
||
16 |
In this section, we'll review some terms that are commonly used in |
|
17 |
networking, but have a specific meaning in |ns3|. |
|
18 |
||
19 |
Node |
|
20 |
++++ |
|
21 |
In Internet jargon, a computing device that connects to a network is called |
|
22 |
a *host* or sometimes an *end system*. Because |ns3| is a |
|
23 |
*network* simulator, not specifically an *Internet* simulator, we |
|
24 |
intentionally do not use the term host since it is closely associated with |
|
25 |
the Internet and its protocols. Instead, we use a more generic term also |
|
26 |
used by other simulators that originates in Graph Theory --- the *node*. |
|
27 |
||
28 |
In |ns3| the basic computing device abstraction is called the |
|
29 |
node. This abstraction is represented in C++ by the class ``Node``. The |
|
30 |
``Node`` class provides methods for managing the representations of |
|
31 |
computing devices in simulations. |
|
32 |
||
33 |
You should think of a ``Node`` as a computer to which you will add |
|
34 |
functionality. One adds things like applications, protocol stacks and |
|
35 |
peripheral cards with their associated drivers to enable the computer to do |
|
36 |
useful work. We use the same basic model in |ns3|. |
|
37 |
||
38 |
Application |
|
39 |
+++++++++++ |
|
40 |
Typically, computer software is divided into two broad classes. *System |
|
41 |
Software* organizes various computer resources such as memory, processor |
|
42 |
cycles, disk, network, etc., according to some computing model. System |
|
43 |
software usually does not use those resources to complete tasks that directly |
|
44 |
benefit a user. A user would typically run an *application* that acquires |
|
45 |
and uses the resources controlled by the system software to accomplish some |
|
46 |
goal. |
|
47 |
||
48 |
Often, the line of separation between system and application software is made |
|
49 |
at the privilege level change that happens in operating system traps. |
|
50 |
In |ns3| there is no real concept of operating system and especially |
|
51 |
no concept of privilege levels or system calls. We do, however, have the |
|
52 |
idea of an application. Just as software applications run on computers to |
|
53 |
perform tasks in the "real world," |ns3| applications run on |
|
54 |
|ns3| ``Nodes`` to drive simulations in the simulated world. |
|
55 |
||
56 |
In |ns3| the basic abstraction for a user program that generates some |
|
57 |
activity to be simulated is the application. This abstraction is represented |
|
58 |
in C++ by the class ``Application``. The ``Application`` class provides |
|
59 |
methods for managing the representations of our version of user-level |
|
60 |
applications in simulations. Developers are expected to specialize the |
|
61 |
``Application`` class in the object-oriented programming sense to create new |
|
62 |
applications. In this tutorial, we will use specializations of class |
|
63 |
``Application`` called ``UdpEchoClientApplication`` and |
|
64 |
``UdpEchoServerApplication``. As you might expect, these applications |
|
65 |
compose a client/server application set used to generate and echo simulated |
|
66 |
network packets |
|
67 |
||
68 |
Channel |
|
69 |
+++++++ |
|
70 |
||
71 |
In the real world, one can connect a computer to a network. Often the media |
|
72 |
over which data flows in these networks are called *channels*. When |
|
73 |
you connect your Ethernet cable to the plug in the wall, you are connecting |
|
74 |
your computer to an Ethernet communication channel. In the simulated world |
|
75 |
of |ns3|, one connects a ``Node`` to an object representing a |
|
76 |
communication channel. Here the basic communication subnetwork abstraction |
|
77 |
is called the channel and is represented in C++ by the class ``Channel``. |
|
78 |
||
79 |
The ``Channel`` class provides methods for managing communication |
|
80 |
subnetwork objects and connecting nodes to them. ``Channels`` may also be |
|
81 |
specialized by developers in the object oriented programming sense. A |
|
82 |
``Channel`` specialization may model something as simple as a wire. The |
|
83 |
specialized ``Channel`` can also model things as complicated as a large |
|
84 |
Ethernet switch, or three-dimensional space full of obstructions in the case |
|
85 |
of wireless networks. |
|
86 |
||
87 |
We will use specialized versions of the ``Channel`` called |
|
88 |
``CsmaChannel``, ``PointToPointChannel`` and ``WifiChannel`` in this |
|
89 |
tutorial. The ``CsmaChannel``, for example, models a version of a |
|
90 |
communication subnetwork that implements a *carrier sense multiple |
|
91 |
access* communication medium. This gives us Ethernet-like functionality. |
|
92 |
||
93 |
Net Device |
|
94 |
++++++++++ |
|
11221
243001f59f66
fix tutorial typos (found by Renan)
Tom Henderson <tomh@tomh.org>
parents:
10950
diff
changeset
|
95 |
It used to be the case that if you wanted to connect a computer to a network, |
6754 | 96 |
you had to buy a specific kind of network cable and a hardware device called |
97 |
(in PC terminology) a *peripheral card* that needed to be installed in |
|
98 |
your computer. If the peripheral card implemented some networking function, |
|
99 |
they were called Network Interface Cards, or *NICs*. Today most |
|
100 |
computers come with the network interface hardware built in and users don't |
|
101 |
see these building blocks. |
|
102 |
||
103 |
A NIC will not work without a software driver to control the hardware. In |
|
104 |
Unix (or Linux), a piece of peripheral hardware is classified as a |
|
105 |
*device*. Devices are controlled using *device drivers*, and network |
|
106 |
devices (NICs) are controlled using *network device drivers* |
|
107 |
collectively known as *net devices*. In Unix and Linux you refer |
|
108 |
to these net devices by names such as *eth0*. |
|
109 |
||
110 |
In |ns3| the *net device* abstraction covers both the software |
|
111 |
driver and the simulated hardware. A net device is "installed" in a |
|
112 |
``Node`` in order to enable the ``Node`` to communicate with other |
|
113 |
``Nodes`` in the simulation via ``Channels``. Just as in a real |
|
114 |
computer, a ``Node`` may be connected to more than one ``Channel`` via |
|
115 |
multiple ``NetDevices``. |
|
116 |
||
117 |
The net device abstraction is represented in C++ by the class ``NetDevice``. |
|
118 |
The ``NetDevice`` class provides methods for managing connections to |
|
119 |
``Node`` and ``Channel`` objects; and may be specialized by developers |
|
120 |
in the object-oriented programming sense. We will use the several specialized |
|
121 |
versions of the ``NetDevice`` called ``CsmaNetDevice``, |
|
122 |
``PointToPointNetDevice``, and ``WifiNetDevice`` in this tutorial. |
|
123 |
Just as an Ethernet NIC is designed to work with an Ethernet network, the |
|
124 |
``CsmaNetDevice`` is designed to work with a ``CsmaChannel``; the |
|
125 |
``PointToPointNetDevice`` is designed to work with a |
|
126 |
``PointToPointChannel`` and a ``WifiNetNevice`` is designed to work with |
|
127 |
a ``WifiChannel``. |
|
128 |
||
129 |
Topology Helpers |
|
130 |
++++++++++++++++ |
|
131 |
In a real network, you will find host computers with added (or built-in) |
|
132 |
NICs. In |ns3| we would say that you will find ``Nodes`` with |
|
133 |
attached ``NetDevices``. In a large simulated network you will need to |
|
134 |
arrange many connections between ``Nodes``, ``NetDevices`` and |
|
135 |
``Channels``. |
|
136 |
||
137 |
Since connecting ``NetDevices`` to ``Nodes``, ``NetDevices`` |
|
138 |
to ``Channels``, assigning IP addresses, etc., are such common tasks |
|
139 |
in |ns3|, we provide what we call *topology helpers* to make |
|
140 |
this as easy as possible. For example, it may take many distinct |
|
141 |
|ns3| core operations to create a NetDevice, add a MAC address, |
|
142 |
install that net device on a ``Node``, configure the node's protocol stack, |
|
143 |
and then connect the ``NetDevice`` to a ``Channel``. Even more |
|
144 |
operations would be required to connect multiple devices onto multipoint |
|
145 |
channels and then to connect individual networks together into internetworks. |
|
146 |
We provide topology helper objects that combine those many distinct operations |
|
147 |
into an easy to use model for your convenience. |
|
148 |
||
149 |
A First ns-3 Script |
|
150 |
******************* |
|
151 |
If you downloaded the system as was suggested above, you will have a release |
|
152 |
of |ns3| in a directory called ``repos`` under your home |
|
153 |
directory. Change into that release directory, and you should find a |
|
154 |
directory structure something like the following: |
|
155 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
156 |
.. sourcecode:: bash |
6754 | 157 |
|
7275 | 158 |
AUTHORS examples scratch utils waf.bat* |
159 |
bindings LICENSE src utils.py waf-tools |
|
160 |
build ns3 test.py* utils.pyc wscript |
|
161 |
CHANGES.html README testpy-output VERSION wutils.py |
|
162 |
doc RELEASE_NOTES testpy.supp waf* wutils.pyc |
|
163 |
||
6754 | 164 |
|
165 |
Change into the ``examples/tutorial`` directory. You should see a file named |
|
166 |
``first.cc`` located there. This is a script that will create a simple |
|
167 |
point-to-point link between two nodes and echo a single packet between the |
|
168 |
nodes. Let's take a look at that script line by line, so go ahead and open |
|
169 |
``first.cc`` in your favorite editor. |
|
170 |
||
171 |
Boilerplate |
|
172 |
+++++++++++ |
|
173 |
The first line in the file is an emacs mode line. This tells emacs about the |
|
174 |
formatting conventions (coding style) we use in our source code. |
|
175 |
||
176 |
:: |
|
177 |
||
178 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
|
179 |
||
180 |
This is always a somewhat controversial subject, so we might as well get it |
|
181 |
out of the way immediately. The |ns3| project, like most large |
|
182 |
projects, has adopted a coding style to which all contributed code must |
|
183 |
adhere. If you want to contribute your code to the project, you will |
|
184 |
eventually have to conform to the |ns3| coding standard as described |
|
185 |
in the file ``doc/codingstd.txt`` or shown on the project web page |
|
186 |
`here |
|
7867
87c579ca5690
fix broken tutorial link
Bruno Haick <bghaick@hotmail.com>
parents:
7479
diff
changeset
|
187 |
<http://www.nsnam.org/developers/contributing-code/coding-style/>`_. |
6754 | 188 |
|
189 |
We recommend that you, well, just get used to the look and feel of |ns3| |
|
190 |
code and adopt this standard whenever you are working with our code. All of |
|
191 |
the development team and contributors have done so with various amounts of |
|
192 |
grumbling. The emacs mode line above makes it easier to get the formatting |
|
193 |
correct if you use the emacs editor. |
|
194 |
||
195 |
The |ns3| simulator is licensed using the GNU General Public |
|
196 |
License. You will see the appropriate GNU legalese at the head of every file |
|
197 |
in the |ns3| distribution. Often you will see a copyright notice for |
|
198 |
one of the institutions involved in the |ns3| project above the GPL |
|
199 |
text and an author listed below. |
|
200 |
||
201 |
:: |
|
202 |
||
203 |
/* |
|
204 |
* This program is free software; you can redistribute it and/or modify |
|
205 |
* it under the terms of the GNU General Public License version 2 as |
|
206 |
* published by the Free Software Foundation; |
|
207 |
* |
|
208 |
* This program is distributed in the hope that it will be useful, |
|
209 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
210 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
211 |
* GNU General Public License for more details. |
|
212 |
* |
|
213 |
* You should have received a copy of the GNU General Public License |
|
214 |
* along with this program; if not, write to the Free Software |
|
215 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
216 |
*/ |
|
217 |
||
218 |
Module Includes |
|
219 |
+++++++++++++++ |
|
220 |
The code proper starts with a number of include statements. |
|
221 |
||
222 |
:: |
|
223 |
||
224 |
#include "ns3/core-module.h" |
|
7137
dbefbad7bee3
Fix module names in documentation
Mitch Watrous <watrous@u.washington.edu>
parents:
7025
diff
changeset
|
225 |
#include "ns3/network-module.h" |
dbefbad7bee3
Fix module names in documentation
Mitch Watrous <watrous@u.washington.edu>
parents:
7025
diff
changeset
|
226 |
#include "ns3/internet-module.h" |
dbefbad7bee3
Fix module names in documentation
Mitch Watrous <watrous@u.washington.edu>
parents:
7025
diff
changeset
|
227 |
#include "ns3/point-to-point-module.h" |
dbefbad7bee3
Fix module names in documentation
Mitch Watrous <watrous@u.washington.edu>
parents:
7025
diff
changeset
|
228 |
#include "ns3/applications-module.h" |
6754 | 229 |
|
230 |
To help our high-level script users deal with the large number of include |
|
231 |
files present in the system, we group includes according to relatively large |
|
232 |
modules. We provide a single include file that will recursively load all of |
|
233 |
the include files used in each module. Rather than having to look up exactly |
|
234 |
what header you need, and possibly have to get a number of dependencies right, |
|
235 |
we give you the ability to load a group of files at a large granularity. This |
|
236 |
is not the most efficient approach but it certainly makes writing scripts much |
|
237 |
easier. |
|
238 |
||
239 |
Each of the |ns3| include files is placed in a directory called |
|
240 |
``ns3`` (under the build directory) during the build process to help avoid |
|
241 |
include file name collisions. The ``ns3/core-module.h`` file corresponds |
|
242 |
to the ns-3 module you will find in the directory ``src/core`` in your |
|
243 |
downloaded release distribution. If you list this directory you will find a |
|
244 |
large number of header files. When you do a build, Waf will place public |
|
245 |
header files in an ``ns3`` directory under the appropriate |
|
246 |
``build/debug`` or ``build/optimized`` directory depending on your |
|
247 |
configuration. Waf will also automatically generate a module include file to |
|
248 |
load all of the public header files. |
|
249 |
||
250 |
Since you are, of course, following this tutorial religiously, you will |
|
251 |
already have done a |
|
252 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
253 |
.. sourcecode:: bash |
6754 | 254 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
255 |
$ ./waf -d debug --enable-examples --enable-tests configure |
6754 | 256 |
|
6998
1c2b8cfb71d2
Make tests not be built by default
Mitch Watrous <watrous@u.washington.edu>
parents:
6754
diff
changeset
|
257 |
in order to configure the project to perform debug builds that include |
7024
4392d52b3536
Make examples not be built by default
Mitch Watrous <watrous@u.washington.edu>
parents:
6998
diff
changeset
|
258 |
examples and tests. You will also have done a |
6754 | 259 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
260 |
.. sourcecode:: bash |
6754 | 261 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
262 |
$ ./waf |
6754 | 263 |
|
264 |
to build the project. So now if you look in the directory |
|
265 |
``../../build/debug/ns3`` you will find the four module include files shown |
|
266 |
above. You can take a look at the contents of these files and find that they |
|
267 |
do include all of the public include files in their respective modules. |
|
268 |
||
269 |
Ns3 Namespace |
|
270 |
+++++++++++++ |
|
271 |
The next line in the ``first.cc`` script is a namespace declaration. |
|
272 |
||
273 |
:: |
|
274 |
||
275 |
using namespace ns3; |
|
276 |
||
277 |
The |ns3| project is implemented in a C++ namespace called |
|
278 |
``ns3``. This groups all |ns3|-related declarations in a scope |
|
279 |
outside the global namespace, which we hope will help with integration with |
|
280 |
other code. The C++ ``using`` statement introduces the |ns3| |
|
281 |
namespace into the current (global) declarative region. This is a fancy way |
|
282 |
of saying that after this declaration, you will not have to type ``ns3::`` |
|
283 |
scope resolution operator before all of the |ns3| code in order to use |
|
284 |
it. If you are unfamiliar with namespaces, please consult almost any C++ |
|
285 |
tutorial and compare the ``ns3`` namespace and usage here with instances of |
|
286 |
the ``std`` namespace and the ``using namespace std;`` statements you |
|
287 |
will often find in discussions of ``cout`` and streams. |
|
288 |
||
289 |
Logging |
|
290 |
+++++++ |
|
291 |
The next line of the script is the following, |
|
292 |
||
293 |
:: |
|
294 |
||
295 |
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample"); |
|
296 |
||
297 |
We will use this statement as a convenient place to talk about our Doxygen |
|
298 |
documentation system. If you look at the project web site, |
|
299 |
`ns-3 project |
|
7479
0ff4a85b9e04
Fix references to old website in Tutorial
Mitch Watrous <watrous@u.washington.edu>
parents:
7275
diff
changeset
|
300 |
<http://www.nsnam.org>`_, you will find a link to "Documentation" in the navigation bar. If you select this link, you will be |
0ff4a85b9e04
Fix references to old website in Tutorial
Mitch Watrous <watrous@u.washington.edu>
parents:
7275
diff
changeset
|
301 |
taken to our documentation page. There |
0ff4a85b9e04
Fix references to old website in Tutorial
Mitch Watrous <watrous@u.washington.edu>
parents:
7275
diff
changeset
|
302 |
is a link to "Latest Release" that will take you to the documentation |
6754 | 303 |
for the latest stable release of |ns3|. |
7479
0ff4a85b9e04
Fix references to old website in Tutorial
Mitch Watrous <watrous@u.washington.edu>
parents:
7275
diff
changeset
|
304 |
If you select the "API Documentation" link, you will be |
0ff4a85b9e04
Fix references to old website in Tutorial
Mitch Watrous <watrous@u.washington.edu>
parents:
7275
diff
changeset
|
305 |
taken to the |ns3| API documentation page. |
6754 | 306 |
|
307 |
Along the left side, you will find a graphical representation of the structure |
|
308 |
of the documentation. A good place to start is the ``NS-3 Modules`` |
|
309 |
"book" in the |ns3| navigation tree. If you expand ``Modules`` |
|
310 |
you will see a list of |ns3| module documentation. The concept of |
|
11681
d0b2d73051f7
update some stale tutorial info
Tom Henderson <tomh@tomh.org>
parents:
11221
diff
changeset
|
311 |
module here ties directly into the module include files discussed above. The |ns3| logging subsystem is discussed in the :ref:`UsingLogging` section, so |
d0b2d73051f7
update some stale tutorial info
Tom Henderson <tomh@tomh.org>
parents:
11221
diff
changeset
|
312 |
we'll get to it later in this tutorial, but you can find out about the above |
d0b2d73051f7
update some stale tutorial info
Tom Henderson <tomh@tomh.org>
parents:
11221
diff
changeset
|
313 |
statement by looking at the ``Core`` module, then expanding the |
d0b2d73051f7
update some stale tutorial info
Tom Henderson <tomh@tomh.org>
parents:
11221
diff
changeset
|
314 |
``Debugging tools`` book, and then selecting the ``Logging`` page. Click |
d0b2d73051f7
update some stale tutorial info
Tom Henderson <tomh@tomh.org>
parents:
11221
diff
changeset
|
315 |
on ``Logging``. |
6754 | 316 |
|
317 |
You should now be looking at the Doxygen documentation for the Logging module. |
|
11681
d0b2d73051f7
update some stale tutorial info
Tom Henderson <tomh@tomh.org>
parents:
11221
diff
changeset
|
318 |
In the list of ``Macros``'s at the top of the page you will see the entry |
6754 | 319 |
for ``NS_LOG_COMPONENT_DEFINE``. Before jumping in, it would probably be |
320 |
good to look for the "Detailed Description" of the logging module to get a |
|
321 |
feel for the overall operation. You can either scroll down or select the |
|
322 |
"More..." link under the collaboration diagram to do this. |
|
323 |
||
324 |
Once you have a general idea of what is going on, go ahead and take a look at |
|
325 |
the specific ``NS_LOG_COMPONENT_DEFINE`` documentation. I won't duplicate |
|
326 |
the documentation here, but to summarize, this line declares a logging |
|
327 |
component called ``FirstScriptExample`` that allows you to enable and |
|
328 |
disable console message logging by reference to the name. |
|
329 |
||
330 |
Main Function |
|
331 |
+++++++++++++ |
|
332 |
The next lines of the script you will find are, |
|
333 |
||
334 |
:: |
|
335 |
||
336 |
int |
|
337 |
main (int argc, char *argv[]) |
|
338 |
{ |
|
339 |
||
340 |
This is just the declaration of the main function of your program (script). |
|
341 |
Just as in any C++ program, you need to define a main function that will be |
|
342 |
the first function run. There is nothing at all special here. Your |
|
343 |
|ns3| script is just a C++ program. |
|
344 |
||
10171
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
345 |
The next line sets the time resolution to one nanosecond, which happens |
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
346 |
to be the default value: |
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
347 |
|
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
348 |
:: |
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
349 |
|
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
350 |
Time::SetResolution (Time::NS); |
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
351 |
|
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
352 |
The resolution is the smallest time value that can be represented (as well as |
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
353 |
the smallest representable difference between two time values). |
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
354 |
You can change the resolution exactly once. The mechanism enabling this |
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
355 |
flexibility is somewhat memory hungry, so once the resolution has been |
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
356 |
set explicitly we release the memory, preventing further updates. (If |
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
357 |
you don't set the resolution explicitly, it will default to one nanosecond, |
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
358 |
and the memory will be released when the simulation starts.) |
0b3ce943b7c8
bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
359 |
|
6754 | 360 |
The next two lines of the script are used to enable two logging components that |
361 |
are built into the Echo Client and Echo Server applications: |
|
362 |
||
363 |
:: |
|
364 |
||
365 |
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO); |
|
366 |
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO); |
|
367 |
||
368 |
If you have read over the Logging component documentation you will have seen |
|
369 |
that there are a number of levels of logging verbosity/detail that you can |
|
370 |
enable on each component. These two lines of code enable debug logging at the |
|
371 |
INFO level for echo clients and servers. This will result in the application |
|
372 |
printing out messages as packets are sent and received during the simulation. |
|
373 |
||
374 |
Now we will get directly to the business of creating a topology and running |
|
375 |
a simulation. We use the topology helper objects to make this job as |
|
376 |
easy as possible. |
|
377 |
||
378 |
Topology Helpers |
|
379 |
++++++++++++++++ |
|
380 |
NodeContainer |
|
381 |
~~~~~~~~~~~~~ |
|
382 |
The next two lines of code in our script will actually create the |
|
383 |
|ns3| ``Node`` objects that will represent the computers in the |
|
384 |
simulation. |
|
385 |
||
386 |
:: |
|
387 |
||
388 |
NodeContainer nodes; |
|
389 |
nodes.Create (2); |
|
390 |
||
391 |
Let's find the documentation for the ``NodeContainer`` class before we |
|
392 |
continue. Another way to get into the documentation for a given class is via |
|
393 |
the ``Classes`` tab in the Doxygen pages. If you still have the Doxygen |
|
394 |
handy, just scroll up to the top of the page and select the ``Classes`` |
|
395 |
tab. You should see a new set of tabs appear, one of which is |
|
396 |
``Class List``. Under that tab you will see a list of all of the |
|
397 |
|ns3| classes. Scroll down, looking for ``ns3::NodeContainer``. |
|
398 |
When you find the class, go ahead and select it to go to the documentation for |
|
399 |
the class. |
|
400 |
||
401 |
You may recall that one of our key abstractions is the ``Node``. This |
|
402 |
represents a computer to which we are going to add things like protocol stacks, |
|
403 |
applications and peripheral cards. The ``NodeContainer`` topology helper |
|
404 |
provides a convenient way to create, manage and access any ``Node`` objects |
|
405 |
that we create in order to run a simulation. The first line above just |
|
406 |
declares a NodeContainer which we call ``nodes``. The second line calls the |
|
407 |
``Create`` method on the ``nodes`` object and asks the container to |
|
408 |
create two nodes. As described in the Doxygen, the container calls down into |
|
409 |
the |ns3| system proper to create two ``Node`` objects and stores |
|
410 |
pointers to those objects internally. |
|
411 |
||
412 |
The nodes as they stand in the script do nothing. The next step in |
|
413 |
constructing a topology is to connect our nodes together into a network. |
|
414 |
The simplest form of network we support is a single point-to-point link |
|
415 |
between two nodes. We'll construct one of those links here. |
|
416 |
||
417 |
PointToPointHelper |
|
418 |
~~~~~~~~~~~~~~~~~~ |
|
419 |
We are constructing a point to point link, and, in a pattern which will become |
|
420 |
quite familiar to you, we use a topology helper object to do the low-level |
|
421 |
work required to put the link together. Recall that two of our key |
|
422 |
abstractions are the ``NetDevice`` and the ``Channel``. In the real |
|
423 |
world, these terms correspond roughly to peripheral cards and network cables. |
|
424 |
Typically these two things are intimately tied together and one cannot expect |
|
425 |
to interchange, for example, Ethernet devices and wireless channels. Our |
|
426 |
Topology Helpers follow this intimate coupling and therefore you will use a |
|
427 |
single ``PointToPointHelper`` to configure and connect |ns3| |
|
428 |
``PointToPointNetDevice`` and ``PointToPointChannel`` objects in this |
|
429 |
script. |
|
430 |
||
431 |
The next three lines in the script are, |
|
432 |
||
433 |
:: |
|
434 |
||
435 |
PointToPointHelper pointToPoint; |
|
436 |
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); |
|
437 |
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); |
|
438 |
||
439 |
The first line, |
|
440 |
||
441 |
:: |
|
442 |
||
443 |
PointToPointHelper pointToPoint; |
|
444 |
||
445 |
instantiates a ``PointToPointHelper`` object on the stack. From a |
|
446 |
high-level perspective the next line, |
|
447 |
||
448 |
:: |
|
449 |
||
450 |
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); |
|
451 |
||
452 |
tells the ``PointToPointHelper`` object to use the value "5Mbps" |
|
453 |
(five megabits per second) as the "DataRate" when it creates a |
|
454 |
``PointToPointNetDevice`` object. |
|
455 |
||
456 |
From a more detailed perspective, the string "DataRate" corresponds |
|
457 |
to what we call an ``Attribute`` of the ``PointToPointNetDevice``. |
|
458 |
If you look at the Doxygen for class ``ns3::PointToPointNetDevice`` and |
|
459 |
find the documentation for the ``GetTypeId`` method, you will find a list |
|
460 |
of ``Attributes`` defined for the device. Among these is the "DataRate" |
|
461 |
``Attribute``. Most user-visible |ns3| objects have similar lists of |
|
462 |
``Attributes``. We use this mechanism to easily configure simulations without |
|
463 |
recompiling as you will see in a following section. |
|
464 |
||
465 |
Similar to the "DataRate" on the ``PointToPointNetDevice`` you will find a |
|
466 |
"Delay" ``Attribute`` associated with the ``PointToPointChannel``. The |
|
467 |
final line, |
|
468 |
||
469 |
:: |
|
470 |
||
471 |
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); |
|
472 |
||
473 |
tells the ``PointToPointHelper`` to use the value "2ms" (two milliseconds) |
|
474 |
as the value of the transmission delay of every point to point channel it |
|
475 |
subsequently creates. |
|
476 |
||
477 |
NetDeviceContainer |
|
478 |
~~~~~~~~~~~~~~~~~~ |
|
479 |
At this point in the script, we have a ``NodeContainer`` that contains |
|
480 |
two nodes. We have a ``PointToPointHelper`` that is primed and ready to |
|
481 |
make ``PointToPointNetDevices`` and wire ``PointToPointChannel`` objects |
|
482 |
between them. Just as we used the ``NodeContainer`` topology helper object |
|
483 |
to create the ``Nodes`` for our simulation, we will ask the |
|
484 |
``PointToPointHelper`` to do the work involved in creating, configuring and |
|
485 |
installing our devices for us. We will need to have a list of all of the |
|
486 |
NetDevice objects that are created, so we use a NetDeviceContainer to hold |
|
487 |
them just as we used a NodeContainer to hold the nodes we created. The |
|
488 |
following two lines of code, |
|
489 |
||
490 |
:: |
|
491 |
||
492 |
NetDeviceContainer devices; |
|
493 |
devices = pointToPoint.Install (nodes); |
|
494 |
||
495 |
will finish configuring the devices and channel. The first line declares the |
|
496 |
device container mentioned above and the second does the heavy lifting. The |
|
497 |
``Install`` method of the ``PointToPointHelper`` takes a |
|
498 |
``NodeContainer`` as a parameter. Internally, a ``NetDeviceContainer`` |
|
499 |
is created. For each node in the ``NodeContainer`` (there must be exactly |
|
500 |
two for a point-to-point link) a ``PointToPointNetDevice`` is created and |
|
501 |
saved in the device container. A ``PointToPointChannel`` is created and |
|
502 |
the two ``PointToPointNetDevices`` are attached. When objects are created |
|
503 |
by the ``PointToPointHelper``, the ``Attributes`` previously set in the |
|
504 |
helper are used to initialize the corresponding ``Attributes`` in the |
|
505 |
created objects. |
|
506 |
||
507 |
After executing the ``pointToPoint.Install (nodes)`` call we will have |
|
508 |
two nodes, each with an installed point-to-point net device and a single |
|
509 |
point-to-point channel between them. Both devices will be configured to |
|
510 |
transmit data at five megabits per second over the channel which has a two |
|
511 |
millisecond transmission delay. |
|
512 |
||
513 |
InternetStackHelper |
|
514 |
~~~~~~~~~~~~~~~~~~~ |
|
515 |
We now have nodes and devices configured, but we don't have any protocol stacks |
|
516 |
installed on our nodes. The next two lines of code will take care of that. |
|
517 |
||
518 |
:: |
|
519 |
||
520 |
InternetStackHelper stack; |
|
521 |
stack.Install (nodes); |
|
522 |
||
523 |
The ``InternetStackHelper`` is a topology helper that is to internet stacks |
|
524 |
what the ``PointToPointHelper`` is to point-to-point net devices. The |
|
525 |
``Install`` method takes a ``NodeContainer`` as a parameter. When it is |
|
526 |
executed, it will install an Internet Stack (TCP, UDP, IP, etc.) on each of |
|
527 |
the nodes in the node container. |
|
528 |
||
529 |
Ipv4AddressHelper |
|
530 |
~~~~~~~~~~~~~~~~~ |
|
531 |
Next we need to associate the devices on our nodes with IP addresses. We |
|
532 |
provide a topology helper to manage the allocation of IP addresses. The only |
|
533 |
user-visible API is to set the base IP address and network mask to use when |
|
534 |
performing the actual address allocation (which is done at a lower level |
|
535 |
inside the helper). |
|
536 |
||
537 |
The next two lines of code in our example script, ``first.cc``, |
|
538 |
||
539 |
:: |
|
540 |
||
541 |
Ipv4AddressHelper address; |
|
542 |
address.SetBase ("10.1.1.0", "255.255.255.0"); |
|
543 |
||
544 |
declare an address helper object and tell it that it should begin allocating IP |
|
545 |
addresses from the network 10.1.1.0 using the mask 255.255.255.0 to define |
|
546 |
the allocatable bits. By default the addresses allocated will start at one |
|
547 |
and increase monotonically, so the first address allocated from this base will |
|
548 |
be 10.1.1.1, followed by 10.1.1.2, etc. The low level |ns3| system |
|
549 |
actually remembers all of the IP addresses allocated and will generate a |
|
550 |
fatal error if you accidentally cause the same address to be generated twice |
|
551 |
(which is a very hard to debug error, by the way). |
|
552 |
||
553 |
The next line of code, |
|
554 |
||
555 |
:: |
|
556 |
||
557 |
Ipv4InterfaceContainer interfaces = address.Assign (devices); |
|
558 |
||
559 |
performs the actual address assignment. In |ns3| we make the |
|
560 |
association between an IP address and a device using an ``Ipv4Interface`` |
|
561 |
object. Just as we sometimes need a list of net devices created by a helper |
|
562 |
for future reference we sometimes need a list of ``Ipv4Interface`` objects. |
|
563 |
The ``Ipv4InterfaceContainer`` provides this functionality. |
|
564 |
||
565 |
Now we have a point-to-point network built, with stacks installed and IP |
|
566 |
addresses assigned. What we need at this point are applications to generate |
|
567 |
traffic. |
|
568 |
||
569 |
Applications |
|
570 |
++++++++++++ |
|
571 |
Another one of the core abstractions of the ns-3 system is the |
|
572 |
``Application``. In this script we use two specializations of the core |
|
573 |
|ns3| class ``Application`` called ``UdpEchoServerApplication`` |
|
574 |
and ``UdpEchoClientApplication``. Just as we have in our previous |
|
575 |
explanations, we use helper objects to help configure and manage the |
|
576 |
underlying objects. Here, we use ``UdpEchoServerHelper`` and |
|
577 |
``UdpEchoClientHelper`` objects to make our lives easier. |
|
578 |
||
579 |
UdpEchoServerHelper |
|
580 |
~~~~~~~~~~~~~~~~~~~ |
|
581 |
The following lines of code in our example script, ``first.cc``, are used |
|
582 |
to set up a UDP echo server application on one of the nodes we have previously |
|
583 |
created. |
|
584 |
||
585 |
:: |
|
586 |
||
587 |
UdpEchoServerHelper echoServer (9); |
|
588 |
||
589 |
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1)); |
|
590 |
serverApps.Start (Seconds (1.0)); |
|
591 |
serverApps.Stop (Seconds (10.0)); |
|
592 |
||
593 |
The first line of code in the above snippet declares the |
|
594 |
``UdpEchoServerHelper``. As usual, this isn't the application itself, it |
|
595 |
is an object used to help us create the actual applications. One of our |
|
596 |
conventions is to place *required* ``Attributes`` in the helper constructor. |
|
597 |
In this case, the helper can't do anything useful unless it is provided with |
|
598 |
a port number that the client also knows about. Rather than just picking one |
|
599 |
and hoping it all works out, we require the port number as a parameter to the |
|
600 |
constructor. The constructor, in turn, simply does a ``SetAttribute`` |
|
601 |
with the passed value. If you want, you can set the "Port" ``Attribute`` |
|
602 |
to another value later using ``SetAttribute``. |
|
603 |
||
604 |
Similar to many other helper objects, the ``UdpEchoServerHelper`` object |
|
605 |
has an ``Install`` method. It is the execution of this method that actually |
|
606 |
causes the underlying echo server application to be instantiated and attached |
|
607 |
to a node. Interestingly, the ``Install`` method takes a |
|
608 |
``NodeContainter`` as a parameter just as the other ``Install`` methods |
|
609 |
we have seen. This is actually what is passed to the method even though it |
|
610 |
doesn't look so in this case. There is a C++ *implicit conversion* at |
|
611 |
work here that takes the result of ``nodes.Get (1)`` (which returns a smart |
|
612 |
pointer to a node object --- ``Ptr<Node>``) and uses that in a constructor |
|
613 |
for an unnamed ``NodeContainer`` that is then passed to ``Install``. |
|
614 |
If you are ever at a loss to find a particular method signature in C++ code |
|
615 |
that compiles and runs just fine, look for these kinds of implicit conversions. |
|
616 |
||
617 |
We now see that ``echoServer.Install`` is going to install a |
|
618 |
``UdpEchoServerApplication`` on the node found at index number one of the |
|
619 |
``NodeContainer`` we used to manage our nodes. ``Install`` will return |
|
620 |
a container that holds pointers to all of the applications (one in this case |
|
621 |
since we passed a ``NodeContainer`` containing one node) created by the |
|
622 |
helper. |
|
623 |
||
624 |
Applications require a time to "start" generating traffic and may take an |
|
625 |
optional time to "stop". We provide both. These times are set using the |
|
626 |
``ApplicationContainer`` methods ``Start`` and ``Stop``. These |
|
627 |
methods take ``Time`` parameters. In this case, we use an *explicit* |
|
628 |
C++ conversion sequence to take the C++ double 1.0 and convert it to an |
|
629 |
|ns3| ``Time`` object using a ``Seconds`` cast. Be aware that |
|
630 |
the conversion rules may be controlled by the model author, and C++ has its |
|
631 |
own rules, so you can't always just assume that parameters will be happily |
|
632 |
converted for you. The two lines, |
|
633 |
||
634 |
:: |
|
635 |
||
636 |
serverApps.Start (Seconds (1.0)); |
|
637 |
serverApps.Stop (Seconds (10.0)); |
|
638 |
||
639 |
will cause the echo server application to ``Start`` (enable itself) at one |
|
640 |
second into the simulation and to ``Stop`` (disable itself) at ten seconds |
|
641 |
into the simulation. By virtue of the fact that we have declared a simulation |
|
642 |
event (the application stop event) to be executed at ten seconds, the simulation |
|
643 |
will last *at least* ten seconds. |
|
644 |
||
645 |
UdpEchoClientHelper |
|
646 |
~~~~~~~~~~~~~~~~~~~ |
|
647 |
||
648 |
The echo client application is set up in a method substantially similar to |
|
649 |
that for the server. There is an underlying ``UdpEchoClientApplication`` |
|
650 |
that is managed by an ``UdpEchoClientHelper``. |
|
651 |
||
652 |
:: |
|
653 |
||
654 |
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9); |
|
655 |
echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); |
|
9112
ede7a74e7411
bug 1514: make TimeValue usage consistent in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
7867
diff
changeset
|
656 |
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); |
6754 | 657 |
echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); |
658 |
||
659 |
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0)); |
|
660 |
clientApps.Start (Seconds (2.0)); |
|
661 |
clientApps.Stop (Seconds (10.0)); |
|
662 |
||
663 |
For the echo client, however, we need to set five different ``Attributes``. |
|
664 |
The first two ``Attributes`` are set during construction of the |
|
665 |
``UdpEchoClientHelper``. We pass parameters that are used (internally to |
|
666 |
the helper) to set the "RemoteAddress" and "RemotePort" ``Attributes`` |
|
667 |
in accordance with our convention to make required ``Attributes`` parameters |
|
668 |
in the helper constructors. |
|
669 |
||
670 |
Recall that we used an ``Ipv4InterfaceContainer`` to keep track of the IP |
|
671 |
addresses we assigned to our devices. The zeroth interface in the |
|
672 |
``interfaces`` container is going to correspond to the IP address of the |
|
673 |
zeroth node in the ``nodes`` container. The first interface in the |
|
674 |
``interfaces`` container corresponds to the IP address of the first node |
|
675 |
in the ``nodes`` container. So, in the first line of code (from above), we |
|
676 |
are creating the helper and telling it so set the remote address of the client |
|
677 |
to be the IP address assigned to the node on which the server resides. We |
|
678 |
also tell it to arrange to send packets to port nine. |
|
679 |
||
680 |
The "MaxPackets" ``Attribute`` tells the client the maximum number of |
|
681 |
packets we allow it to send during the simulation. The "Interval" |
|
682 |
``Attribute`` tells the client how long to wait between packets, and the |
|
683 |
"PacketSize" ``Attribute`` tells the client how large its packet payloads |
|
684 |
should be. With this particular combination of ``Attributes``, we are |
|
685 |
telling the client to send one 1024-byte packet. |
|
686 |
||
687 |
Just as in the case of the echo server, we tell the echo client to ``Start`` |
|
688 |
and ``Stop``, but here we start the client one second after the server is |
|
689 |
enabled (at two seconds into the simulation). |
|
690 |
||
691 |
Simulator |
|
692 |
+++++++++ |
|
693 |
What we need to do at this point is to actually run the simulation. This is |
|
694 |
done using the global function ``Simulator::Run``. |
|
695 |
||
696 |
:: |
|
697 |
||
698 |
Simulator::Run (); |
|
699 |
||
700 |
When we previously called the methods, |
|
701 |
||
702 |
:: |
|
703 |
||
704 |
serverApps.Start (Seconds (1.0)); |
|
705 |
serverApps.Stop (Seconds (10.0)); |
|
706 |
... |
|
707 |
clientApps.Start (Seconds (2.0)); |
|
708 |
clientApps.Stop (Seconds (10.0)); |
|
709 |
||
710 |
we actually scheduled events in the simulator at 1.0 seconds, 2.0 seconds and |
|
711 |
two events at 10.0 seconds. When ``Simulator::Run`` is called, the system |
|
712 |
will begin looking through the list of scheduled events and executing them. |
|
713 |
First it will run the event at 1.0 seconds, which will enable the echo server |
|
714 |
application (this event may, in turn, schedule many other events). Then it |
|
715 |
will run the event scheduled for t=2.0 seconds which will start the echo client |
|
716 |
application. Again, this event may schedule many more events. The start event |
|
717 |
implementation in the echo client application will begin the data transfer phase |
|
718 |
of the simulation by sending a packet to the server. |
|
719 |
||
720 |
The act of sending the packet to the server will trigger a chain of events |
|
721 |
that will be automatically scheduled behind the scenes and which will perform |
|
722 |
the mechanics of the packet echo according to the various timing parameters |
|
723 |
that we have set in the script. |
|
724 |
||
725 |
Eventually, since we only send one packet (recall the ``MaxPackets`` |
|
726 |
``Attribute`` was set to one), the chain of events triggered by |
|
727 |
that single client echo request will taper off and the simulation will go |
|
728 |
idle. Once this happens, the remaining events will be the ``Stop`` events |
|
729 |
for the server and the client. When these events are executed, there are |
|
730 |
no further events to process and ``Simulator::Run`` returns. The simulation |
|
731 |
is then complete. |
|
732 |
||
733 |
All that remains is to clean up. This is done by calling the global function |
|
734 |
``Simulator::Destroy``. As the helper functions (or low level |
|
735 |
|ns3| code) executed, they arranged it so that hooks were inserted in |
|
736 |
the simulator to destroy all of the objects that were created. You did not |
|
737 |
have to keep track of any of these objects yourself --- all you had to do |
|
738 |
was to call ``Simulator::Destroy`` and exit. The |ns3| system |
|
739 |
took care of the hard part for you. The remaining lines of our first |
|
740 |
|ns3| script, ``first.cc``, do just that: |
|
741 |
||
742 |
:: |
|
743 |
||
744 |
Simulator::Destroy (); |
|
745 |
return 0; |
|
746 |
} |
|
747 |
||
10950
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
748 |
When the simulator will stop? |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
749 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
750 |
|
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
751 |
|ns3| is a Discrete Event (DE) simulator. In such a simulator, each event is |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
752 |
associated with its execution time, and the simulation proceeds by executing |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
753 |
events in the temporal order of simulation time. Events may cause future |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
754 |
events to be scheduled (for example, a timer may reschedule itself to |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
755 |
expire at the next interval). |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
756 |
|
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
757 |
The initial events are usually triggered by each object, e.g., IPv6 will |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
758 |
schedule Router Advertisements, Neighbor Solicitations, etc., |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
759 |
an Application schedule the first packet sending event, etc. |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
760 |
|
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
761 |
When an event is processed, it may generate zero, one or more events. |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
762 |
As a simulation executes, events are consumed, but more events may (or may |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
763 |
not) be generated. |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
764 |
The simulation will stop automatically when no further events are in the |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
765 |
event queue, or when a special Stop event is found. The Stop event is |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
766 |
created through the |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
767 |
``Simulator::Stop (stopTime);`` function. |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
768 |
|
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
769 |
There is a typical case where ``Simulator::Stop`` is absolutely necessary |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
770 |
to stop the simulation: when there is a self-sustaining event. |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
771 |
Self-sustaining (or recurring) events are events that always reschedule |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
772 |
themselves. As a consequence, they always keep the event queue non-empty. |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
773 |
|
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
774 |
There are many protocols and modules containing recurring events, e.g.: |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
775 |
|
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
776 |
* FlowMonitor - periodic check for lost packets |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
777 |
* RIPng - periodic broadcast of routing tables update |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
778 |
* etc. |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
779 |
|
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
780 |
In these cases, ``Simulator::Stop`` is necessary to gracefully stop the |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
781 |
simulation. In addition, when |ns3| is in emulation mode, the |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
782 |
``RealtimeSimulator`` is used to keep the simulation clock aligned with |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
783 |
the machine clock, and ``Simulator::Stop`` is necessary to stop the |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
784 |
process. |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
785 |
|
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
786 |
Many of the simulation programs in the tutorial do not explicitly call |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
787 |
``Simulator::Stop``, since the event queue will automatically run out |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
788 |
of events. However, these programs will also accept a call to |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
789 |
``Simulator::Stop``. For example, the following additional statement |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
790 |
in the first example program will schedule an explicit stop at 11 seconds: |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
791 |
|
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
792 |
:: |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
793 |
|
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
794 |
+ Simulator::Stop (Seconds (11.0)); |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
795 |
Simulator::Run (); |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
796 |
Simulator::Destroy (); |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
797 |
return 0; |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
798 |
} |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
799 |
|
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
800 |
The above wil not actually change the behavior of this program, since |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
801 |
this particular simulation naturally ends after 10 seconds. But if you |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
802 |
were to change the stop time in the above statement from 11 seconds to 1 |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
803 |
second, you would notice that the simulation stops before any output is |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
804 |
printed to the screen (since the output occurs around time 2 seconds of |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
805 |
simulation time). |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
806 |
|
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
807 |
It is important to call ``Simulator::Stop`` *before* calling |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
808 |
``Simulator::Run``; otherwise, ``Simulator::Run`` may never return control |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
809 |
to the main program to execute the stop! |
9a28fac0260b
update tutorial to describe Simulator::Stop
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10171
diff
changeset
|
810 |
|
6754 | 811 |
Building Your Script |
812 |
++++++++++++++++++++ |
|
813 |
We have made it trivial to build your simple scripts. All you have to do is |
|
814 |
to drop your script into the scratch directory and it will automatically be |
|
815 |
built if you run Waf. Let's try it. Copy ``examples/tutorial/first.cc`` into |
|
816 |
the ``scratch`` directory after changing back into the top level directory. |
|
817 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
818 |
.. sourcecode:: bash |
6754 | 819 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
820 |
$ cd ../.. |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
821 |
$ cp examples/tutorial/first.cc scratch/myfirst.cc |
6754 | 822 |
|
823 |
Now build your first example script using waf: |
|
824 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
825 |
.. sourcecode:: bash |
6754 | 826 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
827 |
$ ./waf |
6754 | 828 |
|
829 |
You should see messages reporting that your ``myfirst`` example was built |
|
830 |
successfully. |
|
831 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
832 |
.. sourcecode:: bash |
6754 | 833 |
|
834 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
835 |
[614/708] cxx: scratch/myfirst.cc -> build/debug/scratch/myfirst_3.o |
|
836 |
[706/708] cxx_link: build/debug/scratch/myfirst_3.o -> build/debug/scratch/myfirst |
|
837 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
838 |
'build' finished successfully (2.357s) |
|
839 |
||
840 |
You can now run the example (note that if you build your program in the scratch |
|
841 |
directory you must run it out of the scratch directory): |
|
842 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
843 |
.. sourcecode:: bash |
6754 | 844 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
845 |
$ ./waf --run scratch/myfirst |
6754 | 846 |
|
847 |
You should see some output: |
|
848 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
849 |
.. sourcecode:: bash |
6754 | 850 |
|
851 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
852 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
853 |
'build' finished successfully (0.418s) |
|
854 |
Sent 1024 bytes to 10.1.1.2 |
|
855 |
Received 1024 bytes from 10.1.1.1 |
|
856 |
Received 1024 bytes from 10.1.1.2 |
|
857 |
||
858 |
Here you see that the build system checks to make sure that the file has been |
|
859 |
build and then runs it. You see the logging component on the echo client |
|
860 |
indicate that it has sent one 1024 byte packet to the Echo Server on |
|
861 |
10.1.1.2. You also see the logging component on the echo server say that |
|
862 |
it has received the 1024 bytes from 10.1.1.1. The echo server silently |
|
863 |
echoes the packet and you see the echo client log that it has received its |
|
864 |
packet back from the server. |
|
865 |
||
866 |
Ns-3 Source Code |
|
867 |
**************** |
|
868 |
||
869 |
Now that you have used some of the |ns3| helpers you may want to |
|
870 |
have a look at some of the source code that implements that functionality. |
|
871 |
The most recent code can be browsed on our web server at the following link: |
|
872 |
http://code.nsnam.org/ns-3-dev. There, you will see the Mercurial |
|
873 |
summary page for our |ns3| development tree. |
|
874 |
||
875 |
At the top of the page, you will see a number of links, |
|
876 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
877 |
.. sourcecode:: text |
6754 | 878 |
|
879 |
summary | shortlog | changelog | graph | tags | files |
|
880 |
||
881 |
Go ahead and select the ``files`` link. This is what the top-level of |
|
882 |
most of our *repositories* will look: |
|
883 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9195
diff
changeset
|
884 |
.. sourcecode:: text |
6754 | 885 |
|
886 |
drwxr-xr-x [up] |
|
887 |
drwxr-xr-x bindings python files |
|
888 |
drwxr-xr-x doc files |
|
889 |
drwxr-xr-x examples files |
|
890 |
drwxr-xr-x ns3 files |
|
891 |
drwxr-xr-x scratch files |
|
892 |
drwxr-xr-x src files |
|
893 |
drwxr-xr-x utils files |
|
894 |
-rw-r--r-- 2009-07-01 12:47 +0200 560 .hgignore file | revisions | annotate |
|
895 |
-rw-r--r-- 2009-07-01 12:47 +0200 1886 .hgtags file | revisions | annotate |
|
896 |
-rw-r--r-- 2009-07-01 12:47 +0200 1276 AUTHORS file | revisions | annotate |
|
897 |
-rw-r--r-- 2009-07-01 12:47 +0200 30961 CHANGES.html file | revisions | annotate |
|
898 |
-rw-r--r-- 2009-07-01 12:47 +0200 17987 LICENSE file | revisions | annotate |
|
899 |
-rw-r--r-- 2009-07-01 12:47 +0200 3742 README file | revisions | annotate |
|
900 |
-rw-r--r-- 2009-07-01 12:47 +0200 16171 RELEASE_NOTES file | revisions | annotate |
|
901 |
-rw-r--r-- 2009-07-01 12:47 +0200 6 VERSION file | revisions | annotate |
|
902 |
-rwxr-xr-x 2009-07-01 12:47 +0200 88110 waf file | revisions | annotate |
|
903 |
-rwxr-xr-x 2009-07-01 12:47 +0200 28 waf.bat file | revisions | annotate |
|
904 |
-rw-r--r-- 2009-07-01 12:47 +0200 35395 wscript file | revisions | annotate |
|
905 |
-rw-r--r-- 2009-07-01 12:47 +0200 7673 wutils.py file | revisions | annotate |
|
906 |
||
907 |
Our example scripts are in the ``examples`` directory. If you click on ``examples`` |
|
7275 | 908 |
you will see a list of subdirectories. One of the files in ``tutorial`` subdirectory is ``first.cc``. If |
6754 | 909 |
you click on ``first.cc`` you will find the code you just walked through. |
910 |
||
911 |
The source code is mainly in the ``src`` directory. You can view source |
|
912 |
code either by clicking on the directory name or by clicking on the ``files`` |
|
913 |
link to the right of the directory name. If you click on the ``src`` |
|
914 |
directory, you will be taken to the listing of the ``src`` subdirectories. If you |
|
915 |
then click on ``core`` subdirectory, you will find a list of files. The first file |
|
916 |
you will find (as of this writing) is ``abort.h``. If you click on the |
|
917 |
``abort.h`` link, you will be sent to the source file for ``abort.h`` which |
|
918 |
contains useful macros for exiting scripts if abnormal conditions are detected. |
|
919 |
||
920 |
The source code for the helpers we have used in this chapter can be found in the |
|
7147
71adbc0422a5
Fix more paths in the documentation
Mitch Watrous <watrous@u.washington.edu>
parents:
7137
diff
changeset
|
921 |
``src/applications/helper`` directory. Feel free to poke around in the directory tree to |
6754 | 922 |
get a feel for what is there and the style of |ns3| programs. |