doc/tutorial/tutorial.texi
author Tom Henderson <tomh@tomh.org>
Fri, 11 Jan 2008 08:31:38 -0800
changeset 2216 9c209295b0b1
parent 2215 9dda69de4ca7
child 2221 8a6f9a7d8c48
permissions -rw-r--r--
Missing subsection text
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::
2211
3e1cec7d22d6 fix node and up
Craig Dowell <craigdo@ee.washington.edu>
parents: 2210
diff changeset
    85
* Object-Model::
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
* The-Doxygen-Documentation-System::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
* How-To-Change-Things::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
* How-To-Set-Default-Values::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
* How-To-Write-A-New-Application::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
@end menu
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
@node Preface
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
@chapter Preface
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
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
    96
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
    97
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
    98
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
    99
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
   100
features as we go.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
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
   103
documentation 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
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
   105
into the workings of the system.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
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
   108
@itemize @bullet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
@item Software Architecture 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
@item Manual 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
@item Tutorial (this document)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
@end itemize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
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
   115
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
   116
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
   117
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
   118
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
@c Begin document body here
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
@c Introduction
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
@node Introduction
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
@chapter Introduction
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   129
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
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
   131
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
   132
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
   133
@command{ns-2}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   134
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   135
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
   136
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
   137
@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
   138
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
   139
@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
   140
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
   141
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
   142
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
   143
target language into the underlying C++.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   144
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   145
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
   146
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
   147
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
   148
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
   149
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
   150
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
   151
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
   152
@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
   153
scripts, run them and interpret results.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
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
   156
@itemize @bullet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   157
@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
   158
@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
   159
@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
   160
significant complexity;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   161
@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
   162
@item Write new @command{ns-3} applications;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
@item Use the tracing subsystem.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
@end itemize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   165
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   166
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   167
@c Resources
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   169
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   170
@node Resources
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   171
@chapter Resources
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   172
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   173
@menu
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   174
* The-Web::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   175
* Mercurial::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   176
* Waf::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   177
* Environment-Idioms-Design-Patterns::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   178
* Socket-Programming::
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   179
@end menu
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   180
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   181
@node The-Web
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   182
@section The Web
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   183
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   184
@cindex www.nsnam.org
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   185
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
   186
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
   187
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
   188
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
   189
@uref{http://www.nsnam.org/documents.html}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   190
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   191
@cindex documentation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
@cindex architecture
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   193
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
   194
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
   195
system is documented in great detail using 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
@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
   197
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
   198
@uref{http://www.nsnam.org/wiki/}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   199
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   200
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
   201
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
   202
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
   203
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   204
@cindex repository!ns-3-dev
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   205
@cindex repository!releases
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   206
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
   207
@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
   208
developers may also be found there.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   210
@node Mercurial
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   211
@section Mercurial
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   212
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   213
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
   214
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
   215
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
   216
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
   217
the most well known.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   218
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   219
@cindex software configuration management
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   220
@cindex Mercurial
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   221
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
   222
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
   223
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
   224
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
   225
@uref{http://www.selenic.com/mercurial/},
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   226
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
   227
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
   228
also provides a tutorial at 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   229
@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
   230
and a QuickStart guide at
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   231
@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
   232
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   233
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
   234
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
   235
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   236
@node Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   237
@section Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   238
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   239
@cindex Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   240
@cindex make
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   241
@cindex build
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   242
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
   243
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
   244
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
   245
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
   246
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
   247
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
   248
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
   249
using the Python language.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   250
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   251
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
   252
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
   253
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
   254
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
   255
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
   256
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   257
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
   258
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
   259
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   260
@node Environment-Idioms-Design-Patterns
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   261
@section Environment, Idioms, and Design Patterns
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   262
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   263
@cindex C++
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   264
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
   265
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
   266
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
   267
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
   268
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
   269
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
   270
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
   271
in print.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   272
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   273
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
   274
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
   275
before proceeding.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   276
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   277
@subsection Environment
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   278
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   279
@cindex toolchain
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   280
@cindex GNU
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   281
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
   282
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
   283
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
   284
@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
   285
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   286
@cindex Linux
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   287
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
   288
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
   289
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
   290
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
   291
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
   292
@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
   293
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
   294
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
   295
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
   296
software can cause problems.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   297
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   298
@cindex Cygwin
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   299
@cindex MinGW
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   300
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
   301
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
   302
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
   303
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   304
@cindex Logitech
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   305
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
   306
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
   307
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
   308
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
   309
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
   310
running.  Beware of Logitech.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   311
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   312
@subsection Idioms and Design Patterns
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   313
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   314
@cindex idiom
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   315
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
   316
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
   317
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
   318
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
   319
patterns.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   320
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   321
@cindex design pattern
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   322
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
   323
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
   324
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
   325
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
   326
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   327
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   328
  for (;;)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   329
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   330
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   331
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
   332
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
   333
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
   334
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
   335
patterns.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   336
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   337
@cindex functor
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   338
@cindex callback
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   339
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   340
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
   341
@emph{Generalized Functors, Callbacks, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   342
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
   343
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
   344
in the @command{ns-3}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   345
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
   346
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
   347
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
   348
@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
   349
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   350
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
   351
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
   352
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   353
@cindex template
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   354
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
   355
templates.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   356
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
   357
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
   358
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
   359
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
   360
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
   361
somewhat fluent
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   362
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
   363
@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
   364
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   365
@node Socket-Programming
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   366
@section Socket Programming
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   367
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   368
@cindex sockets
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   369
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
   370
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
   371
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
   372
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
   373
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   374
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
   375
book, which you can find at:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   376
@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
   377
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   378
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
   379
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
   380
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
   381
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
   382
@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
   383
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
   384
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   385
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   386
@c The Basics
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   387
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   388
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   389
@node The-Basics
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   390
@chapter The Basics
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   391
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   392
@cindex Linux
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   393
@cindex Cygwin
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   394
@cindex GNU
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   395
@cindex toolchain
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   396
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
   397
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
   398
toolchain installed and verified.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   399
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   400
@cindex Mercurial
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   401
@cindex Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   402
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
   403
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
   404
@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
   405
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   406
@section Downloading
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   407
@cindex tarball
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   408
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
   409
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
   410
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
   411
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   412
@cindex repository
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   413
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
   414
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
   415
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
   416
will be
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   417
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
   418
@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
   419
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   420
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
   421
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
   422
consider downloading an official release.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   423
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   424
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
   425
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
   426
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
   427
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
   428
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
   429
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
   430
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
   431
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
   432
identifier.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   433
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   434
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
   435
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
   436
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
   437
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
   438
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
   439
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   440
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   441
  cd
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   442
  mkdir repos
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   443
  cd !$
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   444
  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
   445
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   446
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   447
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
   448
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   449
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   450
  destination directory: ns-3-dev
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   451
  requesting all changes
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   452
  adding changesets
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   453
  adding manifests
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   454
  adding file changes
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   455
  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
   456
  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
   457
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   458
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   459
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
   460
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
   461
look something like the following:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   462
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   463
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   464
  AUTHORS  RELEASE_NOTES  examples/  src/       waf*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   465
  LICENSE  VERSION        ns3/       tutorial/  waf.bat*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   466
  README   doc/           samples/   utils/     wscript
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   467
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   468
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   469
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
   470
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   471
@section Building
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   472
@cindex Waf!build
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   473
@cindex Waf!configure
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   474
@cindex Waf!debug
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   475
@cindex Waf!compile
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   476
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
   477
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
   478
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
   479
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
   480
command,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   481
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   482
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   483
  ./waf -d debug configure
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   484
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   485
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   486
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
   487
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
   488
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
   489
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   490
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   491
  ~/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
   492
  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
   493
  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
   494
  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
   495
  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
   496
  Checking for compiler could create programs : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   497
  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
   498
  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
   499
  Checking for flags -Wall                       : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   500
  Checking for flags -O2                         : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   501
  Checking for flags -g -DDEBUG                  : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   502
  Checking for flags -g3 -O0 -DDEBUG             : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   503
  Checking for g++                               : 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 stdlib.h                   : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   506
  Checking for header signal.h                   : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   507
  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
   508
  Checking for header stdint.h                   : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   509
  Checking for header inttypes.h                 : ok
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   510
  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
   511
  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
   512
  ~/repos/ns-3-dev >
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   513
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   514
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   515
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
   516
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
   517
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   518
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   519
  ./waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   520
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   521
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   522
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
   523
most important is the last one,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   524
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   525
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   526
  Compilation finished successfully
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   527
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   528
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   529
@section Running a Script
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   530
@cindex Waf!run
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   531
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
   532
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
   533
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
   534
@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
   535
world program by typing the following:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   536
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   537
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   538
  ./waf --run hello-simulator
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   539
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   540
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   541
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
   542
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
   543
produces the following output.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   544
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   545
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   546
  Hello Simulator
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   547
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   548
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   549
@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
   550
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   551
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   552
@c Some Prerequisites
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   553
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   554
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   555
@node Some-Prerequisites
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   556
@chapter Some Prerequisites
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   557
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   558
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
   559
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
   560
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
   561
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
   562
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   563
@section Abstractions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   564
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   565
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
   566
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
   567
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   568
@subsection Node
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   569
@cindex Node
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   570
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
   571
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
   572
@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
   573
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
   574
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
   575
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
   576
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   577
@cindex Node!class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   578
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
   579
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
   580
@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
   581
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
   582
@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
   583
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
   584
@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
   585
@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
   586
and automatically provides core IPv4 networking protocols.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   587
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   588
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
   589
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
   590
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
   591
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
   592
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   593
@subsection Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   594
@cindex Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   595
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
   596
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
   597
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
   598
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
   599
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
   600
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
   601
goal.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   602
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   603
@cindex system call
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   604
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
   605
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
   606
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
   607
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
   608
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
   609
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
   610
@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
   611
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   612
@cindex Application!class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   613
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
   614
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
   615
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
   616
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
   617
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
   618
@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
   619
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
   620
@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
   621
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
   622
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
   623
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   624
@subsection Channel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   625
@cindex Channel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   626
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   627
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
   628
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
   629
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
   630
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
   631
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
   632
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
   633
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
   634
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   635
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
   636
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
   637
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
   638
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
   639
@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
   640
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
   641
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   642
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
   643
@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
   644
@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
   645
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
   646
This gives us Ethernet-like functionality.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   647
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   648
@subsection Net Device
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   649
@cindex NetDevice
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   650
@cindex Ethernet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   651
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   652
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
   653
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
   654
(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
   655
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
   656
@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
   657
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
   658
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   659
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
   660
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
   661
@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
   662
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
   663
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
   664
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
   665
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   666
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
   667
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
   668
@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
   669
@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
   670
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
   671
multiple @code{NetDevice}s.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   672
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   673
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
   674
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
   675
@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
   676
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
   677
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
   678
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
   679
@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
   680
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   681
@subsection Topology Helpers
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   682
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
   683
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
   684
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
   685
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
   686
@code{Channel}s.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   687
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   688
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
   689
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
   690
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
   691
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
   692
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
   693
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
   694
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
   695
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
   696
an easy to use model.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   697
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   698
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
   699
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
   700
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
   701
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
   702
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
   703
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
   704
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
   705
the equivalent of installing the network ``kit.''
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   706
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   707
@section Important Idioms
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   708
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
   709
@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
   710
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
   711
examine this in some detail here.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   712
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   713
@cindex InternetNode
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   714
@cindex Create
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   715
@cindex Ptr
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   716
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
   717
script, we will 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   718
typically do something like the following example:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   719
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   720
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   721
  Ptr<Node> p = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   722
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   723
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   724
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   725
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
   726
@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
   727
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
   728
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
   729
in some detail.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   730
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   731
@subsection Templates 101
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   732
@cindex template
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   733
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
   734
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
   735
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   736
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
   737
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
   738
using C++ @emph{templates}.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   739
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   740
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   741
  Ptr<Node> p = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   742
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   743
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   744
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
   745
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
   746
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
   747
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
   748
quite different.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   749
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   750
@cindex template!declaration
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   751
@cindex template!definition
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   752
@cindex template!use
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   753
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
   754
@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
   755
something like,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   756
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   757
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   758
  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
   759
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   760
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   761
@cindex template!typename
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   762
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
   763
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
   764
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
   765
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
   766
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
   767
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
   768
to compare templates with macros.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   769
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   770
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
   771
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
   772
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
   773
a C++ data type name.  For example,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   774
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   775
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   776
  T Add (T first, T second);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   777
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   778
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   779
might eventually become
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   780
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   781
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   782
  int Add (int first, int second);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   783
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   784
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   785
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
   786
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
   787
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   788
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   789
  template <typename T> 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   790
  T Add (T first, T second)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   791
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   792
    return first + second;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   793
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   794
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   795
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   796
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
   797
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
   798
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
   799
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   800
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
   801
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
   802
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
   803
like,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   804
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   805
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   806
  int x, y, z;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   807
  z = Add<int> (x, y);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   808
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   809
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   810
@cindex template!instantiate
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   811
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
   812
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
   813
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
   814
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
   815
automagically generate code equivalent to,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   816
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   817
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   818
  int Add (int first, int second)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   819
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   820
    return first + second;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   821
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   822
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   823
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   824
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
   825
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
   826
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   827
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   828
  float x, y, z;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   829
  z = Add<float> (x, y);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   830
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   831
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   832
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
   833
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   834
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   835
  float Add (float first, float second)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   836
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   837
    return first + second;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   838
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   839
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   840
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   841
@cindex template!function
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   842
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
   843
@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
   844
are @emph{templating} function declarations and definitions.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   845
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   846
@cindex template!class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   847
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
   848
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
   849
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
   850
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   851
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   852
  template <typename T> 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   853
  class MyStack
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   854
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   855
    void Push (T data);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   856
    T Pop (void);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   857
  };
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   858
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   859
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   860
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
   861
definitions,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   862
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   863
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   864
  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
   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
  };
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   868
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   869
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   870
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
   871
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   872
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   873
  int x, y;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   874
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   875
  MyStack<int> stack;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   876
  stack.Push (x);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   877
  y = stack.Pop ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   878
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   879
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   880
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
   881
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
   882
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
   883
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   884
@subsection Smart Pointers 101
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   885
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
   886
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
   887
counting.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   888
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   889
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   890
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
   891
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
   892
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
   893
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   894
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   895
  Ptr<Node> p = ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   896
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   897
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   898
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
   899
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
   900
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
   901
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
   902
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
   903
``pooter node'' where pooter rhymes with footer.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   904
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   905
@cindex memory management
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   906
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
   907
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
   908
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
   909
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
   910
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
   911
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
   912
respectively.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   913
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   914
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
   915
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
   916
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
   917
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
   918
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
   919
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
   920
@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
   921
deallocation automatic.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   922
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   923
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
   924
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
   925
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
   926
smart pointer.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   927
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   928
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   929
  void SimpleSubroutine (void)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   930
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   931
    Ptr<Node> p;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   932
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   933
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   934
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   935
@cindex scope
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   936
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
   937
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
   938
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
   939
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
   940
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
   941
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
   942
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
   943
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
   944
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
   945
underlying data can be freed.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   946
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   947
@cindex reference counting!intrusive
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   948
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
   949
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
   950
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
   951
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
   952
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
   953
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
   954
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
   955
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
   956
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
   957
(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
   958
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
   959
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
   960
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
   961
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
   962
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
   963
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   964
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
   965
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
   966
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
   967
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
   968
@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
   969
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
   970
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
   971
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
   972
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
   973
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
   974
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
   975
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
   976
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
   977
memory leaks.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   978
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   979
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
   980
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
   981
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
   982
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
   983
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
   984
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
   985
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
   986
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
   987
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
   988
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   989
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   990
  MyClass *p = ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   991
  p->method ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   992
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   993
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   994
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   995
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
   996
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
   997
@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
   998
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
   999
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
  1000
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
  1001
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
  1002
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
  1003
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1004
@cindex template
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1005
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
  1006
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
  1007
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
  1008
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
  1009
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
  1010
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
  1011
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
  1012
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1013
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1014
  SmartPointer<MyClass> p = ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1015
  p->method ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1016
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1017
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1018
@subsection Object Creation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1019
@cindex Create
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1020
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
  1021
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
  1022
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1023
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1024
  ... = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1025
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1026
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1027
@cindex template!function
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1028
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
  1029
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
  1030
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
  1031
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
  1032
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
  1033
count set to one.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1034
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1035
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
  1036
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
  1037
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
  1038
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
  1039
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
  1040
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1041
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1042
  int parm = 1;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1043
  ... = Create<MyClass> (parm);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1044
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1045
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1046
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
  1047
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
  1048
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1049
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1050
  int parm = 1;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1051
  ... = 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
  1052
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1053
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1054
@subsection Type Safety
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1055
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
  1056
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
  1057
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1058
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1059
  Ptr<Node> p = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1060
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1061
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1062
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1063
@cindex Node
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1064
@cindex Create
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1065
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
  1066
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
  1067
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
  1068
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
  1069
@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
  1070
kind of type conversion going on.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1071
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1072
@cindex implicit conversion
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1073
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
  1074
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
  1075
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1076
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1077
  int i = 1;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1078
  double d = 2.;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1079
  if (n == d) ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1080
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1081
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1082
@cindex standard conversion
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1083
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
  1084
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
  1085
@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
  1086
by the C++ standard.  Among them are,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1087
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1088
@itemize @bullet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1089
@item Integral Promotions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1090
@item Integral Conversions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1091
@item Floating Conversions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1092
@item Pointer Conversions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1093
@item Reference Conversions
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1094
@end itemize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1095
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1096
@cindex assignment operator
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1097
@cindex Ptr
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1098
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
  1099
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
  1100
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
  1101
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
  1102
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
  1103
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
  1104
@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
  1105
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1106
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
  1107
@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
  1108
@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
  1109
@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
  1110
@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
  1111
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
  1112
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
  1113
@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
  1114
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
  1115
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1116
@cindex base class
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1117
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
  1118
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
  1119
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1120
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1121
  Ptr<Node> p = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1122
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1123
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1124
or,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1125
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1126
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1127
  Ptr<Channel> p = Create<CsmaChannel> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1128
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1129
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1130
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
  1131
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1132
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1133
  Ptr<Node> p = Create<CsmaChannel> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1134
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1135
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1136
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
  1137
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
  1138
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1139
@subsection Summary
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1140
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
  1141
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
  1142
like:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1143
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1144
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1145
  Ptr<Node> p = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1146
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1147
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1148
@cindex Create
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1149
@cindex InternetNode
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1150
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1151
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
  1152
@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
  1153
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
  1154
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
  1155
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
  1156
@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
  1157
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1158
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
  1159
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
  1160
this tutorial.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1161
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1162
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1163
@c A First ns-3 script
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1164
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1165
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1166
@node A-First-ns-3-Script
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1167
@chapter A First ns-3 script
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1168
@cindex design pattern
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1169
@cindex idiom
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1170
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
  1171
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
  1172
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
  1173
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
  1174
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
  1175
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1176
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1177
  AUTHORS  RELEASE_NOTES  examples/  src/       waf*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1178
  LICENSE  VERSION        ns3/       tutorial/  waf.bat*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1179
  README   doc/           samples/   utils/     wscript
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1180
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1181
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1182
@cindex hello-simulator.cc
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1183
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
  1184
@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
  1185
@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
  1186
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
  1187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1188
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1189
  #include "ns3/log.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1190
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1191
  NS_LOG_COMPONENT_DEFINE ("HelloSimulator");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1192
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1193
  using namespace ns3;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1194
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1195
  int 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1196
  main (int argc, char *argv[])
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1197
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1198
    LogComponentEnable ("HelloSimulator", LOG_LEVEL_INFO);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1199
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1200
    NS_LOG_INFO ("Hello Simulator");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1201
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1202
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1203
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1204
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
  1205
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
  1206
 standard error output stream.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1207
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1208
@cindex logging
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1209
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
  1210
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
  1211
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
  1212
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
  1213
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1214
@section A Simple Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1215
@cindex InternetNode
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1216
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
  1217
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
  1218
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
  1219
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1220
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1221
  #include "ns3/ptr.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1222
  #include "ns3/internet-node.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1223
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1224
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1225
@cindex include files
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1226
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
  1227
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
  1228
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
  1229
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
  1230
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
  1231
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
  1232
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1233
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
  1234
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
  1235
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
  1236
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1237
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1238
  Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1239
  Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1240
  Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1241
  Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1242
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1243
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1244
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
  1245
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
  1246
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
  1247
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
  1248
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1249
@cindex Channel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1250
@cindex CsmaChannel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1251
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
  1252
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
  1253
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
  1254
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1255
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
  1256
declarations:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1257
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1258
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1259
  #include "ns3/csma-channel.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1260
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1261
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1262
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
  1263
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
  1264
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
  1265
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
  1266
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
  1267
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
  1268
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
  1269
existence and initialized.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1270
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1271
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1272
  Ptr<CsmaChannel> lan = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1273
    CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1274
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1275
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1276
@cindex idiom!unnamed parameter
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1277
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
  1278
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
  1279
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
  1280
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
  1281
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
  1282
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
  1283
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1284
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1285
  DataRate rate (5000000);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1286
  Time latency (MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1287
  Ptr<CsmaChannel> lan = CsmaTopology::CreateCsmaChannel (rate, latency);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1288
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1289
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1290
@cindex constructor
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1291
@cindex constructor!Time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1292
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
  1293
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
  1294
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
  1295
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
  1296
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
  1297
written,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1298
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1299
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1300
  Time latency ("2ms");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1301
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1302
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1303
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
  1304
was used in the example):
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1305
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1306
@itemize @bullet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1307
@item @code{Seconds (double)}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1308
@item @code{MilliSeconds (uint64_t)}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1309
@item @code{MicroSeconds (uint64_t)}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1310
@item @code{NanoSeconds (uint64_t)}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1311
@item @code{PicoSeconds (uint64_t)}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1312
@item @code{FemtoSeconds (uint64_t)}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1313
@end itemize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1314
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1315
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
  1316
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
  1317
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
  1318
@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
  1319
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
  1320
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
  1321
@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
  1322
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
  1323
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
  1324
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1325
@cindex MAC!address
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1326
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
  1327
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
  1328
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
  1329
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
  1330
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
  1331
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
  1332
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
  1333
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
  1334
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1335
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1336
  #include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1337
  #include "ns3/csma-net-device.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1338
  #include "ns3/csma-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1339
  #include "ns3/csma-ipv4-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1340
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1341
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1342
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
  1343
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1344
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1345
  uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1346
    Mac48Address("08:00:2e:00:00:00"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1347
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1348
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1349
[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
  1350
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1351
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
  1352
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
  1353
(@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
  1354
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
  1355
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
  1356
addresses).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1357
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1358
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1359
  uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1360
    Mac48Address("08:00:2e:00:00:01"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1361
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1362
  uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1363
    Mac48Address("08:00:2e:00:00:02"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1364
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1365
  uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1366
    Mac48Address("08:00:2e:00:00:03"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1367
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1368
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1369
@cindex IP!address
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1370
@cindex IP!network mask
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1371
@cindex multihome
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1372
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
  1373
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
  1374
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
  1375
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
  1376
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
  1377
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
  1378
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
  1379
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
  1380
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
  1381
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
  1382
@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
  1383
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
  1384
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1385
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1386
  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
  1387
    Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1388
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1389
  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
  1390
    Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1391
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1392
  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
  1393
    Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1394
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1395
  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
  1396
    Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1397
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1398
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1399
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
  1400
something like the following,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1401
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1402
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1403
  #include "ns3/log.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1404
  #include "ns3/ptr.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1405
  #include "ns3/internet-node.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1406
  #include "ns3/csma-channel.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1407
  #include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1408
  #include "ns3/csma-net-device.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1409
  #include "ns3/csma-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1410
  #include "ns3/csma-ipv4-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1411
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1412
  NS_LOG_COMPONENT_DEFINE ("HelloSimulator");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1413
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1414
  using namespace ns3;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1415
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1416
  int 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1417
  main (int argc, char *argv[])
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1418
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1419
    LogComponentEnable ("HelloSimulator", LOG_LEVEL_INFO);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1420
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1421
    NS_LOG_INFO ("Hello Simulator");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1422
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1423
    Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1424
    Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1425
    Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1426
    Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1427
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1428
    Ptr<CsmaChannel> lan = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1429
      CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1430
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1431
    uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1432
      Mac48Address("08:00:2e:00:00:00"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1433
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1434
    uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1435
      Mac48Address("08:00:2e:00:00:01"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1436
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1437
    uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1438
      Mac48Address("08:00:2e:00:00:02"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1439
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1440
    uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1441
      Mac48Address("08:00:2e:00:00:03"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1442
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1443
    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
  1444
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1445
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1446
    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
  1447
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1448
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1449
    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
  1450
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1451
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1452
    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
  1453
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1454
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1455
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1456
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1457
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
  1458
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
  1459
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1460
@section Using Applications
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1461
@cindex Create
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1462
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
  1463
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
  1464
@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
  1465
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
  1466
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
  1467
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
  1468
the constructor arguments.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1469
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1470
@subsection A UDP Echo Client Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1471
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
  1472
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
  1473
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1474
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1475
#include "ns3/udp-echo-client.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1476
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1477
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1478
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
  1479
following code:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1480
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1481
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1482
  uint32_t packetSize = 1024;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1483
  uint16_t port = 7;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1484
  uint32_t maxPacketCount = 1;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1485
  Time interPacketInterval = Seconds (1.);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1486
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1487
  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
  1488
    maxPacketCount, interPacketInterval, packetSize);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1489
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1490
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1491
@cindex packet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1492
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
  1493
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
  1494
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
  1495
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
  1496
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
  1497
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
  1498
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
  1499
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
  1500
to wait for one second.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1501
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1502
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
  1503
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
  1504
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
  1505
of the underlying object.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1506
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1507
@cindex implicit conversion sequence
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1508
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
  1509
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
  1510
@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
  1511
@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
  1512
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
  1513
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
  1514
transparently.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1515
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1516
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
  1517
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
  1518
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
  1519
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
  1520
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
  1521
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
  1522
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1523
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
  1524
explicit named variable as in the following:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1525
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1526
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1527
  Ipv4Address addr ("10.1.1.2");
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
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1530
  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
  1531
    maxPacketCount, interPacketInterval, packetSize);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1532
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1533
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1534
@cindex idiom|unnamed parameter
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1535
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
  1536
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1537
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1538
  Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1539
    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
  1540
    packetSize);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1541
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1542
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1543
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
  1544
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1545
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1546
  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
  1547
    maxPacketCount, interPacketInterval, packetSize);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1548
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1549
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1550
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
  1551
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
  1552
comfortable seeing and using all three methods.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1553
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1554
@subsection A UDP Echo Server Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1555
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
  1556
application:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1557
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1558
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1559
#include "ns3/udp-echo-server.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1560
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1561
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1562
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
  1563
following code:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1564
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1565
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1566
  Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1567
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1568
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1569
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
  1570
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
  1571
@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
  1572
creation idiom.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1573
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1574
@subsection A UDP Echo Client-Server Simulation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1575
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
  1576
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
  1577
``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
  1578
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1579
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1580
  #include "ns3/log.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1581
  #include "ns3/ptr.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1582
  #include "ns3/internet-node.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1583
  #include "ns3/csma-channel.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1584
  #include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1585
  #include "ns3/csma-net-device.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1586
  #include "ns3/csma-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1587
  #include "ns3/csma-ipv4-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1588
  #include "ns3/udp-echo-client.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1589
  #include "ns3/udp-echo-server.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1590
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1591
  NS_LOG_COMPONENT_DEFINE ("UdpEchoSimulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1592
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1593
  using namespace ns3;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1594
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1595
  int 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1596
  main (int argc, char *argv[])
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1597
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1598
    LogComponentEnable ("UdpEchoSimulation", LOG_LEVEL_INFO);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1599
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1600
    NS_LOG_INFO ("UDP Echo Simulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1601
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1602
    Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1603
    Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1604
    Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1605
    Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1606
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1607
    Ptr<CsmaChannel> lan = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1608
      CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1609
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1610
    uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1611
      Mac48Address("08:00:2e:00:00:00"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1612
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1613
    uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1614
      Mac48Address("08:00:2e:00:00:01"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1615
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1616
    uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1617
      Mac48Address("08:00:2e:00:00:02"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1618
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1619
    uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1620
      Mac48Address("08:00:2e:00:00:03"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1621
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1622
    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
  1623
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1624
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1625
    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
  1626
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1627
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1628
    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
  1629
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1630
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1631
    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
  1632
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1633
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1634
    uint32_t packetSize = 1024;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1635
    uint16_t port = 7;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1636
    uint32_t maxPacketCount = 1;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1637
    Time interPacketInterval = Seconds (1.);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1638
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1639
    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
  1640
      maxPacketCount, interPacketInterval, packetSize);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1641
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1642
    Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
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
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1645
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1646
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1647
@section Using the Simulation Engine
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1648
@cindex model
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1649
@cindex simulation executive
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1650
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
  1651
@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
  1652
systems).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1653
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1654
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
  1655
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
  1656
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
  1657
@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
  1658
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
  1659
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
  1660
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
  1661
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1662
@section Models
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1663
@cindex CsmaNetDevice
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1664
@cindex CsmaChannel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1665
@cindex InternetNode
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1666
@cindex NIC
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1667
@cindex CSMA
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1668
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
  1669
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
  1670
@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
  1671
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
  1672
from other CSMA NICs.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1673
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1674
@cindex model
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1675
@cindex CSMA/CD
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1676
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
  1677
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
  1678
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
  1679
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
  1680
@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
  1681
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
  1682
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
  1683
channel residing in our universe.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1684
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1685
@cindex Ethernet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1686
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
  1687
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
  1688
@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
  1689
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
  1690
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
  1691
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
  1692
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
  1693
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
  1694
associated with real-world systems.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1695
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1696
@section Time, Events and Callbacks
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1697
@cindex time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1698
@cindex event
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1699
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
  1700
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
  1701
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
  1702
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
  1703
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
  1704
@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
  1705
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
  1706
from one scheduled event to another.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1707
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1708
@cindex packet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1709
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
  1710
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
  1711
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
  1712
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
  1713
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
  1714
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
  1715
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
  1716
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
  1717
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
  1718
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
  1719
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
  1720
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1721
@cindex functor
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1722
@cindex function object
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
@cindex Callback
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1725
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
  1726
@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
  1727
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
  1728
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
  1729
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
  1730
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
  1731
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
  1732
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
  1733
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
  1734
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
  1735
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
  1736
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
  1737
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
  1738
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
  1739
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
  1740
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
  1741
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1742
@cindex Time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1743
@cindex Callback
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1744
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
  1745
@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
  1746
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
  1747
``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
  1748
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1749
@section Driving the Simulation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1750
@cindex Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1751
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
  1752
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
  1753
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
  1754
through, for example, an @code{Application}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1755
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1756
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
  1757
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
  1758
simulator-specific classes:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1759
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1760
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1761
  #include "ns3/simulator.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1762
  #include "ns3/nstime.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1763
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1764
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1765
@cindex Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1766
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
  1767
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
  1768
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
  1769
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
  1770
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
  1771
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
  1772
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1773
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1774
  server->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1775
  client->Start(Seconds(2.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1776
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1777
  server->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1778
  client->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1779
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1780
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1781
@cindex Application
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 Time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1784
@cindex socket
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1785
@cindex event
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1786
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
  1787
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
  1788
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
  1789
recvfrom (see Donahoo's UDPEchoServer.c).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1790
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1791
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
  1792
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
  1793
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
  1794
(see Donahoo's UDPEchoClient.c).  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1795
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1796
@cindex event
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1797
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
  1798
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
  1799
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
  1800
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1801
@cindex socket!sendto
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1802
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
  1803
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
  1804
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
  1805
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
  1806
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
  1807
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
  1808
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
  1809
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
  1810
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
  1811
application.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1812
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1813
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
  1814
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
  1815
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
  1816
in the client).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1817
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1818
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
  1819
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
  1820
the following static methods:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1821
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1822
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1823
  Simulator::Run ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1824
  Simulator::Destroy ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1825
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1826
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1827
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
  1828
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
  1829
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1830
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1831
  #include "ns3/log.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1832
  #include "ns3/ptr.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1833
  #include "ns3/internet-node.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1834
  #include "ns3/csma-channel.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1835
  #include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1836
  #include "ns3/csma-net-device.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-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1839
  #include "ns3/csma-ipv4-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1840
  #include "ns3/udp-echo-client.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1841
  #include "ns3/udp-echo-server.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1842
  #include "ns3/simulator.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1843
  #include "ns3/nstime.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1844
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1845
  NS_LOG_COMPONENT_DEFINE ("UdpEchoSimulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1846
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1847
  using namespace ns3;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1848
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1849
  int 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1850
  main (int argc, char *argv[])
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1851
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1852
    LogComponentEnable ("UdpEchoSimulation", LOG_LEVEL_INFO);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1853
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1854
    NS_LOG_INFO ("UDP Echo Simulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1855
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1856
    Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1857
    Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1858
    Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1859
    Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1860
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1861
    Ptr<CsmaChannel> lan = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1862
      CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1863
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1864
    uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1865
      Mac48Address("08:00:2e:00:00:00"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1866
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1867
    uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1868
      Mac48Address("08:00:2e:00:00:01"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1869
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1870
    uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1871
      Mac48Address("08:00:2e:00:00:02"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1872
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1873
    uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1874
      Mac48Address("08:00:2e:00:00:03"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1875
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1876
    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
  1877
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1878
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1879
    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
  1880
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1881
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1882
    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
  1883
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1884
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1885
    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
  1886
      Ipv4Mask ("255.255.255.0"));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1887
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1888
    uint32_t packetSize = 1024;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1889
    uint16_t port = 7;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1890
    uint32_t maxPacketCount = 1;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1891
    Time interPacketInterval = Seconds (1.);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1892
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1893
    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
  1894
      maxPacketCount, interPacketInterval, packetSize);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1895
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1896
    Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1897
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1898
    server->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1899
    client->Start(Seconds(2.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1900
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1901
    server->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1902
    client->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1903
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1904
    Simulator::Run ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1905
    Simulator::Destroy ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1906
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1907
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1908
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1909
@cindex csma-echo.cc
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1910
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
  1911
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
  1912
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
  1913
@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
  1914
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
  1915
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
  1916
pedagogic purposes and were not actually necessary.]
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1917
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1918
@section Building the Script
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1919
@cindex Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1920
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
  1921
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
  1922
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
  1923
to Python mode in order to proceed.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1924
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1925
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
  1926
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
  1927
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
  1928
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
  1929
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
  1930
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1931
@cindex wscript
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1932
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
  1933
[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
  1934
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
  1935
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1936
@cindex Python
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1937
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
  1938
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1939
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1940
  def build(bld):
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1941
    obj = bld.create_ns3_program('hello-simulator')
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1942
    obj.source = 'hello-simulator.cc'
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1943
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1944
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1945
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
  1946
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
  1947
@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
  1948
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
  1949
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
  1950
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
  1951
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
  1952
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1953
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
  1954
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
  1955
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
  1956
in the @code{wscript} file,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1957
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1958
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1959
  def build(bld):
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1960
    obj = bld.create_ns3_program('hello-simulator')
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1961
    obj.source = 'hello-simulator.cc'
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1962
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1963
    obj = bld.create_ns3_program('csma-echo')
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1964
    obj.source = 'csma-echo.cc'
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
    ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1967
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1968
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1969
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
  1970
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
  1971
@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
  1972
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
  1973
@code{waf --run} command:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1974
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1975
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1976
~/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
  1977
Entering directory `~/repos/ns-3-dev/build'
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1978
Compilation finished successfully
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1979
UDP Echo Simulation
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1980
~/repos/ns-3-dev/tutorial >
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1981
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1982
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1983
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
  1984
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
  1985
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
  1986
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1987
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1988
@c Tracing at a Glance
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1989
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1990
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1991
@node Tracing-at-a-Glance
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1992
@chapter Tracing at a Glance
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1993
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  1994
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
  1995
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
  1996
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
  1997
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
  1998
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
  1999
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
  2000
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
  2001
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2002
@cindex toaster
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2003
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
  2004
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
  2005
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
  2006
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
  2007
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
  2008
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
  2009
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
  2010
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
  2011
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
  2012
knobs, etc.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2013
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2014
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
  2015
expose the fewest ``knobs.''  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2016
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2017
@section ASCII Trace Wrapper
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2018
@cindex ASCII
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2019
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
  2020
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
  2021
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
  2022
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
  2023
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
  2024
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
  2025
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
  2026
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2027
@subsection Tracing Queue Operations
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2028
@cindex queue
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2029
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
  2030
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
  2031
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2032
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2033
  #include "ns3/ascii-trace.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2034
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2035
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2036
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
  2037
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
  2038
@code{Simulator::Run ();}:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2039
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2040
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2041
  AsciiTrace asciitrace ("tutorial.tr");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2042
  asciitrace.TraceAllQueues ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2043
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2044
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2045
@cindex AsciiTrace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2046
@cindex AsciiTrace!TraceAllQueues
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2047
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
  2048
@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
  2049
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
  2050
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
  2051
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
  2052
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
  2053
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2054
@cindex csma-echo-ascii-trace.cc
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2055
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
  2056
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
  2057
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
  2058
located in the @code{tutorial} directory.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2059
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2060
@cindex Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2061
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
  2062
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2063
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2064
  ./waf --run csma-echo-ascii-trace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2065
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2066
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2067
@cindex tutorial.tr
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2068
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
  2069
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
  2070
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
  2071
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
  2072
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
  2073
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
  2074
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
  2075
@code{tutorial.tr} in your favorite editor.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2076
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2077
@cindex trace event
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2078
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
  2079
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
  2080
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
  2081
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
  2082
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
  2083
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
  2084
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
  2085
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
  2086
@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
  2087
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
  2088
meaning:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2089
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2090
@cindex enqueue
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2091
@cindex dequeue
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2092
@cindex drop
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2093
@itemize @bullet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2094
@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
  2095
@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
  2096
@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
  2097
@end itemize
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2098
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2099
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
  2100
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
  2101
left side:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2102
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2103
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2104
  00  + 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2105
  01  2 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2106
  02  nodeid=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2107
  03  device=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2108
  04  queue-enqueue 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2109
  05  pkt-uid=9 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2110
  06  ETHERNET 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2111
  07    length/type=0x806, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2112
  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
  2113
  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
  2114
  10      ARP(request 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2115
  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
  2116
  12        source ipv4: 10.1.1.1 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2117
  13        dest ipv4: 10.1.1.2) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2118
  14  ETHERNET fcs=0
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2119
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2120
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2121
@cindex trace event
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2122
@cindex simulation time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2123
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
  2124
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
  2125
@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
  2126
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
  2127
@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
  2128
confirmation that this is, indeed, happening.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2129
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2130
@cindex node number
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2131
@cindex net device number
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2132
@cindex smart pointer
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2133
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
  2134
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
  2135
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
  2136
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
  2137
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
  2138
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
  2139
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
  2140
@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
  2141
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
  2142
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
  2143
@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
  2144
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2145
@cindex uid
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2146
@cindex unique ID
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2147
@cindex packet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2148
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
  2149
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
  2150
@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
  2151
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
  2152
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
  2153
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
  2154
clear momentarily.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2155
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2156
@cindex Ethernet
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2157
@cindex MAC address
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2158
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
  2159
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
  2160
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
  2161
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
  2162
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
  2163
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
  2164
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2165
@cindex Address Resolution Protocol
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2166
@cindex ARP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2167
@cindex ARP|request
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2168
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
  2169
(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
  2170
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
  2171
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
  2172
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
  2173
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
  2174
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2175
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2176
  00  - 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2177
  01  2 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2178
  02  nodeid=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2179
  03  device=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2180
  04  queue-dequeue 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2181
  05  pkt-uid=9 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2182
  ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2183
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2184
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2185
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
  2186
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
  2187
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
  2188
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
  2189
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
  2190
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2191
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2192
  00  + 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2193
  01  2.00207
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2194
  02  nodeid=1 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2195
  03  device=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2196
  04  queue-enqueue 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2197
  05  pkt-uid=10 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2198
  06  ETHERNET 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2199
  07    length/type=0x806, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2200
  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
  2201
  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
  2202
  10      ARP(reply 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2203
  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
  2204
  12        source ipv4: 10.1.1.2 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2205
  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
  2206
  14        dest ipv4: 10.1.1.1) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2207
  15  ETHERNET fcs=0
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2208
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2209
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2210
@cindex simulation time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2211
@cindex ARP|response
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2212
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
  2213
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
  2214
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
  2215
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
  2216
(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
  2217
(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
  2218
@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
  2219
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
  2220
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
  2221
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
  2222
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2223
@cindex queue
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2224
@cindex queue|transmit
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2225
@cindex echo
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2226
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
  2227
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
  2228
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
  2229
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
  2230
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
  2231
(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
  2232
(@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
  2233
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2234
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2235
  00 + 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2236
  01 2.00415 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2237
  02 nodeid=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2238
  03 device=0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2239
  04 queue-enqueue 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2240
  05 pkt-uid=7 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2241
  06 ETHERNET 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2242
  07   length/type=0x800, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2243
  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
  2244
  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
  2245
  10   IPV4(
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2246
  11     tos 0x0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2247
  12     ttl 64 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2248
  13     id 0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2249
  14     offset 0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2250
  15     flags [none] 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2251
  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
  2252
  17     UDP(length: 1032) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2253
  18       49153 > 7 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2254
  19       DATA (length 1024) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2255
  20 ETHERNET fcs=0
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2256
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2257
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2258
@cindex simulation time
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2259
@cindex echo
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2260
@cindex ARP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2261
@cindex ARP|request
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2262
@cindex ARP|response
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2263
@cindex IP
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2264
@cindex Ipv4
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2265
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
  2266
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
  2267
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
  2268
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
  2269
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
  2270
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
  2271
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
  2272
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
  2273
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
  2274
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
  2275
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
  2276
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
  2277
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
  2278
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
  2279
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
  2280
@code{UdpEchoClient Application}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2281
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2282
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
  2283
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
  2284
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
  2285
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
  2286
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
  2287
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
  2288
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
  2289
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
  2290
implicitly transmitted back to node one).
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2291
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2292
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
  2293
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2294
@verbatim
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.00786 nodeid=1 ... ARP(request ...
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
  - 2.00994 nodeid=0 ... ARP(reply ...
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2299
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2300
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2301
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
  2302
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
  2303
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
  2304
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2305
@subsection Tracing Device Receive Operations
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2306
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
  2307
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
  2308
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
  2309
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
  2310
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2311
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
  2312
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
  2313
file, but disabled.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2314
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2315
@cindex AsciiTrace!TraceAllNetDeviceRx
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2316
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2317
#if 0
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2318
  asciitrace.TraceAllNetDeviceRx ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2319
#endif
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2320
  asciitrace.TraceAllNetDeviceRx ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2321
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2322
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2323
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
  2324
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
  2325
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2326
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2327
  ./waf --run csma-echo-ascii-trace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2328
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2329
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2330
@cindex ARP!request
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2331
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
  2332
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
  2333
@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
  2334
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
  2335
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
  2336
events are,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2337
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2338
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2339
  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
  2340
  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
  2341
  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
  2342
  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
  2343
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2344
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2345
@cindex unique ID
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2346
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
  2347
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
  2348
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
  2349
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2350
@section PCAP Trace Wrapper
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2351
@cindex pcap
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2352
@cindex Wireshark
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2353
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
  2354
@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
  2355
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
  2356
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
  2357
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
  2358
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2359
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
  2360
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
  2361
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2362
@cindex csma-echo-ascii-trace.cc
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2363
@cindex csma-echo-pcap-trace.cc
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2364
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
  2365
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
  2366
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
  2367
defintions:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2368
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2369
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2370
  #include "ns3/pcap-trace.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2371
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2372
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2373
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
  2374
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2375
@cindex PcapTrace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2376
@cindex PcapTrace!TraceAllIp
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2377
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2378
  PcapTrace pcaptrace ("tutorial.pcap");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2379
  pcaptrace.TraceAllIp ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2380
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2381
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2382
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
  2383
@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
  2384
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
  2385
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
  2386
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
  2387
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
  2388
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2389
@cindex interface index
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2390
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
  2391
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
  2392
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
  2393
@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
  2394
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
  2395
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
  2396
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
  2397
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
  2398
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2399
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
  2400
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2401
@cindex Waf
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2402
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2403
  ./waf --run csma-echo-pcap-trace
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2404
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2405
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2406
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
  2407
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
  2408
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
  2409
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
  2410
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
  2411
those nodes.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2412
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2413
@cindex Wireshark
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2414
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
  2415
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
  2416
@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
  2417
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
  2418
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
  2419
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
  2420
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2421
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2422
@c Other Network Topologies
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2423
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2424
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2425
@node Other-network-topologies
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2426
@chapter Other Network Topologies
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2427
@cindex topology
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2428
@cindex Channel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2429
@cindex NetDevice
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2430
@cindex topology!bus
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2431
@cindex topology!point-to-point
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2432
@cindex PointToPointChannel
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2433
@cindex PointToPointNetDevice
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2434
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2435
@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
  2436
(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
  2437
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
  2438
@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
  2439
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
  2440
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
  2441
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
  2442
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
  2443
@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
  2444
and @code{PointToPointChannel} respectively.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2445
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2446
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
  2447
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
  2448
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2449
@section A Point-to-Point Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2450
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
  2451
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
  2452
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
  2453
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
  2454
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
  2455
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2456
@sp 1
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2457
@center @image{pp,,,,png}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2458
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2459
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
  2460
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
  2461
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
  2462
on the following lines:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2463
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2464
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2465
  Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2466
  Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2467
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2468
  Ptr<PointToPointChannel> link = PointToPointTopology::AddPointToPointLink (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2469
    n0, n1, DataRate (38400), MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2470
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2471
  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
  2472
    n1, "10.1.1.2");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2473
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2474
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2475
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
  2476
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
  2477
@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
  2478
@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
  2479
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
  2480
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
  2481
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
  2482
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2483
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
  2484
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
  2485
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
  2486
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2487
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
  2488
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
  2489
@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
  2490
@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
  2491
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
  2492
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2493
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2494
  Ipv4Mask netmask("255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2495
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2496
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2497
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
  2498
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
  2499
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
  2500
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
  2501
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2502
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
  2503
convenience:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2504
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2505
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2506
/* -*- 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
  2507
/*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2508
 * 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
  2509
 * 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
  2510
 * published by the Free Software Foundation;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2511
 *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2512
 * 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
  2513
 * 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
  2514
 * 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
  2515
 * GNU General Public License for more details.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2516
 *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2517
 * 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
  2518
 * 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
  2519
 * 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
  2520
 */
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2521
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2522
#include "ns3/log.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2523
#include "ns3/ptr.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2524
#include "ns3/internet-node.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2525
#include "ns3/point-to-point-channel.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2526
#include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2527
#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
  2528
#include "ns3/point-to-point-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2529
#include "ns3/udp-echo-client.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2530
#include "ns3/udp-echo-server.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2531
#include "ns3/simulator.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2532
#include "ns3/nstime.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2533
#include "ns3/ascii-trace.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2534
#include "ns3/pcap-trace.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2535
#include "ns3/global-route-manager.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2536
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2537
NS_LOG_COMPONENT_DEFINE ("PointToPointSimulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2538
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2539
using namespace ns3;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2540
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2541
// Network topology
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2542
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2543
//                       point to point
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
//                      |              |
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2546
//                     n0             n1
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2547
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2548
int 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2549
main (int argc, char *argv[])
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2550
{
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2551
  LogComponentEnable ("PointToPointSimulation", LOG_LEVEL_INFO);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2552
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2553
  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
  2554
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2555
  Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2556
  Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2557
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2558
  Ptr<PointToPointChannel> link = PointToPointTopology::AddPointToPointLink (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2559
    n0, n1, DataRate (38400), MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2560
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2561
  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
  2562
    n1, "10.1.1.2");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2563
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2564
  uint16_t port = 7;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2565
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2566
  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
  2567
    1, Seconds(1.), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2568
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2569
  Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2570
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2571
  server->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2572
  client->Start(Seconds(2.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2573
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2574
  server->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2575
  client->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2576
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2577
  AsciiTrace asciitrace ("tutorial.tr");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2578
  asciitrace.TraceAllQueues ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2579
  asciitrace.TraceAllNetDeviceRx ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2580
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2581
  Simulator::Run ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2582
  Simulator::Destroy ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2583
}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2584
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2585
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2586
@section A Star Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2587
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
  2588
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
  2589
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
  2590
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
  2591
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
  2592
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2593
@sp 1
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2594
@center @image{star,,,,png}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2595
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2596
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
  2597
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
  2598
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
  2599
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
  2600
@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
  2601
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
  2602
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
  2603
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2604
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2605
  #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
  2606
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2607
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2608
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
  2609
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
  2610
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
  2611
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2612
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2613
  Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2614
  Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2615
  Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2616
  Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2617
  Ptr<Node> n4 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2618
  Ptr<Node> n5 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2619
  Ptr<Node> n6 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2620
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2621
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2622
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
  2623
helper and the @code{PointToPointIpv4Topology} helper.  The
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2624
@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
  2625
@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
  2626
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
  2627
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
  2628
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
  2629
node one.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2630
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2631
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2632
  Ptr<PointToPointChannel> link01 =
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2633
    PointToPointIpv4Topology::CreateChannel (DataRate (38400),
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2634
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2635
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2636
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2637
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
  2638
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
  2639
20 milliseconds.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2640
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2641
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
  2642
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
  2643
illustrates how we do that:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2644
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2645
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2646
  uint32_t nd01 = PointToPointIpv4Topology::AddNetDevice (n0,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2647
    link01);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2648
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2649
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2650
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
  2651
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
  2652
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
  2653
from node zero to node one.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2654
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2655
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
  2656
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
  2657
to net devices on node zero.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2658
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2659
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
  2660
It looks like the following:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2661
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2662
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2663
  uint32_t nd1 = PointToPointIpv4Topology::AddNetDevice (n1, link01);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2664
  uint32_t nd2 = PointToPointIpv4Topology::AddNetDevice (n2, link02);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2665
  uint32_t nd3 = PointToPointIpv4Topology::AddNetDevice (n3, link03);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2666
  uint32_t nd4 = PointToPointIpv4Topology::AddNetDevice (n4, link04);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2667
  uint32_t nd5 = PointToPointIpv4Topology::AddNetDevice (n5, link05);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2668
  uint32_t nd6 = PointToPointIpv4Topology::AddNetDevice (n6, link06);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2669
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2670
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2671
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
  2672
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
  2673
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
  2674
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
  2675
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
  2676
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
  2677
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
  2678
created net device.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2679
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2680
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
  2681
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
  2682
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
  2683
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
  2684
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
  2685
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
  2686
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
  2687
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
  2688
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
  2689
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
  2690
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2691
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
  2692
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
  2693
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
  2694
network.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2695
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2696
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2697
  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
  2698
    ``255.255.255.252'');
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2699
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2700
  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
  2701
    ``255.255.255.252'');
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2702
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2703
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2704
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
  2705
remaining net devices.  Note that there are no @code{Mac48Address} address
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  2706
assignments --- they are not required.
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2707
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2708
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
  2709
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
  2710
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
  2711
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
  2712
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2713
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
  2714
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2715
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2716
/* -*- 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
  2717
/*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2718
 * 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
  2719
 * 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
  2720
 * published by the Free Software Foundation;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2721
 *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2722
 * 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
  2723
 * 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
  2724
 * 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
  2725
 * GNU General Public License for more details.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2726
 *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2727
 * 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
  2728
 * 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
  2729
 * 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
  2730
 */
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2731
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2732
#include "ns3/log.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2733
#include "ns3/ptr.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2734
#include "ns3/internet-node.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2735
#include "ns3/point-to-point-channel.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2736
#include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2737
#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
  2738
#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
  2739
#include "ns3/udp-echo-client.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2740
#include "ns3/udp-echo-server.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2741
#include "ns3/simulator.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2742
#include "ns3/nstime.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2743
#include "ns3/ascii-trace.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2744
#include "ns3/pcap-trace.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2745
#include "ns3/global-route-manager.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2746
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2747
NS_LOG_COMPONENT_DEFINE ("StarSimulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2748
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2749
using namespace ns3;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2750
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2751
// Network topology
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2752
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2753
//                  n3    n2
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
//                    | /
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2756
//              n4 --- n0 --- n1
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
//                   /    |
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2759
//                  n5    n6
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2760
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2761
int 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2762
main (int argc, char *argv[])
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2763
{
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2764
  LogComponentEnable ("StarSimulation", LOG_LEVEL_INFO);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2765
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2766
  NS_LOG_INFO ("Star Topology Simulation");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2767
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2768
  Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2769
  Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2770
  Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2771
  Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2772
  Ptr<Node> n4 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2773
  Ptr<Node> n5 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2774
  Ptr<Node> n6 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2775
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2776
  Ptr<PointToPointChannel> link01 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2777
    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2778
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2779
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2780
  uint32_t nd01 = PointToPointIpv4Topology::AddNetDevice (n0,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2781
    link01);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2782
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2783
  Ptr<PointToPointChannel> link02 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2784
    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2785
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2786
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2787
  uint32_t nd02 = PointToPointIpv4Topology::AddNetDevice (n0,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2788
    link02);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2789
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2790
  Ptr<PointToPointChannel> link03 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2791
    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2792
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2793
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2794
  uint32_t nd03 = PointToPointIpv4Topology::AddNetDevice (n0,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2795
    link03);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2796
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2797
  Ptr<PointToPointChannel> link04 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2798
    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2799
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2800
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2801
  uint32_t nd04 = PointToPointIpv4Topology::AddNetDevice (n0, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2802
    link04);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2803
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2804
  Ptr<PointToPointChannel> link05 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2805
    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2806
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2807
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2808
  uint32_t nd05 = PointToPointIpv4Topology::AddNetDevice (n0,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2809
    link05);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2810
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2811
  Ptr<PointToPointChannel> link06 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2812
    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2813
    MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2814
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2815
  uint32_t nd06 = PointToPointIpv4Topology::AddNetDevice (n0, link06);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2816
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2817
  uint32_t nd1 = PointToPointIpv4Topology::AddNetDevice (n1, link01);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2818
  uint32_t nd2 = PointToPointIpv4Topology::AddNetDevice (n2, link02);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2819
  uint32_t nd3 = PointToPointIpv4Topology::AddNetDevice (n3, link03);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2820
  uint32_t nd4 = PointToPointIpv4Topology::AddNetDevice (n4, link04);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2821
  uint32_t nd5 = PointToPointIpv4Topology::AddNetDevice (n5, link05);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2822
  uint32_t nd6 = PointToPointIpv4Topology::AddNetDevice (n6, link06);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2823
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2824
  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
  2825
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2826
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2827
  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
  2828
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2829
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2830
  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
  2831
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2832
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2833
  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
  2834
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2835
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2836
  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
  2837
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2838
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2839
  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
  2840
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2841
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2842
  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
  2843
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2844
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2845
  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
  2846
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2847
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2848
  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
  2849
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2850
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2851
  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
  2852
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2853
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2854
  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
  2855
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2856
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2857
  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
  2858
    "255.255.255.252");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2859
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2860
  uint16_t port = 7;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2861
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2862
  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
  2863
    1, Seconds(1.), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2864
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2865
  Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2866
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2867
  server->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2868
  client->Start(Seconds(2.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2869
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2870
  server->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2871
  client->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2872
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2873
  AsciiTrace asciitrace ("tutorial.tr");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2874
  asciitrace.TraceAllQueues ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2875
  asciitrace.TraceAllNetDeviceRx ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2876
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2877
  Simulator::Run ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2878
  Simulator::Destroy ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2879
}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2880
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2881
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2882
@subsection Routing
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2883
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
  2884
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
  2885
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
  2886
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
  2887
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
  2888
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
  2889
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
  2890
enable internetwork routing.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2891
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2892
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
  2893
@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
  2894
tricky and difficult change is shown below:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2895
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2896
@verbatim 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2897
 GlobalRouteManager::PopulateRoutingTables ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2898
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2899
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2900
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
  2901
@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
  2902
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
  2903
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
  2904
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2905
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2906
  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
  2907
    1, Seconds(1.), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2908
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2909
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2910
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
  2911
@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
  2912
routed through the topology.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2913
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2914
@section A Dumbbell Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2915
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
  2916
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
  2917
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
  2918
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2919
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
  2920
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
  2921
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
  2922
@emph{choke point}.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2923
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2924
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
  2925
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2926
@sp 1
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2927
@center @image{dumbbell,,,,png}
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2928
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2929
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
  2930
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
  2931
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
  2932
@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
  2933
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
  2934
script.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2935
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2936
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
  2937
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
  2938
process to create our first example.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2939
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2940
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2941
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2942
// 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
  2943
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2944
  Ptr<Node> n0 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2945
  Ptr<Node> n1 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2946
  Ptr<Node> n2 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2947
  Ptr<Node> n3 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2948
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2949
  Ptr<CsmaChannel> lan1 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2950
    CsmaTopology::CreateCsmaChannel (DataRate (10000000), MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2951
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2952
  uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan1, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2953
    "08:00:2e:00:00:00");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2954
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2955
  uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan1, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2956
    "08:00:2e:00:00:01");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2957
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2958
  uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan1, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2959
    "08:00:2e:00:00:02");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2960
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2961
  uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan1, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2962
    "08:00:2e:00:00:03");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2963
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2964
  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
  2965
  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
  2966
  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
  2967
  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
  2968
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2969
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2970
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
  2971
have been changed.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2972
 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2973
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2974
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2975
// 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
  2976
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2977
  Ptr<Node> n4 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2978
  Ptr<Node> n5 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2979
  Ptr<Node> n6 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2980
  Ptr<Node> n7 = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2981
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2982
  Ptr<CsmaChannel> lan2 = 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2983
    CsmaTopology::CreateCsmaChannel (DataRate (10000000), MilliSeconds (2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2984
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2985
  uint32_t nd4 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n4, lan2, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2986
    "08:00:2e:00:00:04");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2987
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2988
  uint32_t nd5 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n5, lan2, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2989
    "08:00:2e:00:00:05");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2990
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2991
  uint32_t nd6 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n6, lan2, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2992
    "08:00:2e:00:00:06");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2993
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2994
  uint32_t nd7 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n7, lan2, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2995
    "08:00:2e:00:00:07");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2996
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  2997
  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
  2998
  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
  2999
  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
  3000
  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
  3001
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3002
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3003
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
  3004
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
  3005
(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
  3006
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
  3007
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3008
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3009
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3010
// 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
  3011
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3012
  Ptr<PointToPointChannel> link = PointToPointTopology::AddPointToPointLink (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3013
    n3, n4, DataRate (38400), MilliSeconds (20));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3014
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3015
  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
  3016
    n4, "10.1.3.2");
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3017
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3018
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3019
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
  3020
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
  3021
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
  3022
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
  3023
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
  3024
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
  3025
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
  3026
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
  3027
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3028
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3029
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3030
// Create data flows across the link:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3031
//   n0 ==> n4 ==> n0
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3032
//   n1 ==> n5 ==> n1
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3033
//   n2 ==> n6 ==> n2
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3034
//   n3 ==> n7 ==> n3
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3035
//
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3036
  uint16_t port = 7;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3037
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3038
  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
  3039
    100, Seconds(.01), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3040
  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
  3041
    100, Seconds(.01), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3042
  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
  3043
    100, Seconds(.01), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3044
  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
  3045
    100, Seconds(.01), 1024);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3046
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3047
  Ptr<UdpEchoServer> server4 = Create<UdpEchoServer> (n4, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3048
  Ptr<UdpEchoServer> server5 = Create<UdpEchoServer> (n5, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3049
  Ptr<UdpEchoServer> server6 = Create<UdpEchoServer> (n6, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3050
  Ptr<UdpEchoServer> server7 = Create<UdpEchoServer> (n7, port);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3051
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3052
  server4->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3053
  server5->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3054
  server6->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3055
  server7->Start(Seconds(1.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3056
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3057
  client0->Start(Seconds(2.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3058
  client1->Start(Seconds(2.1));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3059
  client2->Start(Seconds(2.2));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3060
  client3->Start(Seconds(2.3));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3061
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3062
  server4->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3063
  server5->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3064
  server6->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3065
  server7->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3066
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3067
  client0->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3068
  client1->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3069
  client2->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3070
  client3->Stop (Seconds(10.));
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3071
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3072
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3073
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
  3074
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
  3075
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
  3076
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
  3077
drop code.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3078
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3079
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
  3080
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
  3081
tracing for details.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3082
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3083
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3084
  00 d 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3085
  01 2.40938 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3086
  02 nodeid=3 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3087
  03 device=1 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3088
  04 queue-drop 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3089
  05 pkt-uid=124 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3090
  06 LLCSNAP(type 0x800) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3091
  07   IPV4(
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3092
  08     tos 0x0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3093
  09     ttl 63 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3094
  10     id 20 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3095
  11     offset 0 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3096
  12     flags [none] 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3097
  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
  3098
  14     UDP(length: 1032) 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3099
  15       49153 > 7 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3100
  16       DATA (length 1024)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3101
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3102
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3103
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
  3104
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3105
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3106
@c Nonlinear Thinking
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3107
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3108
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3109
@node Nonlinear-Thinking
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3110
@chapter Nonlinear Thinking
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3111
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3112
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
  3113
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
  3114
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
  3115
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
  3116
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
  3117
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
  3118
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3119
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
  3120
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
  3121
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
  3122
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
  3123
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
  3124
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
  3125
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
  3126
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
  3127
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
  3128
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
  3129
you can follow.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3130
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3131
@section Object Design 101 --- Class Ipv4BusNetwork
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3132
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
  3133
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
  3134
class.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3135
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3136
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
  3137
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
  3138
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
  3139
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
  3140
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
  3141
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3142
@example
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3143
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
  3144
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
  3145
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
  3146
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
  3147
address.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3148
@end example
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3149
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3150
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
  3151
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
  3152
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3153
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
  3154
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
  3155
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
  3156
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
  3157
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
  3158
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
  3159
@code{class BusNetwork} to represent it.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3160
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3161
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
  3162
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
  3163
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
  3164
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
  3165
@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
  3166
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
  3167
@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
  3168
@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
  3169
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
  3170
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3171
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3172
  class Ipv4Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3173
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3174
  public:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3175
    Ipv4Address m_network;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3176
    Ipv4Mask m_mask;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3177
    Ipv4Address m_baseAddress;
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
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3180
  class Ipv4BusNetwork : public Ipv4Network
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
  };
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3183
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3184
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3185
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
  3186
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
  3187
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
  3188
one member variable is obvious:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3189
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3190
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3191
  Ptr<CsmaChannel> m_channel;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3192
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3193
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3194
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
  3195
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
  3196
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
  3197
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
  3198
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
  3199
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
  3200
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
  3201
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3202
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3203
  std::vector<Ptr<Node> > m_nodes;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3204
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3205
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3206
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
  3207
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
  3208
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
  3209
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
  3210
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
  3211
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3212
@example
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3213
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
  3214
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
  3215
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
  3216
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
  3217
its required bandwidth and delay parameters.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3218
@end example
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3219
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3220
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
  3221
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
  3222
@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
  3223
@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
  3224
@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
  3225
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
  3226
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
  3227
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
  3228
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
  3229
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3230
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3231
  class Ipv4Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3232
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3233
  public:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3234
    Ipv4Network (Ipv4Address network, Ipv4Mask mask, Ipv4Address address);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3235
    virtual ~Ipv4Network ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3236
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3237
    Ipv4Address m_network;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3238
    Ipv4Mask m_mask;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3239
    Ipv4Address m_baseAddress;
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
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3242
  class Ipv4BusNetwork : public Ipv4Network
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3243
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3244
  public:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3245
    Ipv4BusNetwork (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3246
      Ipv4Address   network, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3247
      Ipv4Mask      mask, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3248
      Ipv4Address   startAddress, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3249
      DataRate      bps, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3250
      Time          delay, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3251
      uint32_t      n);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3252
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3253
    virtual ~Ipv4BusNetwork ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3254
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3255
    Ptr<Node> GetNode (uint32_t n);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3256
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3257
  private:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3258
    std::vector<Ptr<Node> > m_nodes;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3259
    Ptr<CsmaChannel> m_channel;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3260
  };
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3261
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3262
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3263
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
  3264
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
  3265
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
  3266
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
  3267
@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
  3268
@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
  3269
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
  3270
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3271
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
  3272
constructor, shown below:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3273
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3274
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3275
  Ipv4BusNetwork::Ipv4BusNetwork (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3276
    Ipv4Address   network, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3277
    Ipv4Mask      mask, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3278
    Ipv4Address   baseAddress, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3279
    DataRate      bps, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3280
    Time          delay, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3281
    uint32_t      n)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3282
  : 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3283
    Ipv4Network (network, mask, baseAddress)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3284
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3285
    Ipv4AddressGenerator::SeedNetwork (mask, network);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3286
    Ipv4AddressGenerator::SeedAddress (mask, baseAddress);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3287
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3288
    m_channel = CsmaTopology::CreateCsmaChannel (bps, delay);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3289
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3290
    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
  3291
      {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3292
        Ptr<Node> node = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3293
        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
  3294
          Mac48Address::Allocate ());
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3295
        Ipv4Address address = Ipv4AddressGenerator::AllocateAddress (mask, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3296
          network);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3297
        CsmaIpv4Topology::AddIpv4Address (node, nd, address, mask);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3298
        m_nodes.push_back (node);
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
  }
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3301
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3302
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3303
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
  3304
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
  3305
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
  3306
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3307
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3308
  : 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3309
    Ipv4Network (network, mask, baseAddress)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3310
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3311
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3312
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3313
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
  3314
@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
  3315
after this initialization.  They are:
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3316
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3317
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3318
    Ipv4AddressGenerator::SeedNetwork (mask, network);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3319
    Ipv4AddressGenerator::SeedAddress (mask, baseAddress);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3320
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3321
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3322
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
  3323
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
  3324
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
  3325
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
  3326
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
  3327
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
  3328
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
  3329
in the loop is actually generated.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3330
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3331
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
  3332
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3333
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3334
  m_nodes.push_back (node);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3335
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3336
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3337
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
  3338
attached to the bus.  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3339
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3340
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
  3341
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3342
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3343
  /* -*-  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
  3344
  /*
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3345
   * Copyright (c) 2007 University of Washington
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3346
   *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3347
   * 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
  3348
   * 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
  3349
   * published by the Free Software Foundation;
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3350
   *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3351
   * 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
  3352
   * 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
  3353
   * 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
  3354
   * GNU General Public License for more details.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3355
   *
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3356
   * 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
  3357
   * 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
  3358
   * 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
  3359
   */
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3360
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3361
  #include "ns3/mac48-address.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3362
  #include "ns3/csma-net-device.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3363
  #include "ns3/csma-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3364
  #include "ns3/csma-ipv4-topology.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3365
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3366
  #include "ipv4-bus-network.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3367
  #include "ipv4-address-generator.h"
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3368
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3369
  namespace ns3 {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3370
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3371
  Ipv4Network::Ipv4Network (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3372
    Ipv4Address   network, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3373
    Ipv4Mask      mask, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3374
    Ipv4Address   address)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3375
  : 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3376
    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
  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
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3380
  Ipv4Network::~Ipv4Network ()
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
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3384
  Ipv4BusNetwork::Ipv4BusNetwork (
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3385
    Ipv4Address   network, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3386
    Ipv4Mask      mask, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3387
    Ipv4Address   baseAddress, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3388
    DataRate      bps, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3389
    Time          delay, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3390
    uint32_t      n)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3391
  : 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3392
    Ipv4Network (network, mask, baseAddress)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3393
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3394
    Ipv4AddressGenerator::SeedNetwork (mask, network);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3395
    Ipv4AddressGenerator::SeedAddress (mask, baseAddress);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3396
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3397
    m_channel = CsmaTopology::CreateCsmaChannel (bps, delay);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3398
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3399
    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
  3400
      {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3401
        Ptr<Node> node = Create<InternetNode> ();
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3402
        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
  3403
          Mac48Address::Allocate ());
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3404
        Ipv4Address address = Ipv4AddressGenerator::AllocateAddress (mask, 
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3405
          network);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3406
        CsmaIpv4Topology::AddIpv4Address (node, nd, address, mask);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3407
        m_nodes.push_back (node);
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
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3411
  Ipv4BusNetwork::~Ipv4BusNetwork ()
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
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3415
    Ptr<Node>
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3416
  Ipv4BusNetwork::GetNode (uint32_t n)
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3417
  {
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3418
    return m_nodes[n];
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
  
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3421
  }; // namespace ns3
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3422
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3423
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3424
@section Using Ipv4BusNetwork
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3425
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
  3426
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
  3427
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
  3428
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
  3429
nodes with no additional effort.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3430
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3431
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
  3432
@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
  3433
interesting code is,
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3434
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3435
@verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3436
  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
  3437
    DataRate(10000000), MilliSeconds(20), 10);
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3438
@end verbatim
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3439
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3440
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
  3441
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
  3442
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
  3443
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
  3444
``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
  3445
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
  3446
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
  3447
network.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3448
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3449
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
  3450
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
  3451
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
  3452
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
  3453
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
  3454
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
  3455
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3456
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3457
@c Summary
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3458
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3459
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3460
@node Summary
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3461
@chapter Summary
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3462
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3463
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
  3464
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
  3465
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
  3466
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
  3467
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3468
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
  3469
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
  3470
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
  3471
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
  3472
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
  3473
a left side and a right side.
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3474
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3475
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
  3476
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
  3477
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
  3478
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
  3479
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
  3480
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  3481
@c ========================================================================
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3482
@c Object Model
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3483
@c ========================================================================
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3484
2211
3e1cec7d22d6 fix node and up
Craig Dowell <craigdo@ee.washington.edu>
parents: 2210
diff changeset
  3485
@node Object-Model
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3486
@chapter Object Model
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3487
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3488
There are two distinctly different meanings associated with the term Object
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3489
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
  3490
view; and the second speaks to the application programming interface (classes
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3491
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
  3492
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3493
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
  3494
associated object model that describes how objects are laid out in memory,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3495
how virtual functions work, how inheritance is implemented, constructor and
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3496
destructor execution ordering, template instantiation, etc.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3497
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3498
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
  3499
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
  3500
application programming interface (API) for HTML and XML documents. It defines
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3501
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
  3502
manipulated.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3503
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3504
The Component Object Model (COM) from Microsoft actually spans both meanings
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3505
of the term and extends further into policy statements.  From a system
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3506
perspective, COM specifies an interface definition language, the layout of
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3507
objects virtual function tables, the formats of Globally Unique Identifiers
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3508
and also specifies lifetime management mechanisms for objects via reference
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3509
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
  3510
Interfaces as well as functions such as CoCreateInstance and various
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3511
threading models.  The COM specification extends to policy by disallowing
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3512
implementation inheritance.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3513
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3514
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
  3515
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
  3516
componentry.  You will find terms like Component, Interface and QueryInterface
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3517
in the following discussion.  It is important to understand from the outset
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3518
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
  3519
Richard Feynman (an American physicist) once described the behavior of matter 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3520
and light on a very small scale in the following way,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3521
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3522
@quotation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3523
``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
  3524
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
  3525
anything that you have ever seen.''
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3526
@end quotation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3527
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3528
Just as students of quantum mechanics must rid themselves of preconceptions
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3529
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
  3530
any preconceptions you may have about components, interfaces and APIs for
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3531
software componentry before continuing.  To paraphrase Feynman, @command{ns-3}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3532
components do not behave like COM Components, or Java Beans, or CORBA
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3533
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
  3534
ever seen  they are @command{ns-3} components.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3535
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3536
@section The C++ Object Model is the Root of all Things
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3537
@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
  3538
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
  3539
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
  3540
superiority of one mechanism or another.  What we will do is provide 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3541
convenience functions that we think will make creating and managing simulation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3542
objects easier.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3543
  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3544
Previously, you have seen objects created using the template function 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3545
@code{Create} as in the following example:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3546
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3547
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3548
  Ptr<Node> n0 = Create<InternetNode> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3549
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3550
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3551
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
  3552
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
  3553
of the @code{Create} template.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3554
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3555
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3556
  template <typename T>
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3557
  Ptr<T> Create (void)
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3558
  {
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3559
    T *obj = new T ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3560
    Ptr<T> p = obj;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3561
    obj->Unref ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3562
    return p;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3563
  }
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3564
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3565
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3566
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
  3567
operator @code{new}.  Its a little harder to find the corresponding delete ---
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3568
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
  3569
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
  3570
@code{Unref} and ultimately the C++ @code{delete} operator will be called.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3571
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3572
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
  3573
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
  3574
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
  3575
the responsibility for managing object lifetime (i.e., do not use the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3576
@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
  3577
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
  3578
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3579
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3580
  MyClass *obj = new MyClass ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3581
  obj->Method();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3582
  delete obj;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3583
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3584
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3585
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
  3586
think are appropriate in your private code.  Remember, however, that the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3587
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
  3588
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
  3589
an API publicly, you should use the same object lifetime management approaches
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3590
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
  3591
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3592
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
  3593
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
  3594
created using the C++ new operator with C++ constructor semantics and are
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3595
ultimately deleted using the C++ delete operator, following C++ destructor
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3596
semantics.  Although it may sometimes appear so, there is really no system-
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3597
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
  3598
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
  3599
built on the normal C++ object model.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3600
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3601
@section Interfaces
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3602
There are many different ideas floating around of what exactly the term
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3603
@emph{Interface} means.  Originally an interface just meant a communication
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3604
boundary between two entities.  As the concepts of object oriented programming
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3605
(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
  3606
collection of access methods for the modular entities that were being defined.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3607
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3608
Two distinct approaches developed regarding specifying access mechanisms for
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3609
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
  3610
to Abstract Data Types (ADT).  These were eventually implemented in the case
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3611
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
  3612
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
  3613
place and using inheritance as the primary reuse mechanism.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3614
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3615
Bjarne Stroustroup, the creator of C++, embraced both approaches.  He makes
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3616
the following interesting observation:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3617
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3618
@quotation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3619
``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
  3620
derived classes. [@dots{}] Some classes, such as class @strong{Shape},
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3621
represent abstract concepts for which objects cannot exist.''
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3622
@end quotation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3623
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3624
@command{Ns-3} does not pick and enforce a particular approach.  In 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3625
@command{ns-3} an interface is determined completely by a class declaration
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3626
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
  3627
an abstract concept that should be implemented by derived classes, by all
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3628
means, use the Abstract Base Class approach to interface declaration.  If you
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3629
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
  3630
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
  3631
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
  3632
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
  3633
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
  3634
We make no restrictions.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3635
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3636
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
  3637
languages, or pure virtual classes, or registries  we just think about C++
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3638
object declarations and their associated methods.  When we instantiate an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3639
@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
  3640
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
  3641
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
  3642
dispatched.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3643
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3644
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
  3645
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
  3646
Object.  This inheritance gives the Interface object a very useful capability.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3647
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3648
@section The Ns-3 Capital I Interface and QueryInterface
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3649
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
  3650
of Interface aggregation and discovery via QueryInterface.  We have embraced
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3651
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
  3652
problem in large software systems.  A good example of this problem happens
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3653
in the @command{ns-3} Node class.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3654
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3655
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
  3656
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
  3657
base class and include functionality to implement such things as internet
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3658
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
  3659
inherit from the node class and specialize in different ways, or further
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3660
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
  3661
result in a complicated inheritance tree in which some specializations are
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3662
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
  3663
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
  3664
and creates pressure to drive functionality up the inheritance tree into the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3665
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
  3666
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
  3667
everything in one place.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3668
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3669
Even if one successfully avoided these swiss army knife base classes, one
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3670
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
  3671
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
  3672
class (@code{Node}) across public APIs.  This introduces @emph{upcasts} prior
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3673
to passing across public APIs and corresponding @emph{downcasts} on the other
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3674
side in order to gain access to required specialized functions.  As the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3675
inheritance tree becomes more complicated, this approach can cause another
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3676
related problem known as the @emph{fragile base class} problem.  This happens
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3677
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
  3678
sundry subclasses.  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3679
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3680
These effects seem always to result in a positive feedback loop driving
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3681
everything into the base class and destroying much of the encapsulation which
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3682
is a hallmark of the object oriented approach.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3683
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3684
@subsection Interface Composition
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3685
There is a completely different way to address the Node specialization
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3686
problem.  Instead of approaching the situation using inheritance, one can
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3687
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
  3688
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
  3689
objects would be instances of the classes implementing the internetwork
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3690
routing code, or the TCP / IP protocol stack described above.  This approach
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3691
preserves the encapsulation and solves the weak base class, base class bloat
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3692
and fragile base class problems; but the question of method dispatch 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3693
immediately comes to mind.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3694
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3695
In many systems, @emph{delegation} is used.  The base class, @code{Node},
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3696
in this approach would provide methods that simply forward to the objects
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3697
implementing the desired functionality.  This situation clearly does not
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3698
address the base class bloat problem since dispatch methods must be added
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3699
to the base class.  The situation is mitigated somewhat by pushing the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3700
implementation of the dispatch methods to contained objects, but the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3701
fundamental problems are still present.  What is really needed is a way
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3702
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
  3703
objects separated.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3704
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3705
Composition, usually called @emph{aggregation}, along with runtime Interface
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3706
discovery is the solution that Microsoft originally championed and that 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3707
@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
  3708
separate Interface objects implementing internetwork routing and TCP/IP.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3709
These contained objects have interfaces in the C++ sense of collections of
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3710
method signatures.  When objects are capable of participating in this
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3711
aggregation process, they are called @command{ns-3} Interfaces and they
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3712
receive the functionality required for this participation by inheriting
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3713
from the base class @code{Object}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3714
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3715
@subsection Object, interfaces and Interfaces
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3716
As mentioned above, the class that implements the aggregation mechanism for
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3717
@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
  3718
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
  3719
to support aggregation and QueryInterface.  Many systems have a base class
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3720
that implements common functionality and these base classes are typically
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3721
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
  3722
primarily to Interface aggregation, although it does provide methods to help
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3723
with intrusive reference counting and tracing as well.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3724
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3725
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
  3726
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
  3727
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
  3728
class, with virtual methods, etc.).   In ns-3, you should associate
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3729
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
  3730
the status of Interface rather than the form of the Interface declaration.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3731
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3732
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
  3733
Interface Identifier.  The Interface Identifer, or @emph{iid}, is the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3734
@command{ns-3} version of the @emph{Universally Unique ID} (UUID) or 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3735
@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
  3736
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
  3737
simply a number which the system will generate for you that uniquely
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3738
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
  3739
specify an interface type to @code{QueryInterface}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3740
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3741
To summarize, when you instantiate an object that inherits from the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3742
@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
  3743
properties:  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3744
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3745
@itemize @bullet
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3746
@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
  3747
@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
  3748
@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
  3749
@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
  3750
@end itemize
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3751
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3752
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
  3753
C++ class has an object access interface that is essentially the collection
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3754
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
  3755
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
  3756
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
  3757
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
  3758
(@code{iid}), and exports methods to aggregate and search other Interfaces
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3759
that are added to its aggregation.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3760
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3761
That last detail is important.  In @command{ns-3} Interfaces are both
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3762
containers and specifications for object method access.  We have previously
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3763
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
  3764
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
  3765
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
  3766
of one Interface, the @code{Node} Interface.  This is generally true ---
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3767
Interfaces are both containers and Interfaces.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3768
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3769
@subsection Aggregations
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3770
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
  3771
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
  3772
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
  3773
because of its superficial similarity to a kind of childs candy.  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3774
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3775
@sp 1
2210
151fbc85dd92 figures
Craig Dowell <craigdo@ee.washington.edu>
parents: 2209
diff changeset
  3776
@center @image{oneif,,,,png}
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3777
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3778
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
  3779
follows,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3780
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3781
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3782
  class A : public Object {
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3783
  public:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3784
    static const InterfaceId iid;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3785
    void MethodA (void);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3786
  };
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3787
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3788
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3789
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
  3790
figure above are the methods inherited from the @code{Object} base class (
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3791
@code{QueryInterface}, @code{Ref}, and @code{Unref}) and those from class 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3792
@code{A} (@code{MethodA}).  Note that you must declare an @code{InterfaceId}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3793
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
  3794
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
  3795
that uniquely identifies objects as being instantiated from this class.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3796
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3797
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
  3798
Interface as part of a connector.  These connectors allow @code{QueryInterface}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3799
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
  3800
aggregation of three Interfaces: A, B and C.  The class declarations for 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3801
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
  3802
@code{A}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3803
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3804
@sp 1
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3805
@center @image{threeif,,,,png}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3806
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3807
You can visualize these Interfaces as being snapped together like Lego
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3808
building blocks if you like.  When the Interfaces are aggregated, a
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3809
@code{QueryInterface} search path is formed through the connectors.  In order
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3810
to create this aggregation we first need to create the Interface objects.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3811
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
  3812
@code{Create} template function and manage using smart pointers.  The 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3813
following code should be obvious to you by now:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3814
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3815
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3816
  Ptr<A> a = Create<A> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3817
  Ptr<B> b = Create<B> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3818
  Ptr<C> c = Create<C> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3819
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3820
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3821
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
  3822
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
  3823
an Interface, you simply call the method @code{AddInterface} that your class
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3824
inherited from @code{Object}.  The following code will aggregate Interface 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3825
@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
  3826
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3827
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3828
  a->AddInterface (b);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3829
  a->AddInterface (c);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3830
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3831
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3832
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
  3833
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
  3834
the Interfaces in the aggregation.  Lets look at a simple example:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3835
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3836
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3837
  Ptr<B> newB = a->QueryInterface<B> (B:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3838
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3839
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3840
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
  3841
@code{B} to help with memory management of the returned Interface pointer.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3842
Object lifetime management is very important when dealing with Interfaces
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3843
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
  3844
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3845
The right hand side illustrates the basic idea of @code{QueryInterface}.  We
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3846
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
  3847
aggregation for an interface associated with an interface identifier with
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3848
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
  3849
@code{B::iid} is the @code{static InterfaceId} of the Interface class 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3850
@code{B}.  Observe that @code{QueryInterface} is a template function and the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3851
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
  3852
smart pointer to return.  In this case @code{QueryInterface} will find an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3853
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
  3854
smart pointer to @code{B} as instructed.  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3855
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3856
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
  3857
the Interfaces in the aggregation for any of the Interfaces in the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3858
aggregation.  For example we could walk the Interfaces asking each for the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3859
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
  3860
smart pointer a to look for the InterfaceId representing @code{B}:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3861
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3862
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3863
  Ptr<B> newB = a->QueryInterface<B> (B:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3864
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3865
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3866
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
  3867
to look for the @code{InterfaceId} representing @code{C}:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3868
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3869
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3870
  Ptr<C> newC = newB->QueryInterface<C> (C:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3871
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3872
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3873
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
  3874
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
  3875
aggregation:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3876
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3877
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3878
  Ptr<A> newA = newC->QueryInterface<A> (A:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3879
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3880
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3881
@code{QueryInterface} (often abbreviated QI) has some important properties
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3882
that we need to go over.  Technically, QI is a @emph{symmetric}, 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3883
@emph{reflexive} and @emph{transitive} operation with respect to the set of
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3884
aggregated Interfaces.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3885
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3886
@subsubsection Symmetry
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3887
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
  3888
Interface for the Interface Id of that same interface, that 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3889
@code{QueryInterface} must succeed.  The existence of interface A in the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3890
aggregation implies the reachability of Interface A in the aggregation.  This
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3891
is usually written (by Microsoft) as,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3892
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3893
@center must succeed (A >> A)
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3894
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3895
We can illustrate this property with the code snippet,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3896
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3897
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3898
  Ptr<A> symmetricA = a->QueryInterface<A> (A:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3899
  NS_ASSERT (symmetricA);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3900
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3901
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3902
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
  3903
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
  3904
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
  3905
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3906
@subsubsection Reflexivity
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3907
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
  3908
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
  3909
from B.  This is usually written as,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3910
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3911
@center must succeed (A >> B, then B >> A)
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3912
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3913
This property can be illustrated with the code snippet,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3914
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3915
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3916
  Ptr<B> b = a->QueryInterface<B> (B:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3917
  Ptr<A> reflexiveA = b->QueryInterface<A> (A:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3918
  NS_ASSERT (reflexiveA);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3919
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3920
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3921
If the first @code{QueryInterface} on Interface A looking for Interface B 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3922
succeeds, then a @code{QueryInterface} on Interface B looking for Interface A 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3923
must succeed.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3924
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3925
@subsubsection Transitivity
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3926
@code{QueryInteface} must also be transitive.  This means that if one can
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3927
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
  3928
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
  3929
written as,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3930
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3931
@center must succeed (A >> B, and B >> C, then A >> C)
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3932
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3933
This property can be illustrated with the code snippet,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3934
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3935
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3936
  Ptr<B> b = a->QueryInterface<B> (B:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3937
  Ptr<C> c = b->QueryInterface<C> (C:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3938
  Ptr<C> transitiveC = a->QueryInterface<C> (C:iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3939
  NS_ASSERT (transitiveC);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3940
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3941
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3942
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
  3943
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
  3944
must also succeed.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3945
2216
9c209295b0b1 Missing subsection text
Tom Henderson <tomh@tomh.org>
parents: 2215
diff changeset
  3946
@subsection Creating the InterfaceId
2209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3947
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
  3948
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
  3949
the @code{.cc} file corresponding to the Interface.  For example, to 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3950
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
  3951
following code to the source file that implements class A,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3952
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3953
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3954
  const InterfaceId A::iid = 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3955
    MakeInterfaceId (``A'', Object::iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3956
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3957
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3958
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
  3959
your main procedure is entered.  The call to MakeInterfaceId will assign a
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3960
process-local unique identifier to your class and associate your interface
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3961
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
  3962
a human readable string.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3963
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3964
An advanced ns-3 specific feature of QueryInterface is exposed here.  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3965
@code{MakeInterfaceId} takes an @code{InterfaceId} as a parameter.  This is
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3966
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
  3967
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
  3968
@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
  3969
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
  3970
base class has a rough equivalence to the @emph{IUnknown} Interface in
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3971
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
  3972
as you might QI for IID_IUnknown in COM.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3973
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3974
The InterfaceId you pass to @code{MakeInterfaceId} is used to create an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3975
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
  3976
walked in @code{QueryInterface} Interface searches.  Consider a simple case
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3977
of a base class and a derived class as shown below,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3978
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3979
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3980
  class Base : public Object
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3981
  {
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3982
  public:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3983
    static const InterfaceId iid;
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
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3987
  class Derived : public Base
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3988
  {
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3989
  public:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3990
    static const InterfaceId iid;
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
  };
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3993
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3994
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3995
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
  3996
to @code{MakeInterfaceId} reflecting the class hierarchy we just created.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3997
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3998
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  3999
  const InterfaceId Base::iid = 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4000
    MakeInterfaceId (``Base'', Object::iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4001
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4002
  const InterfaceId Derived::iid = 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4003
    MakeInterfaceId (``Derived'', Base::iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4004
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4005
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4006
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
  4007
second inherits from class @code{Base}.  We could create these interfaces
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4008
as we usually do,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4009
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4010
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4011
Ptr<Base> base = Create<Base> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4012
Ptr<Derived> derived = Create<Derived> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4013
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4014
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4015
The derived and base @code{InterfaceIds} are either present or not present
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4016
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
  4017
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
  4018
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
  4019
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
  4020
@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
  4021
when done against a @code{Ptr<Derived>}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4022
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4023
This feature allows you to use implementation inheritance to easily create
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4024
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
  4025
was almost universally identified as a problem.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4026
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4027
@subsection A Real Example
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4028
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
  4029
since you already had those pointers laying around when you created the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4030
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
  4031
contained objects and only export a single Interface.  Other Interfaces could
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4032
be discovered using QI.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4033
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4034
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
  4035
as being the container and other Interfaces being aggregated to that
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4036
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
  4037
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
  4038
stacks, internet routing, etc.  So, lets start developing an example by
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4039
calling the container Interface Node instead of A.  The creation of this
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4040
Interface is found all over our example programs.  For example, you will 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4041
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
  4042
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4043
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4044
  Ptr<Node> n = Create<InternetNode> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4045
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4046
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4047
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
  4048
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
  4049
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
  4050
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
  4051
class declaration for class @code{Node}.  There you will find,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4052
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4053
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4054
  class Node : public Object
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4055
  {
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4056
  public:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4057
    static const InterfaceId iid;
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
  };
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4060
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4061
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4062
Class @code{Node} inherits from class @code{Object} and provides an
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4063
@code{InterfaceId}, therefore it is an @command{ns-3} interface.  You now 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4064
know you can use @code{AddInterface} for aggregation and @code{QueryInterface}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4065
for Interface discovery against any @code{Node} in the system.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4066
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4067
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
  4068
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
  4069
is represented by the @code{Ipv4} Interface.  If you look in 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4070
@code{src/node/ipv4.h} you will find,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4071
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4072
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4073
  class Ipv4 : public Object
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4074
  {
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4075
  public:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4076
    static const InterfaceId iid;
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
  };
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4079
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4080
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4081
Since class @code{Ipv4} inherits from class @code{Object} and has a 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4082
@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
  4083
@code{src/node/ipv4.cc} you will find,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4084
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4085
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4086
  const InterfaceId Ipv4::iid = 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4087
    MakeInterfaceId (``Ipv4'', Object::iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4088
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4089
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4090
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
  4091
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
  4092
declares that @code{Ipv4} inherits from class @code{Object}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4093
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4094
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
  4095
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
  4096
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
  4097
implementation class, called @code{Ipv4Impl}.  This class inherits from
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4098
@code{Ipv4} and provides the required virtual methods.  This is where
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4099
understanding what is an Interface and what is not gets tricky.  The 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4100
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
  4101
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
  4102
@code{Ipv4} class is the Interface and has the associated @code{InterfaceId}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4103
The class @code{Ipv4Impl} provides an implementation for the pure virtual
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4104
methods in @code{Ipv4}.  Since class @code{Ipv4} cannot be instantiated, one
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4105
instantiates the @code{Ipv4Impl} class to create an @code{Ipv4} Interface.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4106
Once the @code{Ipv4Impl} class is instantiated, the pointer to it is
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4107
immediately cast to an @code{Ipv4} pointer.  Clients will then use the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4108
@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
  4109
@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
  4110
any further until you thoroughly understand what youve just read.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4111
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4112
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
  4113
will see the following code in @code{InternetNode::Construct} that creates the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4114
@code{Ipv4} Interface and aggregates it to the @code{Node} interface (recall
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4115
that class @code{Node} is an Interface and class @code{InternetNode} inherits
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4116
from class @code{Node}):
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4117
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4118
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4119
  Ptr<Ipv4Impl> ipv4Impl = Create<Ipv4Impl> (ipv4);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4120
  ...
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4121
  Object::AddInterface (ipv4Impl);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4122
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4123
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4124
Note that the parameter @code{ipv4} passed to the @code{Create} template
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4125
function is actually a pointer to an @code{Ipv4L3Protocol} which you can
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4126
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
  4127
@code{Ipv4} Interface.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4128
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4129
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
  4130
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
  4131
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
  4132
decided that mnemonic aids such as Hungarian notation were a stylistic thing
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4133
and you should just refer to the system documentation to determine what
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4134
objects are ns-3 Interfaces and what those Interfaces actually are (RTFM ---
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4135
Read the Fine Manual).
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4136
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4137
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
  4138
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
  4139
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
  4140
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
  4141
@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
  4142
contains an @code{InterfaceId}.  Thus the Interface added is really the 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4143
@code{Ipv4} Interface with the interface Id @code{Ipv4::iid}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4144
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4145
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
  4146
@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
  4147
following code:
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4148
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4149
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4150
  Ptr<Node> n0 = Create<InternetNode> ();
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4151
  ...
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4152
  Ptr<Ipv4> ipv4;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4153
  ipv4 = n0->QueryInterface<Ipv4> (Ipv4::iid);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4154
  ipv4->SetDefaultRoute (Ipv4Address (``10.1.1.2''), 1);
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4155
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4156
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4157
The first line creates an @code{InternetNode} object and casts the resulting
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4158
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
  4159
@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
  4160
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
  4161
should be able to participate in a @code{QueryInterface}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4162
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4163
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
  4164
looking for the @code{Ipv4} Interface (@code{Ipv4::iid}).  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4165
@code{QueryInterface} then returns a smart pointer to its aggregated 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4166
@code{Ipv4} Interface.  [Recall that this Interface was aggregated in
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4167
@code{InternetNode::Construct}.  We knew to start looking for the aggregation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4168
in @code{InternetNode} since we originally created an @code{InternetNode} in 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4169
the @code{Create} template function and then implicitly cast it to a 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4170
@code{Node}.]
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4171
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4172
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
  4173
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
  4174
for the node.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4175
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4176
@section Caveats
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4177
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
  4178
immediately obvious.  
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4179
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4180
@subsection Interface Ids are Associated with Classes not Objects
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4181
Interfaces are identified by an @code{InterfaceId} that is associated with
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4182
the Interface class, not the Interface object.  That is indicated by the
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4183
@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
  4184
interface Id for a given Interface class exists independently of any objects
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4185
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
  4186
type share the same @code{InterfaceId}.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4187
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4188
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
  4189
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
  4190
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
  4191
which you can iterate.  For example, the @code{Node} class provides methods,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4192
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4193
@verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4194
  uint32_t GetNDevices (void) const;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4195
  Ptr<NetDevice> GetDevice (uint32_t index) const;
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4196
@end verbatim
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4197
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4198
that are used iterate over the multiple @code{NetDevice} Interfaces associated
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4199
with it.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4200
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4201
@emph{Interface Ids do not identify objects.}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4202
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4203
@subsection Dont use QI to Check Your Own Type.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4204
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
  4205
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
  4206
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
  4207
appropriated (reimplemented) your type and aggregated themselves onto your 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4208
aggregation.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4209
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4210
Consider a socket factory implementation.  Sockets can be either UDP sockets
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4211
or TCP sockets.  A socket factory will have a generic @code{SocketFactory}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4212
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
  4213
similar TCP-specific interface.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4214
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4215
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
  4216
abstract base class, and then provided separate implementations for UDP and
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4217
TCP specific methods of this factory in separate concrete classes.  Now 
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4218
consider what might happen if you used QueryInterface in your base class
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4219
to determine if you were a UDP or a TCP factory.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4220
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4221
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
  4222
the base class could QueryInterface on itself for the UDP-specific interface.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4223
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
  4224
UDP-specific tasks it could.  [Experienced C++ folks are cringing about how
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4225
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
  4226
a specific and perhaps not-too-obvious problem.]
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4227
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4228
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
  4229
Interface, the base class could QueryInterface on itself for the UDP-specific
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4230
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
  4231
implementation and would then do any TCP-specific tasks it could.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4232
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4233
Now, what happens when these two working objects are aggregated together.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4234
Since the Interfaces are conceptually snapped together the TCP implementation
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4235
would suddenly begin finding the UDP Interface from the other class factory
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4236
and fail.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4237
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4238
@emph{Interface Ids should not be used as run-time type information.}
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4239
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4240
@section Connecting the Dots
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4241
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
  4242
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
  4243
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
  4244
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
  4245
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
  4246
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
  4247
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
  4248
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
  4249
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
  4250
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
  4251
fragile base class problems I explained at the beginning.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4252
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4253
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
  4254
make navigating the QueryInterface environment easier.  The primary issue was
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4255
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
  4256
was an Interface.  One suggestion was to adopt the convention that classes
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4257
that implement Interfaces begin with the letter I.  Microsoft does this, as
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4258
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
  4259
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
  4260
on Interfaces with a pure virtual class specification, the names of which
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4261
begin with an I; and corresponding implementations, the names of which begin
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4262
with a C.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4263
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4264
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
  4265
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
  4266
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
  4267
structure on the source code, nor impose any naming convention.  We will
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4268
rely on our documentation system (Doxygen) to break out all objects with
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4269
InterfaceIds in their class hierarchy into a separate section.  For now,
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4270
until this is implemented, grep is your friend.
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4271
9b98d226e566 interfaces
Craig Dowell <craigdo@ee.washington.edu>
parents: 2187
diff changeset
  4272
@c ========================================================================
2187
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4273
@c Doxygen
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4274
@c ========================================================================
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4275
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4276
@node The-Doxygen-Documentation-System
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4277
@chapter The Doxygen Documentation System
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4278
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4279
@node How-To-Change-Things
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4280
@chapter How to Change Things
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4281
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4282
@node How-To-Set-Default-Values
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4283
@chapter How to Set Default Values
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4284
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4285
@node How-To-Write-A-New-Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4286
@chapter How to Write a New Application
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4287
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4288
@printindex cp
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4289
d13161eb95df put updated tutorial.texi in doc directory
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
  4290
@bye