examples/csma-star.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 10 Dec 2008 01:54:24 -0800
changeset 4004 b181e89d45f1
parent 3858 6559f390009d
child 4210 da9be6abb1b2
child 4218 debf1a8a96d3
permissions -rw-r--r--
bug 440: fix build of tutorial example third.cc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3795
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     2
/*
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     3
 * This program is free software; you can redistribute it and/or modify
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     4
 * it under the terms of the GNU General Public License version 2 as
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     5
 * published by the Free Software Foundation;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     6
 *
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     7
 * This program is distributed in the hope that it will be useful,
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    10
 * GNU General Public License for more details.
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    11
 *
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    12
 * You should have received a copy of the GNU General Public License
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    13
 * along with this program; if not, write to the Free Software
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    15
 *
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
 */
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
#include "ns3/core-module.h"
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
#include "ns3/simulator-module.h"
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
#include "ns3/node-module.h"
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
#include "ns3/helper-module.h"
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
#include "ns3/global-route-manager.h"
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
// Network topology (default)
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
//
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
//            n2     +          +     n3          .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
//             | ... |\        /| ... |           .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
//             ======= \      / =======           .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
//              CSMA    \    /   CSMA             .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
//                       \  /                     .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
//            n1     +--- n0 ---+     n4          .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
//             | ... |   /  \   | ... |           .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
//             =======  /    \  =======           .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
//              CSMA   /      \  CSMA             .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
//                    /        \                  .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
//            n6     +          +     n5          .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
//             | ... |          | ... |           .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
//             =======          =======           .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
//              CSMA             CSMA             .
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
//
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
using namespace ns3;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
NS_LOG_COMPONENT_DEFINE ("CsmaStar");
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
int 
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
main (int argc, char *argv[])
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
{
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
  // Make the random number generators generate reproducible results.
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
  RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
  // Set up some default values for the simulation.
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (137));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
  // ??? try and stick 15kb/s into the data rate
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("14kb/s"));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
  // Default number of nodes in the star.  Overridable by command line argument.
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
  uint32_t nNodes = 7;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
  CommandLine cmd;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
  cmd.AddValue("nNodes", "Number of nodes to place in the star", nNodes);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
  cmd.Parse (argc, argv);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
  NS_LOG_INFO ("Create nodes.");
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
  NodeContainer hubNode;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
  NodeContainer spokeNodes;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
  hubNode.Create (1);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
  Ptr<Node> hub = hubNode.Get (0);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
  spokeNodes.Create (nNodes - 1);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
  CsmaHelper csma;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
  csma.SetChannelAttribute ("Delay", StringValue ("1ms"));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
  NS_LOG_INFO ("Build star topology.");
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
  NetDeviceContainer hubDevices, spokeDevices;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
  csma.InstallStar (hubNode.Get (0), spokeNodes, hubDevices, spokeDevices);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
  NodeContainer fillNodes;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
  // Just to be nasy, hang some more nodes off of the CSMA channel for each
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
  // spoke, so that there are a total of 16 nodes on each channel.  Stash
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
  // all of these new devices into a container.
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
  NetDeviceContainer fillDevices;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
  uint32_t nFill = 14;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
  for (uint32_t i = 0; i < spokeDevices.GetN (); ++i)
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
    {
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
      Ptr<Channel> channel = spokeDevices.Get (i)->GetChannel ();
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
      Ptr<CsmaChannel> csmaChannel = channel->GetObject<CsmaChannel> ();
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
      NodeContainer newNodes;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
      NetDeviceContainer newDevices;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
      newNodes.Create (nFill);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
      fillNodes.Add (newNodes);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
      fillDevices.Add (csma.Install (newNodes, csmaChannel));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
    }
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
  NS_LOG_INFO ("Install internet stack on all nodes.");
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
  InternetStackHelper internet;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
  internet.Install (NodeContainer (hubNode, spokeNodes, fillNodes));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
  NS_LOG_INFO ("Assign IP Addresses.");
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
  Ipv4AddressHelper address;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
  // Assign IPv4 interfaces and IP addresses to the devices we previously
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
  // created.  Keep track of the resulting addresses, one for the addresses
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
  // of the hub node, and one for addresses on the spoke nodes.  Despite the
3858
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   118
  // name of the class (Ipv4InterfaceContainer), what is visible to clients 
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   119
  // is really the address not the interface.
3795
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
  Ipv4InterfaceContainer hubAddresses;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
  Ipv4InterfaceContainer spokeAddresses;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
  for(uint32_t i = 0; i < spokeNodes.GetN (); ++i)
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
  {
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
    std::ostringstream subnet;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
    subnet << "10.1." << i << ".0";
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
    NS_LOG_INFO ("Assign IP Addresses for CSMA subnet " << subnet.str ());
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   129
    address.SetBase (subnet.str ().c_str (), "255.255.255.0");
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
    hubAddresses.Add (address.Assign (hubDevices.Get (i)));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
    spokeAddresses.Add (address.Assign (spokeDevices.Get (i)));
3858
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   132
    //
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   133
    // We assigned addresses to the logical hub and the first "drop" of the 
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   134
    // CSMA network that acts as the spoke, but we also have a number of fill
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   135
    // devices (nFill) also hanging off the CSMA network.  We have got to 
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   136
    // assign addresses to them as well.  We put all of the fill devices into
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   137
    // a single device container, so the first nFill devices are associated
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   138
    // with the channel connected to spokeDevices.Get (0), the second nFill
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   139
    // devices afe associated with the channel connected to spokeDevices.Get (1)
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   140
    // etc.
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   141
    //
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   142
    for (uint32_t j = 0; j < nFill; ++j)
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   143
      {
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   144
        address.Assign (fillDevices.Get (i * nFill + j));
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   145
      }
3795
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
  }
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   147
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
  NS_LOG_INFO ("Create applications.");
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   150
  // Create a packet sink on the star "hub" to receive packets.  
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   151
  // 
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   152
  uint16_t port = 50000;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
  Address hubLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", hubLocalAddress);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
  ApplicationContainer hubApp = packetSinkHelper.Install (hubNode);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   156
  hubApp.Start (Seconds (1.0));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   157
  hubApp.Stop (Seconds (10.0));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   158
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   160
  // Create OnOff applications to send TCP to the hub, one on each spoke node.
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   161
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   162
  OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
  onOffHelper.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
  onOffHelper.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   165
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   166
  ApplicationContainer spokeApps;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   167
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
  for (uint32_t i = 0; i < spokeNodes.GetN (); ++i)
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   169
    {
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   170
      AddressValue remoteAddress (InetSocketAddress (hubAddresses.GetAddress (i), port));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   171
      onOffHelper.SetAttribute ("Remote", remoteAddress);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   172
      spokeApps.Add (onOffHelper.Install (spokeNodes.Get (i)));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   173
    }
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   174
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   175
  spokeApps.Start (Seconds (1.0));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   176
  spokeApps.Stop (Seconds (10.0));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   177
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   178
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   179
  // Because we are evil, we also add OnOff applications to send TCP to the hub 
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   180
  // from the fill devices on each CSMA link.  The first nFill nodes in the 
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   181
  // fillNodes container are on the CSMA network talking to the zeroth device
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   182
  // on the hub node.  The next nFill nodes are on the CSMA network talking to
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   183
  // the first device on the hub node, etc.  So the ith fillNode is associated
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   184
  // with the hub address found on the (i / nFill)th device on the hub node.
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   185
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   186
  ApplicationContainer fillApps;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   187
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   188
  for (uint32_t i = 0; i < fillNodes.GetN (); ++i)
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   189
    {
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   190
      AddressValue remoteAddress (InetSocketAddress (hubAddresses.GetAddress (i / nFill), port));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   191
      onOffHelper.SetAttribute ("Remote", remoteAddress);
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
      fillApps.Add (onOffHelper.Install (fillNodes.Get (i)));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   193
    }
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   194
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   195
  fillApps.Start (Seconds (1.0));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
  fillApps.Stop (Seconds (10.0));
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   197
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   198
  NS_LOG_INFO ("Enable static global routing.");
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   199
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   200
  // Turn on global static routing so we can actually be routed across the star.
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   201
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   202
  GlobalRouteManager::PopulateRoutingTables ();
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   203
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   204
  NS_LOG_INFO ("Enable pcap tracing.");
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   205
  //
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   206
  // Do pcap tracing on all devices on all nodes.
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   207
  //
3858
6559f390009d fix buggy example
craigdo@ee.washington.edu
parents: 3795
diff changeset
   208
  CsmaHelper::EnablePcapAll ("csma-star");
3795
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   210
  NS_LOG_INFO ("Run Simulation.");
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   211
  Simulator::Run ();
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   212
  Simulator::Destroy ();
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   213
  NS_LOG_INFO ("Done.");
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   214
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   215
  return 0;
707acdf0f8b3 csma version of install star and example
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   216
}