add NetDevice::SetReceiveCallback and use it
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 30 Apr 2007 10:37:22 +0200
changeset 451 3fe2c883cb47
parent 450 753aadd3ae77
child 452 e4712748b5a0
add NetDevice::SetReceiveCallback and use it
src/node/internet-node.cc
src/node/internet-node.h
src/node/net-device.cc
src/node/net-device.h
--- a/src/node/internet-node.cc	Mon Apr 30 10:24:43 2007 +0200
+++ b/src/node/internet-node.cc	Mon Apr 30 10:37:22 2007 +0200
@@ -30,6 +30,7 @@
 #include "udp.h"
 #include "ipv4.h"
 #include "arp.h"
+#include "net-device.h"
 
 namespace ns3 {
 
@@ -144,7 +145,20 @@
 void 
 InternetNode::DoAddDevice (NetDevice *device) const
 {
-  //XXX
+  device->SetReceiveCallback (MakeCallback (&InternetNode::ReceiveFromDevice, this));
+}
+
+bool
+InternetNode::ReceiveFromDevice (NetDevice *device, const Packet &p, uint16_t protocolNumber) const
+{
+  L3Protocol *target = GetL3Demux()->Lookup(protocolNumber);
+  if (target != 0) 
+    {
+      Packet packet = p;
+      target->Receive(packet, *device);
+      return true;
+    }
+  return false;
 }
 
 
--- a/src/node/internet-node.h	Mon Apr 30 10:24:43 2007 +0200
+++ b/src/node/internet-node.h	Mon Apr 30 10:37:22 2007 +0200
@@ -31,6 +31,7 @@
 
 namespace ns3 {
 
+class Packet;
 
 class InternetNode : public Node 
 {
@@ -57,6 +58,7 @@
   void SetName(std::string name);
 private:
   virtual void DoAddDevice (NetDevice *device) const;
+  bool ReceiveFromDevice (NetDevice *device, const Packet &p, uint16_t protocolNumber) const;
   // Capabilities
   ApplicationList* m_applicationList;
   L3Demux*         m_l3Demux;
--- a/src/node/net-device.cc	Mon Apr 30 10:24:43 2007 +0200
+++ b/src/node/net-device.cc	Mon Apr 30 10:37:22 2007 +0200
@@ -179,19 +179,15 @@
 bool
 NetDevice::ForwardUp (Packet& packet)
 {
+  bool retval = false;
   LlcSnapHeader llc;
   packet.Peek (llc);
   packet.Remove (llc);
-  if (GetNode()->GetL3Demux() != 0)
+  if (!m_receiveCallback.IsNull ())
     {
-      L3Protocol *target = GetNode()->GetL3Demux()->Lookup(llc.GetType ());
-      if (target != 0) 
-        {
-          target->Receive(packet, *this);
-          return true;
-        }
+      retval = m_receiveCallback (this, packet, llc.GetType ());
     }
-  return false;
+  return retval;
 }
 
 void 
@@ -226,4 +222,10 @@
   return DoNeedsArp ();
 }
 
+void 
+NetDevice::SetReceiveCallback (Callback<bool,NetDevice *,const Packet &,uint16_t> cb)
+{
+  m_receiveCallback = cb;
+}
+
 }; // namespace ns3
--- a/src/node/net-device.h	Mon Apr 30 10:24:43 2007 +0200
+++ b/src/node/net-device.h	Mon Apr 30 10:37:22 2007 +0200
@@ -154,6 +154,8 @@
 
   bool NeedsArp (void) const;
 
+  void SetReceiveCallback (Callback<bool,NetDevice *,const Packet &,uint16_t> cb);
+
  protected:
   /**
    * Enable broadcast support. This method should be
@@ -238,6 +240,7 @@
   bool          m_isMulticast;
   bool          m_isPointToPoint;
   Callback<void> m_linkChangeCallback;
+  Callback<bool,NetDevice *,const Packet &,uint16_t> m_receiveCallback;
 };
 
 }; // namespace ns3