A Discrete-Event Network Simulator
API
nix-simple.cc
Go to the documentation of this file.
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/network-module.h"
19 #include "ns3/internet-module.h"
20 #include "ns3/point-to-point-module.h"
21 #include "ns3/applications-module.h"
22 #include "ns3/ipv4-static-routing-helper.h"
23 #include "ns3/ipv4-list-routing-helper.h"
24 #include "ns3/ipv4-nix-vector-helper.h"
25 
26 /*
27  * Simple point to point links:
28  *
29  * n0 -- n1 -- n2 -- n3
30  *
31  * n0 has UdpEchoClient
32  * n3 has UdpEchoServer
33  *
34  * n0 IP: 10.1.1.1
35  * n1 IP: 10.1.1.2, 10.1.2.1
36  * n2 IP: 10.1.2.2, 10.1.3.1
37  * n3 IP: 10.1.3.2
38  *
39  */
40 
41 using namespace ns3;
42 
43 NS_LOG_COMPONENT_DEFINE ("NixSimpleExample");
44 
45 int
46 main (int argc, char *argv[])
47 {
49  cmd.Parse (argc, argv);
50 
51  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
52  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
53 
54  NodeContainer nodes12;
55  nodes12.Create (2);
56 
57  NodeContainer nodes23;
58  nodes23.Add (nodes12.Get (1));
59  nodes23.Create (1);
60 
61  NodeContainer nodes34;
62  nodes34.Add (nodes23.Get (1));
63  nodes34.Create (1);
64 
66  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
67  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
68 
69  NodeContainer allNodes = NodeContainer (nodes12, nodes23.Get (1), nodes34.Get (1));
70 
71  // NixHelper to install nix-vector routing
72  // on all nodes
73  Ipv4NixVectorHelper nixRouting;
75  stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
76  stack.Install (allNodes);
77 
78  NetDeviceContainer devices12;
79  NetDeviceContainer devices23;
80  NetDeviceContainer devices34;
81  devices12 = pointToPoint.Install (nodes12);
82  devices23 = pointToPoint.Install (nodes23);
83  devices34 = pointToPoint.Install (nodes34);
84 
85  Ipv4AddressHelper address1;
86  address1.SetBase ("10.1.1.0", "255.255.255.0");
87  Ipv4AddressHelper address2;
88  address2.SetBase ("10.1.2.0", "255.255.255.0");
89  Ipv4AddressHelper address3;
90  address3.SetBase ("10.1.3.0", "255.255.255.0");
91 
92  address1.Assign (devices12);
93  address2.Assign (devices23);
94  Ipv4InterfaceContainer interfaces = address3.Assign (devices34);
95 
97 
98  ApplicationContainer serverApps = echoServer.Install (nodes34.Get (1));
99  serverApps.Start (Seconds (1.0));
100  serverApps.Stop (Seconds (10.0));
101 
102  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
103  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
104  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
105  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
106 
107  ApplicationContainer clientApps = echoClient.Install (nodes12.Get (0));
108  clientApps.Start (Seconds (2.0));
109  clientApps.Stop (Seconds (10.0));
110 
111  // Trace routing tables
112  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("nix-simple.routes", std::ios::out);
113  nixRouting.PrintRoutingTableAllAt (Seconds (8), routingStream);
114 
115  Simulator::Run ();
117  return 0;
118 }
holds a vector of ns3::Application pointers.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Hold variables of type string.
Definition: string.h:41
serverApps
Definition: first.py:45
Create an application which sends a UDP packet and waits for an echo of this packet.
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
echoServer
Definition: first.py:43
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Helper class that adds Nix-vector routing to nodes.
aggregate IP/TCP/UDP functionality to existing Nodes.
cmd
Definition: second.py:35
Build a set of PointToPointNetDevice objects.
stack
Definition: first.py:34
Create a server application which waits for input UDP packets and sends them back to the original sen...
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
AttributeValue implementation for Time.
Definition: nstime.h:1076
echoClient
Definition: first.py:49
Hold an unsigned integer type.
Definition: uinteger.h:44
pointToPoint
Definition: first.py:28
LOG_INFO and above.
Definition: log.h:104
holds a vector of ns3::NetDevice pointers
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
interfaces
Definition: first.py:41
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
clientApps
Definition: first.py:54
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.