A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 #include "ns3/point-to-point-grid.h"
20 #include "ns3/internet-stack-helper.h"
21 #include "ns3/point-to-point-helper.h"
22 #include "ns3/constant-position-mobility-model.h"
23 #include "ns3/string.h"
24 #include "ns3/vector.h"
25 #include "ns3/log.h"
26 #include "ns3/ipv6-address-generator.h"
27 
28 NS_LOG_COMPONENT_DEFINE ("PointToPointGridHelper");
29 
30 namespace ns3 {
31 
33  uint32_t nCols,
34  PointToPointHelper pointToPoint)
35  : m_xSize (nCols), m_ySize (nRows)
36 {
37  // Bounds check
38  if (m_xSize < 1 || m_ySize < 1 || (m_xSize < 2 && m_ySize < 2))
39  {
40  NS_FATAL_ERROR ("Need more nodes for grid.");
41  }
42 
43  InternetStackHelper stack;
44 
45  for (uint32_t y = 0; y < nRows; ++y)
46  {
47  NodeContainer rowNodes;
48  NetDeviceContainer rowDevices;
49  NetDeviceContainer colDevices;
50 
51  for (uint32_t x = 0; x < nCols; ++x)
52  {
53  rowNodes.Create (1);
54 
55  // install p2p links across the row
56  if (x > 0)
57  {
58  rowDevices.Add (pointToPoint.
59  Install (rowNodes.Get (x-1), rowNodes.Get (x)));
60  }
61 
62  // install vertical p2p links
63  if (y > 0)
64  {
65  colDevices.Add (pointToPoint.
66  Install ((m_nodes.at (y-1)).Get (x), rowNodes.Get (x)));
67  }
68  }
69 
70  m_nodes.push_back (rowNodes);
71  m_rowDevices.push_back (rowDevices);
72 
73  if (y > 0)
74  m_colDevices.push_back (colDevices);
75  }
76 }
77 
79 {
80 }
81 
82 void
84 {
85  for (uint32_t i = 0; i < m_nodes.size (); ++i)
86  {
87  NodeContainer rowNodes = m_nodes[i];
88  for (uint32_t j = 0; j < rowNodes.GetN (); ++j)
89  {
90  stack.Install (rowNodes.Get (j));
91  }
92  }
93 }
94 
95 void
97 {
98  // Assign addresses to all row devices in the grid.
99  // These devices are stored in a vector. Each row
100  // of the grid has all the row devices in one entry
101  // of the vector. These entries come in pairs.
102  for (uint32_t i = 0; i < m_rowDevices.size (); ++i)
103  {
104  Ipv4InterfaceContainer rowInterfaces;
105  NetDeviceContainer rowContainer = m_rowDevices[i];
106  for (uint32_t j = 0; j < rowContainer.GetN (); j+=2)
107  {
108  rowInterfaces.Add (rowIp.Assign (rowContainer.Get (j)));
109  rowInterfaces.Add (rowIp.Assign (rowContainer.Get (j+1)));
110  rowIp.NewNetwork ();
111  }
112  m_rowInterfaces.push_back (rowInterfaces);
113  }
114 
115  // Assign addresses to all col devices in the grid.
116  // These devices are stored in a vector. Each col
117  // of the grid has all the col devices in one entry
118  // of the vector. These entries come in pairs.
119  for (uint32_t i = 0; i < m_colDevices.size (); ++i)
120  {
121  Ipv4InterfaceContainer colInterfaces;
122  NetDeviceContainer colContainer = m_colDevices[i];
123  for (uint32_t j = 0; j < colContainer.GetN (); j+=2)
124  {
125  colInterfaces.Add (colIp.Assign (colContainer.Get (j)));
126  colInterfaces.Add (colIp.Assign (colContainer.Get (j+1)));
127  colIp.NewNetwork ();
128  }
129  m_colInterfaces.push_back (colInterfaces);
130  }
131 }
132 
133 void
135 {
136  Ipv6AddressGenerator::Init(addrBase, prefix);
137  Ipv6Address v6network;
138  Ipv6AddressHelper addrHelper;
139 
140  // Assign addresses to all row devices in the grid.
141  // These devices are stored in a vector. Each row
142  // of the grid has all the row devices in one entry
143  // of the vector. These entries come in pairs.
144  for (uint32_t i = 0; i < m_rowDevices.size (); ++i)
145  {
146  Ipv6InterfaceContainer rowInterfaces;
147  NetDeviceContainer rowContainer = m_rowDevices[i];
148  for (uint32_t j = 0; j < rowContainer.GetN (); j+=2)
149  {
150  v6network = Ipv6AddressGenerator::GetNetwork (prefix);
151  addrHelper.SetBase(v6network, prefix);
152  Ipv6InterfaceContainer ic = addrHelper.Assign (rowContainer.Get (j));
153  rowInterfaces.Add (ic);
154  ic = addrHelper.Assign (rowContainer.Get (j+1));
155  rowInterfaces.Add (ic);
157  }
158  m_rowInterfaces6.push_back (rowInterfaces);
159  }
160 
161  // Assign addresses to all col devices in the grid.
162  // These devices are stored in a vector. Each col
163  // of the grid has all the col devices in one entry
164  // of the vector. These entries come in pairs.
165  for (uint32_t i = 0; i < m_colDevices.size (); ++i)
166  {
167  Ipv6InterfaceContainer colInterfaces;
168  NetDeviceContainer colContainer = m_colDevices[i];
169  for (uint32_t j = 0; j < colContainer.GetN (); j+=2)
170  {
171  v6network = Ipv6AddressGenerator::GetNetwork (prefix);
172  addrHelper.SetBase(v6network, prefix);
173  Ipv6InterfaceContainer ic = addrHelper.Assign (colContainer.Get (j));
174  colInterfaces.Add (ic);
175  ic = addrHelper.Assign (colContainer.Get (j+1));
176  colInterfaces.Add (ic);
178  }
179  m_colInterfaces6.push_back (colInterfaces);
180  }
181 }
182 
183 void
184 PointToPointGridHelper::BoundingBox (double ulx, double uly,
185  double lrx, double lry)
186 {
187  double xDist;
188  double yDist;
189  if (lrx > ulx)
190  {
191  xDist = lrx - ulx;
192  }
193  else
194  {
195  xDist = ulx - lrx;
196  }
197  if (lry > uly)
198  {
199  yDist = lry - uly;
200  }
201  else
202  {
203  yDist = uly - lry;
204  }
205  double xAdder = xDist / m_xSize;
206  double yAdder = yDist / m_ySize;
207  double yLoc = yDist / 2;
208  for (uint32_t i = 0; i < m_ySize; ++i)
209  {
210  double xLoc = xDist / 2;
211  for (uint32_t j = 0; j < m_xSize; ++j)
212  {
213  Ptr<Node> node = GetNode (i, j);
215  if (loc ==0)
216  {
217  loc = CreateObject<ConstantPositionMobilityModel> ();
218  node->AggregateObject (loc);
219  }
220  Vector locVec (xLoc, yLoc, 0);
221  loc->SetPosition (locVec);
222 
223  xLoc += xAdder;
224  }
225  yLoc += yAdder;
226  }
227 }
228 
229 Ptr<Node>
230 PointToPointGridHelper::GetNode (uint32_t row, uint32_t col)
231 {
232  if (row > m_nodes.size () - 1 ||
233  col > m_nodes.at (row).GetN () - 1)
234  {
235  NS_FATAL_ERROR ("Index out of bounds in PointToPointGridHelper::GetNode.");
236  }
237 
238  return (m_nodes.at (row)).Get (col);
239 }
240 
242 PointToPointGridHelper::GetIpv4Address (uint32_t row, uint32_t col)
243 {
244  if (row > m_nodes.size () - 1 ||
245  col > m_nodes.at (row).GetN () - 1)
246  {
247  NS_FATAL_ERROR ("Index out of bounds in PointToPointGridHelper::GetIpv4Address.");
248  }
249 
250  // Right now this just gets one of the addresses of the
251  // specified node. The exact device can't be specified.
252  // If you picture the grid, the address returned is the
253  // address of the left (row) device of all nodes, with
254  // the exception of the left-most nodes in the grid;
255  // in which case the right (row) device address is
256  // returned
257  if (col == 0)
258  {
259  return (m_rowInterfaces.at (row)).GetAddress (0);
260  }
261  else
262  {
263  return (m_rowInterfaces.at (row)).GetAddress ((2*col)-1);
264  }
265 }
266 
268 PointToPointGridHelper::GetIpv6Address (uint32_t row, uint32_t col)
269 {
270  if (row > m_nodes.size () - 1 ||
271  col > m_nodes.at (row).GetN () - 1)
272  {
273  NS_FATAL_ERROR ("Index out of bounds in PointToPointGridHelper::GetIpv4Address.");
274  }
275 
276  // Right now this just gets one of the addresses of the
277  // specified node. The exact device can't be specified.
278  // If you picture the grid, the address returned is the
279  // address of the left (row) device of all nodes, with
280  // the exception of the left-most nodes in the grid;
281  // in which case the right (row) device address is
282  // returned
283  if (col == 0)
284  {
285  return (m_rowInterfaces6.at (row)).GetAddress (0, 1);
286  }
287  else
288  {
289  return (m_rowInterfaces6.at (row)).GetAddress ((2*col)-1, 1);
290  }
291 }
292 
293 } // namespace ns3