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 
22 #include "node.h"
23 #include "node-list.h"
24 #include "net-device.h"
25 #include "application.h"
26 #include "ns3/packet.h"
27 #include "ns3/simulator.h"
28 #include "ns3/object-vector.h"
29 #include "ns3/uinteger.h"
30 #include "ns3/log.h"
31 #include "ns3/assert.h"
32 #include "ns3/global-value.h"
33 #include "ns3/boolean.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 Time
114 Node::GetLocalTime (void) const
115 {
116  NS_LOG_FUNCTION (this);
117  return Simulator::Now ();
118 }
119 
120 uint32_t
121 Node::GetSystemId (void) const
122 {
123  NS_LOG_FUNCTION (this);
124  return m_sid;
125 }
126 
127 uint32_t
129 {
130  NS_LOG_FUNCTION (this << device);
131  uint32_t index = m_devices.size ();
132  m_devices.push_back (device);
133  device->SetNode (this);
134  device->SetIfIndex (index);
135  device->SetReceiveCallback (MakeCallback (&Node::NonPromiscReceiveFromDevice, this));
137  &NetDevice::Initialize, device);
138  NotifyDeviceAdded (device);
139  return index;
140 }
142 Node::GetDevice (uint32_t index) const
143 {
144  NS_LOG_FUNCTION (this << index);
145  NS_ASSERT_MSG (index < m_devices.size (), "Device index " << index <<
146  " is out of range (only have " << m_devices.size () << " devices).");
147  return m_devices[index];
148 }
149 uint32_t
150 Node::GetNDevices (void) const
151 {
152  NS_LOG_FUNCTION (this);
153  return m_devices.size ();
154 }
155 
156 uint32_t
158 {
159  NS_LOG_FUNCTION (this << application);
160  uint32_t index = m_applications.size ();
161  m_applications.push_back (application);
162  application->SetNode (this);
164  &Application::Initialize, application);
165  return index;
166 }
168 Node::GetApplication (uint32_t index) const
169 {
170  NS_LOG_FUNCTION (this << index);
171  NS_ASSERT_MSG (index < m_applications.size (), "Application index " << index <<
172  " is out of range (only have " << m_applications.size () << " applications).");
173  return m_applications[index];
174 }
175 uint32_t
177 {
178  NS_LOG_FUNCTION (this);
179  return m_applications.size ();
180 }
181 
182 void
184 {
185  NS_LOG_FUNCTION (this);
186  m_deviceAdditionListeners.clear ();
187  m_handlers.clear ();
188  for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
189  i != m_devices.end (); i++)
190  {
191  Ptr<NetDevice> device = *i;
192  device->Dispose ();
193  *i = 0;
194  }
195  m_devices.clear ();
196  for (std::vector<Ptr<Application> >::iterator i = m_applications.begin ();
197  i != m_applications.end (); i++)
198  {
199  Ptr<Application> application = *i;
200  application->Dispose ();
201  *i = 0;
202  }
203  m_applications.clear ();
205 }
206 void
208 {
209  NS_LOG_FUNCTION (this);
210  for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
211  i != m_devices.end (); i++)
212  {
213  Ptr<NetDevice> device = *i;
214  device->Initialize ();
215  }
216  for (std::vector<Ptr<Application> >::iterator i = m_applications.begin ();
217  i != m_applications.end (); i++)
218  {
219  Ptr<Application> application = *i;
220  application->Initialize ();
221  }
222 
224 }
225 
226 void
228  uint16_t protocolType,
229  Ptr<NetDevice> device,
230  bool promiscuous)
231 {
232  NS_LOG_FUNCTION (this << &handler << protocolType << device << promiscuous);
233  struct Node::ProtocolHandlerEntry entry;
234  entry.handler = handler;
235  entry.protocol = protocolType;
236  entry.device = device;
237  entry.promiscuous = promiscuous;
238 
239  // On demand enable promiscuous mode in netdevices
240  if (promiscuous)
241  {
242  if (device == 0)
243  {
244  for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
245  i != m_devices.end (); i++)
246  {
247  Ptr<NetDevice> dev = *i;
248  dev->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
249  }
250  }
251  else
252  {
253  device->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
254  }
255  }
256 
257  m_handlers.push_back (entry);
258 }
259 
260 void
262 {
263  NS_LOG_FUNCTION (this << &handler);
264  for (ProtocolHandlerList::iterator i = m_handlers.begin ();
265  i != m_handlers.end (); i++)
266  {
267  if (i->handler.IsEqual (handler))
268  {
269  m_handlers.erase (i);
270  break;
271  }
272  }
273 }
274 
275 bool
277 {
279  BooleanValue val;
281  return val.Get ();
282 }
283 
284 bool
286  const Address &from, const Address &to, NetDevice::PacketType packetType)
287 {
288  NS_LOG_FUNCTION (this << device << packet << protocol << &from << &to << packetType);
289  return ReceiveFromDevice (device, packet, protocol, from, to, packetType, true);
290 }
291 
292 bool
294  const Address &from)
295 {
296  NS_LOG_FUNCTION (this << device << packet << protocol << &from);
297  return ReceiveFromDevice (device, packet, protocol, from, device->GetAddress (), NetDevice::PacketType (0), false);
298 }
299 
300 bool
301 Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
302  const Address &from, const Address &to, NetDevice::PacketType packetType, bool promiscuous)
303 {
304  NS_LOG_FUNCTION (this << device << packet << protocol << &from << &to << packetType << promiscuous);
305  NS_ASSERT_MSG (Simulator::GetContext () == GetId (), "Received packet with erroneous context ; " <<
306  "make sure the channels in use are correctly updating events context " <<
307  "when transferring events from one node to another.");
308  NS_LOG_DEBUG ("Node " << GetId () << " ReceiveFromDevice: dev "
309  << device->GetIfIndex () << " (type=" << device->GetInstanceTypeId ().GetName ()
310  << ") Packet UID " << packet->GetUid ());
311  bool found = false;
312 
313  for (ProtocolHandlerList::iterator i = m_handlers.begin ();
314  i != m_handlers.end (); i++)
315  {
316  if (i->device == 0 ||
317  (i->device != 0 && i->device == device))
318  {
319  if (i->protocol == 0 ||
320  i->protocol == protocol)
321  {
322  if (promiscuous == i->promiscuous)
323  {
324  i->handler (device, packet, protocol, from, to, packetType);
325  found = true;
326  }
327  }
328  }
329  }
330  return found;
331 }
332 void
334 {
335  NS_LOG_FUNCTION (this << &listener);
336  m_deviceAdditionListeners.push_back (listener);
337  // and, then, notify the new listener about all existing devices.
338  for (std::vector<Ptr<NetDevice> >::const_iterator i = m_devices.begin ();
339  i != m_devices.end (); ++i)
340  {
341  listener (*i);
342  }
343 }
344 void
346 {
347  NS_LOG_FUNCTION (this << &listener);
348  for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin ();
349  i != m_deviceAdditionListeners.end (); i++)
350  {
351  if ((*i).IsEqual (listener))
352  {
353  m_deviceAdditionListeners.erase (i);
354  break;
355  }
356  }
357 }
358 
359 void
361 {
362  NS_LOG_FUNCTION (this << device);
363  for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin ();
364  i != m_deviceAdditionListeners.end (); i++)
365  {
366  (*i) (device);
367  }
368 }
369 
370 
371 } // namespace ns3
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:157
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
uint64_t GetUid(void) const
Returns the packet&#39;s Uid.
Definition: packet.cc:390
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void GetValue(AttributeValue &value) const
Get the value.
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:36
uint32_t GetId(void) const
Definition: node.cc:107
virtual void DoInitialize(void)
Initialize() implementation.
Definition: node.cc:207
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
DeviceAdditionListenerList m_deviceAdditionListeners
Device addition listeners in the node.
Definition: node.h:290
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:142
static bool ChecksumEnabled(void)
Definition: node.cc:276
void NotifyDeviceAdded(Ptr< NetDevice > device)
Notifies all the DeviceAdditionListener about the new device added.
Definition: node.cc:360
static uint32_t GetContext(void)
Get the current simulation context.
Definition: simulator.cc:378
ProtocolHandler handler
the protocol handler
Definition: node.h:274
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Definition: object-vector.h:81
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
std::vector< Ptr< NetDevice > > m_devices
Devices associated to this node.
Definition: node.h:287
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Hold a so-called &#39;global value&#39;.
Definition: global-value.h:73
void UnregisterProtocolHandler(ProtocolHandler handler)
Definition: node.cc:261
a polymophic address class
Definition: address.h:90
The attribute can be read.
Definition: type-id.h:63
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:293
uint32_t m_sid
System id for this node.
Definition: node.h:286
static GlobalValue g_checksumEnabled
A global switch to enable all checksums for all protocols.
Definition: node.cc:44
virtual void DoDispose(void)
The dispose method.
Definition: node.cc:183
Ptr< NetDevice > device
the NetDevice
Definition: node.h:275
Hold an unsigned integer type.
Definition: uinteger.h:44
static uint32_t Add(Ptr< Node > node)
Definition: node-list.cc:223
Ptr< Application > GetApplication(uint32_t index) const
Retrieve the index-th Application associated to this node.
Definition: node.cc:168
uint16_t protocol
the protocol number
Definition: node.h:276
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Time GetLocalTime(void) const
In the future, ns3 nodes may have clock that returned a local time different from the virtual time Si...
Definition: node.cc:114
bool Get(void) const
Definition: boolean.cc:51
ProtocolHandlerList m_handlers
Protocol handlers in the node.
Definition: node.h:289
void RegisterDeviceAdditionListener(DeviceAdditionListener listener)
Definition: node.cc:333
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
uint32_t GetSystemId(void) const
Definition: node.cc:121
The attribute can be written.
Definition: type-id.h:64
void SetNode(Ptr< Node > node)
Definition: application.cc:111
std::vector< Ptr< Application > > m_applications
Applications associated to this node.
Definition: node.h:288
Protocol handler entry.
Definition: node.h:273
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
uint32_t m_id
Node id for this node.
Definition: node.h:285
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
Definition: node.cc:227
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:301
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:1475
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
void UnregisterDeviceAdditionListener(DeviceAdditionListener listener)
Definition: node.cc:345
virtual ~Node()
Definition: node.cc:101
A network Node.
Definition: node.h:56
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
uint32_t GetNApplications(void) const
Definition: node.cc:176
A base class which provides memory management and object aggregation.
Definition: object.h:87
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:296
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:58
void Construct(void)
Finish node&#39;s construction by setting the correct node ID.
Definition: node.cc:95
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
uint32_t GetNDevices(void) const
Definition: node.cc:150
bool promiscuous
true if it is a promiscuous handler
Definition: node.h:277
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:285
Node()
Definition: node.cc:78