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  Ssid ssid = Ssid ("ns-3-ssid");
168 
170 
172  mac.SetType ("ns3::StaWifiMac",
173  "Ssid", SsidValue (ssid),
174  "ActiveProbing", BooleanValue (false));
175  staDevices = wifi.Install (phy, mac, wifiStaNodes);
176 
178  mac.SetType ("ns3::ApWifiMac",
179  "Ssid", SsidValue (ssid));
180  apDevices = wifi.Install (phy, mac, wifiApNode);
181 
183 
184  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
185  "MinX", DoubleValue (0.0),
186  "MinY", DoubleValue (0.0),
187  "DeltaX", DoubleValue (5.0),
188  "DeltaY", DoubleValue (10.0),
189  "GridWidth", UintegerValue (3),
190  "LayoutType", StringValue ("RowFirst"));
191 
192  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
193  "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
194  mobility.Install (wifiStaNodes);
195 
196  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
197  mobility.Install (wifiApNode);
198 
200  stack.Install (csmaNodes);
201  stack.Install (wifiApNode);
202  stack.Install (wifiStaNodes);
203 
205 
206  address.SetBase ("10.1.1.0", "255.255.255.0");
208  p2pInterfaces = address.Assign (p2pDevices);
209 
210  address.SetBase ("10.1.2.0", "255.255.255.0");
213 
214  address.SetBase ("10.1.3.0", "255.255.255.0");
215  address.Assign (staDevices);
216  address.Assign (apDevices);
217 
218  // If this rank is systemCsma,
219  // it should contain the server application,
220  // since it is on one of the csma nodes
221  if (systemId == systemCsma)
222  {
224 
226  serverApps.Start (Seconds (1.0));
227  serverApps.Stop (Seconds (10.0));
228 
229  if (testing)
230  {
231  serverApps.Get (0)->TraceConnectWithoutContext ("RxWithAddresses", MakeCallback (&SinkTracer::SinkTrace));
232  }
233  }
234 
235  // If this rank is systemWifi
236  // it should contain the client application,
237  // since it is on one of the wifi nodes
238  if (systemId == systemWifi)
239  {
241  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
242  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
243  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
244 
246  echoClient.Install (wifiStaNodes.Get (nWifi - 1));
247  clientApps.Start (Seconds (2.0));
248  clientApps.Stop (Seconds (10.0));
249 
250  if (testing)
251  {
252  clientApps.Get (0)->TraceConnectWithoutContext ("RxWithAddresses", MakeCallback (&SinkTracer::SinkTrace));
253  }
254  }
255 
257 
258  Simulator::Stop (Seconds (10.0));
259 
260  if (tracing == true)
261  {
262  // Depending on the system Id (rank), the pcap information
263  // traced will be different. For example, the ethernet pcap
264  // will be empty for rank0, since these nodes are placed on
265  // on rank 1. All ethernet traffic will take place on rank 1.
266  // Similar differences are seen in the p2p and wireless pcaps.
267  if (systemId == systemCsma)
268  {
269  pointToPoint.EnablePcapAll ("third-distributed-csma");
270  phy.EnablePcap ("third-distributed-csma", apDevices.Get (0));
271  csma.EnablePcap ("third-distributed-csma", csmaDevices.Get (0), true);
272  }
273  else // systemWifi
274  {
275  pointToPoint.EnablePcapAll ("third-distributed-wifi");
276  phy.EnablePcap ("third-distributed-wifi", apDevices.Get (0));
277  csma.EnablePcap ("third-distributed-wifi", csmaDevices.Get (0), true);
278  }
279  }
280 
281  Simulator::Run ();
283 
284  if (testing)
285  {
286  SinkTracer::Verify (2);
287  }
288 
289  // Exit the MPI execution environment
291 
292  return 0;
293 }
holds a vector of ns3::Application pointers.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:229
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
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...
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class used to assign positions and mobility models to nodes.
static uint32_t GetSystemId()
Get the id number of this rank.
static uint32_t GetSize()
Get the number of ranks used by ns-3.
static void Disable()
Clean up the ns-3 parallel communications interface.
static void Enable(int *pargc, char ***pargv)
Setup the parallel communication interface.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Build a set of PointToPointNetDevice objects.
a 2d rectangle
Definition: rectangle.h:35
AttributeValue implementation for Rectangle.
Definition: rectangle.h:97
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
static void SinkTrace(const ns3::Ptr< const ns3::Packet > packet, const ns3::Address &srcAddress, const ns3::Address &destAddress)
PacketSink receive trace callback.
static void Init(void)
PacketSink Init.
static void Verify(unsigned long expectedCount)
Verify the sink trace count observed matches the expected count.
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
AttributeValue implementation for Ssid.
Definition: ssid.h:105
Hold variables of type string.
Definition: string.h:41
AttributeValue implementation for Time.
Definition: nstime.h:1308
Create an application which sends a UDP packet and waits for an echo of this packet.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Hold an unsigned integer type.
Definition: uinteger.h:44
helps to create WifiNetDevice objects
Definition: wifi-helper.h:274
create MAC layers for a ns3::WifiNetDevice.
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
Make it easy to create and manage PHY objects for the YANS model.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1268
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Common methods for MPI examples.
echoClient
Definition: first.py:56
address
Definition: first.py:44
serverApps
Definition: first.py:52
pointToPoint
Definition: first.py:35
echoServer
Definition: first.py:50
clientApps
Definition: first.py:61
stack
Definition: first.py:41
Every class exported by the ns3 library is enclosed in the ns3 namespace.
LogLevel
Logging severity classes and levels.
Definition: log.h:94
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition: log.h:120
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
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:1648
nCsma
Definition: second.py:36
p2pNodes
Definition: second.py:50
p2pInterfaces
Definition: second.py:75
csmaInterfaces
Definition: second.py:78
csmaNodes
Definition: second.py:53
csma
Definition: second.py:63
p2pDevices
Definition: second.py:61
cmd
Definition: second.py:35
csmaDevices
Definition: second.py:67
staDevices
Definition: third.py:103
ssid
Definition: third.py:100
channel
Definition: third.py:92
nWifi
Definition: third.py:43
mac
Definition: third.py:99
wifi
Definition: third.py:96
apDevices
Definition: third.py:106
wifiApNode
Definition: third.py:90
mobility
Definition: third.py:108
wifiStaNodes
Definition: third.py:88
phy
Definition: third.py:93
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.
Definition: wifi-bianchi.cc:88