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 
111 using namespace ns3;
112 
113 NS_LOG_COMPONENT_DEFINE ("NixSimpleExample");
114 
115 int
116 main (int argc, char *argv[])
117 {
118  CommandLine cmd (__FILE__);
119  cmd.Parse (argc, argv);
120 
121  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
122  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
123 
124  NodeContainer nodes12;
125  nodes12.Create (2);
126 
127  NodeContainer nodes23;
128  nodes23.Add (nodes12.Get (1));
129  nodes23.Create (1);
130 
131  NodeContainer nodes34;
132  nodes34.Add (nodes23.Get (1));
133  nodes34.Create (1);
134 
135  NodeContainer nodes13;
136  nodes13.Add (nodes12.Get (0));
137  nodes13.Add (nodes34.Get (0));
138 
140  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
141  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
142 
143  NodeContainer allNodes = NodeContainer (nodes12, nodes23.Get (1), nodes34.Get (1));
144 
145  // NixHelper to install nix-vector routing
146  // on all nodes
147  Ipv4NixVectorHelper nixRouting;
149  stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
150  stack.Install (allNodes);
151 
152  NetDeviceContainer devices12;
153  NetDeviceContainer devices23;
154  NetDeviceContainer devices34;
155  NetDeviceContainer devices13;
156  devices12 = pointToPoint.Install (nodes12);
157  devices23 = pointToPoint.Install (nodes23);
158  devices34 = pointToPoint.Install (nodes34);
159  devices13 = pointToPoint.Install (nodes13);
160 
161  Ipv4AddressHelper address1;
162  address1.SetBase ("10.1.1.0", "255.255.255.0");
163  Ipv4AddressHelper address2;
164  address2.SetBase ("10.1.2.0", "255.255.255.0");
165  Ipv4AddressHelper address3;
166  address3.SetBase ("10.1.3.0", "255.255.255.0");
167  Ipv4AddressHelper address4;
168  address4.SetBase ("10.1.4.0", "255.255.255.0");
169 
170  Ipv4InterfaceContainer interfaces12 = address1.Assign (devices12);
171  Ipv4InterfaceContainer interfaces23 = address2.Assign (devices23);
172  Ipv4InterfaceContainer interfaces34 = address3.Assign (devices34);
173  Ipv4InterfaceContainer interfaces13 = address4.Assign (devices13);
174 
176 
177  ApplicationContainer serverApps = echoServer.Install (nodes34.Get (1));
178  serverApps.Start (Seconds (1.0));
179  serverApps.Stop (Seconds (10.0));
180 
181  UdpEchoClientHelper echoClient (interfaces34.GetAddress (1), 9);
182  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
183  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
184  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
185 
186  ApplicationContainer clientApps = echoClient.Install (nodes12.Get (0));
187  clientApps.Start (Seconds (2.0));
188  clientApps.Stop (Seconds (10.0));
189 
190  // Trace routing paths for different source and destinations.
191  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("nix-simple.routes", std::ios::out);
192  nixRouting.PrintRoutingPathAt (Seconds (3), nodes12.Get (0), interfaces34.GetAddress (1), routingStream);
193  nixRouting.PrintRoutingPathAt (Seconds (5), nodes12.Get (1), interfaces34.GetAddress (1), routingStream);
194  nixRouting.PrintRoutingPathAt (Seconds (6), nodes23.Get (1), interfaces12.GetAddress (0), routingStream);
195  nixRouting.PrintRoutingPathAt (Seconds (7), nodes12.Get (1), interfaces12.GetAddress (1), routingStream);
196  // Trace routing tables
197  nixRouting.PrintRoutingTableAllAt (Seconds (8), routingStream);
198 
199  Simulator::Run ();
201  return 0;
202 }
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:52
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:172
echoServer
Definition: first.py:50
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
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:41
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:361
AttributeValue implementation for Time.
Definition: nstime.h:1353
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
echoClient
Definition: first.py:56
Hold an unsigned integer type.
Definition: uinteger.h:44
pointToPoint
Definition: first.py:35
LOG_INFO and above.
Definition: log.h:107
holds a vector of ns3::NetDevice pointers
Parse command-line arguments.
Definition: command-line.h:227
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
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:1289
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
clientApps
Definition: first.py:61
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.