View | Details | Raw Unified | Return to bug 1118
Collapse All | Expand All

(-)715c53e80576 (+153 lines)
Added Link Here 
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
18
#include "ns3/core-module.h"
19
#include "ns3/applications-module.h"
20
#include "ns3/netanim-module.h"
21
#include "ns3/network-module.h"
22
#include "ns3/point-to-point-layout-module.h"
23
#include "ns3/internet-module.h"
24
25
/*
26
           Network topology (default)
27
28
                     R                      (level=0, fan out=3) 
29
        /-----------------------\
30
       /             |           \
31
      /              |            \
32
     n1              n2           n3       (level=1, fan out=3)
33
   / | \           / | \         / | \
34
 n11 n12 n13    n21 n22 n23   n31 n32 n33 (level=2)
35
36
*/
37
38
39
using namespace ns3;
40
41
NS_LOG_COMPONENT_DEFINE ("TreeAnimation");
42
43
int 
44
main (int argc, char *argv[])
45
{
46
47
  //
48
  // Defaults
49
  //
50
  uint32_t nLevels = 3;    // Number of levels in the tree
51
  uint32_t fan_out = 3;    // Number of fan out nodes
52
  std::string animFile = "tree.tr"; // Animation file
53
  uint32_t animPort = 0;   // Animation port
54
  int random_fan_out = -1; // Used if random number of fan out 
55
    // or branches are to be created at each level
56
57
  CommandLine cmd;
58
  cmd.AddValue ("nLevels", "Number of levels in the tree", nLevels);
59
  cmd.AddValue ("fan_out", "Fan out", fan_out);
60
  cmd.AddValue ("animPort", "Port Number for Remote Animation", animPort);
61
  cmd.AddValue ("animFile", "File Name for Animation Output", animFile);
62
  cmd.AddValue ("random_fan_out", "Try a random fan out, usage :--random_fan_out=1, \
63
    fan_out will be used for the upper bound", random_fan_out);
64
65
  cmd.Parse (argc, argv);
66
67
  NS_LOG_INFO ("Build tree topology.");
68
  PointToPointHelper pointToPoint;
69
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
70
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
71
  
72
  Ptr <PointToPointTreeHelper> tree ;
73
  if (random_fan_out == -1)
74
    tree = CreateObject<PointToPointTreeHelper> (nLevels,fan_out,pointToPoint);
75
  else
76
    tree = CreateObject<PointToPointTreeHelper> (nLevels,UniformVariable(0,fan_out),pointToPoint);
77
78
79
  NS_LOG_INFO ("Install internet stack on all nodes.");
80
  InternetStackHelper internet;
81
  tree->InstallStack (internet);
82
83
  NS_LOG_INFO ("Assign IP Addresses.");
84
  tree->AssignIpv4AddressesHierarchical (Ipv4Address ("10.0.0.0"), Ipv4Mask ("255.0.0.0"));
85
86
  NS_LOG_INFO ("Create applications.");
87
  
88
  // Create a packet sink on the last leaf node to receive packets.  
89
   
90
  uint16_t port = 50000;
91
  uint32_t last_leaf_node = (tree->GetLeaves ().GetN ())-1;
92
93
  // Sink related configurations
94
  Address last_leaf_node_address (InetSocketAddress (Ipv4Address::GetAny (), port));
95
  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", last_leaf_node_address);
96
  ApplicationContainer sink_app = packetSinkHelper.Install (tree->GetLeaves ().Get (last_leaf_node));
97
  sink_app.Start (Seconds (1.0));
98
  sink_app.Stop (Seconds (10.0));
99
100
  //
101
  // Create OnOff applications to send TCP to the sink node from each leaf node except 
102
  // last leaf node
103
  //
104
  OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
105
  onOffHelper.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
106
  onOffHelper.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
107
108
  // Create apps on leaves except for last leaf node
109
  ApplicationContainer leafApps;
110
  for (uint32_t i = 0; i < tree->GetLeaves ().GetN (); i++)
111
    {
112
      AddressValue remoteAddress (InetSocketAddress (tree->GetLeafIpv4Address(last_leaf_node), port));
113
      onOffHelper.SetAttribute ("Remote", remoteAddress);
114
      leafApps.Add (onOffHelper.Install (tree->GetLeaves ().Get (i)));
115
      leafApps.Start (Seconds (1.0));
116
      leafApps.Stop (Seconds (10.0));
117
    }
118
  
119
  // Let us also add an application on level 1 node 2
120
  ApplicationContainer level1node2App;
121
  level1node2App.Add (onOffHelper.Install (tree->GetNode (1,2)));
122
  level1node2App.Start (Seconds (1.0));
123
  level1node2App.Stop (Seconds (10.0));
124
125
  NS_LOG_INFO ("Enable static global routing.");
126
  //
127
  // Turn on global static routing so we can actually be routed across the star.
128
  //
129
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
130
131
  // Set the bounding box for animation
132
  tree->BoundingBox (1, 1, 100, 100);
133
  
134
  // Create the animation object and configure for specified output
135
  AnimationInterface anim;
136
  if (animPort > 0)
137
    {
138
      anim.SetServerPort (animPort);
139
    }
140
  else if (!animFile.empty ())
141
    {
142
      anim.SetOutputFile (animFile);
143
    }
144
  anim.StartAnimation ();
145
146
  NS_LOG_INFO ("Run Simulation.");
147
  Simulator::Run ();
148
  Simulator::Destroy ();
149
  NS_LOG_INFO ("Done.");
150
151
  return 0;
152
}
153
(-)a/src/netanim/examples/wscript (+4 lines)
 Lines 12-14    Link Here 
12
    obj = bld.create_ns3_program('star-animation',
12
    obj = bld.create_ns3_program('star-animation',
13
                                 ['netanim', 'applications', 'point-to-point-layout'])
13
                                 ['netanim', 'applications', 'point-to-point-layout'])
14
    obj.source = 'star-animation.cc'
14
    obj.source = 'star-animation.cc'
15
    
16
    obj = bld.create_ns3_program('tree-animation',
17
                                 ['netanim', 'applications', 'point-to-point-layout'])
18
    obj.source = 'tree-animation.cc'
(-)a/src/point-to-point-layout/model/point-to-point-star.cc (-4 / +46 lines)
 Lines 18-34    Link Here 
18
#include <sstream>
18
#include <sstream>
19
19
20
// ns3 includes
20
// ns3 includes
21
#include "ns3/point-to-point-helper.h"
21
#include "ns3/animation-interface.h"
22
#include "ns3/animation-interface.h"
22
#include "ns3/point-to-point-star.h"
23
#include "ns3/point-to-point-star.h"
23
#include "ns3/constant-position-mobility-model.h"
24
#include "ns3/constant-position-mobility-model.h"
24
25
#include "ns3/node-list.h"
25
#include "ns3/node-list.h"
26
#include "ns3/point-to-point-net-device.h"
26
#include "ns3/point-to-point-net-device.h"
27
#include "ns3/vector.h"
27
#include "ns3/vector.h"
28
28
29
namespace ns3 {
30
29
NS_LOG_COMPONENT_DEFINE("PointToPointStarHelper");
31
NS_LOG_COMPONENT_DEFINE("PointToPointStarHelper");
32
NS_OBJECT_ENSURE_REGISTERED (PointToPointStarHelper);
30
33
31
namespace ns3 {
34
TypeId 
35
PointToPointStarHelper::GetTypeId (void)
36
{
37
  static TypeId tid = TypeId ("ns3::PointToPointStarHelper")
38
    .SetParent<Object> ()
39
    .AddConstructor<PointToPointStarHelper> ()
40
    ;
41
  return tid;
42
}
32
  
43
  
33
PointToPointStarHelper::PointToPointStarHelper (uint32_t numSpokes,
44
PointToPointStarHelper::PointToPointStarHelper (uint32_t numSpokes,
34
                   PointToPointHelper p2pHelper)
45
                   PointToPointHelper p2pHelper)
 Lines 44-51    Link Here 
44
    }
55
    }
45
}
56
}
46
57
58
PointToPointStarHelper::PointToPointStarHelper (ns3::NodeContainer hub,uint32_t numSpokes,PointToPointHelper p2pHelper):m_hub(hub)
59
{
60
  m_spokes.Create (numSpokes);
61
  for (uint32_t i = 0; i < m_spokes.GetN (); ++i)
62
    {
63
      NetDeviceContainer nd = p2pHelper.Install (m_hub.Get (0), m_spokes.Get (i));
64
      m_hubDevices.Add (nd.Get (0));
65
      m_spokeDevices.Add (nd.Get (1));
66
    }
67
}
68
69
PointToPointStarHelper::PointToPointStarHelper ()
70
{
71
}
72
47
PointToPointStarHelper::~PointToPointStarHelper ()
73
PointToPointStarHelper::~PointToPointStarHelper ()
48
{}
74
{
75
}
49
76
50
Ptr<Node> 
77
Ptr<Node> 
51
PointToPointStarHelper::GetHub () const
78
PointToPointStarHelper::GetHub () const
 Lines 59-64    Link Here 
59
  return m_spokes.Get (i);
86
  return m_spokes.Get (i);
60
}
87
}
61
88
89
NodeContainer
90
PointToPointStarHelper::GetSpokeNodes () const
91
{
92
  return m_spokes;
93
}
94
62
Ipv4Address 
95
Ipv4Address 
63
PointToPointStarHelper::GetHubIpv4Address (uint32_t i) const
96
PointToPointStarHelper::GetHubIpv4Address (uint32_t i) const
64
{
97
{
 Lines 95-100    Link Here 
95
    }
128
    }
96
}
129
}
97
130
131
void
132
PointToPointStarHelper::AssignIpv4AddressForSingleSpoke (Ipv4AddressHelper address, uint32_t spoke_id)
133
{
134
  NS_ASSERT (spoke_id < SpokeCount ());
135
  m_hubInterfaces.Add (address.Assign (m_hubDevices.Get (spoke_id)));
136
  m_spokeInterfaces.Add (address.Assign (m_spokeDevices.Get (spoke_id)));
137
}
138
98
void 
139
void 
99
PointToPointStarHelper::BoundingBox (double ulx, double uly,
140
PointToPointStarHelper::BoundingBox (double ulx, double uly,
100
                                     double lrx, double lry)
141
                                     double lrx, double lry)
 Lines 152-159    Link Here 
152
        Vector spokeVec (hubVec.x + cos (theta*i) * spokeDist, 
193
        Vector spokeVec (hubVec.x + cos (theta*i) * spokeDist, 
153
                         hubVec.y + sin (theta*i) * spokeDist,
194
                         hubVec.y + sin (theta*i) * spokeDist,
154
                         0);
195
                         0);
155
        spokeLoc->SetPosition (spokeVec);
196
		spokeLoc->SetPosition (spokeVec);
156
    }
197
    }
157
}
198
}
158
199
159
} // namespace ns3
200
} // namespace ns3
201
(-)a/src/point-to-point-layout/model/point-to-point-star.h (-10 / +58 lines)
 Lines 20-30    Link Here 
20
#define POINT_TO_POINT_STAR_HELPER_H
20
#define POINT_TO_POINT_STAR_HELPER_H
21
21
22
#include <string>
22
#include <string>
23
23
#include "ns3/point-to-point-helper.h"
24
#include "point-to-point-helper.h"
24
#include "ns3/ipv4-address-helper.h"
25
#include "ipv4-address-helper.h"
25
#include "ns3/internet-stack-helper.h"
26
#include "internet-stack-helper.h"
26
#include "ns3/ipv4-interface-container.h"
27
#include "ipv4-interface-container.h"
27
#include "ns3/object.h"
28
#include "ns3/ptr.h"
28
29
29
namespace ns3 {
30
namespace ns3 {
30
  
31
  
 Lines 32-38    Link Here 
32
 * \brief A helper to make it easier to create a star topology
33
 * \brief A helper to make it easier to create a star topology
33
 * with PointToPoint links
34
 * with PointToPoint links
34
 */
35
 */
35
class PointToPointStarHelper
36
class PointToPointStarHelper : public Object
36
{
37
{
37
public:
38
public:
38
  /**
39
  /**
 Lines 48-55    Link Here 
48
   */
49
   */
49
  PointToPointStarHelper (uint32_t numSpokes, 
50
  PointToPointStarHelper (uint32_t numSpokes, 
50
                          PointToPointHelper p2pHelper);
51
                          PointToPointHelper p2pHelper);
52
  /**
53
   * Create a PointToPointStarHelper in order to easily create
54
   * star topologies using p2p links.This constructor takes an existent node "hub", 
55
   * creates the specified number of spoke nodes (numSpokes) around it and installs
56
   * point-to-point links between the hub and the spoke nodes
57
   *
58
   * \param hub the NodeContainer containing the already-created hub Node
59
   *
60
   * \param numSpokes the number of links attached to 
61
   *        the hub node, creating a total of 
62
   *        numSpokes + 1 nodes
63
   *
64
   * \param p2pHelper the link helper for p2p links, 
65
   *        used to link nodes together
66
   */
67
  PointToPointStarHelper (NodeContainer hub,uint32_t numSpokes,PointToPointHelper p2pHelper);
51
68
52
  ~PointToPointStarHelper ();
69
  /**
70
   * Create a PointToPointStarHelper (Empty constructor)
71
   */
72
  PointToPointStarHelper ();
73
74
  virtual ~PointToPointStarHelper ();
53
75
54
public:
76
public:
55
  /**
77
  /**
 Lines 66-81    Link Here 
66
  Ptr<Node> GetSpokeNode (uint32_t i) const;
88
  Ptr<Node> GetSpokeNode (uint32_t i) const;
67
89
68
  /**
90
  /**
91
   * \returns a node Container containing the set of spoke nodes
92
   */
93
  NodeContainer GetSpokeNodes() const;
94
  /**
69
   * \param i index into the hub interfaces
95
   * \param i index into the hub interfaces
70
   *
96
   *
71
   * \returns Ipv4Address according to indexed hub interface
97
   * \returns Ipv4Address according to indexed hub Interface
72
   */
98
   */
73
  Ipv4Address GetHubIpv4Address (uint32_t i) const;
99
  Ipv4Address GetHubIpv4Address (uint32_t i) const;
74
100
75
  /**
101
  /**
76
   * \param i index into the spoke interfaces
102
   * \param i index into the spoke interfaces
77
   *
103
   *
78
   * \returns Ipv4Address according to indexed spoke interface
104
   * \returns Ipv4Address according to indexed spoke Interface
79
   */
105
   */
80
  Ipv4Address GetSpokeIpv4Address (uint32_t i) const;
106
  Ipv4Address GetSpokeIpv4Address (uint32_t i) const;
81
107
 Lines 91-96    Link Here 
91
  void InstallStack (InternetStackHelper stack);
117
  void InstallStack (InternetStackHelper stack);
92
118
93
  /**
119
  /**
120
   * Assigns Ipv4 addresses for the interfaces between the hub and 
121
   * all spoke nodes
94
   * \param address an Ipv4AddressHelper which is used to install 
122
   * \param address an Ipv4AddressHelper which is used to install 
95
   *                Ipv4 addresses on all the node interfaces in 
123
   *                Ipv4 addresses on all the node interfaces in 
96
   *                the star
124
   *                the star
 Lines 98-105    Link Here 
98
  void AssignIpv4Addresses (Ipv4AddressHelper address);
126
  void AssignIpv4Addresses (Ipv4AddressHelper address);
99
127
100
  /**
128
  /**
129
   * Assigns Ipv4 addresses for the interfaces between the hub and
130
   * a give spoke node
131
   *
132
   * \param address an Ipv4AddressHelper which is used to install 
133
   *                Ipv4 addresses on all the node interfaces in 
134
   *                the star
135
   *
136
   * \param spoke_id Id of the spoke node. Spoke nodes are zero-indexed
137
   *
138
   */
139
  void AssignIpv4AddressForSingleSpoke (Ipv4AddressHelper address, uint32_t spoke_id);
140
141
  /**
101
   * Sets up the node canvas locations for every node in the star. 
142
   * Sets up the node canvas locations for every node in the star. 
102
   * This is needed for use with the animation interface
143
   * This is needed for use with the animation Interface
103
   *
144
   *
104
   * \param ulx upper left x value
145
   * \param ulx upper left x value
105
   * \param uly upper left y value
146
   * \param uly upper left y value
 Lines 108-113    Link Here 
108
   */
149
   */
109
  void BoundingBox (double ulx, double uly, double lrx, double lry);
150
  void BoundingBox (double ulx, double uly, double lrx, double lry);
110
151
152
   /**
153
   * \brief Get the type ID.
154
   * \return type ID
155
   */
156
  static TypeId GetTypeId ();
157
111
private:
158
private:
112
  NodeContainer m_hub;
159
  NodeContainer m_hub;
113
  NetDeviceContainer m_hubDevices;
160
  NetDeviceContainer m_hubDevices;
 Lines 120-122    Link Here 
120
} // namespace ns3
167
} // namespace ns3
121
168
122
#endif /* POINT_TO_POINT_STAR_HELPER_H */
169
#endif /* POINT_TO_POINT_STAR_HELPER_H */
170
(-)715c53e80576 (+373 lines)
Added Link Here 
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
18
#include "ns3/point-to-point-tree.h"
19
#include "ns3/ptr.h"
20
#include "ns3/point-to-point-star.h"
21
#include "ns3/node.h"
22
#include "ns3/node-container.h"
23
#include "ns3/log.h"
24
#include "ns3/constant-position-mobility-model.h"
25
#include <cmath>
26
27
namespace ns3 {
28
29
NS_LOG_COMPONENT_DEFINE("PointToPointTreeHelper");
30
NS_OBJECT_ENSURE_REGISTERED (PointToPointTreeHelper);
31
32
TypeId 
33
PointToPointTreeHelper::GetTypeId (void)
34
{
35
  static TypeId tid = TypeId ("ns3::PointToPointTreeHelper")
36
    .SetParent<Object> ()
37
    .AddConstructor<PointToPointTreeHelper> ()
38
    ;
39
  return tid;
40
}
41
  
42
43
44
PointToPointTreeHelper::PointToPointTreeHelper (
45
  uint32_t num_levels,
46
  uint32_t fan_out,
47
  PointToPointHelper p2pHelper
48
  ) :m_num_levels (num_levels),m_fan_out (ConstantVariable (fan_out))
49
{
50
  CreateTopologyHelper (num_levels, p2pHelper);
51
}
52
53
PointToPointTreeHelper::PointToPointTreeHelper(
54
  uint32_t num_levels,
55
  RandomVariable rv,
56
  PointToPointHelper p2pHelper
57
  ) :m_num_levels (num_levels),m_fan_out (rv)
58
{
59
  CreateTopologyHelper (num_levels, p2pHelper);
60
}
61
62
PointToPointTreeHelper::PointToPointTreeHelper ()
63
{
64
}
65
66
void 
67
PointToPointTreeHelper::CreateTopologyHelper (uint32_t num_levels, PointToPointHelper p2pHelper)
68
{
69
  if (num_levels <= 0)
70
    {
71
      NS_LOG_WARN ("Number of levels:"<<num_levels<<"<=0\nNo Topology created");
72
    }
73
  
74
  m_root_node.Create (1);
75
  NS_LOG_INFO ("Root Node created");
76
77
  for (uint32_t i = 0 ; i < num_levels ; i++)
78
    {
79
      PerLevelNodeContainer.push_back (NodeContainer());
80
    }
81
  PerLevelNodeContainer[0] = m_root_node; //level 0 container has root node
82
  NS_LOG_INFO ("Begin creating levels starting from level 1");
83
  AddStarTopologyRecursively (m_root_node,1,p2pHelper); //Start from level 1, as root node is at level 0
84
85
}
86
87
void 
88
PointToPointTreeHelper::AddStarTopologyRecursively (NodeContainer parent_node, uint32_t level, PointToPointHelper p2pHelper)
89
{
90
  uint32_t fan_out = m_fan_out.GetInteger ();
91
  Ptr<PointToPointStarHelper> star_helper ;
92
  if (level >= this->m_num_levels)
93
    {
94
      NS_LOG_INFO ("Begin creating levels starting from level 1");
95
      return;
96
    }
97
98
  star_helper =  parent_node.Get (0)->GetObject<PointToPointStarHelper> ();
99
  if (star_helper == 0)
100
    {
101
      star_helper = CreateObject<PointToPointStarHelper> (parent_node,fan_out,p2pHelper);
102
	  PerLevelNodeContainer[level].Add (star_helper->GetSpokeNodes());
103
      parent_node.Get (0)->AggregateObject (star_helper);
104
      NS_LOG_INFO ("After aggregating star to parent node Id:"<<parent_node.Get(0)->GetId()<<" with fan out="<<fan_out);
105
    }
106
  else 
107
    {
108
	  NS_FATAL_ERROR ("Node Id:"<< parent_node.Get (0)->GetId ()<<" Already contains a star set");
109
    }
110
  level++;
111
  for (uint32_t i = 0 ; i < fan_out ; i++) 
112
    {
113
	  NS_LOG_INFO ("Before star for parent node Id:"<<parent_node.Get(0)->GetId()<<" with fan out="<<fan_out);
114
	  AddStarTopologyRecursively (star_helper->GetSpokeNode (i),level,p2pHelper);
115
    }
116
}
117
118
PointToPointTreeHelper::~PointToPointTreeHelper ()
119
{
120
}
121
122
Ptr<Node> 
123
PointToPointTreeHelper::GetNode (uint32_t level, uint32_t index) 
124
{
125
  if (level >= m_num_levels)
126
    {
127
      NS_LOG_WARN ("Requested level:"<<level<<" Does not exist");
128
	  return NULL;
129
    }
130
  if (index >= PerLevelNodeContainer[level].GetN ())
131
    {
132
      NS_LOG_WARN ("Requested index:"<<index<<" Does not exist");
133
	  return NULL;
134
    }
135
  return PerLevelNodeContainer[level].Get (index);
136
}
137
138
 
139
NodeContainer 
140
PointToPointTreeHelper::GetLeaves ()
141
{
142
  return PerLevelNodeContainer[m_num_levels-1];
143
}
144
145
void 
146
PointToPointTreeHelper::InstallStack (InternetStackHelper stack)
147
{
148
  for (uint32_t i = 0; i < m_num_levels ; i++)
149
    {
150
	  stack.Install(PerLevelNodeContainer[i]);
151
    }
152
}
153
154
155
Ipv4Address 
156
PointToPointTreeHelper::GetLeafIpv4Address (uint32_t leaf_index)
157
{
158
  //Get hub(parent) of the leaf node
159
  //Get The node container for penultimate level
160
  //The penultimate levels carries the leaves
161
  NodeContainer nc = PerLevelNodeContainer [m_num_levels-2]; 
162
  Ptr<Node> n = NULL ;
163
  Ptr<PointToPointStarHelper> star_helper = NULL;
164
165
  // Go through each node on the penultimate level
166
  // Obtained the star topology aggregated with each node
167
  // Check if the leaf index falls within the spoke count of each star
168
  for (uint32_t i = 0 ; i < nc.GetN ();i++)
169
    {
170
	  n = nc.Get (i);
171
	  NS_ASSERT (n);
172
	  star_helper = n->GetObject<PointToPointStarHelper> ();
173
      if (star_helper == 0)
174
	    {
175
	      NS_LOG_WARN ("This tree is perhaps has only one node.");
176
		  // This can happen due to a bug, or if only one node exists 
177
		  // in tree (not really a tree in that case!!)
178
		  // This can also happen if the user desired random fan out
179
		  // which chose '0' as a fan out
180
	    }
181
	  else
182
	    {
183
	      // The leaf index does not belong to this star
184
		  if (leaf_index > star_helper->SpokeCount ()-1)
185
		    {
186
		      n = NULL ;
187
			  leaf_index -= star_helper->SpokeCount ();
188
			  continue;
189
		    }
190
		  else
191
		    {
192
              // Found the star
193
		      break;
194
		    }
195
	    }
196
	} //for loop
197
	NS_ASSERT (n);
198
	NS_ASSERT (star_helper);
199
200
	// Found the desired star
201
	return star_helper->GetSpokeIpv4Address (leaf_index);
202
}
203
204
// A helper function that extends a massk, based on the number of subnets required
205
// Examples: given a mask such as /8, if 3 subnets are required, the mask has to be
206
// extended to /10
207
208
Ipv4Mask
209
PointToPointTreeHelper::ExtendMaskForSubnets (Ipv4Mask original_mask, uint32_t subnets_required)
210
{
211
  // If the original mask is already /31 and over we cannot allocate any more subnets
212
  if (subnets_required)
213
    NS_ASSERT (original_mask.GetPrefixLength () <=30);
214
215
  uint8_t num_additional_subnet_bits = 0;//number of additional subnet bits required
216
  for (uint32_t i = 0 ; i < 31 ; i++) //30 is maximum subnets
217
	{
218
	   if( pow (2.0,static_cast<int> (i)) >= subnets_required)
219
	     {
220
		   num_additional_subnet_bits = i ;
221
	       break;
222
	     }
223
	}
224
  uint32_t tmp = 0x10000000 ;
225
  tmp >>= (original_mask.GetPrefixLength () - 1); //Position 1 at the last "1" bit in the mask
226
  uint32_t new_mask = original_mask.Get (); // Initialize new mask
227
  for (uint8_t i = 0 ; i < num_additional_subnet_bits ; i++)
228
	{
229
	  tmp >>= i ;  // Move the 1 right for each additional subnet bit
230
	  new_mask |= tmp; // Add the 1 to the original mask
231
	}
232
  return Ipv4Mask (new_mask);
233
}
234
235
/* 
236
  The function starts with an allocated network address N and mask M
237
  The mask M is extended to allocate X subnets. where X is the 
238
  number of fan out nodes/spokes nodes associated with a node
239
  Each allocated subnet above is further divided into 2 subnets A and B
240
  subnet A is used at the network between the hub and any spoke node
241
  subnet B will be the new network which is used by the spoke nodes 
242
  as part of recursion
243
  For example:
244
  10.0.0.0/8 is allocated for the tree
245
  The Root Node [R] has 4 point-to-point links to 4 spoke nodes
246
  10.0.0.0/8 is divided into 4 subnets
247
  10.0.0.0/10, 10.64.0.0/10, 10.128.0.0/10, 10.192.0.0/10
248
   
249
  10.0.0.0/10 is divided into 2 subnets
250
  10.0.0.0/11 and 10.32.0.0/11
251
 
252
253
                        o(10.32.0.1/11)
254
                       [N1]
255
		        o(10.0.0.2/11)
256
            [N3]      /
257
               \     /
258
                \   /
259
		 o o(10.0.0.1/11)
260
   (10.0.0.0/8)  [R]
261
                 o o(10.64.0.1/11)
262
	        /    \
263
	       /      \
264
	    [N4]       \
265
   	                \
266
	                 o(10.64.0.2/11)
267
			 [N2]
268
			 o(10.96.0.1/11)
269
  
270
*/
271
272
void 
273
PointToPointTreeHelper::AssignIpv4AddressHierarchicalRecursiveHelper (Ptr<Node> n, Ipv4Address network, Ipv4Mask allocated_mask)
274
{
275
  Ptr<PointToPointStarHelper> star_helper ;
276
  star_helper = n->GetObject<PointToPointStarHelper> ();
277
  if (star_helper == 0)
278
    {
279
	  NS_LOG_INFO ("AssignIpv4AddressRecursiveHelper Node Id:"<<n->GetId ());
280
      return ;
281
    }
282
  uint32_t fan_out = star_helper->SpokeCount ();
283
284
  // Prepare mask and address helper for X subnets
285
  // Where X is the number of fan out nodes
286
  Ipv4Mask outer_mask = ExtendMaskForSubnets (allocated_mask,fan_out);
287
  Ipv4AddressHelper outer_addrhelper (network,outer_mask);
288
289
  //Create Mask for Subnet A and Subnet B (2 subnets)
290
  Ipv4Mask inner_mask = ExtendMaskForSubnets (outer_mask,2);
291
292
  Ipv4Address inner_network = network; //Initialization
293
  for (uint32_t i = 0; i < fan_out; ++i)
294
    {
295
      Ipv4AddressHelper inner_addrhelper (inner_network, inner_mask);
296
 	  star_helper->AssignIpv4AddressForSingleSpoke (inner_addrhelper, i);
297
	  AssignIpv4AddressHierarchicalRecursiveHelper (star_helper->GetSpokeNode (i), inner_addrhelper.NewNetwork (), inner_mask);
298
	  inner_network = outer_addrhelper.NewNetwork ();
299
300
    }
301
}
302
void 
303
PointToPointTreeHelper::AssignIpv4AddressesHierarchical (Ipv4Address networkaddress, Ipv4Mask mask)
304
{
305
  // Assign Ipv4 addresses recursively, starting at level 0
306
  AssignIpv4AddressHierarchicalRecursiveHelper (PerLevelNodeContainer[0].Get (0),networkaddress,mask);
307
}
308
309
void 
310
PointToPointTreeHelper::BoundingBoxRecursiveHelper (double xDist, double inter_level_height,uint32_t current_level)
311
{
312
  double inter_node_x_distance = 0 ;
313
  uint32_t num_node_in_current_level = 0;
314
  NS_LOG_FUNCTION ("Current level:"<<current_level);
315
  if (current_level >= this->m_num_levels)
316
    {
317
	  NS_LOG_INFO ("Recursion complete");
318
      return ;
319
    }	
320
  num_node_in_current_level = PerLevelNodeContainer[current_level].GetN (); // number of nodes in the current level
321
  inter_node_x_distance = (xDist/num_node_in_current_level)/2; //  /2 is required to position the nodes at center of each x block
322
323
  for (uint32_t i = 0 ; i <num_node_in_current_level;  i++)
324
    {
325
	  Ptr<Node> n = PerLevelNodeContainer[current_level].Get (i);
326
	  NS_ASSERT (n);
327
      Ptr<ConstantPositionMobilityModel> loc = n->GetObject<ConstantPositionMobilityModel> ();
328
      if (loc == 0)
329
        {
330
          loc = CreateObject<ConstantPositionMobilityModel> ();
331
	    }
332
	  else 
333
	    {
334
           NS_LOG_WARN ("ConstantPositionMobilityModel Aggregated object already exists.Will use this object"); 
335
	    }
336
      n->AggregateObject (loc); // visualizers can retrieve the aggregated mobility model from a node to position it
337
      Vector spokeVec (inter_node_x_distance+(i*(inter_node_x_distance*2)), 
338
                         inter_level_height*current_level,
339
                         0);
340
      loc->SetPosition (spokeVec);
341
    }
342
  current_level++;
343
  BoundingBoxRecursiveHelper (xDist,inter_level_height,current_level);
344
}
345
346
void 
347
PointToPointTreeHelper::BoundingBox (double ulx, double uly, double lrx, double lry)
348
{
349
  double yDist;
350
  double xDist;
351
  if (lrx > ulx)
352
    {
353
      xDist = lrx - ulx;
354
    }
355
  else
356
    {
357
      xDist = ulx - lrx;
358
    }
359
  if (lry > uly)
360
    {
361
      yDist = lry - uly;
362
    }
363
  else
364
    {
365
      yDist = uly - lry;
366
    }
367
  double inter_level_height = yDist/(m_num_levels-1) ;
368
  BoundingBoxRecursiveHelper (xDist,inter_level_height,0);
369
370
}
371
372
} // namespace ns3
373
(-)715c53e80576 (+211 lines)
Added Link Here 
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
// Define an object to create a Tree topology.
18
19
/* A tree in this context, can be described as below
20
 * A singular root node is the first node created in the tree
21
 * The root node has X branches (X is also called the fan out number for the
22
 * node)
23
 * A level is a horizontal row of nodes. In this sense the root node occupies
24
 * level 0 and is the only node at this level. The nodes at the tip of the 
25
 * root node's branches are at level 1 and so on
26
 * Each node at a level can have branches of its own
27
 * The last level (i.e the level with no more branches) carries the leaf nodes
28
 *
29
 *                     r                       (level=0,fan out=3)
30
 *                     |
31
 *         -------------------------
32
 *        /            |             \
33
 *       /             |              \
34
 *     n1              n2             n3       (level=1,fan out=3)
35
 *    / | \          / | \           / | \         
36
 * n11 n12 n13     n21 n22 ns23   n31 n32 n33  (level=2,leaf nodes)
37
 */
38
39
40
41
42
#ifndef POINT_TO_POINT_TREE_H
43
#define POINT_TO_POINT_TREE_H
44
45
#include "ns3/point-to-point-star.h"
46
#include "ns3/random-variable.h"
47
#include <vector>
48
49
using namespace std;
50
51
namespace ns3 {
52
53
/**
54
 * \brief A helper to make it easier to create a tree topology
55
 * with PointToPoint links
56
 */
57
58
class PointToPointTreeHelper : public Object
59
{
60
public:
61
62
  /**
63
   * Create a PointToPointTreeHelper in order to easily create
64
   * tree topologies using p2p links
65
   *
66
   * \param num_levels the number of levels in the tree.Root node is level 0
67
   * 
68
   * \param fan_out the number of fan out nodes for each node (i.e number of branches)
69
   *
70
   * \param p2pHelper the link helper for p2p links, 
71
   *        used to link nodes together
72
   */
73
  PointToPointTreeHelper (
74
	uint32_t num_levels,
75
	uint32_t fan_out,
76
	PointToPointHelper p2pHelper);
77
78
  /**
79
   * Create a PointToPointTreeHelper in order to easily create
80
   * tree topologies using p2p links.This constructor needs to be used if the 
81
   * fan out (number of branches) for each node has to be chosen at random
82
   *
83
   * \param num_levels the number of levels in the tree.Root node is level 0
84
   *
85
   * \param rv A random variable that is used to choose 
86
   *        the number of fan out 
87
   *        nodes for each node during tree creation
88
   *
89
   * \param p2pHelper the link helper for p2p links, 
90
   *        used to link nodes together
91
   */
92
  PointToPointTreeHelper (
93
	uint32_t num_levels,
94
	RandomVariable rv,
95
	PointToPointHelper p2pHelper);
96
97
  /**
98
   * PointToPointTreeHelper empty constructor
99
   */
100
  PointToPointTreeHelper ();
101
102
  /**
103
   * PointToPointTreeHelper destructor
104
   */
105
  virtual ~PointToPointTreeHelper ();
106
107
  /**
108
   * Sets up the node canvas locations for every node in the tree. 
109
   * This is needed for use with the animation Interface
110
   *
111
   * \param ulx upper left x value
112
   * \param uly upper left y value
113
   * \param lrx lower right x value
114
   * \param lry lower right y value
115
   */
116
  void BoundingBox (double ulx, double uly, double lrx, double lry);
117
118
  /**
119
   * Gets the Node at a particular index at a given level of the tree 
120
   * 
121
   * \param level Level of the requested Node [Root node is at level 0.i.e,Tree levels are zero-indexed]
122
   * 
123
   * \param index Index of the requested Node [First node at a level has the index 0. i.e, The nodes at a level are zero-indexed
124
   *
125
   * \returns a node pointer to the requested node.NULL is returned if the Node does not exist
126
   */
127
  Ptr<Node> GetNode (uint32_t level, uint32_t index);
128
129
  /**
130
   * Gets the Leaves of the tree (Nodes at the final level)
131
   * 
132
   * \returns a node container containing the leaves of the tree 
133
   */
134
  NodeContainer GetLeaves ();
135
136
   /**
137
   * \brief Get the type ID.
138
   * \return type ID
139
   */
140
  static TypeId GetTypeId ();
141
142
  /**
143
   * \param stack an InternetStackHelper which is used to install 
144
   *              on every node in the star
145
   */
146
  void InstallStack (InternetStackHelper stack);
147
148
  /**
149
   * Assign Ipv4 addresses hierarchically
150
   * The function starts with an allocated network address N and mask M
151
   * The mask M is extended to allocate X subnets. where X is the 
152
   * number of fan out nodes/spokes nodes/branches associated with a node
153
   * Each allocated subnet above is further divided into 2 subnets A and B
154
   * subnet A is used at the network between the hub and any spoke node
155
   * subnet B will be the new network which is used by the spoke nodes 
156
   * as part of recursion
157
   * For example:
158
   * 10.0.0.0/8 is allocated for the tree
159
   * The Root Node [R] has 4 point-to-point links to 4 spoke node
160
   * 10.0.0.0/8 is divided into 4 subnets
161
   * 10.0.0.0/10, 10.64.0.0/10, 10.128.0.0/10, 10.192.0.0/10
162
   *  
163
   * 10.0.0.0/10 is divided into 2 subnets
164
   * 10.0.0.0/11 and 10.32.0.0/11
165
   * 
166
   *
167
   *                         o(10.32.0.1/11)
168
   *                          [N1]
169
   * 		             o(10.0.0.2/11)
170
   *             [N3]       /
171
   *                \      /
172
   *                 \    /
173
   * 		      o o(10.0.0.1/11)
174
   *    (10.0.0.0/8)  [R]
175
   *                  o o(10.64.0.1/11)
176
   * 		     /   \
177
   * 		    /     \
178
   * 		 [N4]      \
179
   * 		            \
180
   * 		             o(10.64.0.2/11)
181
   * 		             [N2]
182
   * 			     o(10.96.0.1/11)
183
   *
184
   * \param network The network address allocated for this tree
185
   * 
186
   * \param mask The mask allocated for this tree
187
   */
188
  void AssignIpv4AddressesHierarchical (Ipv4Address network, Ipv4Mask mask);
189
190
  /**
191
   * Get the Ipv4Address of the interface of a leaf node
192
   * \param index of the leaf node (zero-indexed)
193
   *
194
   */
195
  Ipv4Address GetLeafIpv4Address (uint32_t leaf_index);
196
197
private:
198
  void AssignIpv4AddressHierarchicalRecursiveHelper (Ptr<Node> n,Ipv4Address network, Ipv4Mask mask);
199
  void CreateTopologyHelper (uint32_t levels, PointToPointHelper p2pHelper);
200
  void AddStarTopologyRecursively (NodeContainer parent_node, uint32_t num_levels, PointToPointHelper p2pHelper);
201
  void BoundingBoxRecursiveHelper (double xDist, double inter_level_height, uint32_t current_level);
202
  Ipv4Mask ExtendMaskForSubnets (Ipv4Mask original_mask, uint32_t subnets_required);
203
  uint32_t m_num_levels;
204
  RandomVariable m_fan_out;
205
  NodeContainer m_root_node;
206
  vector <NodeContainer> PerLevelNodeContainer; //each tree level maintains a container of nodes at that level
207
};
208
209
} // namespace ns3
210
211
#endif
(-)a/src/point-to-point-layout/wscript (+2 lines)
 Lines 7-12    Link Here 
7
        'model/point-to-point-dumbbell.cc',
7
        'model/point-to-point-dumbbell.cc',
8
        'model/point-to-point-grid.cc',
8
        'model/point-to-point-grid.cc',
9
        'model/point-to-point-star.cc',
9
        'model/point-to-point-star.cc',
10
        'model/point-to-point-tree.cc',
10
        ]
11
        ]
11
12
12
    headers = bld.new_task_gen('ns3header')
13
    headers = bld.new_task_gen('ns3header')
 Lines 15-20    Link Here 
15
        'model/point-to-point-dumbbell.h',
16
        'model/point-to-point-dumbbell.h',
16
        'model/point-to-point-grid.h',
17
        'model/point-to-point-grid.h',
17
        'model/point-to-point-star.h',
18
        'model/point-to-point-star.h',
19
        'model/point-to-point-tree.h',
18
        ]
20
        ]
19
21
20
    bld.ns3_python_bindings()
22
    bld.ns3_python_bindings()

Return to bug 1118