doc/tutorial/tutorial.texi
author Craig Dowell <craigdo@ee.washington.edu>
Mon, 07 Jan 2008 16:29:54 -0800
changeset 2210 151fbc85dd92
parent 2209 9b98d226e566
child 2211 3e1cec7d22d6
permissions -rwxr-xr-x
figures
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     1
\input texinfo  @c -*-texinfo-*-
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     2
@c %**start of header
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     3
@setfilename ns-3.info
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     4
@settitle ns-3 tutorial
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     5
@c @setchapternewpage odd
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     6
@c %**end of header
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     7
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     8
@ifinfo
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     9
This @command{ns-3} project document is one of a set of project documents:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    10
@itemize @bullet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    11
@item Software Architecture 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    12
@item Manual 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    13
@item Tutorial (this document)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    14
@end itemize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    15
 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
This document is written in GNU Texinfo and is to be maintained in
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
revision control on the @command{ns-3} code server.  Both PDF and HTML versions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
should be available on the server.  Changes to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
the document should be discussed on the ns-developers@@isi.edu mailing list.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
@end ifinfo
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
@copying
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
This @command{ns-3} project document is one of a set of project documents:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
@itemize @bullet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
@item Software Architecture 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
@item Manual 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
@item Tutorial (this document)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
@end itemize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
This document is written in GNU Texinfo and is to be maintained in
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
revision control on the @command{ns-3} code server.  Both PDF and HTML 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
versions should be available on the server.  Changes to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
the document should be discussed on the ns-developers@@isi.edu mailing list.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
This software is free software; you can redistribute it and/or modify
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
it under the terms of the GNU General Public License as published by
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
the Free Software Foundation; either version 2 of the License, or
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
(at your option) any later version.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
This software is distributed in the hope that it will be useful,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
but WITHOUT ANY WARRANTY; without even the implied warranty of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
GNU General Public License for more details.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
You should have received a copy of the GNU General Public License
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
along with this program.  If not, see @uref{http://www.gnu.org/licenses/}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
@end copying
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
@titlepage
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
@title ns-3 Tutorial
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
@author ns-3 project
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
@author feedback:  ns-developers@@isi.edu
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
@today{}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
@c @page
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
@vskip 0pt plus 1filll
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
@insertcopying
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
@end titlepage
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
@c So the toc is printed at the start.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
@anchor{Full Table of Contents}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
@contents
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
@ifnottex
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
@node Top, Preface, Full Table of Contents 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
@top ns-3 Tutorial (html version)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
For a pdf version of this manual, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
see @uref{http://www.nsnam.org/docs/tutorial.pdf}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
@insertcopying
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
@end ifnottex
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
@menu
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
* Preface::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
* Introduction::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
* Resources::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
* The-Basics::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
* Some-Prerequisites::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
* A-First-ns-3-Script::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
* Tracing-at-a-Glance::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
* Other-network-topologies::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
* Nonlinear-Thinking::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
* Summary::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
* The-Doxygen-Documentation-System::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
* How-To-Change-Things::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
* How-To-Set-Default-Values::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
* How-To-Write-A-New-Application::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
@end menu
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
@node Preface
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
@chapter Preface
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
The purpose of this tutorial is to introduce new @command{ns-3} users to the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
system in a structured way.  It is sometimes difficult for new users to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
glean essential information from detailed manuals and to convert this
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
information into working simulations.  In this tutorial, we will build 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
several example simulations, introducing and explaining key concepts and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
features as we go.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
As the tutorial unfolds, we will introduce the full @command{ns-3} 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
documentation 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
and provide pointers to source code for those interested in delving deeper
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
into the workings of the system.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
This document is one of a set of @command{ns-3} project documents:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
@itemize @bullet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
@item Software Architecture 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
@item Manual 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
@item Tutorial (this document)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
@end itemize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
This document is written in Texinfo and is to be maintained in
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
revision control on the @command{ns-3} code server.  Both PDF and HTML versions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
should be available on the server.  Changes to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
the document should be discussed on the ns-developers@@isi.edu mailing list.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   118
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
@c Begin document body here
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
@c Introduction
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
@node Introduction
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
@chapter Introduction
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   129
The @command{ns-3} project is a discrete-event network simulator targeted
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
primarily for research and educational use.  It is aimed at 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
comprehensively redesigning and enhancing the popular Network Simulator 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   132
@command{ns-2}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   133
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   134
For those familiar with @command{ns-2}, the most visible outward change 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   135
when moving to @command{ns-3} is the choice of scripting language.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   136
@command{ns-2} is typically scripted in Tcl and results of simulations are
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   137
often visualized using the Network Animator @command{nam}.  In 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   138
@command{ns-3} there is currently no visualization module, and multiple 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   139
language bindings are allowed.  In this tutorial, we will concentrate on 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   140
scripting directly in C++ and interpreting results via trace files.  Scripting
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   141
in other languages will typically be done via straightforward bindings of the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   142
target language into the underlying C++.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   143
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   144
The goal of this tutorial is to introduce new users of @command{ns-3} to enough
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   145
of the system to enable them to author simple simulation scripts and extract
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
useful information from the simulations.  We begin by introducing some of the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   147
other important resources that are available to those interested in using or
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
writing scripts, models and even those interested in making contributions to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
the core @command{ns-3} system.  We provide an overview of some of the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   150
important abstractions, design patterns and idioms used when writing 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   151
@command{ns-3} scripts, and then dig right in by begining to write simulation 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   152
scripts, run them and interpret results.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
After completing this tutorial, one should be able to:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
@itemize @bullet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   156
@item Find documentation resources in the distribution and on the web;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   157
@item Download and compile the @command{ns-3} system;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   158
@item Use the provided devices to author network simulations of fairly  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
significant complexity;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   160
@item Use the default value system to configure simulations;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   161
@item Write new @command{ns-3} applications;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   162
@item Use the tracing subsystem.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
@end itemize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   165
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   166
@c Resources
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   167
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   169
@node Resources
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   170
@chapter Resources
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   171
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   172
@menu
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   173
* The-Web::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   174
* Mercurial::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   175
* Waf::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   176
* Environment-Idioms-Design-Patterns::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   177
* Socket-Programming::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   178
@end menu
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   179
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   180
@node The-Web
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   181
@section The Web
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   182
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   183
@cindex www.nsnam.org
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   184
There are several important resources of which any @command{ns-3} user must be
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   185
aware.  The main web site is located at @uref{http://www.nsnam.org}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   186
and provides access to basic information about the @command{ns-3} system.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   187
Detailed documentation is available through the main web site at
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   188
@uref{http://www.nsnam.org/documents.html}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   189
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   190
@cindex documentation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   191
@cindex architecture
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
You can find documents relating to the system architecture from this page,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   193
and also gain access to the detailed software documentation.  The software
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   194
system is documented in great detail using 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   195
@uref{http://www.stack.nl/~dimitri/doxygen/,,Doxygen}.  There is a Wiki that
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
complements the main @command{ns-3} web site which you will find at 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   197
@uref{http://www.nsnam.org/wiki/}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   198
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   199
You will find user and developer FAQs there as well as troubleshooting guides, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   200
third-party contributed code, papers, etc. The source code may be found 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   201
and browsed at @uref{http://code.nsnam.org/}. 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   202
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   203
@cindex repository!ns-3-dev
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   204
@cindex repository!releases
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   205
There you will find the current development tree in the repository named
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   206
@code{ns-3-dev}. Past releases and experimental repositories of the core
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   207
developers may also be found there.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   208
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
@node Mercurial
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   210
@section Mercurial
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   211
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   212
Complex software systems need some way to manage the organization and 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   213
changes to the underlying code and documentation.  There are many ways to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   214
perform this feat, and you may have heard of some of the systems that are
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   215
currently used to do this.  The Concurrent Version System (CVS) is probably
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   216
the most well known.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   217
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   218
@cindex software configuration management
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   219
@cindex Mercurial
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   220
The @command{ns-3} project uses Mercurial as its source code management system.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   221
Although you do not need to know much about Mercurial in order to complete
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   222
this tutorial, we recommend becoming familiar with Mercurial and using it 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   223
to access the source code.  Mercurial has a web site at 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   224
@uref{http://www.selenic.com/mercurial/},
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   225
from which you can get binary or source releases of this Software
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   226
Configuration Management (SCM) system.  Selenic (the developer of Mercurial)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   227
also provides a tutorial at 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   228
@uref{http://www.selenic.com/mercurial/wiki/index.cgi/Tutorial/},
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   229
and a QuickStart guide at
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   230
@uref{http://www.selenic.com/mercurial/wiki/index.cgi/QuickStart/}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   231
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   232
You can also find vital information about using Mercurial and @command{ns-3}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   233
on the main @command{ns-3} web site.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   234
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   235
@node Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   236
@section Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   237
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   238
@cindex Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   239
@cindex make
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   240
@cindex build
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   241
Once you have source code downloaded to your local system, you will need 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   242
to compile that source to produce usable programs.  Just as in the case of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   243
source code management, there are many tools available to perform this 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   244
function.  Probably the most famous of these tools is @code{make}.  Along
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   245
with being the most famous, @code{make} is probably the most difficult to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   246
use in a very large and highly configurable system.  Because of this, many
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   247
alternatives have been developed.  Recently these systems have been developed
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   248
using the Python language.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   249
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   250
The build system @code{Waf} is used on the @command{ns-3} project.  It is one 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   251
of the new generation of Python-based build systems.  You will not need to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   252
understand any Python to build the existing @command{ns-3} system, and will 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   253
only have to understand a tiny and intuitively obvious subset of Python in 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   254
order to extend the system in most cases.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   255
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   256
For those interested in the gory details of Waf, the main web site can be 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   257
found at @uref{http://freehackers.org/\~tnagy/waf.html}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   258
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   259
@node Environment-Idioms-Design-Patterns
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   260
@section Environment, Idioms, and Design Patterns
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   261
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   262
@cindex C++
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   263
As mentioned above, scripting in @command{ns-3} is done in C++.  A working 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   264
knowledge of C++ and object-oriented concepts is assumed in this document.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   265
We will take some time to review some of the more advanced concepts or 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   266
possibly unfamiliar language features, idioms and design patterns as they 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   267
appear.  We don't want this tutorial to devolve into a C++ tutorial, though,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   268
so we do expect a basic command of the language.  There are an almost 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   269
unimaginable number of sources of information on C++ available on the web or
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   270
in print.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   271
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   272
If you are new to C++, you may want to find a tutorial- or cookbook-based
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   273
book or web site and work through at least the basic features of the language
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   274
before proceeding.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   275
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   276
@subsection Environment
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   277
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   278
@cindex toolchain
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   279
@cindex GNU
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   280
The @command{ns-3} system uses the GNU ``toolchain'' for development.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   281
A software toolchain is the set of programming tools available in the given 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   282
environment. For a quick review of what is included in the GNU toolchain see,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   283
@uref{http://en.wikipedia.org/wiki/GNU_toolchain}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   284
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   285
@cindex Linux
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   286
Typically a @command{ns-3} author will work in Linux or a Linux-like
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   287
environment.  For those running under Windows, there do exist environments 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   288
which simulate the Linux environment to various degrees.  The @command{ns-3} 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   289
project supports development in the Cygwin and the MinGW environments for 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   290
these users.  See @uref{http://www.cygwin.com/} and 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   291
@uref{http://www.mingw.org/} for details on downloading and using these
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   292
systems.  I use Cygwin in these cases since it provides all of the Linux tools
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   293
I know and love.  It can, however, sometimes be problematic due to the way it 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   294
actually does its emulation, and sometimes interactions with other Windows
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   295
software can cause problems.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   296
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   297
@cindex Cygwin
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   298
@cindex MinGW
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   299
If you do use Cygwin or MinGW; and use Logitech products, I will save you
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   300
quite a bit of heartburn right off the bat and encourage you to take a look
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   301
at the @uref{http://www.mingw.org/MinGWiki/index.php/FAQ,,MinGW FAQ}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   302
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   303
@cindex Logitech
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   304
Search for ``Logitech'' and read the FAQ entry, ``why does make often 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   305
crash creating a sh.exe.stackdump file when I try to compile my source code.''
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   306
Believe it or not, the @code{Logitech Process Monitor} insinuates itself into
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   307
every DLL in the system when it is running.  It can cause your Cygwin or
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   308
MinGW DLLs to die in mysterious ways and often prevents debuggers from 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   309
running.  Beware of Logitech.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   310
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   311
@subsection Idioms and Design Patterns
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   312
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   313
@cindex idiom
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   314
In any system, there are a number of problems to be solved that happen 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   315
repeatedly.  Often the solutions to these problems can be generalized and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   316
applied in a similar way across the system.  These solutions are called
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   317
Design Patterns.  The @command{ns-3} system relies on several classic design
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   318
patterns.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   319
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   320
@cindex design pattern
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   321
Also, in any language, there are constructs that, while they aren't part of the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   322
language per se, are commonly found and useful.  For example, at the lowest 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   323
level a C programmer should be able to immediately recognize the purpose and 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   324
intent  of the following code without having to reflect on the details:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   325
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   326
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   327
  for (;;)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   328
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   329
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   330
These low-level constructs, or idioms, extend upward in complexity, eventually
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   331
becoming implementations of design patterns.  As you are exposed to more 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   332
and more of the @command{ns-3} system, you will begin to recognize and be 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   333
comfortable with the C++ implementations (idioms) of several important design
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   334
patterns.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   335
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   336
@cindex functor
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   337
@cindex callback
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   338
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   339
The @command{ns-3} code relies heavily on 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   340
@emph{Generalized Functors, Callbacks, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   341
Smart Pointers, Singletons, and Object Factories}.  Although we will 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   342
not assume any detailed knowledge of the idioms and design patterns used 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   343
in the @command{ns-3}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   344
system, it will be useful for readers who intend to delve deeply into the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   345
system to understand some important related concepts.  We recommend two 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   346
resources: @uref{http://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional/dp/0201633612/,,Design Patterns: Elements of Reusable Object-Oriented Software, Gamma et. al.} and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   347
@uref{http://www.amazon.com/exec/obidos/ASIN/0201704315,,Modern C++ Design: Generic Programming and Design Patterns Applied, Alexandrescu}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   348
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   349
Gamma addresses the abstract design patterns, and Alexandrescu addresses the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   350
C++ idioms you will often see throughout the @command{ns-3} code.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   351
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   352
@cindex template
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   353
Almost any use of @command{ns-3} will require some basic knowledge of C++ 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   354
templates.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   355
We will discuss the high-level uses in this tutorial.  However, if you venture
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   356
deeply into the source code, you will see fairly heavy use of relatively
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   357
sophisticated C++ templates in some of low-level modules of the system.  The
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   358
You don't have to be a template guru to complete this tutorial but if you
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   359
expect to work in @command{ns-3} at a low level you will have to be 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   360
somewhat fluent
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   361
with templates.  If you  want to truly grok C++ templates we recommend,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   362
@uref{http://www.amazon.com/Templates-Complete-Guide-David-Vandevoorde/dp/0201734842/,,C++ Templates: The Complete Guide, Vandevoorde and Josuttis}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   363
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   364
@node Socket-Programming
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   365
@section Socket Programming
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   366
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   367
@cindex sockets
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   368
We will assume a basic facility with the Berkeley Sockets API in the examples
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   369
used in this tutorial.  If you are new to sockets, we recommend reviewing the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   370
API and some common usage cases.  For a good overview of programming TCP/IP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   371
sockets we recommend @uref{http://www.elsevier.com/wps/product/cws_home/680765,,Practical TCP/IP Sockets in C, Donahoo and Calvert}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   372
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   373
There is an associated web site that includes source for the examples in the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   374
book, which you can find at:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   375
@uref{http://cs.baylor.edu/~donahoo/practical/CSockets/}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   376
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   377
If you understand the first four chapters of the book (or for those who do
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   378
not have access to a copy of the book, the echo clients and servers shown in 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   379
the website above) you will be in good shape to understand the tutorial.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   380
There is a similar book on Multicast Sockets,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   381
@uref{http://www.elsevier.com/wps/product/cws_home/700736,,Multicast Sockets, Makofske and Almeroth}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   382
that covers material you may need to understand for the multicast examples.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   383
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   384
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   385
@c The Basics
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   386
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   387
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   388
@node The-Basics
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   389
@chapter The Basics
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   390
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   391
@cindex Linux
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   392
@cindex Cygwin
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   393
@cindex GNU
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   394
@cindex toolchain
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   395
From this point forward, we are going to assume that the reader is working in
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   396
Linux or a Linux emulation environment (Linux, Cygwin, etc.) and has the GNU
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   397
toolchain installed and verified.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   398
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   399
@cindex Mercurial
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   400
@cindex Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   401
We are going to assume that you have Mercurial and Waf installed and running
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   402
on the target system as described in the Getting Started section of the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   403
@command{ns-3} web site: @uref{http://www.nsnam.org/getting_started.html}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   404
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   405
@section Downloading
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   406
@cindex tarball
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   407
The @command{ns-3} code is available in Mercurial repositories on the server
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   408
code.nsnam.org.  You can download a tarball, but we recommend working with
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
   409
Mercurial --- it will make your life easier in the long run.
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   410
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   411
@cindex repository
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   412
If you go to the following link: @uref{http://code.nsnam.org/},
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   413
you will see a number of repositories.  Many are the private repositories of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   414
the @command{ns-3} development team.  The repositories of interest to you 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   415
will be
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   416
prefixed with ``ns-3''.  The current development snapshot (unreleased) of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   417
@command{ns-3} may be found at: @uref{http://code.nsnam.org/ns-3-dev/}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   418
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   419
The developers attempt to keep this repository in a consistent, working state
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   420
but it is a development area with unreleased code present, so you may want to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   421
consider downloading an official release.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   422
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   423
There will be a number of released repositories present at code.nsnam.org.
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
   424
These repos will have names like ns-3.0.1 --- which referes to release 3.0.1 
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   425
of the network simulator (or if you like, release 0.1 of @command{ns-3}).  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   426
Since the releases are changing at a rate of one per month, I will stick with 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   427
the more constant ns-3-dev here, but you can replace the string ns-3-dev with
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   428
your choice of release (e.g., ns-3.0.5) below.  You can find the latest 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   429
version  of the code either by inspection of the repository list or by going 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   430
to the ``Getting Started'' web page and looking for the latest release 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   431
identifier.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   432
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   433
I typically create a directory called @code{repos} in my home directory under
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   434
which I keep all of my local Mercurial repositories.  @emph{Hint:  I will
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   435
assume you do this later in the tutorial.} If you adopt that approach, you 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   436
can get a copy of the development version of @command{ns-3} by typing 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   437
the following into your Linux shell (I use bash).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   438
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   439
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   440
  cd
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   441
  mkdir repos
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   442
  cd !$
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   443
  hg clone http://code.nanam.org/ns-3-dev
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   444
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   445
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   446
As the hg command executes, you should see something like the following,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   447
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   448
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   449
  destination directory: ns-3-dev
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   450
  requesting all changes
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   451
  adding changesets
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   452
  adding manifests
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   453
  adding file changes
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   454
  added 1513 changesets with 5687 changes to 733 files
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   455
  358 files updated, 0 files merged, 0 files removed, 0 files unresolved
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   456
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   457
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   458
After the clone command completes, you should have a directory called 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   459
ns-3-dev under your @code{~/repos} directory, the contents of which should 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   460
look something like the following:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   461
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   462
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   463
  AUTHORS  RELEASE_NOTES  examples/  src/       waf*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   464
  LICENSE  VERSION        ns3/       tutorial/  waf.bat*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   465
  README   doc/           samples/   utils/     wscript
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   466
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   467
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   468
You are now ready to build the @command{ns-3} distribution.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   469
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   470
@section Building
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   471
@cindex Waf!build
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   472
@cindex Waf!configure
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   473
@cindex Waf!debug
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   474
@cindex Waf!compile
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   475
We use Waf to build the @command{ns-3} project.  The first thing you 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   476
will need to do is to configure the build.  For reasons that will become clear
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   477
later, we are going to work with debug builds in the tutorial.  To explain to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   478
Waf that it should do debug builds you will need to execute the following 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   479
command,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   480
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   481
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   482
  ./waf -d debug configure
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   483
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   484
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   485
This runs the copy of Waf in the local directory (which is provided as a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   486
convenience for you).  As the build system checks for various dependencies
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   487
you should see output that looks similar to the following,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   488
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   489
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   490
  ~/repos/ns-3-dev >./waf -d debug configure
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   491
  Checking for program g++                : ok /usr/bin/g++
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   492
  Checking for program cpp                : ok /usr/bin/cpp
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   493
  Checking for program ar                 : ok /usr/bin/ar
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   494
  Checking for program ranlib             : ok /usr/bin/ranlib
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   495
  Checking for compiler could create programs : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   496
  Checking for compiler could create shared libs : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   497
  Checking for compiler could create static libs : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   498
  Checking for flags -Wall                       : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   499
  Checking for flags -O2                         : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   500
  Checking for flags -g -DDEBUG                  : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   501
  Checking for flags -g3 -O0 -DDEBUG             : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   502
  Checking for g++                               : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   503
  Checking for header stdlib.h                   : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   504
  Checking for header stdlib.h                   : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   505
  Checking for header signal.h                   : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   506
  Checking for high precision time implementation: 128-bit integer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   507
  Checking for header stdint.h                   : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   508
  Checking for header inttypes.h                 : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   509
  Checking for header sys/inttypes.h             : not found
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   510
  Configuration finished successfully; project is now ready to build.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   511
  ~/repos/ns-3-dev >
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   512
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   513
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   514
The build system is now configured and you can build the debug versions of 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   515
the @command{ns-3} programs by simply typing,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   516
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   517
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   518
  ./waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   519
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   520
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   521
You will see many Waf status messages displayed as the system compiles.  The
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   522
most important is the last one,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   523
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   524
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   525
  Compilation finished successfully
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   526
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   527
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   528
@section Running a Script
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   529
@cindex Waf!run
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   530
We typically run scripts under the control of Waf.  This allows the build 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   531
system to ensure that the shared library paths are set correctly and that
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   532
the libraries are available at run time.  To run a program, simply use the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   533
@code{run} option in Waf.  Let's run the @command{ns-3} equivalent of the hello
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   534
world program by typing the following:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   535
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   536
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   537
  ./waf --run hello-simulator
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   538
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   539
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   540
Waf first checks to make sure that the program is built correctly and 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   541
executes a build if required.  Waf then then executes the program, which 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   542
produces the following output.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   543
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   544
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   545
  Hello Simulator
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   546
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   547
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   548
@emph{Congratulations.  You are now an @command{ns-3} user.}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   549
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   550
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   551
@c Some Prerequisites
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   552
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   553
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   554
@node Some-Prerequisites
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   555
@chapter Some Prerequisites
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   556
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   557
The first thing we need to do before actually starting to code is to explain
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   558
a few core concepts, abstractions and idioms in the system.  Much of this may
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   559
appear transparently obvious to some, but we recommend taking the time to read
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   560
through this chapter just to ensure you are starting on a firm foundation.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   561
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   562
@section Abstractions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   563
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   564
In this section, we'll review some terms that are commonly used in
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   565
networking, but have a specific meaning in @command{ns-3}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   566
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   567
@subsection Node
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   568
@cindex Node
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   569
In Internet jargon, a computing device that connects to a network is called
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   570
a @emph{host} or sometimes an @emph{end system}.  Because @command{ns-3} is a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   571
@emph{network} simulator, not specifically an @emph{Internet} simulator, we 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   572
intentionally do not use the term host since it is closely associated with
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   573
the Internet and its protocols.  Instead, we use a more generic term also
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
   574
used by other simulators that originates in Graph Theory --- the @emph{node}.
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   575
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   576
@cindex Node!class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   577
In @command{ns-3} the basic computing device abstraction is called the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   578
node.  This abstraction is represented in C++ by the class @code{Node}.  The 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   579
@code{Node} class provides methods for managing the representations of 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   580
computing devices in simulations.  Developers are expected to specialize the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   581
@code{Node} in the object-oriented programming sense to create new computing
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   582
device models.  In this tutorial, we will use a specialization of class 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   583
@code{Node} called @code{InternetNode}.  As you might expect, the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   584
@code{InternetNode} is a class that represents a host in the Internet sense,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   585
and automatically provides core IPv4 networking protocols.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   586
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   587
You should think of a @code{Node} as a computer to which you will add 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   588
functionality.  One adds things like applications, protocol stacks and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   589
peripheral cards with their associated drivers to enable the computer to do
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   590
useful work.  We use the same basic model in @command{ns-3}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   591
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   592
@subsection Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   593
@cindex Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   594
Typically, computer software is divided into two broad classes.  @emph{System
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   595
Software} organizes various computer resources such as memory, processor
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   596
cycles, disk, network, etc., according to some computing model.  System
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   597
software usually does not use those resources to complete tasks that directly
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   598
benefit a user.  A user would typically run an @emph{application} that acquires
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   599
and uses the resources controlled by the system software to accomplish some
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   600
goal.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   601
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   602
@cindex system call
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   603
Often, the line of separation between system and application software is made
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   604
at the privilege level change that happens in operating system traps.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   605
In @command{ns-3} there is no real concept of operating system and especially
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   606
no concept of privilege levels or system calls.  We do, however, have the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   607
idea of an application.  Just as software applications run on computers to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   608
perform tasks in the ``real world,'' @command{ns-3} applications run on
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   609
@command{ns-3} @code{Node}s to drive simulations in the simulated world.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   610
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   611
@cindex Application!class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   612
In @command{ns-3} the basic abstraction for a user program that generates some
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   613
activity to be simulated is the application.  This abstraction is represented 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   614
in C++ by the class @code{Application}.  The @code{Application} class provides 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   615
methods for managing the representations of our version of user-level 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   616
applications in simulations.  Developers are expected to specialize the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   617
@code{Application} in the object-oriented programming sense to create new
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   618
applications.  In this tutorial, we will use specializations of class 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   619
@code{Application} called @code{UdpEchoClient} and @code{UdpEchoServer}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   620
As you might expect, these applications compose a client/server application set
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   621
used to generate and echo simulated network packets 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   622
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   623
@subsection Channel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   624
@cindex Channel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   625
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   626
In the real world, one can connect a computer to a network.  Often the media
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   627
over which data flows in these netowrks are called @emph{channels}.  When
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   628
you connect your Ethernet cable to the plug in the wall, you are connecting 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   629
your computer to an Ethernet communication channel.  In the simulated world
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   630
of @command{ns-3} one connects a @code{Node} to an object representing a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   631
communication channel.  Here the basic communication subnetwork abstraction 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   632
is called the channel and is represented in C++ by the class @code{Channel}.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   633
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   634
The @code{Channel} class provides methods for managing communication 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   635
subnetwork objects and connecting nodes to them.  They may also be specialized
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   636
by developers in the object oriented programming sense.  A @code{Channel}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   637
specialization may model something as simple as a wire.  The specialized 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   638
@code{Channel} can also model things as complicated as a large Ethernet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   639
switch, or three-dimensional space in the case of wireless networks.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   640
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   641
We will use specialized versions of the @code{Channel} called
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   642
@code{CsmaChannel} and @code{PointToPointChannel} in this tutorial.  The
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   643
@code{CsmaChannel}, for example, models a version of a communication subnetwork
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   644
that implements a @emph{carrier sense multiple access} communication medium.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   645
This gives us Ethernet-like functionality.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   646
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   647
@subsection Net Device
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   648
@cindex NetDevice
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   649
@cindex Ethernet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   650
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   651
It used to be the case that if you wanted to connect a computers to a network,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   652
you had to buy a specific kind of network cable and a hardware device called
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   653
(in PC terminology) a @emph{peripheral card} that needed to be installed in
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   654
your computer.  These cards were called Network Interface Cards, or 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   655
@emph{NIC}s.  Today most computers come with the network controller hardware
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   656
built in and users don't see these building blocks.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   657
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   658
A NIC will not work without a software driver to control the hardware.  In 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   659
Unix (or Linux), a piece of peripheral hardware is classified as a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   660
@emph{device}.  Devices are controlled using @emph{device drivers}, and network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   661
devices (NICs) are controlled using @emph{network device drivers}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   662
collectively known as @emph{net devices}.  In Unix and Linux you refer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   663
to these net devices by names such as @emph{eth0}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   664
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   665
In @command{ns-3} the @emph{net device} abstraction covers both the software 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   666
driver and the simulated hardware.  A net device is ``attached'' to a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   667
@code{Node} in order to enable the @code{Node} to communicate with other 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   668
@code{Node}s in the simulation via @code{Channel}s.  Just as in a real
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   669
computer, a @code{Node} may be connected to more than one @code{Channel} via
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   670
multiple @code{NetDevice}s.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   671
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   672
The net device abstraction is represented in C++ by the class @code{NetDevice}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   673
The @code{NetDevice} class provides methods for managing connections to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   674
@code{Node} and @code{Channel} objects; and may be specialized by developers
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   675
in the object-oriented programming sense.  We will use the specialized version
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   676
of the @code{NetDevice} called the @code{CsmaNetDevice} in this tutorial.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   677
Just as an Ethernet NIC is designed to work with an Ethernet network, the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   678
@code{CsmaNetDevice} is designed to work with a @code{CsmaChannel}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   679
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   680
@subsection Topology Helpers
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   681
In a real network, you will find host computers with added (or built-in)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   682
NICs.  In @command{ns-3} we would say that you will find @code{Nodes} with 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   683
attached @code{NetDevices}.  In a large simulated network you will need to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   684
arrange many connections between @code{Node}s, @code{NetDevice}s and 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   685
@code{Channel}s.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   686
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   687
Since connecting a @code{NetDevice} to a @code{Node}, and a @code{NetDevice}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   688
to a @code{Channel} is such a common task in @command{ns-3} we provide what we
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   689
call @emph{topology helpers} to make this as easy as possible.  Topology 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   690
helpers perform much of the dirty work of creating and connecting net devices.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   691
For example, it may take several distinct method calls to create a NetDevice,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   692
add a MAC address, connect the net device to a @code{Node} and configure
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   693
the protocol stack, and then connect the @code{NetDevice} to a @code{Channel}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   694
We use topology helper functions to compose those distinct operations into
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   695
an easy to use model.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   696
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   697
Topology helper functions use the abstractions (described above) of Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   698
Interface Cards and Cables.  When you think of adding a new kind of network,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   699
you may think of going out to the local computer retailer and buying a kit.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   700
This kit might include a nework cable and some number of peripheral cards and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   701
thier associated software drivers.  You can think of topology helpers in 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   702
roughly the same way.  Instead of buying a kit for a given type of network,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   703
you will use a topology helper class for a given type of network, to accomplish
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   704
the equivalent of installing the network ``kit.''
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   705
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   706
@section Important Idioms
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   707
Now that we have identified that there are C++ classes in the system called
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   708
@code{Node} and @code{InternetNode}, we need to understand how to bring
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   709
objects of these classes into existance, and manage their lifetimes.  Let's
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   710
examine this in some detail here.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   711
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   712
@cindex InternetNode
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   713
@cindex Create
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   714
@cindex Ptr
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   715
In @command{ns-3}, if we want to create an @code{InternetNode} in a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   716
script, we will 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   717
typically do something like the following example:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   718
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   719
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   720
  Ptr<Node> p = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   721
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   722
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   723
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   724
To some, it may seem intuitively obvious that we're creating an
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   725
@code{InternetNode} object and assigning responsibility for managing the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   726
object to a smart pointer named @code{p}.  For the rest of us, there may be
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   727
a lot in that line that is unfamiliar, so let's look at what this line means 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   728
in some detail.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   729
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   730
@subsection Templates 101
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   731
@cindex template
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   732
If you are familiar with C++ templates, you may skip this section as it is
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   733
just a cursory introduction to function and class templates.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   734
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   735
Referring back to the example line of code, reproduced below for your 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   736
convenience, the angle brackets you see in the code indicate that we are
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   737
using C++ @emph{templates}.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   738
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   739
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   740
  Ptr<Node> p = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   741
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   742
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   743
The purpose of templates is to allow a programmer to write one version of code
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   744
that is applicable over multiple types.  Some people consider templates to be
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   745
an enhancement of the C preprocessor macro functionality.  At some level
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   746
this comparison reveal some similarities, but C++ templates are really 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   747
quite different.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   748
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   749
@cindex template!declaration
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   750
@cindex template!definition
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   751
@cindex template!use
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   752
In C++, just as with most language constructs, templates are @emph{declared}, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   753
@emph{defined} and @emph{used}.  A declaration of a template might look
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   754
something like,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   755
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   756
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   757
  template <typename T> T Add (T first, T second);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   758
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   759
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   760
@cindex template!typename
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   761
This line uses the keyword @code{template} followed by a declaration of a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   762
type name (in this case @code{T}) in angle brackets.  The angle brackets 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   763
should indicate to you that a template is being declared, defined or used.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   764
The type name @code{T} can be thought of as a string that will be substitited
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   765
during the use phase of the template.  For example, the @code{T} may be
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   766
replaced by the word @code{int}.  It is this substitution that leads people
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   767
to compare templates with macros.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   768
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   769
Without going into too much more detail, this snippet declares that a piece
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   770
of code exists that will be able to call a function @code{Add} that will
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   771
add arbitrary types together.  The @code{T} will be eventually replaced by
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   772
a C++ data type name.  For example,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   773
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   774
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   775
  T Add (T first, T second);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   776
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   777
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   778
might eventually become
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   779
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   780
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   781
  int Add (int first, int second);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   782
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   783
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   784
If the template has been declared, we need to @emph{define} what that piece of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   785
code will actually do.  That might look something like,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   786
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   787
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   788
  template <typename T> 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   789
  T Add (T first, T second)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   790
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   791
    return first + second;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   792
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   793
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   794
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   795
All we've done here is to provide an implementation of the template that 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   796
adds the two variables together and returns the result.  Note that this
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   797
implementation works for any type that provides an @code{operator+}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   798
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   799
The puzzle all comes together when you understand that @emph{using} a template
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   800
causes the compiler to automatically instantiate code for a specific function 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   801
according to the given template parameters.  You might use the above template
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   802
like,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   803
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   804
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   805
  int x, y, z;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   806
  z = Add<int> (x, y);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   807
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   808
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   809
@cindex template!instantiate
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   810
When the compiler sees @code{Add<int>} it understands that it needs to make
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   811
sure that code is instantiated (created) to perform the @code{Add} using the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   812
specified type @code{<int>}.  To a first approximation, the compiler will 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   813
replace the typename @code{T} with the specified type @code{int} and 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   814
automagically generate code equivalent to,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   815
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   816
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   817
  int Add (int first, int second)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   818
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   819
    return first + second;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   820
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   821
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   822
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   823
A user of the template definition could just as easily have provided a use
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   824
that assigned the type float.  This would simply be done like,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   825
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   826
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   827
  float x, y, z;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   828
  z = Add<float> (x, y);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   829
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   830
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   831
In this case, the compiler would automatically generate code that looked like,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   832
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   833
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   834
  float Add (float first, float second)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   835
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   836
    return first + second;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   837
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   838
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   839
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   840
@cindex template!function
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   841
This particular kind of template programming uses what are called
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   842
@emph{function templates}.  They are called function templates since you 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   843
are @emph{templating} function declarations and definitions.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   844
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   845
@cindex template!class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   846
Templates can also be used in conjunction with classes, in which case you are
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   847
said to be using, not too surprisingly, @emph{class templates}.  The syntax and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   848
use is similar.  To declare a class template you might use something like,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   849
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   850
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   851
  template <typename T> 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   852
  class MyStack
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   853
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   854
    void Push (T data);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   855
    T Pop (void);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   856
  };
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   857
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   858
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   859
The methods can be defined separately in a method similar to function template
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   860
definitions,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   861
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   862
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   863
  template <typename T> void MyStack<T>::Push (T data)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   864
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   865
    ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   866
  };
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   867
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   868
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   869
You can then use the new templated class in the following way, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   870
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   871
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   872
  int x, y;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   873
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   874
  MyStack<int> stack;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   875
  stack.Push (x);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   876
  y = stack.Pop ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   877
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   878
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   879
Similarly to the function template case, the compiler knows that it has to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   880
automatically generate code to fill out the class and method declarations
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   881
and definitions using the appropriate type specified by @code{<int>}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   882
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   883
@subsection Smart Pointers 101
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   884
If you are familiar with C++ smart pointers, you may skip this section as it
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   885
is just a cursory introduction to smart pointers and intrusive reference
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   886
counting.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   887
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   888
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   889
Referring back to the example line of code, partially reproduced below for 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   890
your convenience below, the left hand side is the declaration and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   891
initialization of a class template that implements a @emph{smart pointer}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   892
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   893
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   894
  Ptr<Node> p = ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   895
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   896
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   897
To a first approximation, you can think of @code{Ptr<Node>} as the a new kind
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   898
of declaration of a pointer to a @code{Node} object.  The difference is that
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   899
a smart pointer is a user-defined data type (instantiated via a templated
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   900
class) that @emph{simulates} a classical pointer but provides additional 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   901
features.  As an aside, you typically pronounce  @code{Ptr<Node>} as 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   902
``pooter node'' where pooter rhymes with footer.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   903
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   904
@cindex memory management
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   905
One of the most important ``additional feature'' provided by smart pointers is
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   906
automatic memory management.  Since you now understand class templates, you
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   907
will understand how the template allows us to write the pointer code once, but
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   908
allows us to point to many different kinds of objects.  Later in the tutorial
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   909
you will see variations such as @code{Ptr<Ipv4>} and @code{Ptr<Channel>}, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   910
which are smart pointers to an IP version 4 object and a channel object, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   911
respectively.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   912
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   913
The use of built-in pointers in C and C++ is a major source of bugs.  Constant
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   914
allocation of, passing of responsibility for, and deallocation of underlying
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   915
data makes it very likely that errors will occur.  In one of these errors,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   916
the usual problem is that the responsibility for deallocating a memory block
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   917
is misplaced.  This may result in a memory leak or a duplicate deallocation.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   918
Smart pointers try to prevent this kind of problem by working with the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   919
@emph{scope} and @emph{extent} rules of the language to make memory 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   920
deallocation automatic.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   921
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   922
The scope of a variable defines where in a program a given variable may be 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   923
referred to.  The extent of a variable defines when in the program's execution
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   924
the variable has a valid value.  Consider a simple subroutine that contains a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   925
smart pointer.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   926
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   927
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   928
  void SimpleSubroutine (void)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   929
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   930
    Ptr<Node> p;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   931
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   932
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   933
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   934
@cindex scope
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   935
The variable named @code{p} has a scope limited to the subroutine itself.  The 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   936
variable is said to @emph{come into scope} as the subroutine is entered during
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   937
execution.  At this time, the constructor of the underlying class is executed
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   938
and a valid variable is available for use.  When the subroutine is done 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   939
executing, the variable is said to @emph{go out of scope}.  This causes the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   940
destructor of the underlying class to be executed and the variable no longer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   941
has a valid value.  This is not a problem since it is no longer valid to refer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   942
to the parameter.  Smart pointers take advantage of these defined actions at
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   943
points where variables must be valid and become discardable to determine when
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   944
underlying data can be freed.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   945
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   946
@cindex reference counting!intrusive
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   947
The @command{ns-3} smart pointer mechanism uses a mechanism called intrusive 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   948
reference counting to determine when a memory block should be automatically 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   949
deallocated.  The term ``intrusive'' means that a reference count (a count of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   950
variables required to have valid data) is stored in the object being managed
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   951
instead of in a proxy object.  This means that each piece of memory managed by
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   952
a @command{ns-3} smart pointer includes a reference count.  When a smart 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   953
pointer to a reference counted object is created, this reference count is 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   954
incremented.  This indicates that a new variable requires a valid data object 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   955
be present.  When a smart pointer to a reference counted object is destroyed
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   956
(for example, when going out of scope) the reference count of the managed 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   957
object is decremented.  When the reference count goes to zero it means that 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   958
all smart pointers to the underlying object have gone out of scope and the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   959
object is no longer needed by any past ``users'' of the object.  This in turn
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   960
means that the object can be safely deallocated, and this is done 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   961
automatically for you as the ``last'' smart pointer goes out of scope.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   962
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   963
Consider how this might work as you pass a smart pointer to an object down
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   964
a protocol stack.  At each level of the stack, you pass the smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   965
by value.  This causes a copy of the smart pointer to be made, which 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   966
increments the reference count of the underlying object.  When the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   967
@emph{calling} method is done executing, the calling smart pointer goes out of 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   968
scope and  the reference count is decremented.  This leaves the single smart 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   969
pointer in the @emph{called} method with a reference to the underlying object.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   970
When the smart pointer in the called method goes out of scope, the destructor
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   971
for the smart pointer is called.  The destructor checks the reference count
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   972
of the underlying object and sees that it becomes zero.  This indicates that 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   973
the object can be deallocated, and the destructor does so.  This results in
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   974
the lifetime management of the underlying object being automatically managed,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   975
a boon if you have experience with ``manual'' memory management and finding
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   976
memory leaks.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   977
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   978
Now, we want to make this feature available as widely as possible to objects
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   979
in the @command{ns-3} system.  The basic operations of the smart pointer class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   980
are the same across any intrusively reference counted object.  C++ provides a
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
   981
mechanism to achieve this kind of generic behavior --- the template.  Let's
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   982
examine the declaration of the smart pointer in more detail.  First consider
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   983
the way you might declare and use a built-in pointer.  For the sake of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   984
simplicity, just assume that a C++ object of the class @code{MyClass} exists.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   985
Further assume that @code{MyClass} provides one method called @code{method}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   986
Using built-in pointers, you could do something like the following:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   987
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   988
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   989
  MyClass *p = ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   990
  p->method ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   991
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   992
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   993
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   994
One of the key design points of smart pointers is that they should simulate
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   995
built-in pointers.  In C++ this is done by overloading @code{operator->}, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   996
@code{operator=} and @code{operator*}.  To implement a smart pointer we need
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   997
to provide a generic class that implements these operators.  This generic 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   998
class should allow operations that appear as if it were a built-in pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   999
to the reference counted object. Typically this is accomplished via a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1000
relatively simple C++ class template.  If you are interested in the details
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1001
of how this may be accomplished, see Alexandrescu for a good treatment,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1002
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1003
@cindex template
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1004
Taking the template as given, in order to declare a smart pointer you will
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1005
need to create a smart pointer object and provide the template parameter
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1006
needed to instantiate the required code.  This parameter will be the name
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1007
of the reference counted class to which you want to point.  The smart
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1008
pointer class overrides @code{operator=} which allows initialization of the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1009
smart pointer just as if it were a built-in pointer.  The end result is that
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1010
you use smart pointers just as if they were built-in pointers:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1011
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1012
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1013
  SmartPointer<MyClass> p = ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1014
  p->method ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1015
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1016
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1017
@subsection Object Creation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1018
@cindex Create
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1019
On the right hand side of the line of code we're examining (reproduced below
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1020
for convenience) is the creation of an @code{InternetNode} object.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1021
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1022
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1023
  ... = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1024
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1025
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1026
@cindex template!function
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1027
This turns out to be an instance of use of a C++ @emph{function template}. The
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1028
definition of the @code{Create<typename T>()} template calls the new operator
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1029
to create an object of the type T.  It then creates a new smart pointer of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1030
the appropriate type (i.e., @code{Ptr<T>}).  This new smart pointer is
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1031
assigned initial responsibility for the new object which has its reference
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1032
count set to one.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1033
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1034
Since the underlying creation mechanism is via the @code{new} operator, and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1035
you can pass parameters to the constructor for an object, we provide several
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1036
templates that you can use for passing parameters to the object constructors.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1037
If the constructor for the object requires a parameter, you simply pass that
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1038
parameter to the @code{Create} function like this,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1039
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1040
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1041
  int parm = 1;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1042
  ... = Create<MyClass> (parm);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1043
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1044
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1045
We provide Create templates with up to seven parameters, so you could 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1046
conceivably use the @code{Create} template in situations such as,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1047
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1048
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1049
  int parm = 1;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1050
  ... = Create<MyClass> (p1, p2, p3, p4, p5, p6, p7);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1051
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1052
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1053
@subsection Type Safety
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1054
Lets take one final look at the now infamous example line of code that we
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1055
have been examining for some time (again reproduced below).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1056
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1057
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1058
  Ptr<Node> p = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1059
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1060
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1061
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1062
@cindex Node
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1063
@cindex Create
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1064
You may have noticed that the smart pointer on the left hand side of the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1065
assignment is associated with the type @code{Node} and the @code{Create}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1066
template on the right hand side creates an @code{InternetNode} object and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1067
returns a @code{Ptr<InternetNode>} smart pointer.  For this assignment of a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1068
@code{Ptr<InternetNode>} to a @code{Ptr<Node>} to work, there must be some
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1069
kind of type conversion going on.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1070
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1071
@cindex implicit conversion
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1072
Many programmers use @code{implicit conversions} without even realizing it
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1073
since they are sometimes so intuitive.  For example, in the following code,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1074
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1075
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1076
  int i = 1;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1077
  double d = 2.;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1078
  if (n == d) ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1079
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1080
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1081
@cindex standard conversion
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1082
the integer (1) is implicitly converted to a double (1.) before the comparison
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1083
takes place.  This conversion is performed using what is known as a C++ 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1084
@emph{standard conversion}.  There are a number of standard conversions defined
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1085
by the C++ standard.  Among them are,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1086
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1087
@itemize @bullet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1088
@item Integral Promotions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1089
@item Integral Conversions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1090
@item Floating Conversions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1091
@item Pointer Conversions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1092
@item Reference Conversions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1093
@end itemize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1094
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1095
@cindex assignment operator
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1096
@cindex Ptr
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1097
For the case of interest here, we need to know what happens in the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1098
assignment operator (@code{operator=}) of our smart pointer @code{Ptr<Node>}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1099
This operator takes a reference to a @code{Ptr<Node>} and not a reference to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1100
a @code{Ptr<InternetNode>}.  The one situation where this works automatically
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1101
in C++ is if the ``destination'' reference is to a visible, unambiguous base
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1102
class of the ``source'' reference.  In this case, the underlying pointer is
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1103
@emph{cast} from one type to the other automatically.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1104
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1105
To summarize: The magic happens in the assignment operator.  Class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1106
@code{InternetNode} inherits from class @code{Node}.  The reference to the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1107
@code{InternetNode} object in question is, in essence, a pointer to an
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1108
@code{InternetNode} object.  The @code{InternetNode} class inherits from the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1109
@code{Node} base class in a way that makes @code{Node} visible and unambiguous.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1110
Therefore, there exists a standard conversion from an @code{InternetNode *} 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1111
to a @code{Node *} and by extension from an @code{InternetNode &} to a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1112
@code{Node &}.  This conversion is applied automatically (and invisibly) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1113
during paramater passing in the assignment operator we are examining.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1114
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1115
@cindex base class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1116
This is a rather involved way of saying there's an invisible pointer cast
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1117
to a base class happening in the assignment.  That means that
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1118
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1119
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1120
  Ptr<Node> p = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1121
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1122
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1123
or,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1124
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1125
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1126
  Ptr<Channel> p = Create<CsmaChannel> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1127
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1128
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1129
will work just fine.  Of course, if you try something @emph{bad} (TM), like:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1130
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1131
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1132
  Ptr<Node> p = Create<CsmaChannel> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1133
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1134
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1135
the compiler will quite appropriately complain that there is no conversion
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1136
between these completely unrelated objects (CsmaChannel and Node).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1137
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1138
@subsection Summary
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1139
Going back to our infamous first line of @command{ns-3} code, we said that if 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1140
we want to create an InternetNode in a script, we will typically do something 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1141
like:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1142
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1143
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1144
  Ptr<Node> p = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1145
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1146
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1147
@cindex Create
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1148
@cindex InternetNode
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1149
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1150
Now we know that this is really a simple statement.  We create an
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1151
@code{InternetNode} object on the heap (indirecly using operator @code{new}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1152
and passing no parameters to its constructor) and assign responsibility for
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1153
managing the new object's lifetime to a smart pointer.  This smart pointer is 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1154
a pointer to a @code{Node} object, so there was a hidden cast from 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1155
@code{InternetNode} to a @code{Node} done via a standard C++ conversion.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1156
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1157
This may have been quite a hurdle to get past that first line of code, but
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1158
we have covered quite a few of the important idioms that you'll encounter in
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1159
this tutorial.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1160
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1161
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1162
@c A First ns-3 script
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1163
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1164
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1165
@node A-First-ns-3-Script
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1166
@chapter A First ns-3 script
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1167
@cindex design pattern
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1168
@cindex idiom
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1169
Lets build a simple network using the @command{ns-3} design patterns, idioms,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1170
classes and helpers we have just looked at.  If you downloaded the system as
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1171
was suggested above, you will have a release of @command{ns-3} in a directory 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1172
called @code{repos} under your home directory.  Change into that directory, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1173
where you should see a directory structure something like the following.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1174
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1175
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1176
  AUTHORS  RELEASE_NOTES  examples/  src/       waf*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1177
  LICENSE  VERSION        ns3/       tutorial/  waf.bat*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1178
  README   doc/           samples/   utils/     wscript
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1179
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1180
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1181
@cindex hello-simulator.cc
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1182
Change into the tutorial directory.  You should see a file named 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1183
@code{hello-simulator.cc} located there.  Copy this file into one named
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1184
@code{simple.cc}.  If you open this new file in your favorite editor you will
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1185
see some copyright information and the following C++ code:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1186
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1187
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1188
  #include "ns3/log.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1189
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1190
  NS_LOG_COMPONENT_DEFINE ("HelloSimulator");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1191
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1192
  using namespace ns3;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1193
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1194
  int 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1195
  main (int argc, char *argv[])
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1196
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1197
    LogComponentEnable ("HelloSimulator", LOG_LEVEL_INFO);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1198
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1199
    NS_LOG_INFO ("Hello Simulator");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1200
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1201
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1202
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1203
This is the @command{ns-3} version of the ubiquitous hello-world program.  It 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1204
uses the @command{ns-3} Log module to print ``Hello Simulator'' into the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1205
 standard error output stream.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1206
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1207
@cindex logging
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1208
Log components are named objects that provide for controlling the verbosity of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1209
debugging output in the system.  We'll have a lot more to say about logging
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1210
later on, but for now you can just consider the macro @code{NS_LOG_INFO} to be
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1211
a kind of fancy printf to the standard error.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1212
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1213
@section A Simple Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1214
@cindex InternetNode
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1215
Let's create a simple network of @code{InternetNode} elements.  In order to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1216
actually create an @code{InternetNode}, you will have to include some header
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1217
files.  Put the following code after the include statement in @code{simple.cc}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1218
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1219
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1220
  #include "ns3/ptr.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1221
  #include "ns3/internet-node.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1222
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1223
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1224
@cindex include files
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1225
The @command{ns-3} build system places the core include files it needs into a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1226
directory called @code{ns-3} and so whenever you need to include one of the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1227
core files you need to explicitly code this.  The file @code{ptr.h} defines
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1228
the generic smart pointer that we use.  The file @code{internet-node.h}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1229
defines the class InternetNode which, as described above, represents an IP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1230
version 4-based computing element in the simulator.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1231
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1232
So let's create a few new @code{InternetNode}s by adding the following lines
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1233
of code after the call to @code{NS_LOG_INFO} in the simple.cc file right 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1234
after the call to @code{NS_LOG_INFO}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1235
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1236
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1237
  Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1238
  Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1239
  Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1240
  Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1241
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1242
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1243
As we now understand, this will create four @code{InternetNode} objects on
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1244
the heap and create four @code{Ptr<Node>} smart pointer objects on the stack
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1245
to manage them.  You should remember that by using the smart pointers you are 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1246
freed from the responsibility to delete the objects you assign to them.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1247
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1248
@cindex Channel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1249
@cindex CsmaChannel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1250
The next step is to create a channel over which these nodes can communicate.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1251
Let's use the CsmaChannel and create a local area network that will allow us 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1252
to hook up nodes similarly to an Ethernet.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1253
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1254
As usual, we'll need to include the file that provides the appropriate class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1255
declarations:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1256
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1257
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1258
  #include "ns3/csma-channel.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1259
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1260
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1261
Next, Add the following line of code (typically done after node creation) to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1262
create a channel with a five megabit per second data rate and a two 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1263
millisecond speed-of-light delay between all nodes.  The idiom for creating
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1264
the channel is similar to that of the node, but the actual @code{Create}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1265
function is hidden from us in the topology code.  Observe that we are 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1266
using a Csma topology helper function to free us from the details regarding
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1267
how the Carrier Sense Multiple Access Channel is actually brought into
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1268
existence and initialized.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1269
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1270
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1271
  Ptr<CsmaChannel> lan = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1272
    CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1273
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1274
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1275
@cindex idiom!unnamed parameter
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1276
You may be unfamiliar with the @emph{unnamed parameter} idiom used here.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1277
When added to a list of parameters, the code @code{DataRate (5000000)} 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1278
constructs a DataRate object on the stack using the appropriate  constructor.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1279
The resulting object has no name, and therefore cannot be referenced 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1280
elsewhere, but is passed to the callee method where it has a valid name and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1281
can be used.  This idiom is essentially a shorthand version of the following:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1282
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1283
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1284
  DataRate rate (5000000);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1285
  Time latency (MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1286
  Ptr<CsmaChannel> lan = CsmaTopology::CreateCsmaChannel (rate, latency);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1287
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1288
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1289
@cindex constructor
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1290
@cindex constructor!Time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1291
We should pause for a moment and discuss the constructor to the @code{Time}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1292
data type.  There are a number of different constructors for these objects, and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1293
so there are a number of ways that this initialization could have been done.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1294
There is a constructor that takes a string argument, consisting of expressions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1295
using the units @code{s, ms, us, ns, ps} or @code{fs}, so this could have been
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1296
written,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1297
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1298
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1299
  Time latency ("2ms");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1300
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1301
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1302
There are also helper functions available that create time units (one of these
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1303
was used in the example):
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1304
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1305
@itemize @bullet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1306
@item @code{Seconds (double)}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1307
@item @code{MilliSeconds (uint64_t)}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1308
@item @code{MicroSeconds (uint64_t)}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1309
@item @code{NanoSeconds (uint64_t)}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1310
@item @code{PicoSeconds (uint64_t)}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1311
@item @code{FemtoSeconds (uint64_t)}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1312
@end itemize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1313
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1314
C++ will attempt to promote parameters appropriately, but you will typically
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1315
see constructions that respect the type corrrectness of the constructor, as
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1316
in @code{Seconds (1.)} and @code{MilliSeconds (2)}.  Notice that the code
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1317
@code{Seconds (1)} will work just as well as @code{Seconds (1.)} since the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1318
integer 1 will be automatically promoted to a double 1. in the former code.
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  1319
The converse will not work --- i.e., you cannot write code that says 
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1320
@code{MilliSeconds (2.)} since a @emph{type demotion} would be required that 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1321
could lose information and the compiler will not do such things ``behind your
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1322
back.''  Don't be thrown off by this kind of automatic conversion.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1323
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1324
@cindex MAC!address
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1325
Okay, now we have code to create four nodes and a local area network.  The 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1326
next step is to wire the network together.  We do this by adding net devices 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1327
to the node.  When we add the net device, we also specify the network to which
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1328
the net device is connected and provide a MAC address appropriate to the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1329
device and network types.  Since we're creating an IP version 4 network using 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1330
a Csma channel, you may expect that we'll be using topology helpers 
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  1331
appropriate to those types --- the CsmaIpv4Topology helper.  As you may expect,
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1332
we'll need to include some files to get the appropriate definitions:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1333
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1334
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1335
  #include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1336
  #include "ns3/csma-net-device.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1337
  #include "ns3/csma-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1338
  #include "ns3/csma-ipv4-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1339
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1340
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1341
Now, all that is left is to do the ``wiring'':
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1342
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1343
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1344
  uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1345
    Mac48Address("08:00:2e:00:00:00"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1346
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1347
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1348
[Note the additional unnamed parameter idiom usage here.]
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1349
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1350
This code calls the topology helper relating to Csma channels and IP version
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1351
four nodes.  It asks to install a Csma net device ``into'' node zero
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1352
(@code{n0}) connecting the device to the channel named (@code{lan}).  It also 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1353
assigns a MAC address to the net device.  You can add similar lines of code 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1354
connecting the other nodes to the lan (remembering to assign new MAC 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1355
addresses).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1356
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1357
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1358
  uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1359
    Mac48Address("08:00:2e:00:00:01"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1360
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1361
  uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1362
    Mac48Address("08:00:2e:00:00:02"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1363
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1364
  uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1365
    Mac48Address("08:00:2e:00:00:03"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1366
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1367
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1368
@cindex IP!address
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1369
@cindex IP!network mask
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1370
@cindex multihome
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1371
Finally, we need to add IP addresses to our nodes.  The pointers to the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1372
nodes are stored in n0, n1, n2 and n3.  We added net devices to each of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1373
the nodes and remembered the net device index numbers as nd0, nd1, nd2 and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1374
nd3.  You can add multiple net devices to each node resulting in a situation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1375
similar to a multi-homed host.  Each time you add a net device, you will get
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1376
a new index.  Since the IP address for a multi-homed host is associated with
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1377
a net device, we need to provide that index (which we have saved) to the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1378
topology helper.  We provide an IP version four address via the @command{ns-3} 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1379
class @code{Ipv4Address} which takes a dotted decimal string as a constructor 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1380
parameter.  We also provide a network mask using the @command{ns-3} class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1381
@code{Ipv4Mask} which also takes a dotted decimal string.  The code to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1382
perform the IP address assignment, then, looks like the following:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1383
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1384
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1385
  CsmaIpv4Topology::AddIpv4Address (n0, nd0, Ipv4Address ("10.1.1.1"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1386
    Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1387
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1388
  CsmaIpv4Topology::AddIpv4Address (n1, nd1, Ipv4Address ("10.1.1.2"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1389
    Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1390
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1391
  CsmaIpv4Topology::AddIpv4Address (n2, nd2, Ipv4Address ("10.1.1.3"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1392
    Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1393
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1394
  CsmaIpv4Topology::AddIpv4Address (n3, nd3, Ipv4Address ("10.1.1.4"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1395
    Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1396
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1397
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1398
We have now constructed a simulated network.  Your code should now look 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1399
something like the following,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1400
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1401
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1402
  #include "ns3/log.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1403
  #include "ns3/ptr.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1404
  #include "ns3/internet-node.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1405
  #include "ns3/csma-channel.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1406
  #include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1407
  #include "ns3/csma-net-device.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1408
  #include "ns3/csma-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1409
  #include "ns3/csma-ipv4-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1410
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1411
  NS_LOG_COMPONENT_DEFINE ("HelloSimulator");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1412
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1413
  using namespace ns3;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1414
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1415
  int 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1416
  main (int argc, char *argv[])
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1417
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1418
    LogComponentEnable ("HelloSimulator", LOG_LEVEL_INFO);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1419
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1420
    NS_LOG_INFO ("Hello Simulator");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1421
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1422
    Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1423
    Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1424
    Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1425
    Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1426
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1427
    Ptr<CsmaChannel> lan = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1428
      CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1429
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1430
    uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1431
      Mac48Address("08:00:2e:00:00:00"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1432
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1433
    uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1434
      Mac48Address("08:00:2e:00:00:01"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1435
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1436
    uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1437
      Mac48Address("08:00:2e:00:00:02"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1438
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1439
    uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1440
      Mac48Address("08:00:2e:00:00:03"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1441
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1442
    CsmaIpv4Topology::AddIpv4Address (n0, nd0, Ipv4Address ("10.1.1.1"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1443
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1444
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1445
    CsmaIpv4Topology::AddIpv4Address (n1, nd1, Ipv4Address ("10.1.1.2"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1446
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1447
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1448
    CsmaIpv4Topology::AddIpv4Address (n2, nd2, Ipv4Address ("10.1.1.3"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1449
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1450
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1451
    CsmaIpv4Topology::AddIpv4Address (n3, nd3, Ipv4Address ("10.1.1.4"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1452
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1453
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1454
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1455
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1456
This script won't actually do anything yet.  The next trick will be to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1457
convince our nodes to try and send some data over the network.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1458
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1459
@section Using Applications
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1460
@cindex Create
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1461
As mentioned above, we use @code{Application}s in @command{ns-3} to generate 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1462
the data used to drive simulations.  An @code{Application} is added to a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1463
@command{ns-3} node conceptually just as if you would add an application to a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1464
computer.  When an application is created (using the @code{Create} template) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1465
we tell the application which @code{Node} it belongs to (and therefore on 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1466
which node it is running) by passing a smart pointer to that @code{Node} in 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1467
the constructor arguments.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1468
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1469
@subsection A UDP Echo Client Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1470
To use an application, we first have to load the header file in which it is
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1471
defined.  For the UDP echo client, this would mean adding the line,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1472
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1473
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1474
#include "ns3/udp-echo-client.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1475
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1476
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1477
In order to create the UDP echo client application we will need to add the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1478
following code:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1479
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1480
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1481
  uint32_t packetSize = 1024;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1482
  uint16_t port = 7;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1483
  uint32_t maxPacketCount = 1;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1484
  Time interPacketInterval = Seconds (1.);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1485
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1486
  Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.1.2", port, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1487
    maxPacketCount, interPacketInterval, packetSize);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1488
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1489
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1490
@cindex packet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1491
The first four lines have broken out the configuration parameters for the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1492
application as named parameters for clarity.  We are telling the application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1493
to generate 1024 byte packets (@code{packetSize = 1024}); and to send these
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1494
packets to port 7 (@code{port = 7;}).  The application is told to send at most
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1495
one packet (@code{maxPacketCount = 1;}); and to delay for one second between
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1496
packet sends (@code{interpacketInterval = Seconds(1.)}) which is not used since
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1497
only one packet is sent.  We will defer addressing the type @code{Time} until
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1498
we discuss the simulator engine.  For now just understand the semantics are
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1499
to wait for one second.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1500
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1501
The code to actually create the @code{UdpEchoClient} application uses the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1502
same creation idiom as we have used previously.  Notice that we have a case
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1503
where the @code{Create} template is used to pass parameters to the constructor
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1504
of the underlying object.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1505
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1506
@cindex implicit conversion sequence
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1507
Notice that a string is passed as the second parameter.  The formal parameter
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1508
to the constructor of the @code{UdpEchoClient} object is actually an
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1509
@code{Ipv4Address}.  We get away with this since C++ allows what are called
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1510
@emph{implicit conversion sequences} to occur between the argument in the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1511
function call and the corresponding parameter in the function declaration.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1512
Basically, C++ will try to figure out a way to convert parameters for you
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1513
transparently.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1514
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1515
In this case the conversion sequence is based on the constructor for the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1516
Ipv4Address that takes a @code{char const *} as a parameter.  C++ notices 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1517
that @code{"10.1.1.2"} refers to a @code{char const *} and knows that it
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1518
needs to get from there to an @code{Ipv4Address}.  The compiler notices that
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1519
there is an @code{Ipv4Address} constructor that takes a @code{char const *} 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1520
and so it uses that constructor transparently to arrange for the conversion.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1521
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1522
You therefore have several options for passing this value.  You can use an 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1523
explicit named variable as in the following:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1524
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1525
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1526
  Ipv4Address addr ("10.1.1.2");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1527
  ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1528
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1529
  Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, addr, port, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1530
    maxPacketCount, interPacketInterval, packetSize);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1531
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1532
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1533
@cindex idiom|unnamed parameter
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1534
You can use the unnamed parameter idiom that we have previously seen:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1535
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1536
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1537
  Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1538
    Ipv4Address ("10.1.1.2"), port, maxPacketCount, interPacketInterval, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1539
    packetSize);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1540
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1541
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1542
Or you can rely on implicit conversion sequences as we just saw:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1543
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1544
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1545
  Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.1.2", port, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1546
    maxPacketCount, interPacketInterval, packetSize);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1547
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1548
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1549
Which approach to take is a matter of style, really, and you will probably
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1550
see all three approaches taken in the @command{ns-3} code.  You should be 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1551
comfortable seeing and using all three methods.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1552
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1553
@subsection A UDP Echo Server Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1554
As usual, to use the UDP echo server we need to add a line to define the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1555
application:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1556
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1557
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1558
#include "ns3/udp-echo-server.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1559
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1560
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1561
In order to create the UDP echo server application we will need to add the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1562
following code:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1563
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1564
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1565
  Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1566
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1567
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1568
We only need to tell the application which node to reside on and which port
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1569
to listen on for UDP packets.  The code to actually create the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1570
@code{UdpEchoServer} application uses the now quite familiar @command{ns-3} object
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1571
creation idiom.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1572
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1573
@subsection A UDP Echo Client-Server Simulation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1574
Now we're getting somewhere.  Your code should look something like the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1575
following (let's change the log component name and program banner from
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1576
``Hello Simulator''to something more descriptive while we're at it).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1577
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1578
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1579
  #include "ns3/log.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1580
  #include "ns3/ptr.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1581
  #include "ns3/internet-node.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1582
  #include "ns3/csma-channel.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1583
  #include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1584
  #include "ns3/csma-net-device.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1585
  #include "ns3/csma-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1586
  #include "ns3/csma-ipv4-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1587
  #include "ns3/udp-echo-client.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1588
  #include "ns3/udp-echo-server.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1589
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1590
  NS_LOG_COMPONENT_DEFINE ("UdpEchoSimulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1591
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1592
  using namespace ns3;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1593
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1594
  int 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1595
  main (int argc, char *argv[])
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1596
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1597
    LogComponentEnable ("UdpEchoSimulation", LOG_LEVEL_INFO);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1598
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1599
    NS_LOG_INFO ("UDP Echo Simulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1600
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1601
    Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1602
    Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1603
    Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1604
    Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1605
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1606
    Ptr<CsmaChannel> lan = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1607
      CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1608
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1609
    uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1610
      Mac48Address("08:00:2e:00:00:00"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1611
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1612
    uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1613
      Mac48Address("08:00:2e:00:00:01"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1614
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1615
    uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1616
      Mac48Address("08:00:2e:00:00:02"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1617
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1618
    uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1619
      Mac48Address("08:00:2e:00:00:03"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1620
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1621
    CsmaIpv4Topology::AddIpv4Address (n0, nd0, Ipv4Address ("10.1.1.1"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1622
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1623
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1624
    CsmaIpv4Topology::AddIpv4Address (n1, nd1, Ipv4Address ("10.1.1.2"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1625
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1626
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1627
    CsmaIpv4Topology::AddIpv4Address (n2, nd2, Ipv4Address ("10.1.1.3"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1628
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1629
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1630
    CsmaIpv4Topology::AddIpv4Address (n3, nd3, Ipv4Address ("10.1.1.4"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1631
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1632
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1633
    uint32_t packetSize = 1024;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1634
    uint16_t port = 7;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1635
    uint32_t maxPacketCount = 1;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1636
    Time interPacketInterval = Seconds (1.);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1637
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1638
    Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.1.2", port, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1639
      maxPacketCount, interPacketInterval, packetSize);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1640
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1641
    Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1642
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1643
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1644
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1645
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1646
@section Using the Simulation Engine
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1647
@cindex model
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1648
@cindex simulation executive
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1649
You could say that the heart of the @command{ns-3} system is the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1650
@emph{simulation engine} (sometimes called the simulation executive in other 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1651
systems).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1652
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1653
In a computer simulation, a computer @emph{model} of a real world @emph{system}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1654
is constructed.  This is typically done to minimize cost since you do not have
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1655
to actually buy, install and maintain physical hardware.  In the case of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1656
@command{ns-3}, a model is a representation of a networking component that is
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1657
designed to imitate some number of important behaviors or characteristics of 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1658
an actual component in a real network.  A system is a collection of models
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1659
arranged for the purpose of analyzing some behavior.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1660
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1661
@section Models
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1662
@cindex CsmaNetDevice
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1663
@cindex CsmaChannel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1664
@cindex InternetNode
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1665
@cindex NIC
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1666
@cindex CSMA
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1667
We have already encountered several @command{ns-3} models without specifically 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1668
calling them so.  The @code{InternetNode}, @code{CsmaNetDevice} and 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1669
@code{CsmaChannel} objects are models of an Internet computing node, a CSMA
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1670
network interface card (NIC), and a network cable able to move data to and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1671
from other CSMA NICs.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1672
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1673
@cindex model
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1674
@cindex CSMA/CD
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1675
It is important to note that the @code{Csma} net devices and the @code{Csma}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1676
channel do not correspond to any real world hardware that you can actually go 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1677
out and buy.  These models implement an approximation, or subset, of the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1678
behaviors that a real CSMA/CD network would have.  In this case, the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1679
@code{CsmaNetDevice} does not simulate collision detection (CD).  It does 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1680
implement carrier sense and performs collision @emph{avoidance} using global 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1681
spatial knowledge available in the channel.  This would be impossible in any
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1682
channel residing in our universe.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1683
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1684
@cindex Ethernet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1685
No model will fully implement @emph{all} of the behaviors of a piece of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1686
hardware.  It is important to understand what is being modeled by the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1687
@command{ns-3} components you are using and what is not.  For example, the Csma
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1688
components we use in this tutorial model a highly abstract multiple access
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1689
network that is topologically equivalent to an Ethernet.  It is not necessarily
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1690
true that results found in a simulation using the Csma models will apply to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1691
a real-world Ethernet network.  You must understand what behaviors are 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1692
simulated in each of the models before trusting that any results can be 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1693
associated with real-world systems.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1694
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1695
@section Time, Events and Callbacks
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1696
@cindex time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1697
@cindex event
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1698
In a @emph{discrete event simulator} time is not something that @emph{flows},
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  1699
nor is it something to be measured --- it is the driving force behind the
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1700
progress of the simulation.  Time is progressed forward by the simulation 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1701
engine and anything that happens in the simulation is ultimately caused by
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1702
an @emph{event}.  An event is some action in the system that is 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1703
@emph{scheduled} to happen at a certain time by the simulation engine.  Time 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1704
does not flow continuously but steps discretely (in possibly large jumps) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1705
from one scheduled event to another.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1706
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1707
@cindex packet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1708
For example, to start the flow of a packet through the system, one would have
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1709
to schedule an event with the simulation engine @emph{before} the simulation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1710
was started.  This is important since the simulation engine only jumps time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1711
forward if there is a next event to process.  The simulation stops if there
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1712
are no more events, which is equivalent to a state where there is ``nothing 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1713
more to do.''  Before the simulation starts, one schedules driving events in 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1714
terms of absolute time.  For example, one could schedule an event to start 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1715
the flow of a first packet at, say, ten simulated seconds.  In this case, the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1716
simulation would start its clock at zero seconds and look for the first event 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1717
in its @emph{event queue}.  It would immediately jump time forward by ten 
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  1718
seconds and @emph{fire} the scheduled event --- that is, make the event happen.
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1719
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1720
@cindex functor
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1721
@cindex function object
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1722
@cindex callback
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1723
@cindex Callback
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1724
In @command{ns-3} an event is basically a pre-packaged function call called a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1725
@emph{functor}.  Functors are also known as @emph{function objects}, which is
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  1726
a more descriptive term --- an object (in the object-oriented programming 
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1727
sense) that can be called as if it was a function.  Typically one uses a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1728
functor to implement @emph{deferred execution} of a function or method.  The
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1729
most commonly encoutered form of deferred execution is in a @emph{callback}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1730
from an I/O system.  In this case, the goal would be to start an I/O
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1731
operation and return immediately, without having to wait for the operation 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1732
to complete.  One asks the I/O subsytem to notify you when an operation is
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1733
complete by calling some function you provide.  This provided function is
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1734
known as a callback function.  [Imagine calling someone on the telephone and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1735
asking them to do something for you.  You also ask them to @emph{call you back}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1736
when they are done.]  Events in the @command{ns-3} system work conceptually
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1737
the same way, except that instead of an I/O completion driving the process,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1738
the arrival of some simulated time drives the process.  The @command{ns-3} 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1739
deferred exectution mechanism is via a class called @code{Callback}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1740
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1741
@cindex Time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1742
@cindex Callback
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1743
The internal details of the classes representing @code{Time} and 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1744
@code{Callback} abstractions will be introduced as required.  We won't see
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1745
events directly for some time, but you should know that they are happening
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1746
``under the sheets'' of the simulations you will be writing.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1747
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1748
@section Driving the Simulation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1749
@cindex Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1750
As mentioned previously, time is the driving force behind the progress of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1751
a @command{ns-3} simulation.  Events are scheduled to happen at certain times
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1752
by calling methods of the simulation engine, either directly or indirectly
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1753
through, for example, an @code{Application}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1754
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1755
In order to get the simulation engine set up and running in our code, we must
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1756
first include the language definitions required to describe time- and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1757
simulator-specific classes:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1758
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1759
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1760
  #include "ns3/simulator.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1761
  #include "ns3/nstime.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1762
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1763
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1764
@cindex Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1765
As we have seen, we need to ``seed'' the simulation with at least one event.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1766
In the case of an @code{Application}, a method to do this is provided.  This
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1767
method must be implemented by each specialization of the class and we must
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1768
call this method in our script before the simulation starts.  We can also
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1769
provide an event (indirectly) to stop the output of the application at a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1770
certain time.  This is done by adding the following lines to our script:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1771
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1772
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1773
  server->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1774
  client->Start(Seconds(2.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1775
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1776
  server->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1777
  client->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1778
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1779
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1780
@cindex Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1781
@cindex time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1782
@cindex Time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1783
@cindex socket
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1784
@cindex event
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1785
In the case of the UdpEchoServer, the call to @code{server->Start ()} gives
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1786
the @code{Application} the chance to schedule an event that will perform the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1787
usual @emph{sockets} server sequence of socket creation, binding and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1788
recvfrom (see Donahoo's UDPEchoServer.c).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1789
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1790
In the case of the UdpEchoClient, the call to @code{client->Start ()} gives
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1791
the @code{Application} the chance to schedule an event that will perform the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1792
usual @emph{sockets} client sequence of socket creation, sendto and recvfrom
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1793
(see Donahoo's UDPEchoClient.c).  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1794
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1795
@cindex event
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1796
Note that the start event for the server is scheduled to happen before the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1797
start event of the client, just as you would start a server application before
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1798
you would attempt to start a client application in the real world.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1799
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1800
@cindex socket!sendto
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1801
The @command{ns-3} equivalent of the call to @code{sendo} in the client will 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1802
schedule (immediately) the transmission of a UDP packet over the just created
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1803
socket.  This will cause the packet to percolate down the protocol stack and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1804
eventually into the channel.  The channel will schedule a reception event in
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1805
the net device on the destination node.  This event will eventually percolate
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1806
up into the server application.  The server application will create a reply
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1807
packet and send it back down its stack and eventually back to the channel.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1808
The channel will schedule a reception event back in the client and this will
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1809
cause the reply to be sent back up the protocol stack to the client 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1810
application.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1811
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1812
The calls to @code{Stop ()} for both applications cause the sockets to be
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1813
torn down and therefore the sending and receiving of packets will be stopped
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1814
irrespective of other application settings (such as max packets and interval
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1815
in the client).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1816
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1817
Finally, we need to run the simulation and when the simulation run is complete,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1818
clean up any resources allocated during the run.  This is done by the calling 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1819
the following static methods:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1820
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1821
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1822
  Simulator::Run ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1823
  Simulator::Destroy ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1824
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1825
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1826
We now have the makings of a complete @command{ns-3} network simulation.  The 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1827
source code for the script should look like the following:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1828
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1829
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1830
  #include "ns3/log.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1831
  #include "ns3/ptr.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1832
  #include "ns3/internet-node.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1833
  #include "ns3/csma-channel.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1834
  #include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1835
  #include "ns3/csma-net-device.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1836
  #include "ns3/csma-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1837
  #include "ns3/csma-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1838
  #include "ns3/csma-ipv4-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1839
  #include "ns3/udp-echo-client.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1840
  #include "ns3/udp-echo-server.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1841
  #include "ns3/simulator.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1842
  #include "ns3/nstime.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1843
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1844
  NS_LOG_COMPONENT_DEFINE ("UdpEchoSimulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1845
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1846
  using namespace ns3;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1847
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1848
  int 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1849
  main (int argc, char *argv[])
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1850
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1851
    LogComponentEnable ("UdpEchoSimulation", LOG_LEVEL_INFO);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1852
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1853
    NS_LOG_INFO ("UDP Echo Simulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1854
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1855
    Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1856
    Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1857
    Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1858
    Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1859
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1860
    Ptr<CsmaChannel> lan = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1861
      CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1862
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1863
    uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1864
      Mac48Address("08:00:2e:00:00:00"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1865
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1866
    uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1867
      Mac48Address("08:00:2e:00:00:01"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1868
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1869
    uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1870
      Mac48Address("08:00:2e:00:00:02"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1871
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1872
    uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1873
      Mac48Address("08:00:2e:00:00:03"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1874
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1875
    CsmaIpv4Topology::AddIpv4Address (n0, nd0, Ipv4Address ("10.1.1.1"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1876
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1877
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1878
    CsmaIpv4Topology::AddIpv4Address (n1, nd1, Ipv4Address ("10.1.1.2"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1879
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1880
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1881
    CsmaIpv4Topology::AddIpv4Address (n2, nd2, Ipv4Address ("10.1.1.3"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1882
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1883
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1884
    CsmaIpv4Topology::AddIpv4Address (n3, nd3, Ipv4Address ("10.1.1.4"), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1885
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1886
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1887
    uint32_t packetSize = 1024;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1888
    uint16_t port = 7;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1889
    uint32_t maxPacketCount = 1;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1890
    Time interPacketInterval = Seconds (1.);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1891
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1892
    Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.1.2", port, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1893
      maxPacketCount, interPacketInterval, packetSize);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1894
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1895
    Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1896
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1897
    server->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1898
    client->Start(Seconds(2.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1899
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1900
    server->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1901
    client->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1902
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1903
    Simulator::Run ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1904
    Simulator::Destroy ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1905
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1906
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1907
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1908
@cindex csma-echo.cc
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1909
Just to make sure you don't get caught up in debugging typographical errors
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1910
we have provided this source code for you (along with a copyright header) in
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1911
the @code{tutorial} subdirectory of the @command{ns-3} distribution as 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1912
@code{csma-echo.cc}.  We used this opportunity to do some ``clean up''
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1913
of some of our example cases by passing parameters using implicit conversion 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1914
sequences and removing some of the named parameters. [These were used for
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1915
pedagogic purposes and were not actually necessary.]
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1916
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1917
@section Building the Script
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1918
@cindex Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1919
C++ is a compiled language, so you know it had to happen.  We have to build
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1920
the script before we run it.  As mentioned before, we use the Waf build system
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1921
which is Python-based.  We have to change gears slightly and switch ourselves
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1922
to Python mode in order to proceed.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1923
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1924
In each subdirectory of the @command{ns-3} distribution in which there are
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1925
source files, you will find two files:  one will be named @code{waf} and one
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1926
will be named @code{wscript}.  The former, @code{waf}, is a link that allows
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1927
one to start the build process from any subdirectory.  We can ignore that one.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1928
The file we need to deal with is @code{wscript}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1929
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1930
@cindex wscript
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1931
Open the file @code{ns-3-dev/tutorial/wscript} in your favorite editor
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1932
[remember I'm assuming that you have the distribution saved in a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1933
repository under a directory called @code{repos} in you home directory.]
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1934
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1935
@cindex Python
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1936
You should see the following Python code (after an emacs mode line).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1937
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1938
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1939
  def build(bld):
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1940
    obj = bld.create_ns3_program('hello-simulator')
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1941
    obj.source = 'hello-simulator.cc'
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1942
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1943
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1944
These are the only instructions required to build a simulation (I told you
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1945
it wasn't going to be too bad).  The line with the method
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1946
@code{bld.create_ns3_program} tells the build system to create an object 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1947
file that is a program (executable) named @code{hello-simulator}.  The
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1948
following line, with the method @code{obj.source} tells the build system that 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1949
the source file for the program is the file @code{hello-simulator.cc'} in the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1950
local directory.  The required libraries are linked for you for free.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1951
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1952
All that needed to be done in order to build the new simulation using the new
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1953
source file was to copy the two lines describing the @code{hello-simulator} 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1954
program and change the names to @code{csma-echo}.  You can see these lines
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1955
in the @code{wscript} file,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1956
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1957
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1958
  def build(bld):
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1959
    obj = bld.create_ns3_program('hello-simulator')
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1960
    obj.source = 'hello-simulator.cc'
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1961
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1962
    obj = bld.create_ns3_program('csma-echo')
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1963
    obj.source = 'csma-echo.cc'
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1964
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1965
    ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1966
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1967
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1968
When you built the system above, you actually already built this new 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1969
simulation and a number of other examples.  Since you have already configured
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1970
@code{Waf} and built the @code{csma-echo} script, you can run the simulation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1971
in the same way as you ran the @code{hello-simulator} script using the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1972
@code{waf --run} command:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1973
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1974
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1975
~/repos/ns-3-dev/tutorial > waf --run csma-echo
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1976
Entering directory `~/repos/ns-3-dev/build'
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1977
Compilation finished successfully
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1978
UDP Echo Simulation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1979
~/repos/ns-3-dev/tutorial >
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1980
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1981
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1982
Wow!  Wasn't that cool!  I'm sure you can barely contain yourself at this
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1983
point.  Okay, well, maybe we should figure out how to get some useful
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1984
information out of that simulation.  It did run ... I promise.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1985
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1986
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1987
@c Tracing at a Glance
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1988
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1989
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1990
@node Tracing-at-a-Glance
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1991
@chapter Tracing at a Glance
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1992
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1993
At this stage we have constructed a real simulation script, but have no way
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1994
of getting information out of the simulation.  Returning to first principles,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1995
a number of questions come immediately to mind:  What do we mean by getting
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1996
information out of the simulation?  What is information?  How is this 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1997
information generated? What generates it? Where does it go?  How can we
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1998
specify the quantity of information we get?  How can we specify the location
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1999
of the sources?  What is a source anyway?
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2000
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2001
@cindex toaster
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2002
The @command{ns-3} tracing system addresses each of these questions.  At the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2003
lowest levels, it is an extremely flexible module that comes with all of the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2004
associated complexity that inevitably comes with flexibility.  To minimize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2005
the amount of work required to get started, we provide wrapper functions to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2006
accomplish common tasks.  This makes the higher levels of the tracing system
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2007
easy to use, but relatively inflexible.  This is a common trade-off.  Consider
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2008
a toaster:  if you want to make toast, you can push a ``toast'' button; but if
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2009
you want to control the color of your toast, you will need a knob to adjust. 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2010
If you want to independently control slice color, you will need a number of 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2011
knobs, etc.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2012
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2013
In this chapter, we will discuss the highest levels of the tracing system that
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2014
expose the fewest ``knobs.''  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2015
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2016
@section ASCII Trace Wrapper
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2017
@cindex ASCII
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2018
The ASCII trace wrapper is a wrapper around the @command{ns-3} low-level 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2019
tracing system that lets you get access to underlying trace events easily.  
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  2020
The output of a trace of a simulation run is an ASCII file --- thus the name.  
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2021
In the spririt of keeping things simple, you won't be able to control or 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2022
configure the output.  The details are all hidden from you, and are therefore 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2023
inaccessible at this level.  Be assured that as you learn more and more about
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2024
the tracing system you will be able to control it to your heart's delight.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2025
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2026
@subsection Tracing Queue Operations
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2027
@cindex queue
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2028
Let's just jump right in.  As usual, we need to include the definitions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2029
related to using ASCII tracing (don't edit any files quite yet):
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2030
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2031
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2032
  #include "ns3/ascii-trace.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2033
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2034
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2035
We then need to add the code to the script to actually enable the ASCII tracing
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2036
code.  The following code must be inserted before the call to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2037
@code{Simulator::Run ();}:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2038
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2039
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2040
  AsciiTrace asciitrace ("tutorial.tr");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2041
  asciitrace.TraceAllQueues ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2042
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2043
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2044
@cindex AsciiTrace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2045
@cindex AsciiTrace!TraceAllQueues
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2046
The first line declares an object of type @code{AsciiTrace} named
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2047
@code{asciitrace} and passes a string parameter to its constructor.  This 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2048
parameter is a file name to which all of the trace information will be written.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2049
The last line, @code{asciitrace.TraceAllQueues ();} asks the trace object to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2050
arrange that all queue operations (enqueue, dequeue, drop) on the queues 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2051
in all of the nodes of the system be traced.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2052
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2053
@cindex csma-echo-ascii-trace.cc
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2054
Again, being the nice guys we are, we have provided you a file with these
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2055
changes already made.  Make sure you understand what we've done before you 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2056
just move on.  The new file is called @code{csma-echo-ascii-trace.cc} and is
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2057
located in the @code{tutorial} directory.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2058
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2059
@cindex Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2060
You can just type the following to run the trace version of the echo program:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2061
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2062
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2063
  ./waf --run csma-echo-ascii-trace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2064
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2065
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2066
@cindex tutorial.tr
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2067
Just as you have seen previously, you will see some messages from @emph{Waf}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2068
and then (I feel joy) the ``Compilation finished successfully'' message.  The
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2069
next message, @code{UDP Echo Simulation} is from the running program.  When 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2070
it ran, the program will have created a file named @code{tutorial.tr}.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2071
Because of the way that Waf works, the file is not created in the local 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2072
directory, it is created at the top-level directory of the repository.  So, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2073
change into the top level directory and take a look at the file 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2074
@code{tutorial.tr} in your favorite editor.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2075
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2076
@cindex trace event
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2077
There's a lot of information there in a pretty dense form, but the first thing
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2078
to notice is that there are a number of distinct lines in this file.  It may
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2079
be difficult to see this clearly unless you widen your windows considerably.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2080
Each line in the file corresponds to a @emph{trace event}.  A trace event 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2081
happens whenever specific conditions happen in the simulation.  In this case
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2082
we are tracing events on the @emph{device queue} present in every net device
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2083
on every node in the simulation.  The device queue is a queue through which
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  2084
every packet destined for a channel must pass --- it is the device
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2085
@emph{transmit} queue.  Note that each line in the trace file begins with a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2086
lone character (has a space after it).  This character will have the following
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2087
meaning:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2088
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2089
@cindex enqueue
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2090
@cindex dequeue
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2091
@cindex drop
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2092
@itemize @bullet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2093
@item @code{+}: An enqueue operation occurred on the device queue;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2094
@item @code{-}: A dequeue operation occurred on the device queue;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2095
@item @code{d}: A packet was dropped, typically because the queue was full.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2096
@end itemize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2097
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2098
Let's take a more detailed view of the first line.  I'll break it down into
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2099
sections (indented for clarity) with a two digit reference number on the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2100
left side:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2101
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2102
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2103
  00  + 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2104
  01  2 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2105
  02  nodeid=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2106
  03  device=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2107
  04  queue-enqueue 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2108
  05  pkt-uid=9 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2109
  06  ETHERNET 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2110
  07    length/type=0x806, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2111
  08    source=08:00:2e:00:00:00, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2112
  09    destination=ff:ff:ff:ff:ff:ff 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2113
  10      ARP(request 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2114
  11        source mac: 08:00:2e:00:00:00 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2115
  12        source ipv4: 10.1.1.1 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2116
  13        dest ipv4: 10.1.1.2) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2117
  14  ETHERNET fcs=0
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2118
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2119
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2120
@cindex trace event
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2121
@cindex simulation time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2122
The first line of this expanded trace event (reference number 00) is the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2123
queue operation.  We have a @code{+} character, so this corresponds to an
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2124
@emph{enqueue} operation.  The second line (reference 01) is the simulation 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2125
time expressed in seconds.  You may recall that we asked the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2126
@code{UdpEchoClient} to start sending packets at two seconds.  Here we see
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2127
confirmation that this is, indeed, happening.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2128
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2129
@cindex node number
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2130
@cindex net device number
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2131
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2132
The next lines of the example listing (references 02 and 03) tell us that
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2133
this trace event originated in a given node and net device.  Each time a node
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2134
is created it is given an identifying number that monotonically increases from
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2135
zero.  Therefore, @code{nodeid=0} means that the node in which the given trace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2136
event originated is the first node we created.  In the case of our script, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2137
this first node is is the node pointed to by the smart pointer @code{n0}.  Not
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2138
too surpsisingly, this is also the node to which we attached the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2139
@code{UdpEchoClient}.  The device number is local to each node, and so the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2140
device given by @code{device=0} is the first net device that we added to the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2141
node in question.  In our simulation, this corresponds to the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2142
@code{CsmaNetDevice} we added to node zero (@code{n0}).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2143
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2144
@cindex uid
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2145
@cindex unique ID
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2146
@cindex packet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2147
The next line (reference 04) is a more readable form of the operation code
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  2148
seen in the first line --- i.e., the character @code{+} means 
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2149
@code{queue-enqueue}.  Reference number 05 indicates that the @emph{unique id}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2150
of the packet being enqueued is @code{9}.  The fact that the first packet we
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2151
see has a unique ID of 9 should indicates to you that other things have 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2152
happened in the protocol stack before we got to this point.  This will become
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2153
clear momentarily.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2154
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2155
@cindex Ethernet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2156
@cindex MAC address
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2157
Reference items 06 and 14 indicate that this is an Ethernet packet with 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2158
a zero (not computed) checksum (note the indentation to make parsing this 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2159
trace event a little easier).  Reference 08 and 09 are the source and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2160
destination addresses of this packet.  The packet is from the MAC address we
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2161
assigned to the node zero net device in the script, and is destined for the
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  2162
broadcast address --- this is a broadcast packet.
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2163
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2164
@cindex Address Resolution Protocol
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2165
@cindex ARP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2166
@cindex ARP|request
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2167
Reference items 10 through 13 make clear what is happening.  This is an ARP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2168
(Address Resolution Protocol) request for the MAC address of the node on
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2169
which the @code{UdpEchoServer} resides.  The protocol stack can't send a UDP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2170
packet to be echoed until it knows (resolves) the MAC address; and this trace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2171
event corresponds to an ARP request being queued for transmission to the local
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2172
network.  The next line in the trace file (partially expanded),
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2173
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2174
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2175
  00  - 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2176
  01  2 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2177
  02  nodeid=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2178
  03  device=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2179
  04  queue-dequeue 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2180
  05  pkt-uid=9 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2181
  ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2182
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2183
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2184
shows the (same) ARP request packet being dequeued from the device queue by
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2185
the net device and (implicitly) being sent down the channel to the broadcast
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2186
MAC address.  We are not tracing net device reception events so we don't 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2187
actually see all of the net devices receiving the broadcast packet.  We do,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2188
however see the following in the third line of the trace file:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2189
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2190
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2191
  00  + 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2192
  01  2.00207
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2193
  02  nodeid=1 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2194
  03  device=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2195
  04  queue-enqueue 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2196
  05  pkt-uid=10 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2197
  06  ETHERNET 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2198
  07    length/type=0x806, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2199
  08    source=08:00:2e:00:00:01, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2200
  09    destination=08:00:2e:00:00:00,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2201
  10      ARP(reply 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2202
  11        source mac: 08:00:2e:00:00:01 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2203
  12        source ipv4: 10.1.1.2 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2204
  13        dest mac: 08:00:2e:00:00:00 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2205
  14        dest ipv4: 10.1.1.1) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2206
  15  ETHERNET fcs=0
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2207
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2208
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2209
@cindex simulation time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2210
@cindex ARP|response
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2211
Notice that this is a queue-enqueue operation (references 00 and 04) happening
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2212
on node one (reference 02) at simulation time 2.00207 seconds (reference 01).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2213
Looking at the packet payload (references 10-14) we see that this is an ARP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2214
reply to the request sent by node one.  Note that the simulation time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2215
(reference 01) is now 2.00207 seconds.  This is direct result of the data rate
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2216
(5 mb/s) and latency (2 ms) parameters that we passed to the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2217
@code{CsmaChannel} when we created it.  Clearly the ARP request packet was 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2218
sent over the channel and received approximately 2 ms later by node one.  A
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2219
corresponding ARP response packet was created and enqueued on node one's net
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2220
device.  It is this enqueue trace event that has being logged.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2221
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2222
@cindex queue
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2223
@cindex queue|transmit
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2224
@cindex echo
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2225
Given the current state of affairs, the next thing you may expect to see is 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2226
this ARP request being received by node zero, but remember we are only looking
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2227
at trace events on the device @emph{transmit} queue.  The reception of the ARP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2228
response by node zero will not directly trigger any trace event in this case, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2229
but it will enable the protocol stack to continue what it was originally doing
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2230
(trying to send an echo packet).  Thus, the next line we see in the trace file
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2231
(@code{tutorial.tr}) is the first UDP echo packet being sent to the net device.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2232
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2233
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2234
  00 + 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2235
  01 2.00415 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2236
  02 nodeid=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2237
  03 device=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2238
  04 queue-enqueue 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2239
  05 pkt-uid=7 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2240
  06 ETHERNET 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2241
  07   length/type=0x800, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2242
  08   source=08:00:2e:00:00:00, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2243
  09   destination=08:00:2e:00:00:01 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2244
  10   IPV4(
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2245
  11     tos 0x0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2246
  12     ttl 64 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2247
  13     id 0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2248
  14     offset 0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2249
  15     flags [none] 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2250
  16     length: 1052) 10.1.1.1 > 10.1.1.2 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2251
  17     UDP(length: 1032) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2252
  18       49153 > 7 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2253
  19       DATA (length 1024) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2254
  20 ETHERNET fcs=0
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2255
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2256
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2257
@cindex simulation time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2258
@cindex echo
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2259
@cindex ARP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2260
@cindex ARP|request
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2261
@cindex ARP|response
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2262
@cindex IP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2263
@cindex Ipv4
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2264
I won't go into too much detail about this packet, but I will point out a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2265
few key items in the trace.  First, the packet was enqueued at simulation time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2266
of 2.00415 seconds.  This time reflects the fact that the echo client 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2267
application started at 2. seconds and there were two ARP packets transmitted
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2268
across the network (two milliseconds + data transmission time each way).  The
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2269
packet unique identifier (reference 05) is 7.  Notice that this is a lower
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2270
number than the ARP request packet, which had a unique ID of 9.  This tells
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  2271
us that the UDP packet was actually created before the ARP request packet ---
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2272
which makes perfect sense since it was the attempt to send packet 7 that 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2273
triggered sending the ARP request packet 9.  Note that this an Ethernet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2274
packet (reference 06) like all other packets in this simulation, however this 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2275
particular packet carries an IPV4 payload and therefore has an IP version 4
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2276
header (indicated by references 10-16).  This Ipv4 in turn contains a UDP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2277
header (references 17, 18) and finally 1024 bytes of data (reference 20).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2278
Clearly, this is the UDP echo packet emitted by the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2279
@code{UdpEchoClient Application}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2280
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2281
The next trace event is an ARP request from node one.  We can infer that node
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2282
one has received the UDP echo packet and the @code{UdpEchoServer Application}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2283
on that node has turned the packet around.  Just as node zero needed to ARP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2284
for the MAC address of node one, now node one must ARP for the MAC address of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2285
node zero.  We see the ARP request enqueued on the transmit queue of node one;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2286
then we see the ARP request dequeued from the tranmit queue of node one (and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2287
implicitly transmitted to node zero).  Then we see an ARP response enqueued
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2288
on the transmit queue of node zero; and finally the ARP response dequeued (and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2289
implicitly transmitted back to node one).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2290
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2291
This exchange is summarized in the following trace event excerpts,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2292
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2293
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2294
  + 2.00786 nodeid=1 ... ARP(request ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2295
  - 2.00786 nodeid=1 ... ARP(request ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2296
  + 2.00994 nodeid=0 ... ARP(reply ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2297
  - 2.00994 nodeid=0 ... ARP(reply ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2298
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2299
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2300
The final two trace events in the @code{tutorial.tr} file correspond to the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2301
echoed packet being enqueued for transmission on the net device for node one,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2302
and that packet being dequeued (and implicitly transmitted back to node zero).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2303
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2304
@subsection Tracing Device Receive Operations
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2305
There is one final ``knob'' we can turn on the ASCII trace wrapper.  We can
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2306
enable net device receive operations.  In our analysis of the existing trace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2307
file we noted several times that we inferred that a packet was received by
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2308
a given node.  We will now enable the event to actually trace that operation.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2309
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2310
All we have to do is add one more line to the file @code{csma-echo.cc} to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2311
enable tracing of net device receive operations.  The code is already in the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2312
file, but disabled.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2313
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2314
@cindex AsciiTrace!TraceAllNetDeviceRx
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2315
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2316
#if 0
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2317
  asciitrace.TraceAllNetDeviceRx ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2318
#endif
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2319
  asciitrace.TraceAllNetDeviceRx ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2320
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2321
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2322
Change the @code{#if 0} to @code{#if 1} using your favorite editor and compile
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2323
and run the file using waf as we have done previously:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2324
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2325
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2326
  ./waf --run csma-echo-ascii-trace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2327
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2328
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2329
@cindex ARP!request
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2330
Now if you look at the trace file (@code{tutorial.tr}) you will see some new
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2331
entries.  These entries all begin with the character @code{r} indicating a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2332
@emph{receive} trace event.  Recall that the first packet sent on the network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2333
was a broadcast ARP request.  We should then see all four nodes receive a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2334
copy of this request.  This is the case, as the first four receive trace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2335
events are,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2336
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2337
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2338
  r 2.00207 nodeid=0 device=0 dev-rx pkt-uid=9 ARP(request ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2339
  r 2.00207 nodeid=1 device=0 dev-rx pkt-uid=9 ARP(request ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2340
  r 2.00207 nodeid=2 device=0 dev-rx pkt-uid=9 ARP(request ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2341
  r 2.00207 nodeid=3 device=0 dev-rx pkt-uid=9 ARP(request ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2342
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2343
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2344
@cindex unique ID
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2345
You can see that a copy of the broadcast packet with unique ID 9 was received
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2346
by the net devices on nodes 0, 1, 2 and 3.  We leave it up to you to parse the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2347
rest of the trace file and understand the remaining reception events.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2348
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2349
@section PCAP Trace Wrapper
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2350
@cindex pcap
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2351
@cindex Wireshark
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2352
The @command{ns-3} @emph{pcap trace wrapper} is used to create trace files in
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2353
@code{.pcap} format.  The acronym pcap (usually written in lower case) stands
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2354
for @emph{p}acket @emph{cap}ture, and is actually an API that includes the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2355
definition of a @code{.pcap} file format.  The most popular program that can
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2356
read and display this format is Wireshark (formerly called Ethereal).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2357
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2358
If you are unfamilar with Wireshark, there is a web site available from which
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2359
you can download programs and documentation:  @uref{http://www.wireshark.org/}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2360
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2361
@cindex csma-echo-ascii-trace.cc
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2362
@cindex csma-echo-pcap-trace.cc
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2363
The code used to enable pcap tracing is similar to that for ASCII tracing.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2364
We have provided another file, @code{csma-echo-pcap-trace.cc} that uses the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2365
pcap trace wrapper.  We have added the code to include the pcap trace wrapper
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2366
defintions:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2367
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2368
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2369
  #include "ns3/pcap-trace.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2370
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2371
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2372
And then added the following code below the AsciiTrace methods:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2373
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2374
@cindex PcapTrace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2375
@cindex PcapTrace!TraceAllIp
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2376
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2377
  PcapTrace pcaptrace ("tutorial.pcap");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2378
  pcaptrace.TraceAllIp ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2379
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2380
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2381
The first line of the code immediately above declares an object of type
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2382
@code{PcapTrace} named @code{pcaptrace} and passes a string parameter to its
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2383
constructor.  This object is used to hide the details of the actual tracing
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2384
subsystem.  The parameter is a base file name from which the actual trace file
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2385
names will be built.  The second line of code tells the @code{PcamTrace}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2386
object to trace all IP activity in all of the nodes present in the simulation.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2387
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2388
@cindex interface index
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2389
Trace files are not created until trace activity is detected.  Each file name 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2390
is composed of the base file name, followed by a @code{'-'}, a node id followed
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2391
by a @code{'-}', and an IP interface index.  You will soon see a file named
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2392
@code{tutorial.pcap-0-1}, for example.  This will be the trace file generated
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2393
as events are detected on node zero, interface index one.  N.B. Interface 
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  2394
indices are different that net device indices --- interface index zero 
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2395
corresponds to the loopback interface and interface index one corresponds to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2396
the first net device you added to a node.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2397
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2398
You may run the new program just like all of the others so far:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2399
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2400
@cindex Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2401
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2402
  ./waf --run csma-echo-pcap-trace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2403
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2404
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2405
If you look at the top level directory of your distribution, you should now
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2406
see three log files:  @code{tutorial.tr} is the ASCII trace file we have 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2407
previously examined.  @code{tutorial.pcap-0-1} and @code{tutorial.pcap-1-1}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2408
are the new pcap files we just generated.  There will not be files 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2409
corresponding to nodes two and three since we have not sent any IP packets to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2410
those nodes.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2411
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2412
@cindex Wireshark
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2413
If you have Wireshark available, you can open each of the trace files and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2414
display the contents as if you had captured the packets using a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2415
@emph{packet sniffer}.  Note that only IP packets are traced using this
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2416
wrapper, so you will not see the ARP exchanges that were logged when using
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2417
the ASCII trace wrapper.  You are encouraged to take a look at the contents
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2418
of these pcap files using your favorite pcap software (or Wireshark).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2419
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2420
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2421
@c Other Network Topologies
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2422
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2423
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2424
@node Other-network-topologies
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2425
@chapter Other Network Topologies
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2426
@cindex topology
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2427
@cindex Channel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2428
@cindex NetDevice
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2429
@cindex topology!bus
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2430
@cindex topology!point-to-point
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2431
@cindex PointToPointChannel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2432
@cindex PointToPointNetDevice
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2433
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2434
@emph{Network topology} is the study of the arrangement of of the elements
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2435
(in @command{ns-3} represented by the classes @code{Channel} and @code{Node})
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2436
of a network.  Two fundamental types of physical topologies are the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2437
@emph{point-to-point} and @emph{bus} topologies.  We have already been exposed
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2438
to the @command{ns-3} channel specialization named @code{CsmaChannel}.  This is
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2439
a simulation of a bus network.  We also provide a simulation of a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2440
point-to-point channel with associated net devices.  As described previously,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2441
the associated C++ classes specialize the @command{ns-3} base classes
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2442
@code{NetDevice} and @code{Channel} and are called @code{PointToPointNetDevice}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2443
and @code{PointToPointChannel} respectively.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2444
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2445
We will use combinations of these bus and point-to-point topology elements
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2446
to show how to create several commonly seen network topologies.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2447
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2448
@section A Point-to-Point Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2449
We're going to take what might be seen as a step backward and look at a simple
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2450
point-to-point network.  We will be building the simplest network you can 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2451
imagine.  A serial link (point to point) between two computers.  When you
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2452
see this point-to-point network, you can think of an RS-422 (or RS-232 for 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2453
you old-timers) cable.  This topology is shown below.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2454
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2455
@sp 1
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2456
@center @image{pp,,,,png}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2457
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2458
We have provided a file for you in the @code{tutorial}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2459
directory called @code{point-to-point.cc}.  You should now be familiar enough
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2460
with the system to pick out fairly easily what has been changed.  Let's focus
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2461
on the following lines:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2462
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2463
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2464
  Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2465
  Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2466
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2467
  Ptr<PointToPointChannel> link = PointToPointTopology::AddPointToPointLink (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2468
    n0, n1, DataRate (38400), MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2469
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2470
  PointToPointTopology::AddIpv4Addresses (link, n0, "10.1.1.1",
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2471
    n1, "10.1.1.2");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2472
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2473
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2474
You can see that we created two @code{InternetNode} objects in the usual way.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2475
Then, instead of creating a @code{CsmaChannel} we create a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2476
@code{PointToPointChannel}.  This point-to-point channel, which we call
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2477
@code{link}, connects node zero (@code{n0}) and node one (@code{n1}) over a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2478
simulated link that runs at 38400 bits per second and has a 20 millisecond
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2479
simulated speed-of-light delay.  This call also creates appropriate net devices
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2480
and attaches them to nodes zero and one.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2481
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2482
We then add IP addresses to the net devices we just created using the topology
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2483
helper @code{AddIpv4Addresses}.  Node zero gets the IP address 10.1.1.1 and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2484
node one gets the IP address 10.1.1.2 assigned.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2485
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2486
The alert tutorial user may wonder what the network number or prefix is of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2487
those IP addresses.  The point-to-point topology assumes that you want a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2488
@code{/30} subnet and assigns an appropriate net mask for you.  It then then
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2489
@emph{asserts} that the network numbers of the two net devices match.  So there
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2490
is an implicit network mask created down in the topology code that looks like,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2491
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2492
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2493
  Ipv4Mask netmask("255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2494
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2495
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2496
The rest of the code you should recognize and understand.  We are just going
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2497
to echo one packet across the point-to-point link.  You should be now be able
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2498
to build and run this example and to locate and interpret the ASCII trace 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2499
file.  This is left as an exercise for you.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2500
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2501
The file @code{point-to-point.cc} is reproduced here for your 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2502
convenience:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2503
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2504
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2505
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2506
/*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2507
 * This program is free software; you can redistribute it and/or modify
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2508
 * it under the terms of the GNU General Public License version 2 as
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2509
 * published by the Free Software Foundation;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2510
 *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2511
 * This program is distributed in the hope that it will be useful,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2512
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2513
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2514
 * GNU General Public License for more details.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2515
 *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2516
 * You should have received a copy of the GNU General Public License
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2517
 * along with this program; if not, write to the Free Software
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2518
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2519
 */
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2520
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2521
#include "ns3/log.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2522
#include "ns3/ptr.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2523
#include "ns3/internet-node.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2524
#include "ns3/point-to-point-channel.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2525
#include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2526
#include "ns3/point-to-point-net-device.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2527
#include "ns3/point-to-point-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2528
#include "ns3/udp-echo-client.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2529
#include "ns3/udp-echo-server.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2530
#include "ns3/simulator.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2531
#include "ns3/nstime.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2532
#include "ns3/ascii-trace.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2533
#include "ns3/pcap-trace.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2534
#include "ns3/global-route-manager.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2535
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2536
NS_LOG_COMPONENT_DEFINE ("PointToPointSimulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2537
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2538
using namespace ns3;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2539
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2540
// Network topology
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2541
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2542
//                       point to point
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2543
//                      +--------------+
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2544
//                      |              |
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2545
//                     n0             n1
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2546
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2547
int 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2548
main (int argc, char *argv[])
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2549
{
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2550
  LogComponentEnable ("PointToPointSimulation", LOG_LEVEL_INFO);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2551
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2552
  NS_LOG_INFO ("Point to Point Topology Simulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2553
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2554
  Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2555
  Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2556
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2557
  Ptr<PointToPointChannel> link = PointToPointTopology::AddPointToPointLink (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2558
    n0, n1, DataRate (38400), MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2559
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2560
  PointToPointTopology::AddIpv4Addresses (link, n0, "10.1.1.1", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2561
    n1, "10.1.1.2");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2562
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2563
  uint16_t port = 7;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2564
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2565
  Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.1.2", port, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2566
    1, Seconds(1.), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2567
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2568
  Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2569
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2570
  server->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2571
  client->Start(Seconds(2.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2572
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2573
  server->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2574
  client->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2575
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2576
  AsciiTrace asciitrace ("tutorial.tr");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2577
  asciitrace.TraceAllQueues ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2578
  asciitrace.TraceAllNetDeviceRx ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2579
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2580
  Simulator::Run ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2581
  Simulator::Destroy ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2582
}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2583
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2584
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2585
@section A Star Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2586
A point-to-point network is considered a special case of a star network.  As
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2587
you might expect, the process of constructing a star network is an extension
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2588
of the very simple process used for a point-to-point link.  We have provided
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2589
a file for you in the @code{tutorial} directory called @code{star.cc}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2590
that implements a simple star network as seen below.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2591
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2592
@sp 1
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2593
@center @image{star,,,,png}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2594
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2595
In order to create a star network, we need to be able to instantiate some
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2596
number (greater than one) of net devices on a node.  In the name of simplicity
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2597
of use, the @code{PointToPointTopology} topology helper does not allow one to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2598
do this.  We provided a separate topology helper class, the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2599
@code{PointToPointIpv4Topology} helper class that provides the slightly finer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2600
granularity we need to accomplish a star network.  In order to use this new
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2601
helper we have to load the definitions by including the appropriate file.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2602
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2603
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2604
  #include "ns3/point-to-point-ipv4-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2605
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2606
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2607
The star that we're going to create has a node in the center (@code{n0}) with
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2608
six nodes surrounding (@code{n1} - @code{n6}).  You should be able to easily
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2609
find and understand the code that creates these nodes.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2610
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2611
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2612
  Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2613
  Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2614
  Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2615
  Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2616
  Ptr<Node> n4 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2617
  Ptr<Node> n5 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2618
  Ptr<Node> n6 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2619
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2620
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2621
Next, we get into the differences between the @code{PointToPointTopology}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2622
helper and the @code{PointToPointIpv4Topology} helper.  The
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2623
@code{PointToPointIpv4Topology} helper looks and feels a little like the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2624
@code{CsmaIpv4Topology} helper.  Just like you created a CSMA channel 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2625
previously, you need to create a point-to-point channel.  The following
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2626
code creates a @code{PointToPointChannel} and calls it @code{link01}.  You can
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2627
interpret this name as being the channel (or @emph{link}) from node zero to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2628
node one.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2629
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2630
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2631
  Ptr<PointToPointChannel> link01 =
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2632
    PointToPointIpv4Topology::CreateChannel (DataRate (38400),
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2633
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2634
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2635
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2636
You need to provide a data rate for the channel which we set at 38400 bits 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2637
per second.  You must also provide a speed-of-light delay which we set at
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2638
20 milliseconds.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2639
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2640
Just as you added a net device to the nodes in the CSMA tutorial section, you
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2641
do the same here but with a point-to-point net device.  The following code
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2642
illustrates how we do that:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2643
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2644
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2645
  uint32_t nd01 = PointToPointIpv4Topology::AddNetDevice (n0,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2646
    link01);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2647
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2648
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2649
We call the @code{PointToPointIpv4Topology} helper and ask it to add a net
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2650
device to node zero (@code{n0}) and connect it to the appropriate 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2651
point-to-point link (@code{link01}) which you will recall is the serial link
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2652
from node zero to node one.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2653
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2654
If you look at the following code, you will see the same calls are repeated
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2655
to create the remaining five point-to-point channels and connect them
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2656
to net devices on node zero.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2657
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2658
The next new code is found after the ``spokes'' of the star have been created.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2659
It looks like the following:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2660
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2661
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2662
  uint32_t nd1 = PointToPointIpv4Topology::AddNetDevice (n1, link01);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2663
  uint32_t nd2 = PointToPointIpv4Topology::AddNetDevice (n2, link02);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2664
  uint32_t nd3 = PointToPointIpv4Topology::AddNetDevice (n3, link03);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2665
  uint32_t nd4 = PointToPointIpv4Topology::AddNetDevice (n4, link04);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2666
  uint32_t nd5 = PointToPointIpv4Topology::AddNetDevice (n5, link05);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2667
  uint32_t nd6 = PointToPointIpv4Topology::AddNetDevice (n6, link06);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2668
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2669
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2670
Here we are creating the net devices on the nodes surrounding the center node.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2671
In the first call, we are adding a net device on node one (@code{n1}) and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2672
connecting that net device to the channel named @code{link01}.  Remember that
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2673
we created the channel @code{link01} as the channel connecting node zero and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2674
node one.  We previously created a net device on node zero and attached that
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2675
device to @code{link01}.  Here we are connecting the other side of that link
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2676
to node one.  The return value from this call is the net device index of the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2677
created net device.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2678
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2679
The next section of code adds addresses to the net devices we just created.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2680
The first call adds the IP address 10.1.1.1 to the net device going from 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2681
node zero to node one.  Recall that we first created a node named @code{n0}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2682
and a channel called @code{link01}.  We added a net device to @code{n0} and
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2683
remembered the net device index as the @code{uint32_t nd01}.  This meant
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2684
the net device @emph{nd} on node @emph{0} that we connected to node @emph{1}. 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2685
We call @code{AddAddress} to add an IP address (10.1.1.1) to the net device
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2686
on node zero identified by the net device index @code{nd01}.  We provide a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2687
net mask suitable for a point to point network.  This is typically a /30 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2688
address but we don't force that in this API.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2689
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2690
After setting up the address on node zero, we do the same for the node on
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  2691
the other end of the ``spoke'' --- in this case node one, with its single
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2692
net device.  Note that the network number is the same on both sides of this
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2693
network.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2694
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2695
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2696
  PointToPointIpv4Topology::AddAddress (n0, nd01, "10.1.1.1",
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2697
    ``255.255.255.252'');
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2698
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2699
  PointToPointIpv4Topology::AddAddress (n1, nd1, "10.1.1.2",
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2700
    ``255.255.255.252'');
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2701
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2702
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2703
The following code repeats this pattern assining similar IP addresses to the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2704
remaining net devices.  Note that there are no @code{Mac48Address} address
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  2705
assignments --- they are not required.
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2706
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2707
The rest of the code you should recognize and understand.  We are just going
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2708
to echo one packet across the point-to-point link.  You should be now be able
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2709
to build and run this example and to locate and interpret the ASCII trace 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2710
file.  This is left as an exercise for you.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2711
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2712
The file @code{star.cc} is reproduced here for your convenience:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2713
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2714
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2715
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2716
/*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2717
 * This program is free software; you can redistribute it and/or modify
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2718
 * it under the terms of the GNU General Public License version 2 as
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2719
 * published by the Free Software Foundation;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2720
 *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2721
 * This program is distributed in the hope that it will be useful,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2722
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2723
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2724
 * GNU General Public License for more details.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2725
 *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2726
 * You should have received a copy of the GNU General Public License
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2727
 * along with this program; if not, write to the Free Software
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2728
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2729
 */
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2730
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2731
#include "ns3/log.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2732
#include "ns3/ptr.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2733
#include "ns3/internet-node.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2734
#include "ns3/point-to-point-channel.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2735
#include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2736
#include "ns3/point-to-point-net-device.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2737
#include "ns3/point-to-point-ipv4-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2738
#include "ns3/udp-echo-client.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2739
#include "ns3/udp-echo-server.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2740
#include "ns3/simulator.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2741
#include "ns3/nstime.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2742
#include "ns3/ascii-trace.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2743
#include "ns3/pcap-trace.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2744
#include "ns3/global-route-manager.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2745
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2746
NS_LOG_COMPONENT_DEFINE ("StarSimulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2747
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2748
using namespace ns3;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2749
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2750
// Network topology
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2751
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2752
//                  n3    n2
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2753
//                   |   /
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2754
//                    | /
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2755
//              n4 --- n0 --- n1
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2756
//                    /  |
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2757
//                   /    |
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2758
//                  n5    n6
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2759
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2760
int 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2761
main (int argc, char *argv[])
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2762
{
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2763
  LogComponentEnable ("StarSimulation", LOG_LEVEL_INFO);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2764
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2765
  NS_LOG_INFO ("Star Topology Simulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2766
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2767
  Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2768
  Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2769
  Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2770
  Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2771
  Ptr<Node> n4 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2772
  Ptr<Node> n5 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2773
  Ptr<Node> n6 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2774
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2775
  Ptr<PointToPointChannel> link01 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2776
    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2777
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2778
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2779
  uint32_t nd01 = PointToPointIpv4Topology::AddNetDevice (n0,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2780
    link01);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2781
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2782
  Ptr<PointToPointChannel> link02 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2783
    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2784
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2785
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2786
  uint32_t nd02 = PointToPointIpv4Topology::AddNetDevice (n0,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2787
    link02);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2788
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2789
  Ptr<PointToPointChannel> link03 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2790
    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2791
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2792
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2793
  uint32_t nd03 = PointToPointIpv4Topology::AddNetDevice (n0,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2794
    link03);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2795
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2796
  Ptr<PointToPointChannel> link04 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2797
    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2798
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2799
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2800
  uint32_t nd04 = PointToPointIpv4Topology::AddNetDevice (n0, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2801
    link04);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2802
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2803
  Ptr<PointToPointChannel> link05 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2804
    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2805
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2806
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2807
  uint32_t nd05 = PointToPointIpv4Topology::AddNetDevice (n0,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2808
    link05);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2809
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2810
  Ptr<PointToPointChannel> link06 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2811
    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2812
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2813
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2814
  uint32_t nd06 = PointToPointIpv4Topology::AddNetDevice (n0, link06);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2815
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2816
  uint32_t nd1 = PointToPointIpv4Topology::AddNetDevice (n1, link01);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2817
  uint32_t nd2 = PointToPointIpv4Topology::AddNetDevice (n2, link02);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2818
  uint32_t nd3 = PointToPointIpv4Topology::AddNetDevice (n3, link03);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2819
  uint32_t nd4 = PointToPointIpv4Topology::AddNetDevice (n4, link04);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2820
  uint32_t nd5 = PointToPointIpv4Topology::AddNetDevice (n5, link05);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2821
  uint32_t nd6 = PointToPointIpv4Topology::AddNetDevice (n6, link06);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2822
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2823
  PointToPointIpv4Topology::AddAddress (n0, nd01, "10.1.1.1", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2824
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2825
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2826
  PointToPointIpv4Topology::AddAddress (n1, nd1, "10.1.1.2", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2827
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2828
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2829
  PointToPointIpv4Topology::AddAddress (n0, nd02, "10.1.2.1", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2830
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2831
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2832
  PointToPointIpv4Topology::AddAddress (n2, nd2, "10.1.2.2", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2833
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2834
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2835
  PointToPointIpv4Topology::AddAddress (n0, nd03, "10.1.3.1", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2836
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2837
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2838
  PointToPointIpv4Topology::AddAddress (n3, nd3, "10.1.2.2", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2839
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2840
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2841
  PointToPointIpv4Topology::AddAddress (n0, nd04, "10.1.4.1", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2842
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2843
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2844
  PointToPointIpv4Topology::AddAddress (n4, nd4, "10.1.4.2", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2845
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2846
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2847
  PointToPointIpv4Topology::AddAddress (n0, nd05, "10.1.5.1", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2848
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2849
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2850
  PointToPointIpv4Topology::AddAddress (n5, nd5, "10.1.5.2", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2851
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2852
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2853
  PointToPointIpv4Topology::AddAddress (n0, nd06, "10.1.6.1", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2854
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2855
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2856
  PointToPointIpv4Topology::AddAddress (n6, nd6, "10.1.6.2", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2857
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2858
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2859
  uint16_t port = 7;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2860
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2861
  Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.1.2", port, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2862
    1, Seconds(1.), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2863
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2864
  Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2865
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2866
  server->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2867
  client->Start(Seconds(2.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2868
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2869
  server->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2870
  client->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2871
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2872
  AsciiTrace asciitrace ("tutorial.tr");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2873
  asciitrace.TraceAllQueues ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2874
  asciitrace.TraceAllNetDeviceRx ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2875
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2876
  Simulator::Run ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2877
  Simulator::Destroy ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2878
}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2879
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2880
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2881
@subsection Routing
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2882
If you are really excited about this simulator you may have already tried to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2883
modify the scripts outside the tutorial.  I know that one of the first things
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2884
that would have occurred to me when I saw the star network would have been to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2885
start trying to add applications to echo packets from nodes other than zero.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2886
If you tried, for example, to start the echo client on node one instead of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2887
node zero, you would have found an empty trace file.  The reason for this
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2888
is that you have now created an internetwork.  This means you will need to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2889
enable internetwork routing.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2890
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2891
We have provided a file for you in the @code{tutorial} directory called 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2892
@code{star-routing.cc} to show you how this is done.  This extremely
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2893
tricky and difficult change is shown below:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2894
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2895
@verbatim 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2896
 GlobalRouteManager::PopulateRoutingTables ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2897
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2898
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2899
This one-line addition, located just before the simulation runs, tells the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2900
@command{ns-3} @emph{global route manager} to walk the topology you created and 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2901
build internetwork routing tables for all of the nodes in the simulation.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2902
We changed the client application so that it runs on node four:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2903
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2904
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2905
  Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n4, "10.1.1.2", port,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2906
    1, Seconds(1.), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2907
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2908
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2909
Now if you build and run @code{star-routing.cc} you can examine the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2910
@code{tutorial.tr} file and see that your UDP echo packets are now correctly
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2911
routed through the topology.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2912
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2913
@section A Dumbbell Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2914
One of the most interesting simple topologies (from a phenomenological point of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2915
view) is commonly called a dumbbell network.  The name derives from a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2916
superficial similarity in form to a piece of exercise equipment.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2917
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2918
The dumbbell model is typically composed of two bus or star network elements
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2919
connected via a point-to-point link.  The point-to-point link is usually 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2920
configured with a lower bandwidth than the bus elements to provide a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2921
@emph{choke point}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2922
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2923
The following is a representation of the topology.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2924
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2925
@sp 1
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2926
@center @image{dumbbell,,,,png}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2927
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2928
We have provided a file that constructs this dumbbell network and creates 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2929
enough data flowing across the choke point that some packets will be dropped.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2930
The file is called @code{linear-dumbbell.cc} and is located in the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2931
@code{tutorial} directory.  We have already covered all of the code used to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2932
create this network, so we will just quickly go over the main sections of the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2933
script.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2934
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2935
The first section creates a CSMA lan that will become the left side of the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2936
dumbbell network.  This code should be very familiar since we used the same
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2937
process to create our first example.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2938
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2939
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2940
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2941
// Create the lan on the left side of the dumbbell.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2942
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2943
  Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2944
  Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2945
  Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2946
  Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2947
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2948
  Ptr<CsmaChannel> lan1 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2949
    CsmaTopology::CreateCsmaChannel (DataRate (10000000), MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2950
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2951
  uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan1, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2952
    "08:00:2e:00:00:00");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2953
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2954
  uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan1, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2955
    "08:00:2e:00:00:01");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2956
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2957
  uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan1, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2958
    "08:00:2e:00:00:02");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2959
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2960
  uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan1, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2961
    "08:00:2e:00:00:03");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2962
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2963
  CsmaIpv4Topology::AddIpv4Address (n0, nd0, "10.1.1.1", "255.255.255.0");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2964
  CsmaIpv4Topology::AddIpv4Address (n1, nd1, "10.1.1.2", "255.255.255.0");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2965
  CsmaIpv4Topology::AddIpv4Address (n2, nd2, "10.1.1.3", "255.255.255.0");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2966
  CsmaIpv4Topology::AddIpv4Address (n3, nd3, "10.1.1.4", "255.255.255.0");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2967
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2968
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2969
The code to generate the CSMA lan on the right side is similar; only the names
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2970
have been changed.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2971
 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2972
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2973
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2974
// Create the lan on the right side of the dumbbell.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2975
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2976
  Ptr<Node> n4 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2977
  Ptr<Node> n5 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2978
  Ptr<Node> n6 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2979
  Ptr<Node> n7 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2980
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2981
  Ptr<CsmaChannel> lan2 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2982
    CsmaTopology::CreateCsmaChannel (DataRate (10000000), MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2983
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2984
  uint32_t nd4 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n4, lan2, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2985
    "08:00:2e:00:00:04");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2986
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2987
  uint32_t nd5 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n5, lan2, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2988
    "08:00:2e:00:00:05");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2989
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2990
  uint32_t nd6 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n6, lan2, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2991
    "08:00:2e:00:00:06");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2992
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2993
  uint32_t nd7 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n7, lan2, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2994
    "08:00:2e:00:00:07");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2995
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2996
  CsmaIpv4Topology::AddIpv4Address (n4, nd4, "10.1.2.1", "255.255.255.0");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2997
  CsmaIpv4Topology::AddIpv4Address (n5, nd5, "10.1.2.2", "255.255.255.0");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2998
  CsmaIpv4Topology::AddIpv4Address (n6, nd6, "10.1.2.3", "255.255.255.0");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2999
  CsmaIpv4Topology::AddIpv4Address (n7, nd7, "10.1.2.4", "255.255.255.0");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3000
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3001
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3002
Next, we create a point to point link to connect the two lans.  We connect
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3003
the point-to-point channel between nodes three (on the left lan) and four
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3004
(on the right lan).  You should recoginze this as substantially similar to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3005
the link setup from the @code{point-to-point} example.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3006
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3007
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3008
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3009
// Create the point-to-point link to connect the two lans.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3010
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3011
  Ptr<PointToPointChannel> link = PointToPointTopology::AddPointToPointLink (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3012
    n3, n4, DataRate (38400), MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3013
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3014
  PointToPointTopology::AddIpv4Addresses (link, n3, "10.1.3.1", 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3015
    n4, "10.1.3.2");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3016
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3017
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3018
Then we configure data flows.  We create four echo clients that send UDP 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3019
packets from the left side lan to servers created on the right side lan.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3020
Notice that we send 100 packets with an inter-packet gap of ten milliseconds
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3021
instead of the single packet we have previously used.  This data rate is
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3022
sufficient to saturate the point-to-point link and will cause packets to be
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3023
dropped when the queue on the link net devices overflows (the default maximum
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3024
queue depth is 100 packets).  Note that we stagger the start of the echo 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3025
clients to slowly bring up the data rates.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3026
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3027
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3028
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3029
// Create data flows across the link:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3030
//   n0 ==> n4 ==> n0
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3031
//   n1 ==> n5 ==> n1
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3032
//   n2 ==> n6 ==> n2
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3033
//   n3 ==> n7 ==> n3
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3034
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3035
  uint16_t port = 7;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3036
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3037
  Ptr<UdpEchoClient> client0 = Create<UdpEchoClient> (n0, "10.1.2.1", port, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3038
    100, Seconds(.01), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3039
  Ptr<UdpEchoClient> client1 = Create<UdpEchoClient> (n1, "10.1.2.2", port, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3040
    100, Seconds(.01), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3041
  Ptr<UdpEchoClient> client2 = Create<UdpEchoClient> (n2, "10.1.2.3", port, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3042
    100, Seconds(.01), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3043
  Ptr<UdpEchoClient> client3 = Create<UdpEchoClient> (n3, "10.1.2.4", port, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3044
    100, Seconds(.01), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3045
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3046
  Ptr<UdpEchoServer> server4 = Create<UdpEchoServer> (n4, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3047
  Ptr<UdpEchoServer> server5 = Create<UdpEchoServer> (n5, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3048
  Ptr<UdpEchoServer> server6 = Create<UdpEchoServer> (n6, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3049
  Ptr<UdpEchoServer> server7 = Create<UdpEchoServer> (n7, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3050
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3051
  server4->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3052
  server5->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3053
  server6->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3054
  server7->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3055
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3056
  client0->Start(Seconds(2.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3057
  client1->Start(Seconds(2.1));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3058
  client2->Start(Seconds(2.2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3059
  client3->Start(Seconds(2.3));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3060
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3061
  server4->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3062
  server5->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3063
  server6->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3064
  server7->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3065
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3066
  client0->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3067
  client1->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3068
  client2->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3069
  client3->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3070
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3071
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3072
The remainder of the file should be quite familiar to you.  Go ahead and 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3073
run @code{linear-dumbbell}.  Now take a look at the trace (@code{tutorial.tr})
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3074
file.  You will now see trace lines that begin with @code{d}.  Alternatively 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3075
you can search for the string ``queue-drop'' which is the expansion of the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3076
drop code.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3077
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3078
Interpretation of a dropped packet is straightforward.  We have expanded
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3079
the first @code{queue-drop} trace for you below.  See the section on ASCII 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3080
tracing for details.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3081
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3082
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3083
  00 d 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3084
  01 2.40938 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3085
  02 nodeid=3 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3086
  03 device=1 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3087
  04 queue-drop 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3088
  05 pkt-uid=124 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3089
  06 LLCSNAP(type 0x800) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3090
  07   IPV4(
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3091
  08     tos 0x0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3092
  09     ttl 63 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3093
  10     id 20 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3094
  11     offset 0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3095
  12     flags [none] 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3096
  13     length: 1052) 10.1.1.3 > 10.1.2.3 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3097
  14     UDP(length: 1032) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3098
  15       49153 > 7 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3099
  16       DATA (length 1024)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3100
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3101
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3102
We leave it as an exercise to examine the trace files in more detail.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3103
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3104
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3105
@c Nonlinear Thinking
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3106
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3107
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3108
@node Nonlinear-Thinking
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3109
@chapter Nonlinear Thinking
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3110
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3111
One thing that all of our examples so far have in common is that they are
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3112
composed of a linear collection of calls into the @command{ns-3} system.  The
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3113
programmers among the readers may have wondered why there is not as much
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3114
as a for-loop in all of the examples.  The answer is that we wanted to 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3115
introduce you to @command{ns-3} scripting with a minimum of conceptual 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3116
overhead.  We're going to remedy that situation shortly.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3117
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3118
We have written a number of @command{ns-3} scripts in C++.  Although we have 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3119
been perfectly linear in our script implementations, just like any other C++ 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3120
program, an @command{ns-3} script can use any features of the language you
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3121
desire.  If you will look back at the @code{linear-dumbbell.cc}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3122
example, you may notice that the code to create the left and right sides of
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3123
the dumbbell is operationally identical --- only the names change.  An obvious
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3124
improvement of this program would be to use subroutines to create the sides.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3125
Since we are working with C++, we should probably do this in an 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3126
object-oriented way.  Since object-oriented design is somewhat of a black art
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3127
to some people, we'll take some time here and outline a simple methodology 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3128
you can follow.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3129
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3130
@section Object Design 101 --- Class Ipv4BusNetwork
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3131
If you are a master of object oriented design, feel free to skip or skim this
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3132
section, in which we derive a simplistic but fully operational bus network 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3133
class.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3134
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3135
So you want to create a BusNetwork class.  Often the biggest hurdle in a 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3136
design is figuring out how to get started.  One of the simplest and most 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3137
straightforward ways to do an object decomposition of a problem is to simply 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3138
write down a description of the problem and take a look at the words 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3139
you used.  Let's take some time and do that, first at a very high level.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3140
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3141
@example
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3142
A bus network is an implementation of a particular network topology that
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3143
contains some number of nodes.  Each of these nodes is attached to a single 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3144
multi-drop channel.  The network itself has some attributes independent of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3145
the topology such as a network mask, network number (prefix) and base IP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3146
address.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3147
@end example
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3148
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3149
The first thing to do is to focus on the nouns and adjectives.  These will
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3150
give you a starting point for required classes and member variables.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3151
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3152
Immediately we can notice that at the highest level we are talking about the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3153
noun @emph{network}.  This probably won't surprise you.  We also have an 
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3154
adjective that modifies the noun --- @emph{bus}.  This should lead us to our 
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3155
first class defintion.  Usually class names are constructed in the same way
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3156
as an English language sentence would be spoken.  For example, one would speak
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3157
of a @emph{bus network} in conversation, so we would normally create a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3158
@code{class BusNetwork} to represent it.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3159
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3160
One thing to note is that we have used two words in our description quite 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3161
naturally: @emph{is} and @emph{has}.  When you see these words should should
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3162
immediately think of the object-oriented concepts of @emph{ISA} (inheritance)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3163
and @emph{HASA} (containment) respectively.  We wrote that a bus network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3164
@emph{is} an implementation of a particular network topology.  Perhaps you 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3165
will agree that there is a natural base class called @code{Network} that 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3166
@emph{has} the attributes discussed above.  The fact that a @code{BusNetwork}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3167
@emph{ISA} kind of @code{Network} suggests inheritance.  Let's capture that 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3168
thought right away remembering that we're focused on IP version four here:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3169
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3170
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3171
  class Ipv4Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3172
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3173
  public:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3174
    Ipv4Address m_network;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3175
    Ipv4Mask m_mask;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3176
    Ipv4Address m_baseAddress;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3177
  };
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3178
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3179
  class Ipv4BusNetwork : public Ipv4Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3180
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3181
  };
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3182
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3183
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3184
Let's take a look at the @emph{HASA} relationships of the bus network.  Clearly
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3185
it will @emph{have} a reference to the underlying channel that implements the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3186
actual communications medium.  We use smart pointers for those references, so 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3187
one member variable is obvious:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3188
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3189
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3190
  Ptr<CsmaChannel> m_channel;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3191
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3192
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3193
A bus network will also need to contain references to all of the nodes we
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3194
eventually want to create.  If you are working in C++ and see the words contain
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3195
or container, you should immediately think of the Standard Template Library
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3196
or STL.  A quick search of the available containers there will probably lead
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3197
you to consider the vector class.  A vector is a container that looks like an
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3198
array.  This is just what we need here.  Again, we want to use smart pointers
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3199
to reference our nodes, so the declaration of the vector would look like,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3200
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3201
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3202
  std::vector<Ptr<Node> > m_nodes;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3203
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3204
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3205
It will save you headaches in the future if you notice that the space between
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3206
the two right brackets is required to differentiate this situation from a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3207
right-shift operator.  So we have a pretty good start already after just a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3208
little work.  Now we need to turn our attention to actions.  Let's write 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3209
another little description of the things you consider doing to a Bus network.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3210
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3211
@example
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3212
We need to be able to create a bus network.  We need to be able to delete a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3213
bus network.  We need to be able to get a handle to a node in order to add
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3214
applications.  We need to be able to set the network, mask and base address
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3215
somehow, specify how many nodes to create and provide the underlying channel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3216
its required bandwidth and delay parameters.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3217
@end example
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3218
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3219
We now look at the @emph{verbs} in that sentence.  These will give a good 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3220
starting point for the methods of the classes.  For example, the verbs 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3221
@emph{create} and @emph{delete} should suggest @emph{constructor} and 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3222
@emph{destructor}.  The verb @emph{get} leads us to providing a method called
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3223
@code{GetNode}.  We have to provide a number of parameters so we can either
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3224
provide @emph{setters} or we can simply pass them in as parameters to our
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3225
constructors.  Since this is a simple example, we won't bother to implement
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3226
getters and setters (methods to get and set member variables to enhance data
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3227
hiding).  Let's use this guidance to finish up our class declarations:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3228
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3229
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3230
  class Ipv4Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3231
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3232
  public:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3233
    Ipv4Network (Ipv4Address network, Ipv4Mask mask, Ipv4Address address);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3234
    virtual ~Ipv4Network ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3235
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3236
    Ipv4Address m_network;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3237
    Ipv4Mask m_mask;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3238
    Ipv4Address m_baseAddress;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3239
  };
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3240
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3241
  class Ipv4BusNetwork : public Ipv4Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3242
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3243
  public:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3244
    Ipv4BusNetwork (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3245
      Ipv4Address   network, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3246
      Ipv4Mask      mask, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3247
      Ipv4Address   startAddress, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3248
      DataRate      bps, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3249
      Time          delay, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3250
      uint32_t      n);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3251
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3252
    virtual ~Ipv4BusNetwork ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3253
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3254
    Ptr<Node> GetNode (uint32_t n);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3255
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3256
  private:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3257
    std::vector<Ptr<Node> > m_nodes;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3258
    Ptr<CsmaChannel> m_channel;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3259
  };
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3260
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3261
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3262
That's it.  We have actually already walked through almost all of the code
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3263
required to construct a bus network in our @code{csma-echo.cc} 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3264
example, so let's just jump forward and take a look at an implementation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3265
of this thing.  We provide an implementation for you in the files 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3266
@code{ipv4-bus-network.h} and @code{ipv4-bus-network.cc} located in the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3267
@code{tutorial} directory.  We also provide an example that uses the new
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3268
class in the file @code{bus-network.cc}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3269
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3270
The interesting method from our current perspective is the Ipv4BusNetwork
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3271
constructor, shown below:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3272
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3273
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3274
  Ipv4BusNetwork::Ipv4BusNetwork (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3275
    Ipv4Address   network, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3276
    Ipv4Mask      mask, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3277
    Ipv4Address   baseAddress, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3278
    DataRate      bps, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3279
    Time          delay, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3280
    uint32_t      n)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3281
  : 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3282
    Ipv4Network (network, mask, baseAddress)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3283
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3284
    Ipv4AddressGenerator::SeedNetwork (mask, network);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3285
    Ipv4AddressGenerator::SeedAddress (mask, baseAddress);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3286
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3287
    m_channel = CsmaTopology::CreateCsmaChannel (bps, delay);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3288
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3289
    for (uint32_t i = 0; i < n; ++i)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3290
      {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3291
        Ptr<Node> node = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3292
        uint32_t nd = CsmaIpv4Topology::AddIpv4CsmaNetDevice (node, m_channel, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3293
          Mac48Address::Allocate ());
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3294
        Ipv4Address address = Ipv4AddressGenerator::AllocateAddress (mask, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3295
          network);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3296
        CsmaIpv4Topology::AddIpv4Address (node, nd, address, mask);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3297
        m_nodes.push_back (node);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3298
      }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3299
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3300
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3301
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3302
Notice that we do the simple and straightforward thing and pass all of our
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3303
parameters to the constructor.  For those unfamiliar with C++, the line after
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3304
the colon and before the opening brace (shown below),
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3305
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3306
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3307
  : 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3308
    Ipv4Network (network, mask, baseAddress)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3309
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3310
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3311
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3312
Passes the appropriate parameters to the constructor of the base class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3313
@code{Ipv4Network}.  There are two new calls that we haven't seen immediately
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3314
after this initialization.  They are:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3315
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3316
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3317
    Ipv4AddressGenerator::SeedNetwork (mask, network);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3318
    Ipv4AddressGenerator::SeedAddress (mask, baseAddress);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3319
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3320
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3321
We provide an IP address generator class to allow us to programatically 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3322
allocate IP addresses.  The first call to @code{SeedNetwork} gives the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3323
address generator a starting network number to use when generating addresses.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3324
The second call to @code{SeedAddress} gives the address generator a starting
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3325
IP address to use.  There is a starting network and starting address for each
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3326
of the 32 possible network masks.  Later in the for loop, you will see a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3327
call to @code{AllocateAddress} in which the IP address for each node created
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3328
in the loop is actually generated.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3329
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3330
The only unfamiliar call in the reset of the constructor will be:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3331
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3332
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3333
  m_nodes.push_back (node);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3334
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3335
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3336
This is the STL code to add the newly created node to the vector of nodes
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3337
attached to the bus.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3338
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3339
For your convenience, we reproduce the entire bus network implementation below:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3340
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3341
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3342
  /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3343
  /*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3344
   * Copyright (c) 2007 University of Washington
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3345
   *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3346
   * This program is free software; you can redistribute it and/or modify
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3347
   * it under the terms of the GNU General Public License version 2 as
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3348
   * published by the Free Software Foundation;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3349
   *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3350
   * This program is distributed in the hope that it will be useful,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3351
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3352
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3353
   * GNU General Public License for more details.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3354
   *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3355
   * You should have received a copy of the GNU General Public License
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3356
   * along with this program; if not, write to the Free Software
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3357
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3358
   */
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3359
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3360
  #include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3361
  #include "ns3/csma-net-device.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3362
  #include "ns3/csma-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3363
  #include "ns3/csma-ipv4-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3364
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3365
  #include "ipv4-bus-network.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3366
  #include "ipv4-address-generator.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3367
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3368
  namespace ns3 {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3369
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3370
  Ipv4Network::Ipv4Network (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3371
    Ipv4Address   network, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3372
    Ipv4Mask      mask, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3373
    Ipv4Address   address)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3374
  : 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3375
    m_network (network), m_mask (mask), m_baseAddress (address)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3376
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3377
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3378
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3379
  Ipv4Network::~Ipv4Network ()
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3380
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3381
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3382
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3383
  Ipv4BusNetwork::Ipv4BusNetwork (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3384
    Ipv4Address   network, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3385
    Ipv4Mask      mask, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3386
    Ipv4Address   baseAddress, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3387
    DataRate      bps, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3388
    Time          delay, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3389
    uint32_t      n)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3390
  : 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3391
    Ipv4Network (network, mask, baseAddress)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3392
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3393
    Ipv4AddressGenerator::SeedNetwork (mask, network);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3394
    Ipv4AddressGenerator::SeedAddress (mask, baseAddress);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3395
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3396
    m_channel = CsmaTopology::CreateCsmaChannel (bps, delay);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3397
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3398
    for (uint32_t i = 0; i < n; ++i)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3399
      {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3400
        Ptr<Node> node = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3401
        uint32_t nd = CsmaIpv4Topology::AddIpv4CsmaNetDevice (node, m_channel, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3402
          Mac48Address::Allocate ());
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3403
        Ipv4Address address = Ipv4AddressGenerator::AllocateAddress (mask, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3404
          network);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3405
        CsmaIpv4Topology::AddIpv4Address (node, nd, address, mask);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3406
        m_nodes.push_back (node);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3407
      }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3408
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3409
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3410
  Ipv4BusNetwork::~Ipv4BusNetwork ()
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3411
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3412
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3413
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3414
    Ptr<Node>
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3415
  Ipv4BusNetwork::GetNode (uint32_t n)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3416
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3417
    return m_nodes[n];
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3418
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3419
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3420
  }; // namespace ns3
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3421
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3422
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3423
@section Using Ipv4BusNetwork
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3424
If all you ever want to do with a bus network can be captured in a topology
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3425
with four nodes on the bus, the preceeding section may seem like a colossal
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3426
waste of time.  This is probably not the case, though.  Now that we have a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3427
relatively abstract bus class, we can create bus networks with 4, 40 or 4000
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3428
nodes with no additional effort.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3429
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3430
A use of the bus network class is shown in the file 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3431
@code{bus-netowrk.cc} located in the @code{tutorial} directory.  The
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3432
interesting code is,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3433
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3434
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3435
  Ipv4BusNetwork bus ("10.1.0.0", "255.255.0.0", "0.0.0.3",
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3436
    DataRate(10000000), MilliSeconds(20), 10);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3437
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3438
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3439
Here we create a bus network with the network number ``10.1.0.0'' and the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3440
network mask ``255.255.0.0'' that completes the IP network definition.  You 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3441
can consider these together as ``10.1.0.0/16'' if you prefer.  The next 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3442
parameter tells the bus to start numbering IP addresses of contained nodes at
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3443
``10.1.0.3'' (remember the network number will be combined).  We provided a
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3444
data rate of 10 megabits per second and a latency of 20 milliseconds.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3445
Finally, we ask the @code{Ipv4BusNetwork} object to create ten nodes in the
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3446
network.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3447
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3448
If you are feeling brave, go ahead and change the number of nodes to be 100,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3449
1000, 10,000 or more to generate larger and larger networks.  Before you go 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3450
too far, remember that a trace file will be generated when you run your 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3451
resulting program and ee asked the trace facility to trace all net device 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3452
receive events.  This will include the reception of the broadcast ARP request 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3453
by all of the nodes in the simulation, so this can add up quickly.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3454
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3455
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3456
@c Summary
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3457
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3458
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3459
@node Summary
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3460
@chapter Summary
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3461
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3462
This concludes the first part of the tutorial.  We have focused on
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3463
using the @command{ns-3} system to construct various network topologies and to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3464
simulate sendng data across the networks; and we've shown you how to use the 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3465
trace facility to get access to simulation results.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3466
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3467
We now encourage you to play with the system a little.  Experiment with what
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3468
we have provided.  Build a hierarchical network simulation.  Perhaps exercise
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3469
your object design skills and create a new @code{Ipv4DumbbellNetwork} class 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3470
to create dumbbell networks using the Ipv4BusNetwork class we just created.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3471
Hint:  An Ipv4DumbbellNetwork @emph{has} two @code{Ipv4BusNetwork} objects; 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3472
a left side and a right side.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3473
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3474
In the next part of the tutorial we are going to drop down a level and begin
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3475
examining the lower levels of the system in more detail.  We are going to
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3476
explain how to change the behavior of the system and eventually how to write
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3477
new models and applications.  This is a good time to make sure that you
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3478
thorougly understand what we've gone over so far.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3479
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3480
@c ========================================================================
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3481
@c Object Model
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3482
@c ========================================================================
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3483
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3484
@node Object Model
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3485
@chapter Object Model
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3486
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3487
There are two distinctly different meanings associated with the term Object
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3488
Model.  The first speaks to the implementation of an object system --- a system
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3489
view; and the second speaks to the application programming interface (classes
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3490
or objects) one uses to access some service or system --- an application view.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3491
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3492
As an example of the system view sense of the term, the C++ language has an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3493
associated object model that describes how objects are laid out in memory,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3494
how virtual functions work, how inheritance is implemented, constructor and
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3495
destructor execution ordering, template instantiation, etc.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3496
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3497
In the case of the application view, the Document Object Model is a good
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3498
example.  In the words of W3C, the Document Object Model (DOM) is an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3499
application programming interface (API) for HTML and XML documents. It defines
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3500
the logical structure of documents and the way a document is accessed and
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3501
manipulated.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3502
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3503
The Component Object Model (COM) from Microsoft actually spans both meanings
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3504
of the term and extends further into policy statements.  From a system
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3505
perspective, COM specifies an interface definition language, the layout of
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3506
objects virtual function tables, the formats of Globally Unique Identifiers
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3507
and also specifies lifetime management mechanisms for objects via reference
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3508
counting.  From the point of view of the API, COM specifies a number of
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3509
Interfaces as well as functions such as CoCreateInstance and various
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3510
threading models.  The COM specification extends to policy by disallowing
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3511
implementation inheritance.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3512
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3513
The @command{ns-3} object model takes the C++ language (system level) object 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3514
model as its basis, and extends that model by providing an API for software
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3515
componentry.  You will find terms like Component, Interface and QueryInterface
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3516
in the following discussion.  It is important to understand from the outset
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3517
that this is the @command{ns-3} object model, and not any other object model.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3518
Richard Feynman (an American physicist) once described the behavior of matter 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3519
and light on a very small scale in the following way,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3520
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3521
@quotation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3522
``They do not behave like waves, they do not behave like particles, they do
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3523
not behave like clouds, or billiard balls, or weights on springs, or like
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3524
anything that you have ever seen.''
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3525
@end quotation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3526
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3527
Just as students of quantum mechanics must rid themselves of preconceptions
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3528
regarding the behavior of matter at small scales, you should rid yourself of
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3529
any preconceptions you may have about components, interfaces and APIs for
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3530
software componentry before continuing.  To paraphrase Feynman, @command{ns-3}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3531
components do not behave like COM Components, or Java Beans, or CORBA
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3532
objects, or clouds or weights on springs, or like anything that you have 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3533
ever seen  they are @command{ns-3} components.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3534
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3535
@section The C++ Object Model is the Root of all Things
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3536
@command{Ns-3} is primarily a C++ system.  The system is written in C++ and
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3537
one can use standard C++ mechanisms for creating and using ns-3 objects.  We 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3538
do not change this at all, nor do we make any pronouncements about the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3539
superiority of one mechanism or another.  What we will do is provide 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3540
convenience functions that we think will make creating and managing simulation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3541
objects easier.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3542
  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3543
Previously, you have seen objects created using the template function 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3544
@code{Create} as in the following example:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3545
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3546
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3547
  Ptr<Node> n0 = Create<InternetNode> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3548
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3549
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3550
This line of code, while it may be unfamiliar to some, is pure C++.  If you
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3551
were to look in the header file ptr.h, you would find the following definition
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3552
of the @code{Create} template.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3553
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3554
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3555
  template <typename T>
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3556
  Ptr<T> Create (void)
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3557
  {
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3558
    T *obj = new T ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3559
    Ptr<T> p = obj;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3560
    obj->Unref ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3561
    return p;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3562
  }
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3563
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3564
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3565
As you can see, this template creates objects of type @code{T} using the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3566
operator @code{new}.  Its a little harder to find the corresponding delete ---
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3567
it's in the file @code{object.cc} inside the method @code{Object::MaybeDelete},
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3568
but when that @code{Ptr} which you see above goes out of scope it will call
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3569
@code{Unref} and ultimately the C++ @code{delete} operator will be called.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3570
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3571
The ns-3 system uses the C++ @code{new} and @code{delete} operators, so there
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3572
is really no reason that you as a user of the ns-3 system are forbidden from
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3573
using these or any other C++ mechanism.  If you so desire, you can take on
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3574
the responsibility for managing object lifetime (i.e., do not use the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3575
@code{Ptr} smart pointer), work directly with the @code{new} and @code{delete}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3576
operators and call methods like any C++ object as in the following example:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3577
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3578
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3579
  MyClass *obj = new MyClass ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3580
  obj->Method();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3581
  delete obj;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3582
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3583
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3584
You, as a competent model author, are encouraged to use whatever methods you
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3585
think are appropriate in your private code.  Remember, however, that the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3586
public ns-3 APIs do use smart pointers to pass objects around in an effort to
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3587
reduce the burden of object lifetime management.  If you do intend to export
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3588
an API publicly, you should use the same object lifetime management approaches
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3589
as those found in the ns-3 public API if only for consistency.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3590
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3591
These APIs are there for convenience and consistency, but do not change the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3592
fact that in ns-3 all of the objects are really just C++ objects, ultimately
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3593
created using the C++ new operator with C++ constructor semantics and are
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3594
ultimately deleted using the C++ delete operator, following C++ destructor
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3595
semantics.  Although it may sometimes appear so, there is really no system-
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3596
level magic going on in ns-3.  Ns-3 components and interfaces are C++ objects
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3597
just like any other object and our object model is simply a collection of APIs
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3598
built on the normal C++ object model.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3599
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3600
@section Interfaces
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3601
There are many different ideas floating around of what exactly the term
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3602
@emph{Interface} means.  Originally an interface just meant a communication
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3603
boundary between two entities.  As the concepts of object oriented programming
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3604
(OOP) were surfacing in the 1980s, the term interface was applied to the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3605
collection of access methods for the modular entities that were being defined.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3606
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3607
Two distinct approaches developed regarding specifying access mechanisms for
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3608
objects.  The OOP purists were very concerned about object reuse and were led
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3609
to Abstract Data Types (ADT).  These were eventually implemented in the case
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3610
of C++, as pure virtual methods in Abstract Base Classes (ABC).  Another group
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3611
of folks was more interested in simply specifying object access methods in one
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3612
place and using inheritance as the primary reuse mechanism.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3613
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3614
Bjarne Stroustroup, the creator of C++, embraced both approaches.  He makes
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3615
the following interesting observation:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3616
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3617
@quotation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3618
``Many classes [@dots{}] are useful both as themselves and also as bases for
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3619
derived classes. [@dots{}] Some classes, such as class @strong{Shape},
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3620
represent abstract concepts for which objects cannot exist.''
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3621
@end quotation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3622
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3623
@command{Ns-3} does not pick and enforce a particular approach.  In 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3624
@command{ns-3} an interface is determined completely by a class declaration
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3625
just as any C++ object interface is declared.  If you think of an object as
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3626
an abstract concept that should be implemented by derived classes, by all
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3627
means, use the Abstract Base Class approach to interface declaration.  If you
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3628
think that an object should be completely concrete and you foresee no need
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3629
to ever modify its behavior, feel free to avoid declaring any methods virtual.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3630
If you think that an object could be useful as a base class, feel free to
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3631
declare its methods virtual.  If you like to use the PIMPL idiom, again, feel
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3632
free.  If you want to use any combination of these techniques, feel free.  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3633
We make no restrictions.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3634
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3635
When we speak of an ns-3 interface, we do not worry about interface definition
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3636
languages, or pure virtual classes, or registries  we just think about C++
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3637
object declarations and their associated methods.  When we instantiate an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3638
@command{ns-3} Interface, it is the C++ object model that dictates how that
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3639
object is brought into existence.  When a method is called on an @command{ns-3}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3640
Interface, it is the C++ object model that dictates how that method is 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3641
dispatched.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3642
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3643
The only difference between a vanilla C++ object and an ns-3 Interface, is
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3644
that an object acting as an ns-3 Interface must inherit from the base class
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3645
Object.  This inheritance gives the Interface object a very useful capability.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3646
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3647
@section The Ns-3 Capital I Interface and QueryInterface
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3648
One thing that Microsoft got right in the Component Object Model was the idea
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3649
of Interface aggregation and discovery via QueryInterface.  We have embraced
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3650
these ideas in @command{ns-3}.  This was done primarily to address a common
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3651
problem in large software systems.  A good example of this problem happens
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3652
in the @command{ns-3} Node class.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3653
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3654
If one were to take the standard OOP view of specializing a @code{Node} into
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3655
an internet host, for example, one would typically inherit from the @code{Node}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3656
base class and include functionality to implement such things as internet
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3657
routing and a TCP / IP protocol stack.  Other types of @code{Node}s might 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3658
inherit from the node class and specialize in different ways, or further
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3659
specialize the internet host class, treating it as a base class.  This can
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3660
result in a complicated inheritance tree in which some specializations are
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3661
simply not available to other branches of the tree which can make reuse
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3662
difficult or impossible.  This is known as the @emph{weak base class} problem
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3663
and creates pressure to drive functionality up the inheritance tree into the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3664
base classes.  This, in turn, results in @emph{base class bloat} and the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3665
resulting @emph{swiss army knife} base classes which end up trying to do
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3666
everything in one place.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3667
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3668
Even if one successfully avoided these swiss army knife base classes, one
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3669
would also want to be able to treat new specializations of @code{Node}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3670
generically in the system.  This means one would pass references to the base
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3671
class (@code{Node}) across public APIs.  This introduces @emph{upcasts} prior
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3672
to passing across public APIs and corresponding @emph{downcasts} on the other
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3673
side in order to gain access to required specialized functions.  As the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3674
inheritance tree becomes more complicated, this approach can cause another
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3675
related problem known as the @emph{fragile base class} problem.  This happens
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3676
when changes to the base class cause unexpected problems in the various and 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3677
sundry subclasses.  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3678
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3679
These effects seem always to result in a positive feedback loop driving
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3680
everything into the base class and destroying much of the encapsulation which
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3681
is a hallmark of the object oriented approach.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3682
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3683
@subsection Interface Composition
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3684
There is a completely different way to address the Node specialization
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3685
problem.  Instead of approaching the situation using inheritance, one can
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3686
look at the problem as one of composition.  We can look at the @code{Node}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3687
class as a container of sorts that holds other objects.  In this case, the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3688
objects would be instances of the classes implementing the internetwork
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3689
routing code, or the TCP / IP protocol stack described above.  This approach
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3690
preserves the encapsulation and solves the weak base class, base class bloat
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3691
and fragile base class problems; but the question of method dispatch 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3692
immediately comes to mind.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3693
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3694
In many systems, @emph{delegation} is used.  The base class, @code{Node},
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3695
in this approach would provide methods that simply forward to the objects
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3696
implementing the desired functionality.  This situation clearly does not
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3697
address the base class bloat problem since dispatch methods must be added
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3698
to the base class.  The situation is mitigated somewhat by pushing the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3699
implementation of the dispatch methods to contained objects, but the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3700
fundamental problems are still present.  What is really needed is a way
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3701
to compose objects but at the same time keep the interfaces to those
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3702
objects separated.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3703
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3704
Composition, usually called @emph{aggregation}, along with runtime Interface
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3705
discovery is the solution that Microsoft originally championed and that 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3706
@command{ns-3} has adopted.  In our example a @code{Node} would contain 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3707
separate Interface objects implementing internetwork routing and TCP/IP.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3708
These contained objects have interfaces in the C++ sense of collections of
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3709
method signatures.  When objects are capable of participating in this
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3710
aggregation process, they are called @command{ns-3} Interfaces and they
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3711
receive the functionality required for this participation by inheriting
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3712
from the base class @code{Object}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3713
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3714
@subsection Object, interfaces and Interfaces
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3715
As mentioned above, the class that implements the aggregation mechanism for
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3716
@command{ns-3} objects is called @code{Object}.  The class named @code{Object}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3717
is simply a base class that you will inherit from if you want your objects
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3718
to support aggregation and QueryInterface.  Many systems have a base class
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3719
that implements common functionality and these base classes are typically
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3720
called Object.  The @command{ns-3} version of this object base class relates
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3721
primarily to Interface aggregation, although it does provide methods to help
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3722
with intrusive reference counting and tracing as well.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3723
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3724
When a C++ object inherits from the ns-3 Object base class, it is conceptually
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3725
promoted to an ns-3 Interface (note the capital I in Interface) irrespective
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3726
of how the object was declared (e.g., as an abstract base class, concrete
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3727
class, with virtual methods, etc.).   In ns-3, you should associate
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3728
inheritance from the class named @code{Object} with promotion of an object to 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3729
the status of Interface rather than the form of the Interface declaration.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3730
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3731
When you inherit from @code{Object}, you will get new methods and an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3732
Interface Identifier.  The Interface Identifer, or @emph{iid}, is the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3733
@command{ns-3} version of the @emph{Universally Unique ID} (UUID) or 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3734
@emph{Globally Unique ID} (GUID) found in other systems.  Unlike the GUID, it
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3735
is really a dynamically created process-local ID.  For now, consider it as
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3736
simply a number which the system will generate for you that uniquely
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3737
identifies an Interface class within the ns-3 system and allows you to
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3738
specify an interface type to @code{QueryInterface}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3739
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3740
To summarize, when you instantiate an object that inherits from the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3741
@code{Object} class, you will have a C++ object that has four important 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3742
properties:  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3743
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3744
@itemize @bullet
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3745
@item The object has a C++ interface defined by the collection of method signatures in its inheritance tree; 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3746
@item The object has an Interface ID that uniquely identifies the C++ interface of its class;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3747
@item The object is a container that has the ability to aggregate other interfaces;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3748
@item The object exports a method that allows for discovery of aggregated interfaces (@code{QueryInterface}) according to Interface ID.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3749
@end itemize
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3750
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3751
It is crucially important to understand what we have described here.  A given
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3752
C++ class has an object access interface that is essentially the collection
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3753
of method signatures specified in its inheritance tree.  This is a C++ object
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3754
model thing.  Ns-3 provides a base class from which the class in question can
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3755
inherit and be promoted to the status of Interface.  Once a class becomes
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3756
an Interface it has inherited the ability to set its own interface identifier
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3757
(@code{iid}), and exports methods to aggregate and search other Interfaces
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3758
that are added to its aggregation.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3759
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3760
That last detail is important.  In @command{ns-3} Interfaces are both
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3761
containers and specifications for object method access.  We have previously
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3762
mentioned the @code{Node} class acts as a container.  In fact, the @code{Node}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3763
class inherits from @code{Object} and is itself also an @command{ns-3}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3764
Interface.  When the @code{Node} object is created it is really an aggregation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3765
of one Interface, the @code{Node} Interface.  This is generally true ---
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3766
Interfaces are both containers and Interfaces.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3767
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3768
@subsection Aggregations
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3769
The figure below shows how an Interface could be illustrated in detail.  The 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3770
line with the circle at the top of the diagram represents the appearance of the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3771
Interface to the external world.  This circle and line are called a lollipop
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3772
because of its superficial similarity to a kind of childs candy.  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3773
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3774
@sp 1
2210
151fbc85dd92 figures
Craig Dowell <craigdo@ee.washington.edu>
parents: 2209
diff changeset
  3775
@center @image{oneif,,,,png}
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3776
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3777
You could declare this interface quite simply using a non-virtual class as 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3778
follows,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3779
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3780
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3781
  class A : public Object {
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3782
  public:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3783
    static const InterfaceId iid;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3784
    void MethodA (void);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3785
  };
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3786
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3787
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3788
The methods that are then available via the Interface labeled @code{A} in the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3789
figure above are the methods inherited from the @code{Object} base class (
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3790
@code{QueryInterface}, @code{Ref}, and @code{Unref}) and those from class 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3791
@code{A} (@code{MethodA}).  Note that you must declare an @code{InterfaceId}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3792
for your Interface class, and it must be declared static to make it class-wide
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3793
in scope.  This @code{iid} can be thought of as a kind of type information
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3794
that uniquely identifies objects as being instantiated from this class.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3795
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3796
You can think of the arc and arrow device coming off each side of the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3797
Interface as part of a connector.  These connectors allow @code{QueryInterface}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3798
to search aggregations for a particular @code{iid}.  The figure below shows an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3799
aggregation of three Interfaces: A, B and C.  The class declarations for 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3800
classes @code{B} and @code{C} are substantially similar to that of class 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3801
@code{A}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3802
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3803
@sp 1
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3804
@center @image{threeif,,,,png}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3805
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3806
You can visualize these Interfaces as being snapped together like Lego
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3807
building blocks if you like.  When the Interfaces are aggregated, a
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3808
@code{QueryInterface} search path is formed through the connectors.  In order
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3809
to create this aggregation we first need to create the Interface objects.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3810
These are just normal, everyday C++ objects that we can create using the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3811
@code{Create} template function and manage using smart pointers.  The 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3812
following code should be obvious to you by now:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3813
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3814
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3815
  Ptr<A> a = Create<A> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3816
  Ptr<B> b = Create<B> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3817
  Ptr<C> c = Create<C> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3818
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3819
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3820
When you create an aggregation, you pick one of the Interfaces to act as
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3821
the container.  In this case well pick Interface A.  In order to aggregate
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3822
an Interface, you simply call the method @code{AddInterface} that your class
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3823
inherited from @code{Object}.  The following code will aggregate Interface 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3824
@code{B} and Interface @code{C} onto the Interface (and container) @code{A}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3825
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3826
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3827
  a->AddInterface (b);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3828
  a->AddInterface (c);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3829
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3830
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3831
Thats all there is to it.  Now that you have those connectors snapped
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3832
together, you can ask each of the Interfaces in the aggregation for any of
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3833
the Interfaces in the aggregation.  Lets look at a simple example:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3834
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3835
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3836
  Ptr<B> newB = a->QueryInterface<B> (B:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3837
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3838
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3839
The left hand side of this assignment declares a smart pointer to the class
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3840
@code{B} to help with memory management of the returned Interface pointer.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3841
Object lifetime management is very important when dealing with Interfaces
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3842
and our smart pointer will simply take care of it all for you.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3843
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3844
The right hand side illustrates the basic idea of @code{QueryInterface}.  We
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3845
take a take a (smart) pointer to Interface @code{A} and ask it to search the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3846
aggregation for an interface associated with an interface identifier with
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3847
the value of @code{B:iid} which is passed as a parameter.  Recall that 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3848
@code{B::iid} is the @code{static InterfaceId} of the Interface class 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3849
@code{B}.  Observe that @code{QueryInterface} is a template function and the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3850
type specified in the angle brackets, here @code{<B>}, tells it what kind of 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3851
smart pointer to return.  In this case @code{QueryInterface} will find an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3852
Interface object of type @code{B::iid} in its list of Interfaces and return a
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3853
smart pointer to @code{B} as instructed.  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3854
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3855
Now that you have those connectors snapped together, you can ask each of
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3856
the Interfaces in the aggregation for any of the Interfaces in the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3857
aggregation.  For example we could walk the Interfaces asking each for the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3858
next in the aggregation.  First we would ask the Interface pointed to by the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3859
smart pointer a to look for the InterfaceId representing @code{B}:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3860
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3861
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3862
  Ptr<B> newB = a->QueryInterface<B> (B:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3863
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3864
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3865
Next, we can ask the Interface pointed to by the smart pointer @code{newB}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3866
to look for the @code{InterfaceId} representing @code{C}:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3867
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3868
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3869
  Ptr<C> newC = newB->QueryInterface<C> (C:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3870
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3871
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3872
Then, we can ask the Interface pointed to by the smart pointer @code{newC}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3873
to look for the InterfaceId representing A and complete our circuit of the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3874
aggregation:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3875
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3876
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3877
  Ptr<A> newA = newC->QueryInterface<A> (A:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3878
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3879
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3880
@code{QueryInterface} (often abbreviated QI) has some important properties
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3881
that we need to go over.  Technically, QI is a @emph{symmetric}, 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3882
@emph{reflexive} and @emph{transitive} operation with respect to the set of
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3883
aggregated Interfaces.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3884
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3885
@subsubsection Symmetry
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3886
The symmetric nature of QI guarantees that if one performs a QI on a given
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3887
Interface for the Interface Id of that same interface, that 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3888
@code{QueryInterface} must succeed.  The existence of interface A in the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3889
aggregation implies the reachability of Interface A in the aggregation.  This
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3890
is usually written (by Microsoft) as,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3891
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3892
@center must succeed (A >> A)
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3893
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3894
We can illustrate this property with the code snippet,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3895
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3896
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3897
  Ptr<A> symmetricA = a->QueryInterface<A> (A:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3898
  NS_ASSERT (symmetricA);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3899
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3900
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3901
Here we take as given an interface (smart) pointer named a on which we
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3902
perform a QI looking for the InterfaceId of that same Interface.  This call
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3903
must always succeed and a smart pointer to the Interface a is returned by QI.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3904
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3905
@subsubsection Reflexivity
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3906
Calls to QI must also be reflexive.  This means that if you successfully QI
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3907
for interface B from interface A, then you must always be able to QI for A
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3908
from B.  This is usually written as,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3909
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3910
@center must succeed (A >> B, then B >> A)
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3911
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3912
This property can be illustrated with the code snippet,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3913
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3914
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3915
  Ptr<B> b = a->QueryInterface<B> (B:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3916
  Ptr<A> reflexiveA = b->QueryInterface<A> (A:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3917
  NS_ASSERT (reflexiveA);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3918
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3919
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3920
If the first @code{QueryInterface} on Interface A looking for Interface B 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3921
succeeds, then a @code{QueryInterface} on Interface B looking for Interface A 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3922
must succeed.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3923
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3924
@subsubsection Transitivity
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3925
@code{QueryInteface} must also be transitive.  This means that if one can
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3926
find Interface B from Interface A, and Interface C from Interface B, then one
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3927
must also be able to find interface C from Interface A.  This is usually
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3928
written as,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3929
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3930
@center must succeed (A >> B, and B >> C, then A >> C)
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3931
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3932
This property can be illustrated with the code snippet,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3933
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3934
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3935
  Ptr<B> b = a->QueryInterface<B> (B:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3936
  Ptr<C> c = b->QueryInterface<C> (C:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3937
  Ptr<C> transitiveC = a->QueryInterface<C> (C:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3938
  NS_ASSERT (transitiveC);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3939
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3940
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3941
If you can get to Interface B from Interface A, and you can get to Interface C
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3942
from Interface B, then a QueryInterface on Interface A looking for Interface C
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3943
must also succeed.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3944
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3945
@subsection
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3946
The final piece of this puzzle is to locate where the interface Ids actually
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3947
come from.  The answer is from a static initializer that must be located in 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3948
the @code{.cc} file corresponding to the Interface.  For example, to 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3949
initialize the Interface Id for the class A above, you would simply add the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3950
following code to the source file that implements class A,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3951
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3952
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3953
  const InterfaceId A::iid = 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3954
    MakeInterfaceId (``A'', Object::iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3955
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3956
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3957
This code is guaranteed by the C++ language definition to be executed before
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3958
your main procedure is entered.  The call to MakeInterfaceId will assign a
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3959
process-local unique identifier to your class and associate your interface
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3960
with the name (string) ``A.''  This allows you to look up an InterfaceId by
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3961
a human readable string.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3962
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3963
An advanced ns-3 specific feature of QueryInterface is exposed here.  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3964
@code{MakeInterfaceId} takes an @code{InterfaceId} as a parameter.  This is
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3965
the @code{iid} of the base class from which you inherited.  In most cases
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3966
this will be @code{Object::iid}, which is the @code{InterfaceId} of the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3967
@code{Object} base class.  In @command{ns-3}, the @code{Object} base class
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3968
has its own @code{iid} and you can QI for that @code{iid}.  The @code{Object}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3969
base class has a rough equivalence to the @emph{IUnknown} Interface in
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3970
Microsofts COM, so you can QI for @code{Object::iid} in @command{ns-3}f just
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3971
as you might QI for IID_IUnknown in COM.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3972
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3973
The InterfaceId you pass to @code{MakeInterfaceId} is used to create an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3974
inheritance tree in the ns-3 interface manager.  This inheritance tree is also
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3975
walked in @code{QueryInterface} Interface searches.  Consider a simple case
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3976
of a base class and a derived class as shown below,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3977
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3978
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3979
  class Base : public Object
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3980
  {
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3981
  public:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3982
    static const InterfaceId iid;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3983
    ...
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3984
  };
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3985
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3986
  class Derived : public Base
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3987
  {
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3988
  public:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3989
    static const InterfaceId iid;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3990
    ...
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3991
  };
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3992
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3993
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3994
To assign the InterfaceId for each of these classes, we could add two calls
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3995
to @code{MakeInterfaceId} reflecting the class hierarchy we just created.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3996
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3997
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3998
  const InterfaceId Base::iid = 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3999
    MakeInterfaceId (``Base'', Object::iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4000
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4001
  const InterfaceId Derived::iid = 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4002
    MakeInterfaceId (``Derived'', Base::iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4003
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4004
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4005
The first Interface is shown to inherit from class @code{Object} and the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4006
second inherits from class @code{Base}.  We could create these interfaces
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4007
as we usually do,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4008
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4009
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4010
Ptr<Base> base = Create<Base> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4011
Ptr<Derived> derived = Create<Derived> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4012
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4013
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4014
The derived and base @code{InterfaceIds} are either present or not present
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4015
based on the inheritance tree.  For example, a QI for the @code{Base
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4016
InterfaceId} must succeed when done against a @code{Ptr<Base>}; but a QI for
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4017
the @code{Derived InterfaceId} must fail when done against a @code{Ptr<Base>}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4018
However, a QI for the @code{Base InterfaceId} must succeed when done against a
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4019
@code{Ptr<Derived>}; and a QI for the @code{Derived InterfaceId} must succeed
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4020
when done against a @code{Ptr<Derived>}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4021
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4022
This feature allows you to use implementation inheritance to easily create
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4023
new Interfaces.  You are prevented from doing so in Microsoft COM, but this
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4024
was almost universally identified as a problem.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4025
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4026
@subsection A Real Example
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4027
At this point you may be asking yourself what the point of all of this is,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4028
since you already had those pointers laying around when you created the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4029
objects.  The typical case is that you would forget about the pointers to the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4030
contained objects and only export a single Interface.  Other Interfaces could
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4031
be discovered using QI.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4032
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4033
Generally one tends to think of one of the Interfaces in the aggregation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4034
as being the container and other Interfaces being aggregated to that
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4035
container.  In the case of a Node, for example, it is quite natural to think
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4036
of the Node as being the container which contains Interfaces for the protocol
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4037
stacks, internet routing, etc.  So, lets start developing an example by
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4038
calling the container Interface Node instead of A.  The creation of this
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4039
Interface is found all over our example programs.  For example, you will 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4040
find code like the following in @code{samples/simple-point-to-point.cc}:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4041
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4042
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4043
  Ptr<Node> n = Create<InternetNode> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4044
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4045
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4046
This code is described in detail in previous sections, but the important thing
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4047
to realize here is that the resulting @code{Node} is an @command{ns-3}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4048
Interface.  This is not at all obvious -- you must look at the source code to
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4049
see that this is true.  Take a look at @code{src/node/node.h} and find the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4050
class declaration for class @code{Node}.  There you will find,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4051
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4052
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4053
  class Node : public Object
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4054
  {
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4055
  public:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4056
    static const InterfaceId iid;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4057
    ...
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4058
  };
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4059
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4060
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4061
Class @code{Node} inherits from class @code{Object} and provides an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4062
@code{InterfaceId}, therefore it is an @command{ns-3} interface.  You now 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4063
know you can use @code{AddInterface} for aggregation and @code{QueryInterface}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4064
for Interface discovery against any @code{Node} in the system.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4065
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4066
We spoke of a protocol stack that is aggregated to a @code{Node} in our
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4067
discussions above, what we see in the real @command{ns-3} code is that this
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4068
is represented by the @code{Ipv4} Interface.  If you look in 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4069
@code{src/node/ipv4.h} you will find,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4070
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4071
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4072
  class Ipv4 : public Object
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4073
  {
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4074
  public:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4075
    static const InterfaceId iid;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4076
    ...
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4077
  };
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4078
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4079
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4080
Since class @code{Ipv4} inherits from class @code{Object} and has a 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4081
@code{static InterfaceId}, it is an @command{ns-3} Interface.  If you look in
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4082
@code{src/node/ipv4.cc} you will find,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4083
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4084
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4085
  const InterfaceId Ipv4::iid = 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4086
    MakeInterfaceId (``Ipv4'', Object::iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4087
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4088
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4089
After all of this reading you now know that this code snippet is asking the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4090
system to create a unique @code{InterfaceId} for the @code{Ipv4} class and
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4091
declares that @code{Ipv4} inherits from class @code{Object}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4092
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4093
It turns out that the Ipv4 class is an abstract base class (ABC).  There are
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4094
a number of pure virtual methods declared in that class.  This means that
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4095
an @code{Ipv4} object may not be instantiated.  What is instantiated is an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4096
implementation class, called @code{Ipv4Impl}.  This class inherits from
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4097
@code{Ipv4} and provides the required virtual methods.  This is where
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4098
understanding what is an Interface and what is not gets tricky.  The 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4099
Interface is the @code{Ipv4} class since that is where the @code{InterfaceId}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4100
is found.  The fact that you see @code{ipv4::iid} tells you that the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4101
@code{Ipv4} class is the Interface and has the associated @code{InterfaceId}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4102
The class @code{Ipv4Impl} provides an implementation for the pure virtual
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4103
methods in @code{Ipv4}.  Since class @code{Ipv4} cannot be instantiated, one
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4104
instantiates the @code{Ipv4Impl} class to create an @code{Ipv4} Interface.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4105
Once the @code{Ipv4Impl} class is instantiated, the pointer to it is
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4106
immediately cast to an @code{Ipv4} pointer.  Clients will then use the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4107
@code{Ipv4} object access methods (see @code{ipv4.h}) to talk to the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4108
@code{Ipv4Impl} object over the @code{Ipv4} Interface.  I urge you to not go
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4109
any further until you thoroughly understand what youve just read.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4110
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4111
If you now look in the file, @code{src/internet-node/internet-node.cc} you
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4112
will see the following code in @code{InternetNode::Construct} that creates the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4113
@code{Ipv4} Interface and aggregates it to the @code{Node} interface (recall
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4114
that class @code{Node} is an Interface and class @code{InternetNode} inherits
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4115
from class @code{Node}):
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4116
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4117
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4118
  Ptr<Ipv4Impl> ipv4Impl = Create<Ipv4Impl> (ipv4);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4119
  ...
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4120
  Object::AddInterface (ipv4Impl);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4121
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4122
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4123
Note that the parameter @code{ipv4} passed to the @code{Create} template
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4124
function is actually a pointer to an @code{Ipv4L3Protocol} which you can
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4125
ignore at this point --- it doesn't really have anything to do with the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4126
@code{Ipv4} Interface.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4127
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4128
This last example does illustrate that the fact that whether an @command{ns-3}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4129
object is or is not an Interface can be quite well hidden.  The designers of
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4130
the system had long and involved discussions on this issue and in the end
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4131
decided that mnemonic aids such as Hungarian notation were a stylistic thing
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4132
and you should just refer to the system documentation to determine what
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4133
objects are ns-3 Interfaces and what those Interfaces actually are (RTFM ---
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4134
Read the Fine Manual).
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4135
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4136
In this case, you know that the class @code{Ipv4Impl} inherits from some
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4137
Interface since there is a call to @code{AddInterface} that refers to it.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4138
You can go to the header file @code{src/internet-node/ipv4-impl.h} and find
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4139
that @code{Ipv4Impl} inherits from class @code{Ipv4}.  You then go to file
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4140
@code{src/node/ipv4.h} and see that it inherits from @code{Object} and
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4141
contains an @code{InterfaceId}.  Thus the Interface added is really the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4142
@code{Ipv4} Interface with the interface Id @code{Ipv4::iid}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4143
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4144
Returning to some @command{ns-3} example code, lets take a look at 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4145
@code{src/examples/simple-point-to-point.cc} again.  You will find the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4146
following code:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4147
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4148
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4149
  Ptr<Node> n0 = Create<InternetNode> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4150
  ...
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4151
  Ptr<Ipv4> ipv4;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4152
  ipv4 = n0->QueryInterface<Ipv4> (Ipv4::iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4153
  ipv4->SetDefaultRoute (Ipv4Address (``10.1.1.2''), 1);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4154
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4155
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4156
The first line creates an @code{InternetNode} object and casts the resulting
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4157
smart pointer to a @code{Node}.  The next line declares a smart pointer to an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4158
@code{Ipv4} object.  Because youve been through the code with us, you know
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4159
that both the @code{Node} and the @code{Ipv4} objects are Interfaces.  They 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4160
should be able to participate in a @code{QueryInterface}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4161
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4162
The next line confirms it.  We do a @code{QueryInterface} on the @code{Node},
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4163
looking for the @code{Ipv4} Interface (@code{Ipv4::iid}).  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4164
@code{QueryInterface} then returns a smart pointer to its aggregated 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4165
@code{Ipv4} Interface.  [Recall that this Interface was aggregated in
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4166
@code{InternetNode::Construct}.  We knew to start looking for the aggregation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4167
in @code{InternetNode} since we originally created an @code{InternetNode} in 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4168
the @code{Create} template function and then implicitly cast it to a 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4169
@code{Node}.]
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4170
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4171
Once you have the @code{Ipv4} smart pointer, you simply use it as if it were
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4172
any other C++ object.  The last line shows this by setting the default route 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4173
for the node.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4174
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4175
@section Caveats
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4176
There are a few things that you should remember but which may not be 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4177
immediately obvious.  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4178
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4179
@subsection Interface Ids are Associated with Classes not Objects
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4180
Interfaces are identified by an @code{InterfaceId} that is associated with
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4181
the Interface class, not the Interface object.  That is indicated by the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4182
@code{static} keyword in the declaration of the @code{iid} in the class.  The
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4183
interface Id for a given Interface class exists independently of any objects
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4184
of that class that you may instantiate; and all objects of a given Interface
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4185
type share the same @code{InterfaceId}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4186
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4187
You cannot add more than one Interface of a given type (@code{iid}) to an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4188
aggregation.  If you need to contain a number of Interfaces of the same type
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4189
in the same aggregation, you will need to provide a separate container over
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4190
which you can iterate.  For example, the @code{Node} class provides methods,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4191
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4192
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4193
  uint32_t GetNDevices (void) const;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4194
  Ptr<NetDevice> GetDevice (uint32_t index) const;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4195
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4196
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4197
that are used iterate over the multiple @code{NetDevice} Interfaces associated
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4198
with it.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4199
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4200
@emph{Interface Ids do not identify objects.}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4201
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4202
@subsection Dont use QI to Check Your Own Type.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4203
It is tempting to use @code{QueryInterface} as a form of runtime type
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4204
information.  Dont do it.  You have no control over what other object may be
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4205
added to your aggregation and this may cause problems.  Someone else may have
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4206
appropriated (reimplemented) your type and aggregated themselves onto your 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4207
aggregation.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4208
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4209
Consider a socket factory implementation.  Sockets can be either UDP sockets
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4210
or TCP sockets.  A socket factory will have a generic @code{SocketFactory}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4211
Interface and either a UDP specific interface for setting UDP parameters or a
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4212
similar TCP-specific interface.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4213
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4214
Consider what might happen if you declared your socket factory as a partially
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4215
abstract base class, and then provided separate implementations for UDP and
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4216
TCP specific methods of this factory in separate concrete classes.  Now 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4217
consider what might happen if you used QueryInterface in your base class
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4218
to determine if you were a UDP or a TCP factory.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4219
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4220
If a factory, say the UDP version, were not aggregated to any other Interface,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4221
the base class could QueryInterface on itself for the UDP-specific interface.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4222
It could then infer that it was a UDP implementation and would then do any 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4223
UDP-specific tasks it could.  [Experienced C++ folks are cringing about how
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4224
horrible this design is, but bear with me --- its a simple illustration of 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4225
a specific and perhaps not-too-obvious problem.]
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4226
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4227
If another factory, say the TCP version, were not aggregated to any other
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4228
Interface, the base class could QueryInterface on itself for the UDP-specific
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4229
interface.  If this failed, it could then infer that it had a TCP
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4230
implementation and would then do any TCP-specific tasks it could.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4231
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4232
Now, what happens when these two working objects are aggregated together.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4233
Since the Interfaces are conceptually snapped together the TCP implementation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4234
would suddenly begin finding the UDP Interface from the other class factory
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4235
and fail.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4236
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4237
@emph{Interface Ids should not be used as run-time type information.}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4238
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4239
@section Connecting the Dots
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4240
This may all sound very complicated to you if this is your first exposure to
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4241
these concepts.  It may be annoying if I tell you that its really not as hard
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4242
as it sounds.  Rest assured that if you take some time, look at and understand
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4243
the examples and write a little test code it will all come together for you.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4244
Grep around the system for AddInterface and QueryInterface and take a look at
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4245
how we have used them.  This will also give you a good idea of what our core
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4246
Interfaces are.  If you grep for @code{::iid} you will find most, if not all
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4247
of the interface declarations in the system.  The more you see this idiom in
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4248
use, the more comfortable you will be with the idea and the more you will see
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4249
how this addresses the weak base class, swiss army knife base class, and
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4250
fragile base class problems I explained at the beginning.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4251
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4252
As I alluded to earlier, the developers had long discussions regarding how to
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4253
make navigating the QueryInterface environment easier.  The primary issue was
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4254
how we could make it easier to convey to you, the model writer, that an object
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4255
was an Interface.  One suggestion was to adopt the convention that classes
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4256
that implement Interfaces begin with the letter I.  Microsoft does this, as
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4257
exemplified by the class IUnknown.  We also toyed with the idea of beginning
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4258
our header files with i- as in i-ipv4.h. We considered forcing some structure
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4259
on Interfaces with a pure virtual class specification, the names of which
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4260
begin with an I; and corresponding implementations, the names of which begin
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4261
with a C.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4262
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4263
In the end we decided that we were really discussing issues of programming
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4264
style, and we really could not come up with a strong reason to impose any
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4265
particular solution.  In the end, we decided that we would not impose any
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4266
structure on the source code, nor impose any naming convention.  We will
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4267
rely on our documentation system (Doxygen) to break out all objects with
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4268
InterfaceIds in their class hierarchy into a separate section.  For now,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4269
until this is implemented, grep is your friend.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4270
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4271
@c ========================================================================
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4272
@c Doxygen
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4273
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4274
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4275
@node The-Doxygen-Documentation-System
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4276
@chapter The Doxygen Documentation System
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4277
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4278
@node How-To-Change-Things
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4279
@chapter How to Change Things
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4280
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4281
@node How-To-Set-Default-Values
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4282
@chapter How to Set Default Values
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4283
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4284
@node How-To-Write-A-New-Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4285
@chapter How to Write a New Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4286
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4287
@printindex cp
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4288
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4289
@bye