rework the NetDevice <-> Node interface
This commit is contained in:
@@ -197,18 +197,19 @@ NetDevice::GetChannel (void) const
|
||||
|
||||
// Receive packets from below
|
||||
bool
|
||||
NetDevice::ForwardUp(Packet& p, uint32_t param)
|
||||
NetDevice::ForwardUp(const Packet& p, uint32_t param, const Address &from)
|
||||
{
|
||||
bool retval = false;
|
||||
Packet packet = p;
|
||||
|
||||
NS_DEBUG ("NetDevice::ForwardUp: UID is " << packet.GetUid()
|
||||
NS_DEBUG ("NetDevice::ForwardUp: UID is " << p.GetUid()
|
||||
<< " device is: " << GetName());
|
||||
|
||||
if (!m_receiveCallback.IsNull ())
|
||||
{
|
||||
retval = m_receiveCallback (this, packet, param);
|
||||
} else {
|
||||
retval = m_receiveCallback (this, p, param, from);
|
||||
}
|
||||
else
|
||||
{
|
||||
NS_DEBUG ("NetDevice::Receive call back is NULL");
|
||||
}
|
||||
|
||||
@@ -248,7 +249,7 @@ NetDevice::NeedsArp (void) const
|
||||
}
|
||||
|
||||
void
|
||||
NetDevice::SetReceiveCallback (Callback<bool,Ptr<NetDevice>,const Packet &,uint16_t> cb)
|
||||
NetDevice::SetReceiveCallback (ReceiveCallback cb)
|
||||
{
|
||||
m_receiveCallback = cb;
|
||||
}
|
||||
|
||||
@@ -177,11 +177,24 @@ public:
|
||||
*/
|
||||
bool NeedsArp (void) const;
|
||||
|
||||
/**
|
||||
* \param device a pointer to the net device which is calling this callback
|
||||
* \param packet the packet received
|
||||
* \param protocol the 16 bit protocol number associated with this packet.
|
||||
* This protocol number is expected to be the same protocol number
|
||||
* given to the Send method by the user on the sender side.
|
||||
* \param address the address of the sender
|
||||
* \returns true if the callback could handle the packet successfully, false
|
||||
* otherwise.
|
||||
*/
|
||||
typedef Callback<bool,Ptr<NetDevice>,const Packet &,uint16_t,const Address &> ReceiveCallback;
|
||||
|
||||
/**
|
||||
* \param cb callback to invoke whenever a packet has been received and must
|
||||
* be forwarded to the higher layers.
|
||||
*
|
||||
*/
|
||||
void SetReceiveCallback (Callback<bool,Ptr<NetDevice>,const Packet &,uint16_t> cb);
|
||||
void SetReceiveCallback (ReceiveCallback cb);
|
||||
|
||||
protected:
|
||||
/**
|
||||
@@ -230,6 +243,7 @@ public:
|
||||
* \param p packet sent from below up to Network Device
|
||||
* \param param Extra parameter extracted from header and needed by
|
||||
* some protocols
|
||||
* \param address the address of the sender of this packet.
|
||||
* \returns true if the packet was forwarded successfully,
|
||||
* false otherwise.
|
||||
*
|
||||
@@ -237,7 +251,7 @@ public:
|
||||
* forwards it to the higher layers by calling this method
|
||||
* which is responsible for passing it up to the Rx callback.
|
||||
*/
|
||||
bool ForwardUp (Packet& p, uint32_t param);
|
||||
bool ForwardUp (const Packet& p, uint32_t param, const Address &address);
|
||||
|
||||
|
||||
/**
|
||||
@@ -248,8 +262,6 @@ public:
|
||||
*/
|
||||
virtual void DoDispose (void);
|
||||
|
||||
Callback<bool,Ptr<NetDevice>,const Packet &,uint16_t> m_receiveCallback;
|
||||
|
||||
private:
|
||||
/**
|
||||
* \param p packet to send
|
||||
@@ -297,6 +309,7 @@ public:
|
||||
bool m_isMulticast;
|
||||
bool m_isPointToPoint;
|
||||
Callback<void> m_linkChangeCallback;
|
||||
ReceiveCallback m_receiveCallback;
|
||||
};
|
||||
|
||||
}; // namespace ns3
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "node-list.h"
|
||||
#include "net-device.h"
|
||||
#include "application.h"
|
||||
#include "packet-socket-factory.h"
|
||||
#include "ns3/simulator.h"
|
||||
#include "ns3/empty-trace-resolver.h"
|
||||
|
||||
@@ -33,16 +34,23 @@ Node::Node()
|
||||
: m_id(0),
|
||||
m_sid(0)
|
||||
{
|
||||
SetInterfaceId (Node::iid);
|
||||
m_id = NodeList::Add (this);
|
||||
Construct ();
|
||||
}
|
||||
|
||||
Node::Node(uint32_t sid)
|
||||
: m_id(0),
|
||||
m_sid(sid)
|
||||
{
|
||||
Construct ();
|
||||
}
|
||||
|
||||
void
|
||||
Node::Construct (void)
|
||||
{
|
||||
SetInterfaceId (Node::iid);
|
||||
m_id = NodeList::Add (this);
|
||||
Ptr<PacketSocketFactory> socketFactory = Create<PacketSocketFactory> ();
|
||||
AddInterface (socketFactory);
|
||||
}
|
||||
|
||||
Node::~Node ()
|
||||
@@ -69,8 +77,8 @@ Node::GetSystemId (void) const
|
||||
uint32_t
|
||||
Node::AddDevice (Ptr<NetDevice> device)
|
||||
{
|
||||
uint32_t index = m_devices.size ();
|
||||
m_devices.push_back (device);
|
||||
uint32_t index = m_devices.size ();
|
||||
device->SetIfIndex(index);
|
||||
device->SetReceiveCallback (MakeCallback (&Node::ReceiveFromDevice, this));
|
||||
NotifyDeviceAdded (device);
|
||||
@@ -79,7 +87,14 @@ Node::AddDevice (Ptr<NetDevice> device)
|
||||
Ptr<NetDevice>
|
||||
Node::GetDevice (uint32_t index) const
|
||||
{
|
||||
return m_devices[index];
|
||||
if (index == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_devices[index - 1];
|
||||
}
|
||||
}
|
||||
uint32_t
|
||||
Node::GetNDevices (void) const
|
||||
@@ -143,24 +158,11 @@ Node::RegisterProtocolHandler (ProtocolHandler handler,
|
||||
{
|
||||
struct Node::ProtocolHandlerEntry entry;
|
||||
entry.handler = handler;
|
||||
entry.isSpecificProtocol = true;
|
||||
entry.protocol = protocolType;
|
||||
entry.device = device;
|
||||
m_handlers.push_back (entry);
|
||||
}
|
||||
|
||||
void
|
||||
Node::RegisterProtocolHandler (ProtocolHandler handler,
|
||||
Ptr<NetDevice> device)
|
||||
{
|
||||
struct Node::ProtocolHandlerEntry entry;
|
||||
entry.handler = handler;
|
||||
entry.isSpecificProtocol = false;
|
||||
entry.protocol = 0;
|
||||
entry.device = device;
|
||||
m_handlers.push_back (entry);
|
||||
}
|
||||
|
||||
void
|
||||
Node::UnregisterProtocolHandler (ProtocolHandler handler)
|
||||
{
|
||||
@@ -176,7 +178,8 @@ Node::UnregisterProtocolHandler (ProtocolHandler handler)
|
||||
}
|
||||
|
||||
bool
|
||||
Node::ReceiveFromDevice (Ptr<NetDevice> device, const Packet &packet, uint16_t protocol)
|
||||
Node::ReceiveFromDevice (Ptr<NetDevice> device, const Packet &packet,
|
||||
uint16_t protocol, const Address &from)
|
||||
{
|
||||
bool found = false;
|
||||
for (ProtocolHandlerList::iterator i = m_handlers.begin ();
|
||||
@@ -185,10 +188,10 @@ Node::ReceiveFromDevice (Ptr<NetDevice> device, const Packet &packet, uint16_t p
|
||||
if (i->device == 0 ||
|
||||
(i->device != 0 && i->device == device))
|
||||
{
|
||||
if (!i->isSpecificProtocol ||
|
||||
(i->isSpecificProtocol && i->protocol == protocol))
|
||||
if (i->protocol == 0 ||
|
||||
i->protocol == protocol)
|
||||
{
|
||||
i->handler (packet, protocol, device);
|
||||
i->handler (device, packet, protocol, from);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ class TraceResolver;
|
||||
class NetDevice;
|
||||
class Application;
|
||||
class Packet;
|
||||
class Address;
|
||||
|
||||
/**
|
||||
* \brief A network Node.
|
||||
@@ -56,6 +57,17 @@ class Node : public Object
|
||||
public:
|
||||
static const InterfaceId iid;
|
||||
|
||||
/**
|
||||
* Must be invoked by subclasses only.
|
||||
*/
|
||||
Node();
|
||||
/**
|
||||
* \param systemId a unique integer used for parallel simulations.
|
||||
*
|
||||
* Must be invoked by subclasses only.
|
||||
*/
|
||||
Node(uint32_t systemId);
|
||||
|
||||
virtual ~Node();
|
||||
|
||||
/**
|
||||
@@ -91,11 +103,15 @@ public:
|
||||
* Associate this device to this node.
|
||||
* This method is called automatically from NetDevice::NetDevice
|
||||
* so the user has little reason to call this method himself.
|
||||
* The index returned is always non-zero.
|
||||
*/
|
||||
uint32_t AddDevice (Ptr<NetDevice> device);
|
||||
/**
|
||||
* \param index the index of the requested NetDevice
|
||||
* \returns the requested NetDevice associated to this Node.
|
||||
*
|
||||
* The indexes used by the GetDevice method start at one and
|
||||
* end at GetNDevices ()
|
||||
*/
|
||||
Ptr<NetDevice> GetDevice (uint32_t index) const;
|
||||
/**
|
||||
@@ -128,11 +144,15 @@ public:
|
||||
/**
|
||||
* A protocol handler
|
||||
*/
|
||||
typedef Callback<void,const Packet &,uint16_t,Ptr<NetDevice> > ProtocolHandler;
|
||||
typedef Callback<void,Ptr<NetDevice>, const Packet &,uint16_t,const Address &> ProtocolHandler;
|
||||
/**
|
||||
* \param handler the handler to register
|
||||
* \param protocolType the type of protocol this handler is
|
||||
* interested in.
|
||||
* interested in. This protocol type is a so-called
|
||||
* EtherType, as registered here:
|
||||
* http://standards.ieee.org/regauth/ethertype/eth.txt
|
||||
* the value zero is interpreted as matching all
|
||||
* protocols.
|
||||
* \param device the device attached to this handler. If the
|
||||
* value is zero, the handler is attached to all
|
||||
* devices on this node.
|
||||
@@ -140,18 +160,6 @@ public:
|
||||
void RegisterProtocolHandler (ProtocolHandler handler,
|
||||
uint16_t protocolType,
|
||||
Ptr<NetDevice> device);
|
||||
/**
|
||||
* \param handler the handler to register
|
||||
* \param device the device attached to this handler. If the
|
||||
* value is zero, the handler is attached to all
|
||||
* devices on this node.
|
||||
*
|
||||
* Register a handler to receive all packets for all
|
||||
* protocols.
|
||||
*/
|
||||
void RegisterProtocolHandler (ProtocolHandler handler,
|
||||
Ptr<NetDevice> device);
|
||||
|
||||
/**
|
||||
* \param handler the handler to unregister
|
||||
*
|
||||
@@ -161,16 +169,6 @@ public:
|
||||
void UnregisterProtocolHandler (ProtocolHandler handler);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Must be invoked by subclasses only.
|
||||
*/
|
||||
Node();
|
||||
/**
|
||||
* \param systemId a unique integer used for parallel simulations.
|
||||
*
|
||||
* Must be invoked by subclasses only.
|
||||
*/
|
||||
Node(uint32_t systemId);
|
||||
/**
|
||||
* The dispose method. Subclasses must override this method
|
||||
* and must chain up to it by calling Node::DoDispose at the
|
||||
@@ -195,11 +193,12 @@ private:
|
||||
*/
|
||||
virtual void NotifyDeviceAdded (Ptr<NetDevice> device);
|
||||
|
||||
bool ReceiveFromDevice (Ptr<NetDevice> device, const Packet &packet, uint16_t protocol);
|
||||
bool ReceiveFromDevice (Ptr<NetDevice> device, const Packet &packet,
|
||||
uint16_t protocol, const Address &from);
|
||||
void Construct (void);
|
||||
|
||||
struct ProtocolHandlerEntry {
|
||||
ProtocolHandler handler;
|
||||
bool isSpecificProtocol;
|
||||
uint16_t protocol;
|
||||
Ptr<NetDevice> device;
|
||||
};
|
||||
|
||||
@@ -5,19 +5,19 @@ namespace ns3 {
|
||||
Socket::~Socket ()
|
||||
{}
|
||||
|
||||
void
|
||||
int
|
||||
Socket::Close(Callback<void, Ptr<Socket> > closeCompleted)
|
||||
{
|
||||
DoClose (closeCompleted);
|
||||
return DoClose (closeCompleted);
|
||||
}
|
||||
|
||||
void
|
||||
int
|
||||
Socket::Connect(const Address & address,
|
||||
Callback<void, Ptr<Socket> > connectionSucceeded,
|
||||
Callback<void, Ptr<Socket> > connectionFailed,
|
||||
Callback<void, Ptr<Socket> > halfClose)
|
||||
{
|
||||
DoConnect (address, connectionSucceeded, connectionFailed, halfClose);
|
||||
return DoConnect (address, connectionSucceeded, connectionFailed, halfClose);
|
||||
}
|
||||
int
|
||||
Socket::Accept(Callback<bool, Ptr<Socket>, const Address&> connectionRequest,
|
||||
|
||||
@@ -52,7 +52,9 @@ public:
|
||||
ERROR_AGAIN,
|
||||
ERROR_SHUTDOWN,
|
||||
ERROR_OPNOTSUPP,
|
||||
ERROR_AFNOSUPPORT,
|
||||
ERROR_INVAL,
|
||||
ERROR_BADF,
|
||||
SOCKET_ERRNO_LAST
|
||||
};
|
||||
|
||||
@@ -92,7 +94,7 @@ public:
|
||||
* After the Close call, the socket is no longer valid, and cannot
|
||||
* safely be used for subsequent operations.
|
||||
*/
|
||||
void Close(Callback<void, Ptr<Socket> > closeCompleted = MakeCallback (&Socket::DummyCallbackVoidSocket));
|
||||
int Close(Callback<void, Ptr<Socket> > closeCompleted = MakeCallback (&Socket::DummyCallbackVoidSocket));
|
||||
|
||||
/**
|
||||
* \returns zero on success, -1 on failure.
|
||||
@@ -122,10 +124,10 @@ public:
|
||||
* \param halfClose XXX When exactly is this callback invoked ? If it invoked when the
|
||||
* other side closes the connection ? Or when I call Close ?
|
||||
*/
|
||||
void Connect(const Address &address,
|
||||
Callback<void, Ptr<Socket> > connectionSucceeded = MakeCallback(&Socket::DummyCallbackVoidSocket),
|
||||
Callback<void, Ptr<Socket> > connectionFailed = MakeCallback(&Socket::DummyCallbackVoidSocket),
|
||||
Callback<void, Ptr<Socket> > halfClose = MakeCallback(&Socket::DummyCallbackVoidSocket));
|
||||
int Connect(const Address &address,
|
||||
Callback<void, Ptr<Socket> > connectionSucceeded = MakeCallback(&Socket::DummyCallbackVoidSocket),
|
||||
Callback<void, Ptr<Socket> > connectionFailed = MakeCallback(&Socket::DummyCallbackVoidSocket),
|
||||
Callback<void, Ptr<Socket> > halfClose = MakeCallback(&Socket::DummyCallbackVoidSocket));
|
||||
|
||||
/**
|
||||
* \brief Accept connection requests from remote hosts
|
||||
@@ -201,11 +203,11 @@ public:
|
||||
MakeCallback (&Socket::DummyCallbackVoidSocketUi32Address));
|
||||
|
||||
private:
|
||||
virtual void DoClose(Callback<void, Ptr<Socket> > closeCompleted) = 0;
|
||||
virtual void DoConnect(const Address & address,
|
||||
Callback<void, Ptr<Socket> > connectionSucceeded,
|
||||
Callback<void, Ptr<Socket> > connectionFailed,
|
||||
Callback<void, Ptr<Socket> > halfClose) = 0;
|
||||
virtual int DoClose(Callback<void, Ptr<Socket> > closeCompleted) = 0;
|
||||
virtual int DoConnect(const Address & address,
|
||||
Callback<void, Ptr<Socket> > connectionSucceeded,
|
||||
Callback<void, Ptr<Socket> > connectionFailed,
|
||||
Callback<void, Ptr<Socket> > halfClose) = 0;
|
||||
virtual int DoAccept(Callback<bool, Ptr<Socket>, const Address&> connectionRequest,
|
||||
Callback<void, Ptr<Socket>, const Address&> newConnectionCreated,
|
||||
Callback<void, Ptr<Socket> > closeRequested) = 0;
|
||||
|
||||
Reference in New Issue
Block a user