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"
30 #include "ns3/assert.h"
31 #include "ns3/global-value.h"
32 #include "ns3/boolean.h"
33 #include "ns3/simulator.h"
34 #include "ns3/arp-l3-protocol.h"
40 NS_OBJECT_ENSURE_REGISTERED (Node);
42 GlobalValue g_checksumEnabled = GlobalValue (
"ChecksumEnabled",
43 "A global switch to enable all checksums for all protocols",
45 MakeBooleanChecker ());
48 Node::GetTypeId (
void)
50 static TypeId tid = TypeId (
"ns3::Node")
52 .AddConstructor<Node> ()
53 .AddAttribute (
"DeviceList",
"The list of devices associated to this Node.",
55 MakeObjectVectorAccessor (&Node::m_devices),
56 MakeObjectVectorChecker<NetDevice> ())
57 .AddAttribute (
"ApplicationList",
"The list of applications associated to this Node.",
59 MakeObjectVectorAccessor (&Node::m_applications),
60 MakeObjectVectorChecker<Application> ())
61 .AddAttribute (
"Id",
"The id (unique integer) of this Node.",
64 MakeUintegerAccessor (&Node::m_id),
65 MakeUintegerChecker<uint32_t> ())
66 .AddAttribute (
"SystemId",
"The systemId of this node: a unique integer used for parallel simulations.",
69 MakeUintegerAccessor (&Node::m_sid),
70 MakeUintegerChecker<uint32_t> ())
78 m_isCognitiveRadio (false)
84 Node::Node(uint32_t sid)
93 Node::Construct (
void)
122 uint32_t index = m_devices.size ();
123 m_devices.push_back (device);
124 device->SetNode (
this);
125 device->SetIfIndex (index);
126 device->SetReceiveCallback (
MakeCallback (&Node::NonPromiscReceiveFromDevice,
this));
129 NotifyDeviceAdded (device);
136 NS_ASSERT_MSG (index < m_devices.size (),
"Device index " << index <<
137 " is out of range (only have " << m_devices.size () <<
" devices).");
138 return m_devices[index];
144 return m_devices.size ();
151 uint32_t index = m_applications.size ();
152 m_applications.push_back (application);
153 application->SetNode (
this);
162 NS_ASSERT_MSG (index < m_applications.size (),
"Application index " << index <<
163 " is out of range (only have " << m_applications.size () <<
" applications).");
164 return m_applications[index];
170 return m_applications.size ();
177 m_deviceAdditionListeners.clear ();
179 for (std::vector<
Ptr<NetDevice> >::iterator i = m_devices.begin ();
180 i != m_devices.end (); i++)
188 i != m_applications.end (); i++)
191 application->Dispose ();
194 m_applications.clear ();
201 for (std::vector<
Ptr<NetDevice> >::iterator i = m_devices.begin ();
202 i != m_devices.end (); i++)
205 device->Initialize ();
208 i != m_applications.end (); i++)
211 application->Initialize ();
219 uint16_t protocolType,
223 NS_LOG_FUNCTION (
this << &handler << protocolType << device << promiscuous);
225 entry.handler = handler;
226 entry.protocol = protocolType;
227 entry.device = device;
228 entry.promiscuous = promiscuous;
235 for (std::vector<
Ptr<NetDevice> >::iterator i = m_devices.begin ();
236 i != m_devices.end (); i++)
239 dev->SetPromiscReceiveCallback (
MakeCallback (&Node::PromiscReceiveFromDevice,
this));
244 device->SetPromiscReceiveCallback (
MakeCallback (&Node::PromiscReceiveFromDevice,
this));
248 m_handlers.push_back (entry);
255 for (ProtocolHandlerList::iterator i = m_handlers.begin ();
256 i != m_handlers.end (); i++)
258 if (i->handler.IsEqual (handler))
260 m_handlers.erase (i);
279 NS_LOG_FUNCTION (
this << device << packet << protocol << &from << &to << packetType);
280 return ReceiveFromDevice (device, packet, protocol, from, to, packetType,
true);
284 Node::NonPromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
288 return ReceiveFromDevice (device, packet, protocol, from, device->GetAddress (),
NetDevice::PacketType (0),
false);
292 Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
295 NS_LOG_FUNCTION (
this << device << packet << protocol << &from << &to << packetType << promiscuous);
297 "make sure the channels in use are correctly updating events context " <<
298 "when transfering events from one node to another.");
300 << device->GetIfIndex () <<
" (type=" << device->GetInstanceTypeId ().GetName ()
301 <<
") Packet UID " << packet->GetUid ());
304 for (ProtocolHandlerList::iterator i = m_handlers.begin ();
305 i != m_handlers.end (); i++)
307 if (i->device == 0 ||
310 if (i->protocol == 0 ||
311 i->protocol == protocol)
314 if (device !=
GetDevice(CONTROL_RADIO) && protocol == ArpL3Protocol::PROT_NUMBER)
316 if (promiscuous == i->promiscuous)
318 if (device->GetInstanceTypeId().GetName() ==
"ns3::LoopbackNetDevice") {
319 i->handler(device, packet, protocol, from, to, packetType);
322 i->handler(
GetDevice(CONTROL_RADIO), packet, protocol, from, to, packetType);
335 m_deviceAdditionListeners.push_back (listener);
337 for (std::vector<
Ptr<NetDevice> >::const_iterator i = m_devices.begin ();
338 i != m_devices.end (); ++i)
347 for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin ();
348 i != m_deviceAdditionListeners.end (); i++)
350 if ((*i).IsEqual (listener))
352 m_deviceAdditionListeners.erase (i);
362 for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin ();
363 i != m_deviceAdditionListeners.end (); i++)
370 Node::SetCognitiveRadio(
bool isCR)
372 m_isCognitiveRadio = isCR;
376 Node::IsCognitiveRadio(
void)
378 return m_isCognitiveRadio;
uint32_t AddApplication(Ptr< Application > application)
smart pointer class similar to boost::intrusive_ptr
#define NS_LOG_FUNCTION(parameters)
uint32_t GetNApplications(void) const
virtual void DoInitialize(void)
static bool ChecksumEnabled(void)
static uint32_t GetContext(void)
#define NS_LOG_COMPONENT_DEFINE(name)
virtual void DoDispose(void)
#define NS_LOG_FUNCTION_NOARGS()
uint32_t GetSystemId(void) const
void UnregisterProtocolHandler(ProtocolHandler handler)
a polymophic address class
Ptr< Application > GetApplication(uint32_t index) const
virtual void DoDispose(void)
static uint32_t Add(Ptr< Node > node)
Ptr< NetDevice > GetDevice(uint32_t index) const
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
uint32_t GetNDevices(void) const
void RegisterDeviceAdditionListener(DeviceAdditionListener listener)
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
#define NS_ASSERT_MSG(condition, message)
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
uint32_t AddDevice(Ptr< NetDevice > device)
uint32_t GetId(void) const
void UnregisterDeviceAdditionListener(DeviceAdditionListener listener)
#define NS_LOG_DEBUG(msg)
virtual void DoInitialize(void)
void GetValue(AttributeValue &value) const