A Discrete-Event Network Simulator
API
point-to-point-grid.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  * Author: Josh Pelkey <jpelkey@gatech.edu>
17  */
18 
19 // Implement an object to create a grid topology.
20 
21 #include "ns3/point-to-point-grid.h"
22 #include "ns3/internet-stack-helper.h"
23 #include "ns3/point-to-point-helper.h"
24 #include "ns3/constant-position-mobility-model.h"
25 #include "ns3/string.h"
26 #include "ns3/vector.h"
27 #include "ns3/log.h"
28 #include "ns3/ipv6-address-generator.h"
29 
30 namespace ns3 {
31 
32 NS_LOG_COMPONENT_DEFINE ("PointToPointGridHelper");
33 
35  uint32_t nCols,
37  : m_xSize (nCols), m_ySize (nRows)
38 {
39  // Bounds check
40  if (m_xSize < 1 || m_ySize < 1 || (m_xSize < 2 && m_ySize < 2))
41  {
42  NS_FATAL_ERROR ("Need more nodes for grid.");
43  }
44 
46 
47  for (uint32_t y = 0; y < nRows; ++y)
48  {
49  NodeContainer rowNodes;
50  NetDeviceContainer rowDevices;
51  NetDeviceContainer colDevices;
52 
53  for (uint32_t x = 0; x < nCols; ++x)
54  {
55  rowNodes.Create (1);
56 
57  // install p2p links across the row
58  if (x > 0)
59  {
60  rowDevices.Add (pointToPoint.
61  Install (rowNodes.Get (x-1), rowNodes.Get (x)));
62  }
63 
64  // install vertical p2p links
65  if (y > 0)
66  {
67  colDevices.Add (pointToPoint.
68  Install ((m_nodes.at (y-1)).Get (x), rowNodes.Get (x)));
69  }
70  }
71 
72  m_nodes.push_back (rowNodes);
73  m_rowDevices.push_back (rowDevices);
74 
75  if (y > 0)
76  m_colDevices.push_back (colDevices);
77  }
78 }
79 
81 {
82 }
83 
84 void
86 {
87  for (uint32_t i = 0; i < m_nodes.size (); ++i)
88  {
89  NodeContainer rowNodes = m_nodes[i];
90  for (uint32_t j = 0; j < rowNodes.GetN (); ++j)
91  {
92  stack.Install (rowNodes.Get (j));
93  }
94  }
95 }
96 
97 void
99 {
100  // Assign addresses to all row devices in the grid.
101  // These devices are stored in a vector. Each row
102  // of the grid has all the row devices in one entry
103  // of the vector. These entries come in pairs.
104  for (uint32_t i = 0; i < m_rowDevices.size (); ++i)
105  {
106  Ipv4InterfaceContainer rowInterfaces;
107  NetDeviceContainer rowContainer = m_rowDevices[i];
108  for (uint32_t j = 0; j < rowContainer.GetN (); j+=2)
109  {
110  rowInterfaces.Add (rowIp.Assign (rowContainer.Get (j)));
111  rowInterfaces.Add (rowIp.Assign (rowContainer.Get (j+1)));
112  rowIp.NewNetwork ();
113  }
114  m_rowInterfaces.push_back (rowInterfaces);
115  }
116 
117  // Assign addresses to all col devices in the grid.
118  // These devices are stored in a vector. Each col
119  // of the grid has all the col devices in one entry
120  // of the vector. These entries come in pairs.
121  for (uint32_t i = 0; i < m_colDevices.size (); ++i)
122  {
123  Ipv4InterfaceContainer colInterfaces;
124  NetDeviceContainer colContainer = m_colDevices[i];
125  for (uint32_t j = 0; j < colContainer.GetN (); j+=2)
126  {
127  colInterfaces.Add (colIp.Assign (colContainer.Get (j)));
128  colInterfaces.Add (colIp.Assign (colContainer.Get (j+1)));
129  colIp.NewNetwork ();
130  }
131  m_colInterfaces.push_back (colInterfaces);
132  }
133 }
134 
135 void
137 {
138  Ipv6AddressGenerator::Init(addrBase, prefix);
139  Ipv6Address v6network;
140  Ipv6AddressHelper addrHelper;
141 
142  // Assign addresses to all row devices in the grid.
143  // These devices are stored in a vector. Each row
144  // of the grid has all the row devices in one entry
145  // of the vector. These entries come in pairs.
146  for (uint32_t i = 0; i < m_rowDevices.size (); ++i)
147  {
148  Ipv6InterfaceContainer rowInterfaces;
149  NetDeviceContainer rowContainer = m_rowDevices[i];
150  for (uint32_t j = 0; j < rowContainer.GetN (); j+=2)
151  {
152  v6network = Ipv6AddressGenerator::GetNetwork (prefix);
153  addrHelper.SetBase(v6network, prefix);
154  Ipv6InterfaceContainer ic = addrHelper.Assign (rowContainer.Get (j));
155  rowInterfaces.Add (ic);
156  ic = addrHelper.Assign (rowContainer.Get (j+1));
157  rowInterfaces.Add (ic);
159  }
160  m_rowInterfaces6.push_back (rowInterfaces);
161  }
162 
163  // Assign addresses to all col devices in the grid.
164  // These devices are stored in a vector. Each col
165  // of the grid has all the col devices in one entry
166  // of the vector. These entries come in pairs.
167  for (uint32_t i = 0; i < m_colDevices.size (); ++i)
168  {
169  Ipv6InterfaceContainer colInterfaces;
170  NetDeviceContainer colContainer = m_colDevices[i];
171  for (uint32_t j = 0; j < colContainer.GetN (); j+=2)
172  {
173  v6network = Ipv6AddressGenerator::GetNetwork (prefix);
174  addrHelper.SetBase(v6network, prefix);
175  Ipv6InterfaceContainer ic = addrHelper.Assign (colContainer.Get (j));
176  colInterfaces.Add (ic);
177  ic = addrHelper.Assign (colContainer.Get (j+1));
178  colInterfaces.Add (ic);
180  }
181  m_colInterfaces6.push_back (colInterfaces);
182  }
183 }
184 
185 void
186 PointToPointGridHelper::BoundingBox (double ulx, double uly,
187  double lrx, double lry)
188 {
189  double xDist;
190  double yDist;
191  if (lrx > ulx)
192  {
193  xDist = lrx - ulx;
194  }
195  else
196  {
197  xDist = ulx - lrx;
198  }
199  if (lry > uly)
200  {
201  yDist = lry - uly;
202  }
203  else
204  {
205  yDist = uly - lry;
206  }
207  double xAdder = xDist / m_xSize;
208  double yAdder = yDist / m_ySize;
209  double yLoc = yDist / 2;
210  for (uint32_t i = 0; i < m_ySize; ++i)
211  {
212  double xLoc = xDist / 2;
213  for (uint32_t j = 0; j < m_xSize; ++j)
214  {
215  Ptr<Node> node = GetNode (i, j);
217  if (loc ==0)
218  {
219  loc = CreateObject<ConstantPositionMobilityModel> ();
220  node->AggregateObject (loc);
221  }
222  Vector locVec (xLoc, yLoc, 0);
223  loc->SetPosition (locVec);
224 
225  xLoc += xAdder;
226  }
227  yLoc += yAdder;
228  }
229 }
230 
231 Ptr<Node>
232 PointToPointGridHelper::GetNode (uint32_t row, uint32_t col)
233 {
234  if (row > m_nodes.size () - 1 ||
235  col > m_nodes.at (row).GetN () - 1)
236  {
237  NS_FATAL_ERROR ("Index out of bounds in PointToPointGridHelper::GetNode.");
238  }
239 
240  return (m_nodes.at (row)).Get (col);
241 }
242 
244 PointToPointGridHelper::GetIpv4Address (uint32_t row, uint32_t col)
245 {
246  if (row > m_nodes.size () - 1 ||
247  col > m_nodes.at (row).GetN () - 1)
248  {
249  NS_FATAL_ERROR ("Index out of bounds in PointToPointGridHelper::GetIpv4Address.");
250  }
251 
252  // Right now this just gets one of the addresses of the
253  // specified node. The exact device can't be specified.
254  // If you picture the grid, the address returned is the
255  // address of the left (row) device of all nodes, with
256  // the exception of the left-most nodes in the grid;
257  // in which case the right (row) device address is
258  // returned
259  if (col == 0)
260  {
261  return (m_rowInterfaces.at (row)).GetAddress (0);
262  }
263  else
264  {
265  return (m_rowInterfaces.at (row)).GetAddress ((2*col)-1);
266  }
267 }
268 
270 PointToPointGridHelper::GetIpv6Address (uint32_t row, uint32_t col)
271 {
272  if (row > m_nodes.size () - 1 ||
273  col > m_nodes.at (row).GetN () - 1)
274  {
275  NS_FATAL_ERROR ("Index out of bounds in PointToPointGridHelper::GetIpv6Address.");
276  }
277 
278  // Right now this just gets one of the addresses of the
279  // specified node. The exact device can't be specified.
280  // If you picture the grid, the address returned is the
281  // address of the left (row) device of all nodes, with
282  // the exception of the left-most nodes in the grid;
283  // in which case the right (row) device address is
284  // returned
285  if (col == 0)
286  {
287  return (m_rowInterfaces6.at (row)).GetAddress (0, 1);
288  }
289  else
290  {
291  return (m_rowInterfaces6.at (row)).GetAddress ((2*col)-1, 1);
292  }
293 }
294 
295 } // namespace ns3
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Keep track of a set of IPv6 interfaces.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
std::vector< NetDeviceContainer > m_colDevices
NetDevices in a column.
void AssignIpv4Addresses(Ipv4AddressHelper rowIp, Ipv4AddressHelper colIp)
Assigns Ipv4 addresses to all the row and column interfaces.
void AssignIpv6Addresses(Ipv6Address network, Ipv6Prefix prefix)
Assigns Ipv6 addresses to all the row and column interfaces.
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
void Add(Ptr< Ipv6 > ipv6, uint32_t interface)
Add a couple IPv6/interface.
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
Mobility model for which the current position does not change once it has been set and until it is se...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
uint32_t m_xSize
X size of the grid (number of columns)
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
Ipv6Address GetIpv6Address(uint32_t row, uint32_t col)
This returns an Ipv6 address at the node specified by the (row, col) address.
Build a set of PointToPointNetDevice objects.
Ptr< Node > GetNode(uint32_t row, uint32_t col)
stack
Definition: first.py:34
std::vector< NodeContainer > m_nodes
all the nodes in the grid
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
PointToPointGridHelper(uint32_t nRows, uint32_t nCols, PointToPointHelper pointToPoint)
Create a PointToPointGridHelper in order to easily create grid topologies using p2p links...
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
pointToPoint
Definition: first.py:28
holds a vector of ns3::NetDevice pointers
std::vector< Ipv6InterfaceContainer > m_rowInterfaces6
IPv6 interfaces in a row.
std::vector< Ipv4InterfaceContainer > m_rowInterfaces
IPv4 interfaces in a row.
std::vector< NetDeviceContainer > m_rowDevices
NetDevices in a row.
Ipv4Address GetIpv4Address(uint32_t row, uint32_t col)
This returns an Ipv4 address at the node specified by the (row, col) address.
uint32_t GetN(void) const
Get the number of Ptr<Node> stored in this container.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
std::vector< Ipv6InterfaceContainer > m_colInterfaces6
IPv6 interfaces in a column.
Helper class to auto-assign global IPv6 unicast addresses.
static Ipv6Address NextNetwork(const Ipv6Prefix prefix)
Get the next network according to the given Ipv6Prefix.
static void Init(const Ipv6Address net, const Ipv6Prefix prefix, const Ipv6Address interfaceId="::1")
Initialise the base network and interfaceId for the generator.
Describes an IPv6 address.
Definition: ipv6-address.h:49
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
uint32_t GetN(void) const
Get the number of Ptr<NetDevice> stored in this container.
void InstallStack(InternetStackHelper stack)
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Describes an IPv6 prefix.
Definition: ipv6-address.h:428
Ipv4Address NewNetwork(void)
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
uint32_t m_ySize
Y size of the grid (number of rows)
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
std::vector< Ipv4InterfaceContainer > m_colInterfaces
IPv4 interfaces in a column.
void BoundingBox(double ulx, double uly, double lrx, double lry)
Sets up the node canvas locations for every node in the grid.
static Ipv6Address GetNetwork(const Ipv6Prefix prefix)
Get the current network of the given Ipv6Prefix.