examples/nix-simple.cc
changeset 5224 06f639d05b8f
equal deleted inserted replaced
5223:d6bbde9c6712 5224:06f639d05b8f
       
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * This program is free software; you can redistribute it and/or modify
       
     4  * it under the terms of the GNU General Public License version 2 as
       
     5  * published by the Free Software Foundation;
       
     6  *
       
     7  * This program is distributed in the hope that it will be useful,
       
     8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
     9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    10  * GNU General Public License for more details.
       
    11  *
       
    12  * You should have received a copy of the GNU General Public License
       
    13  * along with this program; if not, write to the Free Software
       
    14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    15  */
       
    16 
       
    17 #include "ns3/core-module.h"
       
    18 #include "ns3/simulator-module.h"
       
    19 #include "ns3/node-module.h"
       
    20 #include "ns3/helper-module.h"
       
    21 
       
    22 /*
       
    23  *  Simple point to point links:
       
    24  *
       
    25  *  n0 -- n1 -- n2 -- n3
       
    26  *
       
    27  *  n0 has UdpEchoClient 
       
    28  *  n3 has UdpEchoServer
       
    29  *
       
    30  *  n0 IP: 10.1.1.1
       
    31  *  n1 IP: 10.1.1.2, 10.1.2.1
       
    32  *  n2 IP: 10.1.2.2, 10.1.3.1
       
    33  *  n3 IP: 10.1.3.2
       
    34  *
       
    35  */
       
    36 
       
    37 using namespace ns3;
       
    38 
       
    39 NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
       
    40 
       
    41   int 
       
    42 main (int argc, char *argv[])
       
    43 {
       
    44   LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
       
    45   LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
       
    46 
       
    47   NodeContainer nodes12;
       
    48   nodes12.Create (2);
       
    49 
       
    50   NodeContainer nodes23;
       
    51   nodes23.Add (nodes12.Get (1));
       
    52   nodes23.Create (1);
       
    53 
       
    54   NodeContainer nodes34;
       
    55   nodes34.Add(nodes23.Get (1));
       
    56   nodes34.Create (1);
       
    57 
       
    58   PointToPointHelper pointToPoint;
       
    59   pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
       
    60   pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
       
    61 
       
    62   NodeContainer allNodes = NodeContainer (nodes12, nodes23.Get (1), nodes34.Get (1));
       
    63   
       
    64   // NixHelper to install nix-vector routing
       
    65   // on all nodes
       
    66   Ipv4NixVectorHelper nixRouting;
       
    67   Ipv4StaticRoutingHelper staticRouting;
       
    68 
       
    69   Ipv4ListRoutingHelper list;
       
    70   list.Add (staticRouting, 0);
       
    71   list.Add (nixRouting, 10);
       
    72 
       
    73   InternetStackHelper stack;
       
    74   stack.SetRoutingHelper (list);
       
    75   stack.Install (allNodes);
       
    76 
       
    77   NetDeviceContainer devices12;
       
    78   NetDeviceContainer devices23;
       
    79   NetDeviceContainer devices34;
       
    80   devices12 = pointToPoint.Install (nodes12);
       
    81   devices23 = pointToPoint.Install (nodes23);
       
    82   devices34 = pointToPoint.Install (nodes34);
       
    83 
       
    84   Ipv4AddressHelper address1;
       
    85   address1.SetBase ("10.1.1.0", "255.255.255.0");
       
    86   Ipv4AddressHelper address2;
       
    87   address2.SetBase ("10.1.2.0", "255.255.255.0");
       
    88   Ipv4AddressHelper address3;
       
    89   address3.SetBase ("10.1.3.0", "255.255.255.0");
       
    90 
       
    91   address1.Assign (devices12);
       
    92   address2.Assign (devices23);
       
    93   Ipv4InterfaceContainer interfaces = address3.Assign (devices34);
       
    94 
       
    95   UdpEchoServerHelper echoServer (9);
       
    96 
       
    97   ApplicationContainer serverApps = echoServer.Install (nodes34.Get (1));
       
    98   serverApps.Start (Seconds (1.0));
       
    99   serverApps.Stop (Seconds (10.0));
       
   100 
       
   101   UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
       
   102   echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
       
   103   echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
       
   104   echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
       
   105 
       
   106   ApplicationContainer clientApps = echoClient.Install (nodes12.Get (0));
       
   107   clientApps.Start (Seconds (2.0));
       
   108   clientApps.Stop (Seconds (10.0));
       
   109 
       
   110 
       
   111   Simulator::Run ();
       
   112   Simulator::Destroy ();
       
   113   return 0;
       
   114 }