A Discrete-Event Network Simulator
API
node.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 Georgia Tech Research Corporation, INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: George F. Riley<riley@ece.gatech.edu>
19  * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  */
21 #include "node.h"
22 #include "node-list.h"
23 #include "net-device.h"
24 #include "application.h"
25 #include "ns3/packet.h"
26 #include "ns3/simulator.h"
27 #include "ns3/object-vector.h"
28 #include "ns3/uinteger.h"
29 #include "ns3/log.h"
30 #include "ns3/assert.h"
31 #include "ns3/global-value.h"
32 #include "ns3/boolean.h"
33 #include "ns3/simulator.h"
34 
35 namespace ns3 {
36 
38 
40 
44 static GlobalValue g_checksumEnabled = GlobalValue ("ChecksumEnabled",
45  "A global switch to enable all checksums for all protocols",
46  BooleanValue (false),
48 
49 TypeId
51 {
52  static TypeId tid = TypeId ("ns3::Node")
53  .SetParent<Object> ()
54  .SetGroupName("Network")
55  .AddConstructor<Node> ()
56  .AddAttribute ("DeviceList", "The list of devices associated to this Node.",
59  MakeObjectVectorChecker<NetDevice> ())
60  .AddAttribute ("ApplicationList", "The list of applications associated to this Node.",
63  MakeObjectVectorChecker<Application> ())
64  .AddAttribute ("Id", "The id (unique integer) of this Node.",
65  TypeId::ATTR_GET, // allow only getting it.
66  UintegerValue (0),
68  MakeUintegerChecker<uint32_t> ())
69  .AddAttribute ("SystemId", "The systemId of this node: a unique integer used for parallel simulations.",
71  UintegerValue (0),
73  MakeUintegerChecker<uint32_t> ())
74  ;
75  return tid;
76 }
77 
79  : m_id (0),
80  m_sid (0)
81 {
82  NS_LOG_FUNCTION (this);
83  Construct ();
84 }
85 
86 Node::Node(uint32_t sid)
87  : m_id (0),
88  m_sid (sid)
89 {
90  NS_LOG_FUNCTION (this << sid);
91  Construct ();
92 }
93 
94 void
96 {
97  NS_LOG_FUNCTION (this);
98  m_id = NodeList::Add (this);
99 }
100 
102 {
103  NS_LOG_FUNCTION (this);
104 }
105 
106 uint32_t
107 Node::GetId (void) const
108 {
109  NS_LOG_FUNCTION (this);
110  return m_id;
111 }
112 
113 uint32_t
114 Node::GetSystemId (void) const
115 {
116  NS_LOG_FUNCTION (this);
117  return m_sid;
118 }
119 
120 uint32_t
122 {
123  NS_LOG_FUNCTION (this << device);
124  uint32_t index = m_devices.size ();
125  m_devices.push_back (device);
126  device->SetNode (this);
127  device->SetIfIndex (index);
128  device->SetReceiveCallback (MakeCallback (&Node::NonPromiscReceiveFromDevice, this));
130  &NetDevice::Initialize, device);
131  NotifyDeviceAdded (device);
132  return index;
133 }
135 Node::GetDevice (uint32_t index) const
136 {
137  NS_LOG_FUNCTION (this << index);
138  NS_ASSERT_MSG (index < m_devices.size (), "Device index " << index <<
139  " is out of range (only have " << m_devices.size () << " devices).");
140  return m_devices[index];
141 }
142 uint32_t
143 Node::GetNDevices (void) const
144 {
145  NS_LOG_FUNCTION (this);
146  return m_devices.size ();
147 }
148 
149 uint32_t
151 {
152  NS_LOG_FUNCTION (this << application);
153  uint32_t index = m_applications.size ();
154  m_applications.push_back (application);
155  application->SetNode (this);
157  &Application::Initialize, application);
158  return index;
159 }
161 Node::GetApplication (uint32_t index) const
162 {
163  NS_LOG_FUNCTION (this << index);
164  NS_ASSERT_MSG (index < m_applications.size (), "Application index " << index <<
165  " is out of range (only have " << m_applications.size () << " applications).");
166  return m_applications[index];
167 }
168 uint32_t
170 {
171  NS_LOG_FUNCTION (this);
172  return m_applications.size ();
173 }
174 
175 void
177 {
178  NS_LOG_FUNCTION (this);
179  m_deviceAdditionListeners.clear ();
180  m_handlers.clear ();
181  for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
182  i != m_devices.end (); i++)
183  {
184  Ptr<NetDevice> device = *i;
185  device->Dispose ();
186  *i = 0;
187  }
188  m_devices.clear ();
189  for (std::vector<Ptr<Application> >::iterator i = m_applications.begin ();
190  i != m_applications.end (); i++)
191  {
192  Ptr<Application> application = *i;
193  application->Dispose ();
194  *i = 0;
195  }
196  m_applications.clear ();
198 }
199 void
201 {
202  NS_LOG_FUNCTION (this);
203  for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
204  i != m_devices.end (); i++)
205  {
206  Ptr<NetDevice> device = *i;
207  device->Initialize ();
208  }
209  for (std::vector<Ptr<Application> >::iterator i = m_applications.begin ();
210  i != m_applications.end (); i++)
211  {
212  Ptr<Application> application = *i;
213  application->Initialize ();
214  }
215 
217 }
218 
219 void
221  uint16_t protocolType,
222  Ptr<NetDevice> device,
223  bool promiscuous)
224 {
225  NS_LOG_FUNCTION (this << &handler << protocolType << device << promiscuous);
226  struct Node::ProtocolHandlerEntry entry;
227  entry.handler = handler;
228  entry.protocol = protocolType;
229  entry.device = device;
230  entry.promiscuous = promiscuous;
231 
232  // On demand enable promiscuous mode in netdevices
233  if (promiscuous)
234  {
235  if (device == 0)
236  {
237  for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
238  i != m_devices.end (); i++)
239  {
240  Ptr<NetDevice> dev = *i;
241  dev->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
242  }
243  }
244  else
245  {
246  device->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
247  }
248  }
249 
250  m_handlers.push_back (entry);
251 }
252 
253 void
255 {
256  NS_LOG_FUNCTION (this << &handler);
257  for (ProtocolHandlerList::iterator i = m_handlers.begin ();
258  i != m_handlers.end (); i++)
259  {
260  if (i->handler.IsEqual (handler))
261  {
262  m_handlers.erase (i);
263  break;
264  }
265  }
266 }
267 
268 bool
270 {
272  BooleanValue val;
273  g_checksumEnabled.GetValue (val);
274  return val.Get ();
275 }
276 
277 bool
279  const Address &from, const Address &to, NetDevice::PacketType packetType)
280 {
281  NS_LOG_FUNCTION (this << device << packet << protocol << &from << &to << packetType);
282  return ReceiveFromDevice (device, packet, protocol, from, to, packetType, true);
283 }
284 
285 bool
287  const Address &from)
288 {
289  NS_LOG_FUNCTION (this << device << packet << protocol << &from);
290  return ReceiveFromDevice (device, packet, protocol, from, device->GetAddress (), NetDevice::PacketType (0), false);
291 }
292 
293 bool
294 Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
295  const Address &from, const Address &to, NetDevice::PacketType packetType, bool promiscuous)
296 {
297  NS_LOG_FUNCTION (this << device << packet << protocol << &from << &to << packetType << promiscuous);
298  NS_ASSERT_MSG (Simulator::GetContext () == GetId (), "Received packet with erroneous context ; " <<
299  "make sure the channels in use are correctly updating events context " <<
300  "when transfering events from one node to another.");
301  NS_LOG_DEBUG ("Node " << GetId () << " ReceiveFromDevice: dev "
302  << device->GetIfIndex () << " (type=" << device->GetInstanceTypeId ().GetName ()
303  << ") Packet UID " << packet->GetUid ());
304  bool found = false;
305 
306  for (ProtocolHandlerList::iterator i = m_handlers.begin ();
307  i != m_handlers.end (); i++)
308  {
309  if (i->device == 0 ||
310  (i->device != 0 && i->device == device))
311  {
312  if (i->protocol == 0 ||
313  i->protocol == protocol)
314  {
315  if (promiscuous == i->promiscuous)
316  {
317  i->handler (device, packet, protocol, from, to, packetType);
318  found = true;
319  }
320  }
321  }
322  }
323  return found;
324 }
325 void
327 {
328  NS_LOG_FUNCTION (this << &listener);
329  m_deviceAdditionListeners.push_back (listener);
330  // and, then, notify the new listener about all existing devices.
331  for (std::vector<Ptr<NetDevice> >::const_iterator i = m_devices.begin ();
332  i != m_devices.end (); ++i)
333  {
334  listener (*i);
335  }
336 }
337 void
339 {
340  NS_LOG_FUNCTION (this << &listener);
341  for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin ();
342  i != m_deviceAdditionListeners.end (); i++)
343  {
344  if ((*i).IsEqual (listener))
345  {
346  m_deviceAdditionListeners.erase (i);
347  break;
348  }
349  }
350 }
351 
352 void
354 {
355  NS_LOG_FUNCTION (this << device);
356  for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin ();
357  i != m_deviceAdditionListeners.end (); i++)
358  {
359  (*i) (device);
360  }
361 }
362 
363 
364 } // namespace ns3
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:150
bool Get(void) const
Definition: boolean.cc:51
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
AttributeValue implementation for Boolean.
Definition: boolean.h:34
uint32_t GetNApplications(void) const
Definition: node.cc:169
virtual void DoInitialize(void)
Initialize() implementation.
Definition: node.cc:200
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
DeviceAdditionListenerList m_deviceAdditionListeners
Device addition listeners in the node.
Definition: node.h:279
static bool ChecksumEnabled(void)
Definition: node.cc:269
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:380
void NotifyDeviceAdded(Ptr< NetDevice > device)
Notifies all the DeviceAdditionListener about the new device added.
Definition: node.cc:353
static uint32_t GetContext(void)
Get the current simulation context.
Definition: simulator.cc:343
ProtocolHandler handler
the protocol handler
Definition: node.h:263
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Definition: object-vector.h:81
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:272
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:339
std::vector< Ptr< NetDevice > > m_devices
Devices associated to this node.
Definition: node.h:276
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
hold a so-called 'global value'.
Definition: global-value.h:54
uint32_t GetSystemId(void) const
Definition: node.cc:114
void UnregisterProtocolHandler(ProtocolHandler handler)
Definition: node.cc:254
a polymophic address class
Definition: address.h:90
The attribute can be read.
Definition: type-id.h:64
bool NonPromiscReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &from)
Receive a packet from a device in non-promiscuous mode.
Definition: node.cc:286
uint32_t m_sid
System id for this node.
Definition: node.h:275
static GlobalValue g_checksumEnabled
A global switch to enable all checksums for all protocols.
Definition: node.cc:44
Ptr< Application > GetApplication(uint32_t index) const
Retrieve the index-th Application associated to this node.
Definition: node.cc:161
virtual void DoDispose(void)
The dispose method.
Definition: node.cc:176
Ptr< NetDevice > device
the NetDevice
Definition: node.h:264
Hold an unsigned integer type.
Definition: uinteger.h:44
static uint32_t Add(Ptr< Node > node)
Definition: node-list.cc:223
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:135
uint16_t protocol
the protocol number
Definition: node.h:265
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1296
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:899
uint32_t GetNDevices(void) const
Definition: node.cc:143
ProtocolHandlerList m_handlers
Protocol handlers in the node.
Definition: node.h:278
void RegisterDeviceAdditionListener(DeviceAdditionListener listener)
Definition: node.cc:326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
The attribute can be written.
Definition: type-id.h:65
void SetNode(Ptr< Node > node)
Definition: application.cc:111
std::vector< Ptr< Application > > m_applications
Applications associated to this node.
Definition: node.h:277
Protocol handler entry.
Definition: node.h:262
uint32_t m_id
Node id for this node.
Definition: node.h:274
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
Definition: node.cc:220
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
static TypeId GetTypeId(void)
Get the type ID.
Definition: node.cc:50
bool ReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet >, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType, bool promisc)
Receive a packet from a device.
Definition: node.cc:294
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:121
uint32_t GetId(void) const
Definition: node.cc:107
void UnregisterDeviceAdditionListener(DeviceAdditionListener listener)
Definition: node.cc:338
virtual ~Node()
Definition: node.cc:101
A network Node.
Definition: node.h:55
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:866
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
A base class which provides memory management and object aggregation.
Definition: object.h:87
Container for a set of ns3::Object pointers.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:57
void Construct(void)
Finish node's construction by setting the correct node ID.
Definition: node.cc:95
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
void Dispose(void)
Dispose of this Object.
Definition: object.cc:208
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:346
void GetValue(AttributeValue &value) const
bool promiscuous
true if it is a promiscuous handler
Definition: node.h:266
bool PromiscReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Receive a packet from a device in promiscuous mode.
Definition: node.cc:278
Node()
Definition: node.cc:78