A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
36 
37 namespace ns3 {
38 
40 
42  "A global switch to enable all checksums for all protocols",
43  BooleanValue (false),
44  MakeBooleanChecker ());
45 
46 TypeId
48 {
49  static TypeId tid = TypeId ("ns3::Node")
50  .SetParent<Object> ()
51  .AddConstructor<Node> ()
52  .AddAttribute ("DeviceList", "The list of devices associated to this Node.",
55  MakeObjectVectorChecker<NetDevice> ())
56  .AddAttribute ("ApplicationList", "The list of applications associated to this Node.",
59  MakeObjectVectorChecker<Application> ())
60  .AddAttribute ("Id", "The id (unique integer) of this Node.",
61  TypeId::ATTR_GET, // allow only getting it.
62  UintegerValue (0),
63  MakeUintegerAccessor (&Node::m_id),
64  MakeUintegerChecker<uint32_t> ())
65  .AddAttribute ("SystemId", "The systemId of this node: a unique integer used for parallel simulations.",
67  UintegerValue (0),
68  MakeUintegerAccessor (&Node::m_sid),
69  MakeUintegerChecker<uint32_t> ())
70  ;
71  return tid;
72 }
73 
75  : m_id (0),
76  m_sid (0)
77 {
78  NS_LOG_FUNCTION (this);
79  Construct ();
80 }
81 
82 Node::Node(uint32_t sid)
83  : m_id (0),
84  m_sid (sid)
85 {
86  NS_LOG_FUNCTION (this << sid);
87  Construct ();
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION (this);
94  m_id = NodeList::Add (this);
95 }
96 
98 {
99  NS_LOG_FUNCTION (this);
100 }
101 
102 uint32_t
103 Node::GetId (void) const
104 {
105  NS_LOG_FUNCTION (this);
106  return m_id;
107 }
108 
109 uint32_t
110 Node::GetSystemId (void) const
111 {
112  NS_LOG_FUNCTION (this);
113  return m_sid;
114 }
115 
116 uint32_t
118 {
119  NS_LOG_FUNCTION (this << device);
120  uint32_t index = m_devices.size ();
121  m_devices.push_back (device);
122  device->SetNode (this);
123  device->SetIfIndex (index);
124  device->SetReceiveCallback (MakeCallback (&Node::NonPromiscReceiveFromDevice, this));
125  Simulator::ScheduleWithContext (GetId (), Seconds (0.0),
126  &NetDevice::Initialize, device);
127  NotifyDeviceAdded (device);
128  return index;
129 }
131 Node::GetDevice (uint32_t index) const
132 {
133  NS_LOG_FUNCTION (this << index);
134  NS_ASSERT_MSG (index < m_devices.size (), "Device index " << index <<
135  " is out of range (only have " << m_devices.size () << " devices).");
136  return m_devices[index];
137 }
138 uint32_t
139 Node::GetNDevices (void) const
140 {
141  NS_LOG_FUNCTION (this);
142  return m_devices.size ();
143 }
144 
145 uint32_t
147 {
148  NS_LOG_FUNCTION (this << application);
149  uint32_t index = m_applications.size ();
150  m_applications.push_back (application);
151  application->SetNode (this);
152  Simulator::ScheduleWithContext (GetId (), Seconds (0.0),
153  &Application::Initialize, application);
154  return index;
155 }
157 Node::GetApplication (uint32_t index) const
158 {
159  NS_LOG_FUNCTION (this << index);
160  NS_ASSERT_MSG (index < m_applications.size (), "Application index " << index <<
161  " is out of range (only have " << m_applications.size () << " applications).");
162  return m_applications[index];
163 }
164 uint32_t
166 {
167  NS_LOG_FUNCTION (this);
168  return m_applications.size ();
169 }
170 
171 void
173 {
174  NS_LOG_FUNCTION (this);
175  m_deviceAdditionListeners.clear ();
176  m_handlers.clear ();
177  for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
178  i != m_devices.end (); i++)
179  {
180  Ptr<NetDevice> device = *i;
181  device->Dispose ();
182  *i = 0;
183  }
184  m_devices.clear ();
185  for (std::vector<Ptr<Application> >::iterator i = m_applications.begin ();
186  i != m_applications.end (); i++)
187  {
188  Ptr<Application> application = *i;
189  application->Dispose ();
190  *i = 0;
191  }
192  m_applications.clear ();
194 }
195 void
197 {
198  NS_LOG_FUNCTION (this);
199  for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
200  i != m_devices.end (); i++)
201  {
202  Ptr<NetDevice> device = *i;
203  device->Initialize ();
204  }
205  for (std::vector<Ptr<Application> >::iterator i = m_applications.begin ();
206  i != m_applications.end (); i++)
207  {
208  Ptr<Application> application = *i;
209  application->Initialize ();
210  }
211 
213 }
214 
215 void
217  uint16_t protocolType,
218  Ptr<NetDevice> device,
219  bool promiscuous)
220 {
221  NS_LOG_FUNCTION (this << &handler << protocolType << device << promiscuous);
222  struct Node::ProtocolHandlerEntry entry;
223  entry.handler = handler;
224  entry.protocol = protocolType;
225  entry.device = device;
226  entry.promiscuous = promiscuous;
227 
228  // On demand enable promiscuous mode in netdevices
229  if (promiscuous)
230  {
231  if (device == 0)
232  {
233  for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
234  i != m_devices.end (); i++)
235  {
236  Ptr<NetDevice> dev = *i;
237  dev->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
238  }
239  }
240  else
241  {
242  device->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
243  }
244  }
245 
246  m_handlers.push_back (entry);
247 }
248 
249 void
251 {
252  NS_LOG_FUNCTION (this << &handler);
253  for (ProtocolHandlerList::iterator i = m_handlers.begin ();
254  i != m_handlers.end (); i++)
255  {
256  if (i->handler.IsEqual (handler))
257  {
258  m_handlers.erase (i);
259  break;
260  }
261  }
262 }
263 
264 bool
266 {
268  BooleanValue val;
270  return val.Get ();
271 }
272 
273 bool
275  const Address &from, const Address &to, NetDevice::PacketType packetType)
276 {
277  NS_LOG_FUNCTION (this << device << packet << protocol << &from << &to << packetType);
278  return ReceiveFromDevice (device, packet, protocol, from, to, packetType, true);
279 }
280 
281 bool
283  const Address &from)
284 {
285  NS_LOG_FUNCTION (this << device << packet << protocol << &from);
286  return ReceiveFromDevice (device, packet, protocol, from, device->GetAddress (), NetDevice::PacketType (0), false);
287 }
288 
289 bool
290 Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
291  const Address &from, const Address &to, NetDevice::PacketType packetType, bool promiscuous)
292 {
293  NS_LOG_FUNCTION (this << device << packet << protocol << &from << &to << packetType << promiscuous);
294  NS_ASSERT_MSG (Simulator::GetContext () == GetId (), "Received packet with erroneous context ; " <<
295  "make sure the channels in use are correctly updating events context " <<
296  "when transfering events from one node to another.");
297  NS_LOG_DEBUG ("Node " << GetId () << " ReceiveFromDevice: dev "
298  << device->GetIfIndex () << " (type=" << device->GetInstanceTypeId ().GetName ()
299  << ") Packet UID " << packet->GetUid ());
300  bool found = false;
301 
302  for (ProtocolHandlerList::iterator i = m_handlers.begin ();
303  i != m_handlers.end (); i++)
304  {
305  if (i->device == 0 ||
306  (i->device != 0 && i->device == device))
307  {
308  if (i->protocol == 0 ||
309  i->protocol == protocol)
310  {
311  if (promiscuous == i->promiscuous)
312  {
313  i->handler (device, packet, protocol, from, to, packetType);
314  found = true;
315  }
316  }
317  }
318  }
319  return found;
320 }
321 void
323 {
324  NS_LOG_FUNCTION (this << &listener);
325  m_deviceAdditionListeners.push_back (listener);
326  // and, then, notify the new listener about all existing devices.
327  for (std::vector<Ptr<NetDevice> >::const_iterator i = m_devices.begin ();
328  i != m_devices.end (); ++i)
329  {
330  listener (*i);
331  }
332 }
333 void
335 {
336  NS_LOG_FUNCTION (this << &listener);
337  for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin ();
338  i != m_deviceAdditionListeners.end (); i++)
339  {
340  if ((*i).IsEqual (listener))
341  {
342  m_deviceAdditionListeners.erase (i);
343  break;
344  }
345  }
346 }
347 
348 void
350 {
351  NS_LOG_FUNCTION (this << device);
352  for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin ();
353  i != m_deviceAdditionListeners.end (); i++)
354  {
355  (*i) (device);
356  }
357 }
358 
359 
360 } // namespace ns3
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberContainer)
Definition: object-vector.h:51
uint32_t AddApplication(Ptr< Application > application)
Definition: node.cc:146
bool Get(void) const
Definition: boolean.cc:45
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
Hold a bool native type.
Definition: boolean.h:38
uint32_t GetNApplications(void) const
Definition: node.cc:165
virtual void DoInitialize(void)
Definition: node.cc:196
DeviceAdditionListenerList m_deviceAdditionListeners
Definition: node.h:230
static bool ChecksumEnabled(void)
Definition: node.cc:265
uint64_t GetUid(void) const
Definition: packet.cc:393
void NotifyDeviceAdded(Ptr< NetDevice > device)
Definition: node.cc:349
static uint32_t GetContext(void)
Definition: simulator.cc:300
ProtocolHandler handler
Definition: node.h:217
virtual void DoDispose(void)
Definition: object.cc:335
std::vector< Ptr< NetDevice > > m_devices
Definition: node.h:227
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
hold a so-called 'global value'.
Definition: global-value.h:47
uint32_t GetSystemId(void) const
Definition: node.cc:110
void UnregisterProtocolHandler(ProtocolHandler handler)
Definition: node.cc:250
a polymophic address class
Definition: address.h:86
uint32_t m_sid
Definition: node.h:226
GlobalValue g_checksumEnabled
Definition: node.cc:41
Ptr< Application > GetApplication(uint32_t index) const
Definition: node.cc:157
virtual void DoDispose(void)
Definition: node.cc:172
Ptr< NetDevice > device
Definition: node.h:218
bool PromiscReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet >, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Definition: node.cc:274
Hold an unsigned integer type.
Definition: uinteger.h:46
static uint32_t Add(Ptr< Node > node)
Definition: node-list.cc:174
NS_OBJECT_ENSURE_REGISTERED(AntennaModel)
Ptr< NetDevice > GetDevice(uint32_t index) const
Definition: node.cc:131
uint16_t protocol
Definition: node.h:219
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:904
uint32_t GetNDevices(void) const
Definition: node.cc:139
ProtocolHandlerList m_handlers
Definition: node.h:229
NS_LOG_COMPONENT_DEFINE("Node")
void RegisterDeviceAdditionListener(DeviceAdditionListener listener)
Definition: node.cc:322
std::vector< Ptr< Application > > m_applications
Definition: node.h:228
Definition: node.h:216
uint32_t m_id
Definition: node.h:225
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
Definition: node.cc:216
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
static TypeId GetTypeId(void)
Definition: node.cc:47
bool ReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet >, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType, bool promisc)
Definition: node.cc:290
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:117
uint32_t GetId(void) const
Definition: node.cc:103
void UnregisterDeviceAdditionListener(DeviceAdditionListener listener)
Definition: node.cc:334
virtual ~Node()
Definition: node.cc:97
#define NS_LOG_DEBUG(msg)
Definition: log.h:255
void Initialize(void)
Definition: object.cc:179
bool NonPromiscReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet >, uint16_t protocol, const Address &from)
Definition: node.cc:282
a base class which provides memory management and object aggregation
Definition: object.h:63
contain a set of ns3::Object pointers.
a unique identifier for an interface.
Definition: type-id.h:49
void Construct(void)
Definition: node.cc:91
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
virtual void DoInitialize(void)
Definition: object.cc:342
void GetValue(AttributeValue &value) const
bool promiscuous
Definition: node.h:220
Node()
Definition: node.cc:74