doc/tutorial/source/conceptual-overview.rst
changeset 6754 7ff69b244b5b
child 6998 1c2b8cfb71d2
equal deleted inserted replaced
6753:c9133c87760d 6754:7ff69b244b5b
       
     1 .. include:: replace.txt
       
     2 
       
     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 ++++++++++
       
    95 It used to be the case that if you wanted to connect a computers to a network,
       
    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 
       
   156 ::
       
   157 
       
   158   AUTHORS       doc/       README         src/     waf.bat*
       
   159   bindings/     examples/  RELEASE_NOTES  utils/   wscript
       
   160   build/        LICENSE    samples/       VERSION  wutils.py
       
   161   CHANGES.html  ns3/       scratch/       waf*     wutils.pyc
       
   162 
       
   163 Change into the ``examples/tutorial`` directory.  You should see a file named 
       
   164 ``first.cc`` located there.  This is a script that will create a simple
       
   165 point-to-point link between two nodes and echo a single packet between the
       
   166 nodes.  Let's take a look at that script line by line, so go ahead and open
       
   167 ``first.cc`` in your favorite editor.
       
   168 
       
   169 Boilerplate
       
   170 +++++++++++
       
   171 The first line in the file is an emacs mode line.  This tells emacs about the
       
   172 formatting conventions (coding style) we use in our source code.  
       
   173 
       
   174 ::
       
   175 
       
   176   /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
   177 
       
   178 This is always a somewhat controversial subject, so we might as well get it
       
   179 out of the way immediately.  The |ns3| project, like most large 
       
   180 projects, has adopted a coding style to which all contributed code must 
       
   181 adhere.  If you want to contribute your code to the project, you will 
       
   182 eventually have to conform to the |ns3| coding standard as described 
       
   183 in the file ``doc/codingstd.txt`` or shown on the project web page
       
   184 `here
       
   185 <http://www.nsnam.org/codingstyle.html>`_.
       
   186 
       
   187 We recommend that you, well, just get used to the look and feel of |ns3|
       
   188 code and adopt this standard whenever you are working with our code.  All of 
       
   189 the development team and contributors have done so with various amounts of 
       
   190 grumbling.  The emacs mode line above makes it easier to get the formatting 
       
   191 correct if you use the emacs editor.
       
   192 
       
   193 The |ns3| simulator is licensed using the GNU General Public 
       
   194 License.  You will see the appropriate GNU legalese at the head of every file 
       
   195 in the |ns3| distribution.  Often you will see a copyright notice for
       
   196 one of the institutions involved in the |ns3| project above the GPL
       
   197 text and an author listed below.
       
   198 
       
   199 ::
       
   200 
       
   201   /*
       
   202    * This program is free software; you can redistribute it and/or modify
       
   203    * it under the terms of the GNU General Public License version 2 as
       
   204    * published by the Free Software Foundation;
       
   205    *
       
   206    * This program is distributed in the hope that it will be useful,
       
   207    * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
   208    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
   209    * GNU General Public License for more details.
       
   210    *
       
   211    * You should have received a copy of the GNU General Public License
       
   212    * along with this program; if not, write to the Free Software
       
   213    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
       
   214    */
       
   215 
       
   216 Module Includes
       
   217 +++++++++++++++
       
   218 The code proper starts with a number of include statements.  
       
   219 
       
   220 ::
       
   221 
       
   222   #include "ns3/core-module.h"
       
   223   #include "ns3/simulator-module.h"
       
   224   #include "ns3/node-module.h"
       
   225   #include "ns3/helper-module.h"
       
   226 
       
   227 To help our high-level script users deal with the large number of include 
       
   228 files present in the system, we group includes according to relatively large 
       
   229 modules.  We provide a single include file that will recursively load all of 
       
   230 the include files used in each module.  Rather than having to look up exactly
       
   231 what header you need, and possibly have to get a number of dependencies right,
       
   232 we give you the ability to load a group of files at a large granularity.  This
       
   233 is not the most efficient approach but it certainly makes writing scripts much
       
   234 easier.
       
   235 
       
   236 Each of the |ns3| include files is placed in a directory called 
       
   237 ``ns3`` (under the build directory) during the build process to help avoid
       
   238 include file name collisions.  The ``ns3/core-module.h`` file corresponds 
       
   239 to the ns-3 module you will find in the directory ``src/core`` in your 
       
   240 downloaded release distribution.  If you list this directory you will find a
       
   241 large number of header files.  When you do a build, Waf will place public 
       
   242 header files in an ``ns3`` directory under the appropriate 
       
   243 ``build/debug`` or ``build/optimized`` directory depending on your 
       
   244 configuration.  Waf will also automatically generate a module include file to
       
   245 load all of the public header files.
       
   246 
       
   247 Since you are, of course, following this tutorial religiously, you will 
       
   248 already have done a
       
   249 
       
   250 ::
       
   251 
       
   252   ./waf -d debug configure
       
   253 
       
   254 in order to configure the project to perform debug builds.  You will also have
       
   255 done a
       
   256 
       
   257 ::
       
   258 
       
   259   ./waf
       
   260 
       
   261 to build the project.  So now if you look in the directory 
       
   262 ``../../build/debug/ns3`` you will find the four module include files shown 
       
   263 above.  You can take a look at the contents of these files and find that they
       
   264 do include all of the public include files in their respective modules.
       
   265 
       
   266 Ns3 Namespace
       
   267 +++++++++++++
       
   268 The next line in the ``first.cc`` script is a namespace declaration.
       
   269 
       
   270 ::
       
   271 
       
   272   using namespace ns3;
       
   273 
       
   274 The |ns3| project is implemented in a C++ namespace called 
       
   275 ``ns3``.  This groups all |ns3|-related declarations in a scope
       
   276 outside the global namespace, which we hope will help with integration with 
       
   277 other code.  The C++ ``using`` statement introduces the |ns3|
       
   278 namespace into the current (global) declarative region.  This is a fancy way
       
   279 of saying that after this declaration, you will not have to type ``ns3::``
       
   280 scope resolution operator before all of the |ns3| code in order to use
       
   281 it.  If you are unfamiliar with namespaces, please consult almost any C++ 
       
   282 tutorial and compare the ``ns3`` namespace and usage here with instances of
       
   283 the ``std`` namespace and the ``using namespace std;`` statements you 
       
   284 will often find in discussions of ``cout`` and streams.
       
   285 
       
   286 Logging
       
   287 +++++++
       
   288 The next line of the script is the following,
       
   289 
       
   290 ::
       
   291 
       
   292   NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
       
   293 
       
   294 We will use this statement as a convenient place to talk about our Doxygen
       
   295 documentation system.  If you look at the project web site, 
       
   296 `ns-3 project
       
   297 <http://www.nsnam.org>`_, you will find a link to "Doxygen 
       
   298 (ns-3-dev)" in the navigation bar.  If you select this link, you will be
       
   299 taken to our documentation page for the current development release.  There 
       
   300 is also a link to "Doxygen (stable)" that will take you to the documentation
       
   301 for the latest stable release of |ns3|.
       
   302 
       
   303 Along the left side, you will find a graphical representation of the structure
       
   304 of the documentation.  A good place to start is the ``NS-3 Modules``
       
   305 "book" in the |ns3| navigation tree.  If you expand ``Modules`` 
       
   306 you will see a list of |ns3| module documentation.  The concept of 
       
   307 module here ties directly into the module include files discussed above.  It 
       
   308 turns out that the |ns3| logging subsystem is part of the ``core`` 
       
   309 module, so go ahead and expand that documentation node.  Now, expand the 
       
   310 ``Debugging`` book and then select the ``Logging`` page.
       
   311 
       
   312 You should now be looking at the Doxygen documentation for the Logging module.
       
   313 In the list of ``#define``s at the top of the page you will see the entry
       
   314 for ``NS_LOG_COMPONENT_DEFINE``.  Before jumping in, it would probably be 
       
   315 good to look for the "Detailed Description" of the logging module to get a 
       
   316 feel for the overall operation.  You can either scroll down or select the
       
   317 "More..." link under the collaboration diagram to do this.
       
   318 
       
   319 Once you have a general idea of what is going on, go ahead and take a look at
       
   320 the specific ``NS_LOG_COMPONENT_DEFINE`` documentation.  I won't duplicate
       
   321 the documentation here, but to summarize, this line declares a logging 
       
   322 component called ``FirstScriptExample`` that allows you to enable and 
       
   323 disable console message logging by reference to the name.
       
   324 
       
   325 Main Function
       
   326 +++++++++++++
       
   327 The next lines of the script you will find are,
       
   328 
       
   329 ::
       
   330 
       
   331   int
       
   332   main (int argc, char *argv[])
       
   333   {
       
   334 
       
   335 This is just the declaration of the main function of your program (script).
       
   336 Just as in any C++ program, you need to define a main function that will be 
       
   337 the first function run.  There is nothing at all special here.  Your 
       
   338 |ns3| script is just a C++ program.
       
   339 
       
   340 The next two lines of the script are used to enable two logging components that
       
   341 are built into the Echo Client and Echo Server applications:
       
   342 
       
   343 ::
       
   344 
       
   345     LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
       
   346     LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
       
   347 
       
   348 If you have read over the Logging component documentation you will have seen
       
   349 that there are a number of levels of logging verbosity/detail that you can 
       
   350 enable on each component.  These two lines of code enable debug logging at the
       
   351 INFO level for echo clients and servers.  This will result in the application
       
   352 printing out messages as packets are sent and received during the simulation.
       
   353 
       
   354 Now we will get directly to the business of creating a topology and running 
       
   355 a simulation.  We use the topology helper objects to make this job as
       
   356 easy as possible.
       
   357 
       
   358 Topology Helpers
       
   359 ++++++++++++++++
       
   360 NodeContainer
       
   361 ~~~~~~~~~~~~~
       
   362 The next two lines of code in our script will actually create the 
       
   363 |ns3| ``Node`` objects that will represent the computers in the 
       
   364 simulation.  
       
   365 
       
   366 ::
       
   367 
       
   368     NodeContainer nodes;
       
   369     nodes.Create (2);
       
   370 
       
   371 Let's find the documentation for the ``NodeContainer`` class before we
       
   372 continue.  Another way to get into the documentation for a given class is via 
       
   373 the ``Classes`` tab in the Doxygen pages.  If you still have the Doxygen 
       
   374 handy, just scroll up to the top of the page and select the ``Classes`` 
       
   375 tab.  You should see a new set of tabs appear, one of which is 
       
   376 ``Class List``.  Under that tab you will see a list of all of the 
       
   377 |ns3| classes.  Scroll down, looking for ``ns3::NodeContainer``.
       
   378 When you find the class, go ahead and select it to go to the documentation for
       
   379 the class.
       
   380 
       
   381 You may recall that one of our key abstractions is the ``Node``.  This
       
   382 represents a computer to which we are going to add things like protocol stacks,
       
   383 applications and peripheral cards.  The ``NodeContainer`` topology helper
       
   384 provides a convenient way to create, manage and access any ``Node`` objects
       
   385 that we create in order to run a simulation.  The first line above just 
       
   386 declares a NodeContainer which we call ``nodes``.  The second line calls the
       
   387 ``Create`` method on the ``nodes`` object and asks the container to 
       
   388 create two nodes.  As described in the Doxygen, the container calls down into
       
   389 the |ns3| system proper to create two ``Node`` objects and stores
       
   390 pointers to those objects internally.
       
   391 
       
   392 The nodes as they stand in the script do nothing.  The next step in 
       
   393 constructing a topology is to connect our nodes together into a network.
       
   394 The simplest form of network we support is a single point-to-point link 
       
   395 between two nodes.  We'll construct one of those links here.
       
   396 
       
   397 PointToPointHelper
       
   398 ~~~~~~~~~~~~~~~~~~
       
   399 We are constructing a point to point link, and, in a pattern which will become
       
   400 quite familiar to you, we use a topology helper object to do the low-level
       
   401 work required to put the link together.  Recall that two of our key 
       
   402 abstractions are the ``NetDevice`` and the ``Channel``.  In the real
       
   403 world, these terms correspond roughly to peripheral cards and network cables.  
       
   404 Typically these two things are intimately tied together and one cannot expect
       
   405 to interchange, for example, Ethernet devices and wireless channels.  Our 
       
   406 Topology Helpers follow this intimate coupling and therefore you will use a
       
   407 single ``PointToPointHelper`` to configure and connect |ns3|
       
   408 ``PointToPointNetDevice`` and ``PointToPointChannel`` objects in this 
       
   409 script.
       
   410 
       
   411 The next three lines in the script are,
       
   412 
       
   413 ::
       
   414 
       
   415     PointToPointHelper pointToPoint;
       
   416     pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
       
   417     pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
       
   418 
       
   419 The first line,
       
   420 
       
   421 ::
       
   422 
       
   423     PointToPointHelper pointToPoint;
       
   424 
       
   425 instantiates a ``PointToPointHelper`` object on the stack.  From a 
       
   426 high-level perspective the next line,
       
   427 
       
   428 ::
       
   429 
       
   430     pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
       
   431 
       
   432 tells the ``PointToPointHelper`` object to use the value "5Mbps"
       
   433 (five megabits per second) as the "DataRate" when it creates a 
       
   434 ``PointToPointNetDevice`` object.
       
   435 
       
   436 From a more detailed perspective, the string "DataRate" corresponds
       
   437 to what we call an ``Attribute`` of the ``PointToPointNetDevice``.
       
   438 If you look at the Doxygen for class ``ns3::PointToPointNetDevice`` and 
       
   439 find the documentation for the ``GetTypeId`` method, you will find a list
       
   440 of  ``Attributes`` defined for the device.  Among these is the "DataRate"
       
   441 ``Attribute``.  Most user-visible |ns3| objects have similar lists of 
       
   442 ``Attributes``.  We use this mechanism to easily configure simulations without
       
   443 recompiling as you will see in a following section.
       
   444 
       
   445 Similar to the "DataRate" on the ``PointToPointNetDevice`` you will find a
       
   446 "Delay" ``Attribute`` associated with the ``PointToPointChannel``.  The 
       
   447 final line,
       
   448 
       
   449 ::
       
   450 
       
   451     pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
       
   452 
       
   453 tells the ``PointToPointHelper`` to use the value "2ms" (two milliseconds)
       
   454 as the value of the transmission delay of every point to point channel it 
       
   455 subsequently creates.
       
   456 
       
   457 NetDeviceContainer
       
   458 ~~~~~~~~~~~~~~~~~~
       
   459 At this point in the script, we have a ``NodeContainer`` that contains
       
   460 two nodes.  We have a ``PointToPointHelper`` that is primed and ready to 
       
   461 make ``PointToPointNetDevices`` and wire ``PointToPointChannel`` objects
       
   462 between them.  Just as we used the ``NodeContainer`` topology helper object
       
   463 to create the ``Nodes`` for our simulation, we will ask the 
       
   464 ``PointToPointHelper`` to do the work involved in creating, configuring and
       
   465 installing our devices for us.  We will need to have a list of all of the 
       
   466 NetDevice objects that are created, so we use a NetDeviceContainer to hold 
       
   467 them just as we used a NodeContainer to hold the nodes we created.  The 
       
   468 following two lines of code,
       
   469 
       
   470 ::
       
   471 
       
   472     NetDeviceContainer devices;
       
   473     devices = pointToPoint.Install (nodes);
       
   474 
       
   475 will finish configuring the devices and channel.  The first line declares the 
       
   476 device container mentioned above and the second does the heavy lifting.  The 
       
   477 ``Install`` method of the ``PointToPointHelper`` takes a 
       
   478 ``NodeContainer`` as a parameter.  Internally, a ``NetDeviceContainer`` 
       
   479 is created.  For each node in the ``NodeContainer`` (there must be exactly 
       
   480 two for a point-to-point link) a ``PointToPointNetDevice`` is created and 
       
   481 saved in the device container.  A ``PointToPointChannel`` is created and 
       
   482 the two ``PointToPointNetDevices`` are attached.  When objects are created
       
   483 by the ``PointToPointHelper``, the ``Attributes`` previously set in the 
       
   484 helper are used to initialize the corresponding ``Attributes`` in the 
       
   485 created objects.
       
   486 
       
   487 After executing the ``pointToPoint.Install (nodes)`` call we will have
       
   488 two nodes, each with an installed point-to-point net device and a single
       
   489 point-to-point channel between them.  Both devices will be configured to 
       
   490 transmit data at five megabits per second over the channel which has a two 
       
   491 millisecond transmission delay.
       
   492 
       
   493 InternetStackHelper
       
   494 ~~~~~~~~~~~~~~~~~~~
       
   495 We now have nodes and devices configured, but we don't have any protocol stacks
       
   496 installed on our nodes.  The next two lines of code will take care of that.
       
   497 
       
   498 ::
       
   499 
       
   500     InternetStackHelper stack;
       
   501     stack.Install (nodes);
       
   502 
       
   503 The ``InternetStackHelper`` is a topology helper that is to internet stacks
       
   504 what the ``PointToPointHelper`` is to point-to-point net devices.  The
       
   505 ``Install`` method takes a ``NodeContainer`` as a parameter.  When it is
       
   506 executed, it will install an Internet Stack (TCP, UDP, IP, etc.) on each of
       
   507 the nodes in the node container.
       
   508 
       
   509 Ipv4AddressHelper
       
   510 ~~~~~~~~~~~~~~~~~
       
   511 Next we need to associate the devices on our nodes with IP addresses.  We 
       
   512 provide a topology helper to manage the allocation of IP addresses.  The only
       
   513 user-visible API is to set the base IP address and network mask to use when
       
   514 performing the actual address allocation (which is done at a lower level 
       
   515 inside the helper).
       
   516 
       
   517 The next two lines of code in our example script, ``first.cc``,
       
   518 
       
   519 ::
       
   520 
       
   521     Ipv4AddressHelper address;
       
   522     address.SetBase ("10.1.1.0", "255.255.255.0");
       
   523 
       
   524 declare an address helper object and tell it that it should begin allocating IP
       
   525 addresses from the network 10.1.1.0 using the mask 255.255.255.0 to define 
       
   526 the allocatable bits.  By default the addresses allocated will start at one
       
   527 and increase monotonically, so the first address allocated from this base will
       
   528 be 10.1.1.1, followed by 10.1.1.2, etc.  The low level |ns3| system
       
   529 actually remembers all of the IP addresses allocated and will generate a
       
   530 fatal error if you accidentally cause the same address to be generated twice 
       
   531 (which is a very hard to debug error, by the way).
       
   532 
       
   533 The next line of code,
       
   534 
       
   535 ::
       
   536 
       
   537     Ipv4InterfaceContainer interfaces = address.Assign (devices);
       
   538 
       
   539 performs the actual address assignment.  In |ns3| we make the
       
   540 association between an IP address and a device using an ``Ipv4Interface``
       
   541 object.  Just as we sometimes need a list of net devices created by a helper 
       
   542 for future reference we sometimes need a list of ``Ipv4Interface`` objects.
       
   543 The ``Ipv4InterfaceContainer`` provides this functionality.
       
   544 
       
   545 Now we have a point-to-point network built, with stacks installed and IP 
       
   546 addresses assigned.  What we need at this point are applications to generate
       
   547 traffic.
       
   548 
       
   549 Applications
       
   550 ++++++++++++
       
   551 Another one of the core abstractions of the ns-3 system is the 
       
   552 ``Application``.  In this script we use two specializations of the core
       
   553 |ns3| class ``Application`` called ``UdpEchoServerApplication``
       
   554 and ``UdpEchoClientApplication``.  Just as we have in our previous 
       
   555 explanations,  we use helper objects to help configure and manage the 
       
   556 underlying objects.  Here, we use ``UdpEchoServerHelper`` and
       
   557 ``UdpEchoClientHelper`` objects to make our lives easier.
       
   558 
       
   559 UdpEchoServerHelper
       
   560 ~~~~~~~~~~~~~~~~~~~
       
   561 The following lines of code in our example script, ``first.cc``, are used
       
   562 to set up a UDP echo server application on one of the nodes we have previously
       
   563 created.
       
   564 
       
   565 ::
       
   566 
       
   567     UdpEchoServerHelper echoServer (9);
       
   568 
       
   569     ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
       
   570     serverApps.Start (Seconds (1.0));
       
   571     serverApps.Stop (Seconds (10.0));
       
   572 
       
   573 The first line of code in the above snippet declares the 
       
   574 ``UdpEchoServerHelper``.  As usual, this isn't the application itself, it
       
   575 is an object used to help us create the actual applications.  One of our 
       
   576 conventions is to place *required* ``Attributes`` in the helper constructor.
       
   577 In this case, the helper can't do anything useful unless it is provided with
       
   578 a port number that the client also knows about.  Rather than just picking one 
       
   579 and hoping it all works out, we require the port number as a parameter to the 
       
   580 constructor.  The constructor, in turn, simply does a ``SetAttribute``
       
   581 with the passed value.  If you want, you can set the "Port" ``Attribute``
       
   582 to another value later using ``SetAttribute``.
       
   583 
       
   584 Similar to many other helper objects, the ``UdpEchoServerHelper`` object 
       
   585 has an ``Install`` method.  It is the execution of this method that actually
       
   586 causes the underlying echo server application to be instantiated and attached
       
   587 to a node.  Interestingly, the ``Install`` method takes a
       
   588 ``NodeContainter`` as a parameter just as the other ``Install`` methods
       
   589 we have seen.  This is actually what is passed to the method even though it 
       
   590 doesn't look so in this case.  There is a C++ *implicit conversion* at
       
   591 work here that takes the result of ``nodes.Get (1)`` (which returns a smart
       
   592 pointer to a node object --- ``Ptr<Node>``) and uses that in a constructor
       
   593 for an unnamed ``NodeContainer`` that is then passed to ``Install``.
       
   594 If you are ever at a loss to find a particular method signature in C++ code
       
   595 that compiles and runs just fine, look for these kinds of implicit conversions.  
       
   596 
       
   597 We now see that ``echoServer.Install`` is going to install a
       
   598 ``UdpEchoServerApplication`` on the node found at index number one of the
       
   599 ``NodeContainer`` we used to manage our nodes.  ``Install`` will return
       
   600 a container that holds pointers to all of the applications (one in this case 
       
   601 since we passed a ``NodeContainer`` containing one node) created by the 
       
   602 helper.
       
   603 
       
   604 Applications require a time to "start" generating traffic and may take an
       
   605 optional time to "stop".  We provide both.  These times are set using  the
       
   606 ``ApplicationContainer`` methods ``Start`` and ``Stop``.  These 
       
   607 methods take ``Time`` parameters.  In this case, we use an *explicit*
       
   608 C++ conversion sequence to take the C++ double 1.0 and convert it to an 
       
   609 |ns3| ``Time`` object using a ``Seconds`` cast.  Be aware that
       
   610 the conversion rules may be controlled by the model author, and C++ has its
       
   611 own rules, so you can't always just assume that parameters will be happily 
       
   612 converted for you.  The two lines,
       
   613 
       
   614 ::
       
   615 
       
   616     serverApps.Start (Seconds (1.0));
       
   617     serverApps.Stop (Seconds (10.0));
       
   618 
       
   619 will cause the echo server application to ``Start`` (enable itself) at one
       
   620 second into the simulation and to ``Stop`` (disable itself) at ten seconds
       
   621 into the simulation.  By virtue of the fact that we have declared a simulation
       
   622 event (the application stop event) to be executed at ten seconds, the simulation
       
   623 will last *at least* ten seconds.
       
   624 
       
   625 UdpEchoClientHelper
       
   626 ~~~~~~~~~~~~~~~~~~~
       
   627 
       
   628 The echo client application is set up in a method substantially similar to
       
   629 that for the server.  There is an underlying ``UdpEchoClientApplication``
       
   630 that is managed by an ``UdpEchoClientHelper``.
       
   631 
       
   632 ::
       
   633 
       
   634     UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
       
   635     echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
       
   636     echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
       
   637     echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
       
   638 
       
   639     ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
       
   640     clientApps.Start (Seconds (2.0));
       
   641     clientApps.Stop (Seconds (10.0));
       
   642 
       
   643 For the echo client, however, we need to set five different ``Attributes``.
       
   644 The first two ``Attributes`` are set during construction of the 
       
   645 ``UdpEchoClientHelper``.  We pass parameters that are used (internally to
       
   646 the helper) to set the "RemoteAddress" and "RemotePort" ``Attributes``
       
   647 in accordance with our convention to make required ``Attributes`` parameters
       
   648 in the helper constructors.  
       
   649 
       
   650 Recall that we used an ``Ipv4InterfaceContainer`` to keep track of the IP 
       
   651 addresses we assigned to our devices.  The zeroth interface in the 
       
   652 ``interfaces`` container is going to correspond to the IP address of the 
       
   653 zeroth node in the ``nodes`` container.  The first interface in the 
       
   654 ``interfaces`` container corresponds to the IP address of the first node 
       
   655 in the ``nodes`` container.  So, in the first line of code (from above), we
       
   656 are creating the helper and telling it so set the remote address of the client
       
   657 to be  the IP address assigned to the node on which the server resides.  We 
       
   658 also tell it to arrange to send packets to port nine.
       
   659 
       
   660 The "MaxPackets" ``Attribute`` tells the client the maximum number of 
       
   661 packets we allow it to send during the simulation.  The "Interval" 
       
   662 ``Attribute`` tells the client how long to wait between packets, and the
       
   663 "PacketSize" ``Attribute`` tells the client how large its packet payloads
       
   664 should be.  With this particular combination of ``Attributes``, we are 
       
   665 telling the client to send one 1024-byte packet.
       
   666 
       
   667 Just as in the case of the echo server, we tell the echo client to ``Start``
       
   668 and ``Stop``, but here we start the client one second after the server is
       
   669 enabled (at two seconds into the simulation).
       
   670 
       
   671 Simulator
       
   672 +++++++++
       
   673 What we need to do at this point is to actually run the simulation.  This is 
       
   674 done using the global function ``Simulator::Run``.
       
   675 
       
   676 ::
       
   677 
       
   678     Simulator::Run ();
       
   679 
       
   680 When we previously called the methods,
       
   681 
       
   682 ::
       
   683 
       
   684     serverApps.Start (Seconds (1.0));
       
   685     serverApps.Stop (Seconds (10.0));
       
   686     ...
       
   687     clientApps.Start (Seconds (2.0));
       
   688     clientApps.Stop (Seconds (10.0));
       
   689 
       
   690 we actually scheduled events in the simulator at 1.0 seconds, 2.0 seconds and
       
   691 two events at 10.0 seconds.  When ``Simulator::Run`` is called, the system 
       
   692 will begin looking through the list of scheduled events and executing them.  
       
   693 First it will run the event at 1.0 seconds, which will enable the echo server 
       
   694 application (this event may, in turn, schedule many other events).  Then it 
       
   695 will run the event scheduled for t=2.0 seconds which will start the echo client
       
   696 application.  Again, this event may schedule many more events.  The start event
       
   697 implementation in the echo client application will begin the data transfer phase
       
   698 of the simulation by sending a packet to the server.
       
   699 
       
   700 The act of sending the packet to the server will trigger a chain of events
       
   701 that will be automatically scheduled behind the scenes and which will perform 
       
   702 the mechanics of the packet echo according to the various timing parameters 
       
   703 that we have set in the script.
       
   704 
       
   705 Eventually, since we only send one packet (recall the ``MaxPackets`` 
       
   706 ``Attribute`` was set to one), the chain of events triggered by 
       
   707 that single client echo request will taper off and the simulation will go 
       
   708 idle.  Once this happens, the remaining events will be the ``Stop`` events
       
   709 for the server and the client.  When these events are executed, there are
       
   710 no further events to process and ``Simulator::Run`` returns.  The simulation
       
   711 is then complete.
       
   712 
       
   713 All that remains is to clean up.  This is done by calling the global function 
       
   714 ``Simulator::Destroy``.  As the helper functions (or low level 
       
   715 |ns3| code) executed, they arranged it so that hooks were inserted in
       
   716 the simulator to destroy all of the objects that were created.  You did not 
       
   717 have to keep track of any of these objects yourself --- all you had to do 
       
   718 was to call ``Simulator::Destroy`` and exit.  The |ns3| system
       
   719 took care of the hard part for you.  The remaining lines of our first 
       
   720 |ns3| script, ``first.cc``, do just that:
       
   721 
       
   722 ::
       
   723 
       
   724     Simulator::Destroy ();
       
   725     return 0;
       
   726   }
       
   727 
       
   728 Building Your Script
       
   729 ++++++++++++++++++++
       
   730 We have made it trivial to build your simple scripts.  All you have to do is 
       
   731 to drop your script into the scratch directory and it will automatically be 
       
   732 built if you run Waf.  Let's try it.  Copy ``examples/tutorial/first.cc`` into 
       
   733 the ``scratch`` directory after changing back into the top level directory.
       
   734 
       
   735 ::
       
   736 
       
   737   cd ..
       
   738   cp examples/tutorial/first.cc scratch/myfirst.cc
       
   739 
       
   740 Now build your first example script using waf:
       
   741 
       
   742 ::
       
   743 
       
   744   ./waf
       
   745 
       
   746 You should see messages reporting that your ``myfirst`` example was built
       
   747 successfully.
       
   748 
       
   749 ::
       
   750 
       
   751   Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build'
       
   752   [614/708] cxx: scratch/myfirst.cc -> build/debug/scratch/myfirst_3.o
       
   753   [706/708] cxx_link: build/debug/scratch/myfirst_3.o -> build/debug/scratch/myfirst
       
   754   Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build'
       
   755   'build' finished successfully (2.357s)
       
   756 
       
   757 You can now run the example (note that if you build your program in the scratch
       
   758 directory you must run it out of the scratch directory):
       
   759 
       
   760 ::
       
   761 
       
   762   ./waf --run scratch/myfirst
       
   763 
       
   764 You should see some output:
       
   765 
       
   766 ::
       
   767 
       
   768   Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build'
       
   769   Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build'
       
   770   'build' finished successfully (0.418s)
       
   771   Sent 1024 bytes to 10.1.1.2
       
   772   Received 1024 bytes from 10.1.1.1
       
   773   Received 1024 bytes from 10.1.1.2
       
   774 
       
   775 Here you see that the build system checks to make sure that the file has been
       
   776 build and then runs it.  You see the logging component on the echo client 
       
   777 indicate that it has sent one 1024 byte packet to the Echo Server on 
       
   778 10.1.1.2.  You also see the logging component on the echo server say that
       
   779 it has received the 1024 bytes from 10.1.1.1.  The echo server silently 
       
   780 echoes the packet and you see the echo client log that it has received its 
       
   781 packet back from the server.
       
   782 
       
   783 Ns-3 Source Code
       
   784 ****************
       
   785 
       
   786 Now that you have used some of the |ns3| helpers you may want to 
       
   787 have a look at some of the source code that implements that functionality.
       
   788 The most recent code can be browsed on our web server at the following link:
       
   789 http://code.nsnam.org/ns-3-dev.  There, you will see the Mercurial
       
   790 summary page for our |ns3| development tree.
       
   791 
       
   792 At the top of the page, you will see a number of links,
       
   793 
       
   794 ::
       
   795 
       
   796   summary | shortlog | changelog | graph | tags | files 
       
   797 
       
   798 Go ahead and select the ``files`` link.  This is what the top-level of
       
   799 most of our *repositories* will look:
       
   800 
       
   801 ::
       
   802 
       
   803   drwxr-xr-x                               [up]     
       
   804   drwxr-xr-x                               bindings python  files
       
   805   drwxr-xr-x                               doc              files
       
   806   drwxr-xr-x                               examples         files
       
   807   drwxr-xr-x                               ns3              files
       
   808   drwxr-xr-x                               samples          files
       
   809   drwxr-xr-x                               scratch          files
       
   810   drwxr-xr-x                               src              files
       
   811   drwxr-xr-x                               utils            files
       
   812   -rw-r--r-- 2009-07-01 12:47 +0200 560    .hgignore        file | revisions | annotate
       
   813   -rw-r--r-- 2009-07-01 12:47 +0200 1886   .hgtags          file | revisions | annotate
       
   814   -rw-r--r-- 2009-07-01 12:47 +0200 1276   AUTHORS          file | revisions | annotate
       
   815   -rw-r--r-- 2009-07-01 12:47 +0200 30961  CHANGES.html     file | revisions | annotate
       
   816   -rw-r--r-- 2009-07-01 12:47 +0200 17987  LICENSE          file | revisions | annotate
       
   817   -rw-r--r-- 2009-07-01 12:47 +0200 3742   README           file | revisions | annotate
       
   818   -rw-r--r-- 2009-07-01 12:47 +0200 16171  RELEASE_NOTES    file | revisions | annotate
       
   819   -rw-r--r-- 2009-07-01 12:47 +0200 6      VERSION          file | revisions | annotate
       
   820   -rwxr-xr-x 2009-07-01 12:47 +0200 88110  waf              file | revisions | annotate
       
   821   -rwxr-xr-x 2009-07-01 12:47 +0200 28     waf.bat          file | revisions | annotate
       
   822   -rw-r--r-- 2009-07-01 12:47 +0200 35395  wscript          file | revisions | annotate
       
   823   -rw-r--r-- 2009-07-01 12:47 +0200 7673   wutils.py        file | revisions | annotate
       
   824   
       
   825 Our example scripts are in the ``examples`` directory.  If you click on ``examples``
       
   826 you will see a list of files.  One of the files in that directory is ``first.cc``.  If
       
   827 you click on ``first.cc`` you will find the code you just walked through.
       
   828 
       
   829 The source code is mainly in the ``src`` directory.  You can view source
       
   830 code either by clicking on the directory name or by clicking on the ``files``
       
   831 link to the right of the directory name.  If you click on the ``src``
       
   832 directory, you will be taken to the listing of the ``src`` subdirectories.  If you 
       
   833 then click on ``core`` subdirectory, you will find a list of files.  The first file
       
   834 you will find (as of this writing) is ``abort.h``.  If you click on the 
       
   835 ``abort.h`` link, you will be sent to the source file for ``abort.h`` which 
       
   836 contains useful macros for exiting scripts if abnormal conditions are detected.
       
   837 
       
   838 The source code for the helpers we have used in this chapter can be found in the 
       
   839 ``src/helper`` directory.  Feel free to poke around in the directory tree to
       
   840 get a feel for what is there and the style of |ns3| programs.