A Discrete-Event Network Simulator
API
third-distributed.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 "mpi-test-fixtures.h"
18 
19 #include "ns3/core-module.h"
20 #include "ns3/point-to-point-module.h"
21 #include "ns3/network-module.h"
22 #include "ns3/applications-module.h"
23 #include "ns3/mobility-module.h"
24 #include "ns3/csma-module.h"
25 #include "ns3/internet-module.h"
26 #include "ns3/mpi-module.h"
27 #include "ns3/yans-wifi-helper.h"
28 #include "ns3/ssid.h"
29 
30 #include <iomanip>
31 
55 using namespace ns3;
56 
57 NS_LOG_COMPONENT_DEFINE ("ThirdExampleDistributed");
58 
59 int
60 main (int argc, char *argv[])
61 {
62  bool verbose = false;
63  uint32_t nCsma = 3;
64  uint32_t nWifi = 3;
65  bool tracing = false;
66  bool nullmsg = false;
67  bool testing = false;
68 
69  CommandLine cmd (__FILE__);
70  cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
71  cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);
72  cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
73  cmd.AddValue ("tracing", "Enable pcap tracing", tracing);
74  cmd.AddValue ("nullmsg", "Enable the use of null-message synchronization", nullmsg);
75  cmd.AddValue ("test", "Enable regression test output", testing);
76 
77  cmd.Parse (argc,argv);
78 
79  // The underlying restriction of 18 is due to the grid position
80  // allocator's configuration; the grid layout will exceed the
81  // bounding box if more than 18 nodes are provided.
82  if (nWifi > 18)
83  {
84  std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box" << std::endl;
85  return 1;
86  }
87 
88  if (verbose)
89  {
90  LogComponentEnable ("UdpEchoClientApplication", (LogLevel)(LOG_LEVEL_INFO | LOG_PREFIX_NODE | LOG_PREFIX_TIME));
91  LogComponentEnable ("UdpEchoServerApplication", (LogLevel)(LOG_LEVEL_INFO | LOG_PREFIX_NODE | LOG_PREFIX_TIME));
92  }
93 
94  // Sequential fallback values
95  uint32_t systemId = 0;
96  uint32_t systemCount = 1;
97 
98  // Distributed simulation setup; by default use granted time window algorithm.
99  if(nullmsg)
100  {
101  GlobalValue::Bind ("SimulatorImplementationType",
102  StringValue ("ns3::NullMessageSimulatorImpl"));
103  }
104  else
105  {
106  GlobalValue::Bind ("SimulatorImplementationType",
107  StringValue ("ns3::DistributedSimulatorImpl"));
108  }
109 
110  MpiInterface::Enable (&argc, &argv);
111 
112  SinkTracer::Init ();
113 
114  systemId = MpiInterface::GetSystemId ();
115  systemCount = MpiInterface::GetSize ();
116 
117  // Check for valid distributed parameters.
118  // Must have 2 and only 2 Logical Processors (LPs)
119  if (systemCount != 2)
120  {
121  std::cout << "This simulation requires 2 and only 2 logical processors." << std::endl;
122  return 1;
123  }
124 
125  // System id of Wifi side
126  uint32_t systemWifi = 0;
127 
128  // System id of CSMA side
129  uint32_t systemCsma = systemCount - 1;
130 
132  // Create each end of the P2P link on a separate system (rank)
133  Ptr<Node> p2pNode1 = CreateObject<Node> (systemWifi);
134  Ptr<Node> p2pNode2 = CreateObject<Node> (systemCsma);
135  p2pNodes.Add (p2pNode1);
136  p2pNodes.Add (p2pNode2);
137 
139  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
140  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
141 
143  p2pDevices = pointToPoint.Install (p2pNodes);
144 
146  csmaNodes.Add (p2pNodes.Get (1));
147  // Create the csma nodes on one system (rank)
148  csmaNodes.Create (nCsma, systemCsma);
149 
151  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
152  csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
153 
155  csmaDevices = csma.Install (csmaNodes);
156 
158  // Create the wifi nodes on the other system (rank)
159  wifiStaNodes.Create (nWifi, systemWifi);
161 
164  phy.SetChannel (channel.Create ());
165 
167  wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
168 
170  Ssid ssid = Ssid ("ns-3-ssid");
171  mac.SetType ("ns3::StaWifiMac",
172  "Ssid", SsidValue (ssid),
173  "ActiveProbing", BooleanValue (false));
174 
176  staDevices = wifi.Install (phy, mac, wifiStaNodes);
177 
178  mac.SetType ("ns3::ApWifiMac",
179  "Ssid", SsidValue (ssid));
180 
182  apDevices = wifi.Install (phy, mac, wifiApNode);
183 
185 
186  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
187  "MinX", DoubleValue (0.0),
188  "MinY", DoubleValue (0.0),
189  "DeltaX", DoubleValue (5.0),
190  "DeltaY", DoubleValue (10.0),
191  "GridWidth", UintegerValue (3),
192  "LayoutType", StringValue ("RowFirst"));
193 
194  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
195  "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
196  mobility.Install (wifiStaNodes);
197 
198  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
199  mobility.Install (wifiApNode);
200 
202  stack.Install (csmaNodes);
203  stack.Install (wifiApNode);
204  stack.Install (wifiStaNodes);
205 
207 
208  address.SetBase ("10.1.1.0", "255.255.255.0");
210  p2pInterfaces = address.Assign (p2pDevices);
211 
212  address.SetBase ("10.1.2.0", "255.255.255.0");
215 
216  address.SetBase ("10.1.3.0", "255.255.255.0");
217  address.Assign (staDevices);
218  address.Assign (apDevices);
219 
220  // If this rank is systemCsma,
221  // it should contain the server application,
222  // since it is on one of the csma nodes
223  if (systemId == systemCsma)
224  {
226 
228  serverApps.Start (Seconds (1.0));
229  serverApps.Stop (Seconds (10.0));
230 
231  if (testing)
232  {
233  serverApps.Get (0)->TraceConnectWithoutContext ("RxWithAddresses", MakeCallback (&SinkTracer::SinkTrace));
234  }
235  }
236 
237  // If this rank is systemWifi
238  // it should contain the client application,
239  // since it is on one of the wifi nodes
240  if (systemId == systemWifi)
241  {
243  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
244  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
245  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
246 
248  echoClient.Install (wifiStaNodes.Get (nWifi - 1));
249  clientApps.Start (Seconds (2.0));
250  clientApps.Stop (Seconds (10.0));
251 
252  if (testing)
253  {
254  clientApps.Get (0)->TraceConnectWithoutContext ("RxWithAddresses", MakeCallback (&SinkTracer::SinkTrace));
255  }
256  }
257 
259 
260  Simulator::Stop (Seconds (10.0));
261 
262  if (tracing == true)
263  {
264  // Depending on the system Id (rank), the pcap information
265  // traced will be different. For example, the ethernet pcap
266  // will be empty for rank0, since these nodes are placed on
267  // on rank 1. All ethernet traffic will take place on rank 1.
268  // Similar differences are seen in the p2p and wireless pcaps.
269  if (systemId == systemCsma)
270  {
271  pointToPoint.EnablePcapAll ("third-distributed-csma");
272  phy.EnablePcap ("third-distributed-csma", apDevices.Get (0));
273  csma.EnablePcap ("third-distributed-csma", csmaDevices.Get (0), true);
274  }
275  else // systemWifi
276  {
277  pointToPoint.EnablePcapAll ("third-distributed-wifi");
278  phy.EnablePcap ("third-distributed-wifi", apDevices.Get (0));
279  csma.EnablePcap ("third-distributed-wifi", csmaDevices.Get (0), true);
280  }
281  }
282 
283  Simulator::Run ();
285 
286  if (testing)
287  {
288  SinkTracer::Verify (2);
289  }
290 
291  // Exit the MPI execution environment
293 
294  return 0;
295 }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::YansWifiChannelHelper::Default
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
Definition: yans-wifi-helper.cc:41
ns3::YansWifiPhyHelper
Make it easy to create and manage PHY objects for the YANS model.
Definition: yans-wifi-helper.h:161
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
third.staDevices
staDevices
Definition: third.py:103
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition: boolean.h:37
ns3::LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
second.p2pInterfaces
p2pInterfaces
Definition: second.py:75
tracing
bool tracing
Flag to enable/disable generation of tracing files.
Definition: wifi-bianchi.cc:85
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::WifiHelper
helps to create WifiNetDevice objects
Definition: wifi-helper.h:327
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::UdpEchoClientHelper
Create an application which sends a UDP packet and waits for an echo of this packet.
Definition: udp-echo-helper.h:107
ns3::LOG_PREFIX_NODE
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition: log.h:120
ns3::RectangleValue
AttributeValue implementation for Rectangle.
Definition: rectangle.h:97
ns3::MpiInterface::Disable
static void Disable()
Clean up the ns-3 parallel communications interface.
Definition: mpi-interface.cc:146
third.channel
channel
Definition: third.py:92
ns3::LogComponentEnable
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
third.mac
mac
Definition: third.py:99
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
ns3::SinkTracer::Verify
static void Verify(unsigned long expectedCount)
Verify the sink trace count observed matches the expected count.
Definition: mpi-test-fixtures.cc:58
ns3::SsidValue
AttributeValue implementation for Ssid.
Definition: ssid.h:105
ns3::CsmaHelper
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
ns3::Ssid
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
third.apDevices
apDevices
Definition: third.py:106
third.wifi
wifi
Definition: third.py:96
ns3::Ptr< Node >
ns3::Ipv4GlobalRoutingHelper::PopulateRoutingTables
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Definition: ipv4-global-routing-helper.cc:61
first.stack
stack
Definition: first.py:41
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
mpi-test-fixtures.h
Common methods for MPI examples.
ns3::NanoSeconds
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1313
second.csmaInterfaces
csmaInterfaces
Definition: second.py:78
ns3::MpiInterface::Enable
static void Enable(int *pargc, char ***pargv)
Setup the parallel communication interface.
Definition: mpi-interface.cc:115
second.p2pNodes
p2pNodes
Definition: second.py:50
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
verbose
bool verbose
Definition: openflow-switch.cc:50
first.address
address
Definition: first.py:44
third.wifiApNode
wifiApNode
Definition: third.py:90
second.cmd
cmd
Definition: second.py:35
second.p2pDevices
p2pDevices
Definition: second.py:61
ns3::Rectangle
a 2d rectangle
Definition: rectangle.h:35
ns3::MakeCallback
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::GlobalValue::Bind
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
Definition: global-value.cc:155
first.echoClient
echoClient
Definition: first.py:56
ns3::SinkTracer::SinkTrace
static void SinkTrace(const ns3::Ptr< const ns3::Packet > packet, const ns3::Address &srcAddress, const ns3::Address &destAddress)
PacketSink receive trace callback.
Definition: mpi-test-fixtures.cc:50
ns3::SinkTracer::Init
static void Init(void)
PacketSink receive trace callback.
Definition: mpi-test-fixtures.cc:40
ns3::LOG_PREFIX_TIME
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
first.clientApps
clientApps
Definition: first.py:61
second.nCsma
nCsma
Definition: second.py:36
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::LogLevel
LogLevel
Logging severity classes and levels.
Definition: log.h:94
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
first.serverApps
serverApps
Definition: first.py:52
third.ssid
ssid
Definition: third.py:100
first.pointToPoint
pointToPoint
Definition: first.py:35
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::PointToPointHelper
Build a set of PointToPointNetDevice objects.
Definition: point-to-point-helper.h:45
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
third.nWifi
nWifi
Definition: third.py:43
third.wifiStaNodes
wifiStaNodes
Definition: third.py:88
second.csmaDevices
csmaDevices
Definition: second.py:67
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
second.csmaNodes
csmaNodes
Definition: second.py:53
ns3::YansWifiChannelHelper
manage and create wifi channel objects for the YANS model.
Definition: yans-wifi-helper.h:37
ns3::WifiMacHelper
create MAC layers for a ns3::WifiNetDevice.
Definition: wifi-mac-helper.h:48
ns3::UdpEchoServerHelper
Create a server application which waits for input UDP packets and sends them back to the original sen...
Definition: udp-echo-helper.h:38
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
second.csma
csma
Definition: second.py:63
ns3::MpiInterface::GetSystemId
static uint32_t GetSystemId()
Get the id number of this rank.
Definition: mpi-interface.cc:50
ns3::MobilityHelper
Helper class used to assign positions and mobility models to nodes.
Definition: mobility-helper.h:43
third.mobility
mobility
Definition: third.py:108
ns3::MpiInterface::GetSize
static uint32_t GetSize()
Get the number of ranks used by ns-3.
Definition: mpi-interface.cc:59
first.echoServer
echoServer
Definition: first.py:50
third.phy
phy
Definition: third.py:93