split callback registration from socket operations in socket.h API.
--- a/samples/main-simple.cc Wed Aug 01 17:24:55 2007 +0200
+++ b/samples/main-simple.cc Wed Aug 01 18:48:24 2007 +0200
@@ -33,7 +33,8 @@
static void
PrintTraffic (Ptr<Socket> socket)
{
- socket->RecvDummy (MakeCallback (&SocketPrinter));
+ socket->SetRecvCallback (MakeNullCallback<void,Ptr<Socket>,const uint8_t *,uint32_t,const Address &> (),
+ MakeCallback (&SocketPrinter));
}
void
--- a/src/internet-node/ipv4-loopback-interface.cc Wed Aug 01 17:24:55 2007 +0200
+++ b/src/internet-node/ipv4-loopback-interface.cc Wed Aug 01 18:48:24 2007 +0200
@@ -22,6 +22,7 @@
#include "ns3/empty-trace-resolver.h"
#include "ns3/net-device.h"
#include "ns3/node.h"
+#include "ns3/eui48-address.h"
#include "ipv4-loopback-interface.h"
#include "ipv4-l3-protocol.h"
@@ -44,7 +45,7 @@
Ipv4LoopbackInterface::SendTo (Packet packet, Ipv4Address dest)
{
Ptr<Ipv4L3Protocol> ipv4 = m_node->QueryInterface<Ipv4L3Protocol> (Ipv4L3Protocol::iid);
- ipv4->Receive (GetDevice (), packet, Ipv4L3Protocol::PROT_NUMBER, GetDevice ()->GetAddress ());
+ ipv4->Receive (GetDevice (), packet, Ipv4L3Protocol::PROT_NUMBER, Eui48Address ("ff:ff:ff:ff:ff:ff").ConvertTo ());
}
}//namespace ns3
--- a/src/internet-node/udp-socket.cc Wed Aug 01 17:24:55 2007 +0200
+++ b/src/internet-node/udp-socket.cc Wed Aug 01 18:48:24 2007 +0200
@@ -57,6 +57,12 @@
m_udp = 0;
}
+enum Socket::SocketErrno
+UdpSocket::GetErrno (void) const
+{
+ return m_errno;
+}
+
Ptr<Node>
UdpSocket::GetNode (void) const
{
@@ -118,11 +124,6 @@
return FinishBind ();
}
-enum Socket::SocketErrno
-UdpSocket::GetErrno (void) const
-{
- return m_errno;
-}
int
UdpSocket::ShutdownSend (void)
{
@@ -137,44 +138,25 @@
}
int
-UdpSocket::DoClose(Callback<void, Ptr<Socket> > closeCompleted)
+UdpSocket::Close(void)
{
- // XXX: we should set the close state and check it in all API methods.
- if (!closeCompleted.IsNull ())
- {
- closeCompleted (this);
- }
+ NotifyCloseCompleted ();
return 0;
}
+
int
-UdpSocket::DoConnect(const Address & address,
- Callback<void, Ptr<Socket> > connectionSucceeded,
- Callback<void, Ptr<Socket> > connectionFailed,
- Callback<void, Ptr<Socket> > halfClose)
+UdpSocket::Connect(const Address & address)
{
InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
m_defaultAddress = transport.GetIpv4 ();
m_defaultPort = transport.GetPort ();
- if (!connectionSucceeded.IsNull ())
- {
- connectionSucceeded (this);
- }
+ NotifyConnectionSucceeded ();
m_connected = true;
return 0;
}
-int
-UdpSocket::DoAccept(Callback<bool, Ptr<Socket>, const Address&> connectionRequest,
- Callback<void, Ptr<Socket>, const Address&> newConnectionCreated,
- Callback<void, Ptr<Socket> > closeRequested)
-{
- // calling accept on a udp socket is a programming error.
- m_errno = ERROR_OPNOTSUPP;
- return -1;
-}
int
-UdpSocket::DoSend (const uint8_t* buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent)
+UdpSocket::Send (const uint8_t* buffer,
+ uint32_t size)
{
if (!m_connected)
{
@@ -190,20 +172,18 @@
{
p = Packet (buffer, size);
}
- return DoSendPacketTo (p, m_defaultAddress, m_defaultPort, dataSent);
+ return DoSendTo (p, m_defaultAddress, m_defaultPort);
}
int
-UdpSocket::DoSendPacketTo (const Packet &p, const Address &address,
- Callback<void, Ptr<Socket>, uint32_t> dataSent)
+UdpSocket::DoSendTo (const Packet &p, const Address &address)
{
InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
Ipv4Address ipv4 = transport.GetIpv4 ();
uint16_t port = transport.GetPort ();
- return DoSendPacketTo (p, ipv4, port, dataSent);
+ return DoSendTo (p, ipv4, port);
}
int
-UdpSocket::DoSendPacketTo (const Packet &p, Ipv4Address ipv4, uint16_t port,
- Callback<void, Ptr<Socket>, uint32_t> dataSent)
+UdpSocket::DoSendTo (const Packet &p, Ipv4Address ipv4, uint16_t port)
{
if (m_endPoint == 0)
{
@@ -221,17 +201,13 @@
}
m_udp->Send (p, m_endPoint->GetLocalAddress (), ipv4,
m_endPoint->GetLocalPort (), port);
- if (!dataSent.IsNull ())
- {
- dataSent (this, p.GetSize ());
- }
+ NotifyDataSent (p.GetSize ());
return 0;
}
int
-UdpSocket::DoSendTo(const Address &address,
+UdpSocket::SendTo(const Address &address,
const uint8_t *buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent)
+ uint32_t size)
{
if (m_connected)
{
@@ -250,17 +226,7 @@
InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
Ipv4Address ipv4 = transport.GetIpv4 ();
uint16_t port = transport.GetPort ();
- return DoSendPacketTo (p, ipv4, port, dataSent);
-}
-void
-UdpSocket::DoRecv(Callback<void, Ptr<Socket>, const uint8_t*, uint32_t,const Address&> callback)
-{
- m_rxCallback = callback;
-}
-void
-UdpSocket::DoRecvDummy(Callback<void, Ptr<Socket>, uint32_t,const Address&> callback)
-{
- m_dummyRxCallback = callback;
+ return DoSendTo (p, ipv4, port);
}
void
@@ -273,14 +239,7 @@
Address address = InetSocketAddress (ipv4, port).ConvertTo ();
Packet p = packet;
- if (!m_dummyRxCallback.IsNull ())
- {
- m_dummyRxCallback (this, p.GetSize (), address);
- }
- if (!m_rxCallback.IsNull ())
- {
- m_rxCallback (this, p.PeekData (), p.GetSize (), address);
- }
+ NotifyDataReceived (p, address);
}
}//namespace ns3
--- a/src/internet-node/udp-socket.h Wed Aug 01 17:24:55 2007 +0200
+++ b/src/internet-node/udp-socket.h Wed Aug 01 18:48:24 2007 +0200
@@ -47,27 +47,14 @@
virtual Ptr<Node> GetNode (void) const;
virtual int Bind (void);
virtual int Bind (const Address &address);
+ virtual int Close (void);
virtual int ShutdownSend (void);
virtual int ShutdownRecv (void);
+ virtual int Connect(const Address &address);
+ virtual int Send (const uint8_t* buffer, uint32_t size);
+ virtual int SendTo(const Address &address,const uint8_t *buffer, uint32_t size);
private:
- virtual int DoClose(Callback<void, Ptr<Socket> > closeCompleted);
- virtual int DoConnect(const Address & address,
- Callback<void, Ptr<Socket> > connectionSucceeded,
- Callback<void, Ptr<Socket> > connectionFailed,
- Callback<void, Ptr<Socket> > halfClose);
- virtual int DoAccept(Callback<bool, Ptr<Socket>, const Address&> connectionRequest,
- Callback<void, Ptr<Socket>, const Address&> newConnectionCreated,
- Callback<void, Ptr<Socket> > closeRequested);
- virtual int DoSend (const uint8_t* buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent);
- virtual int DoSendTo(const Address &address,
- const uint8_t *buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent);
- virtual void DoRecv(Callback<void, Ptr<Socket>, const uint8_t*, uint32_t,const Address&>);
- virtual void DoRecvDummy(Callback<void, Ptr<Socket>, uint32_t,const Address&>);
private:
friend class Udp;
@@ -75,10 +62,8 @@
int FinishBind (void);
void ForwardUp (const Packet &p, Ipv4Address ipv4, uint16_t port);
void Destroy (void);
- int DoSendPacketTo (const Packet &p, const Address &daddr,
- Callback<void, Ptr<Socket>, uint32_t> dataSent);
- int DoSendPacketTo (const Packet &p, Ipv4Address daddr, uint16_t dport,
- Callback<void, Ptr<Socket>, uint32_t> dataSent);
+ int DoSendTo (const Packet &p, const Address &daddr);
+ int DoSendTo (const Packet &p, Ipv4Address daddr, uint16_t dport);
Ipv4EndPoint *m_endPoint;
Ptr<Node> m_node;
--- a/src/node/packet-socket.cc Wed Aug 01 17:24:55 2007 +0200
+++ b/src/node/packet-socket.cc Wed Aug 01 18:48:24 2007 +0200
@@ -52,13 +52,18 @@
m_device = 0;
}
+enum Socket::SocketErrno
+PacketSocket::GetErrno (void) const
+{
+ return m_errno;
+}
+
Ptr<Node>
PacketSocket::GetNode (void) const
{
return m_node;
}
-
int
PacketSocket::Bind (void)
{
@@ -111,11 +116,6 @@
return 0;
}
-enum Socket::SocketErrno
-PacketSocket::GetErrno (void) const
-{
- return m_errno;
-}
int
PacketSocket::ShutdownSend (void)
{
@@ -139,26 +139,20 @@
return 0;
}
int
-PacketSocket::DoClose(ns3::Callback<void, Ptr<Socket> > closeCompleted)
+PacketSocket::Close(void)
{
if (m_state == STATE_CLOSED)
{
m_errno = ERROR_BADF;
return -1;
}
- if (!closeCompleted.IsNull ())
- {
- closeCompleted (this);
- }
m_state = STATE_CLOSED;
+ NotifyCloseCompleted ();
return 0;
}
int
-PacketSocket::DoConnect(const Address &ad,
- ns3::Callback<void, Ptr<Socket> > connectionSucceeded,
- ns3::Callback<void, Ptr<Socket> > connectionFailed,
- ns3::Callback<void, Ptr<Socket> > halfClose)
+PacketSocket::Connect(const Address &ad)
{
PacketSocketAddress address;
if (m_state == STATE_CLOSED)
@@ -184,33 +178,16 @@
}
m_destAddr = ad;
m_state = STATE_CONNECTED;
- if (!connectionSucceeded.IsNull ())
- {
- connectionSucceeded (this);
- }
+ NotifyConnectionSucceeded ();
return 0;
error:
- if (!connectionFailed.IsNull ())
- {
- connectionFailed (this);
- }
+ NotifyConnectionFailed ();
return -1;
}
int
-PacketSocket::DoAccept(ns3::Callback<bool, Ptr<Socket>, const Address &> connectionRequest,
- ns3::Callback<void, Ptr<Socket>, const Address &> newConnectionCreated,
- ns3::Callback<void, Ptr<Socket> > closeRequested)
-{
- // calling accept on a packet socket is a programming error.
- m_errno = ERROR_OPNOTSUPP;
- return -1;
-}
-
-int
-PacketSocket::DoSend (const uint8_t* buffer,
- uint32_t size,
- ns3::Callback<void, Ptr<Socket>, uint32_t> dataSent)
+PacketSocket::Send (const uint8_t* buffer,
+ uint32_t size)
{
if (m_state == STATE_OPEN ||
m_state == STATE_BOUND)
@@ -218,14 +195,13 @@
m_errno = ERROR_NOTCONN;
return -1;
}
- return DoSendTo (m_destAddr, buffer, size, dataSent);
+ return SendTo (m_destAddr, buffer, size);
}
int
-PacketSocket::DoSendTo(const Address &address,
+PacketSocket::SendTo(const Address &address,
const uint8_t *buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent)
+ uint32_t size)
{
PacketSocketAddress ad;
if (m_state == STATE_CLOSED)
@@ -282,9 +258,9 @@
}
}
}
- if (!error && !dataSent.IsNull ())
+ if (!error)
{
- dataSent (this, p.GetSize ());
+ NotifyDataSent (p.GetSize ());
}
if (error)
@@ -299,18 +275,6 @@
}
void
-PacketSocket::DoRecv(ns3::Callback<void, Ptr<Socket>, const uint8_t*, uint32_t,const Address &> callback)
-{
- m_rxCallback = callback;
-}
-
-void
-PacketSocket::DoRecvDummy(ns3::Callback<void, Ptr<Socket>, uint32_t, const Address &> callback)
-{
- m_dummyRxCallback = callback;
-}
-
-void
PacketSocket::ForwardUp (Ptr<NetDevice> device, const Packet &packet,
uint16_t protocol, const Address &from)
{
@@ -328,14 +292,7 @@
NS_DEBUG ("PacketSocket::ForwardUp: UID is " << packet.GetUid()
<< " PacketSocket " << this);
- if (!m_dummyRxCallback.IsNull ())
- {
- m_dummyRxCallback (this, p.GetSize (), address.ConvertTo ());
- }
- if (!m_rxCallback.IsNull ())
- {
- m_rxCallback (this, p.PeekData (), p.GetSize (), address.ConvertTo ());
- }
+ NotifyDataReceived (p, address.ConvertTo ());
}
}//namespace ns3
--- a/src/node/packet-socket.h Wed Aug 01 17:24:55 2007 +0200
+++ b/src/node/packet-socket.h Wed Aug 01 18:48:24 2007 +0200
@@ -78,27 +78,15 @@
virtual Ptr<Node> GetNode (void) const;
virtual int Bind (void);
virtual int Bind (const Address & address);
+ virtual int Close (void);
virtual int ShutdownSend (void);
virtual int ShutdownRecv (void);
+ virtual int Connect(const Address &address);
+ virtual int Send (const uint8_t* buffer, uint32_t size);
+ virtual int SendTo(const Address &address,const uint8_t *buffer, uint32_t size);
+
private:
- virtual int DoClose(Callback<void, Ptr<Socket> > closeCompleted);
- virtual int DoConnect(const Address & address,
- Callback<void, Ptr<Socket> > connectionSucceeded,
- Callback<void, Ptr<Socket> > connectionFailed,
- Callback<void, Ptr<Socket> > halfClose);
- virtual int DoAccept(Callback<bool, Ptr<Socket>, const Address&> connectionRequest,
- Callback<void, Ptr<Socket>, const Address&> newConnectionCreated,
- Callback<void, Ptr<Socket> > closeRequested);
- virtual int DoSend (const uint8_t* buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent);
- virtual int DoSendTo(const Address &address,
- const uint8_t *buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent);
- virtual void DoRecv(Callback<void, Ptr<Socket>, const uint8_t*, uint32_t,const Address&> receive);
- virtual void DoRecvDummy(Callback<void, Ptr<Socket>, uint32_t,const Address&>);
private:
void Init (void);
@@ -114,8 +102,6 @@
STATE_CLOSED
};
Ptr<Node> m_node;
- Callback<void,Ptr<Socket>,uint32_t,const Address &> m_dummyRxCallback;
- Callback<void,Ptr<Socket>,uint8_t const*,uint32_t, const Address &> m_rxCallback;
enum SocketErrno m_errno;
bool m_shutdownSend;
bool m_shutdownRecv;
--- a/src/node/socket.cc Wed Aug 01 17:24:55 2007 +0200
+++ b/src/node/socket.cc Wed Aug 01 18:48:24 2007 +0200
@@ -1,79 +1,150 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2006 Georgia Tech Research Corporation
+ * 2007 INRIA
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Authors: George F. Riley<riley@ece.gatech.edu>
+ * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
+ */
#include "socket.h"
+#include "ns3/packet.h"
namespace ns3 {
Socket::~Socket ()
{}
-int
-Socket::Close(Callback<void, Ptr<Socket> > closeCompleted)
+void
+Socket::SetCloseCallback (Callback<void,Ptr<Socket> > closeCompleted)
+{
+ m_closeCompleted = closeCompleted;
+}
+void
+Socket::SetConnectCallback (Callback<void, Ptr<Socket> > connectionSucceeded,
+ Callback<void, Ptr<Socket> > connectionFailed,
+ Callback<void, Ptr<Socket> > halfClose)
{
- return DoClose (closeCompleted);
+ m_connectionSucceeded = connectionSucceeded;
+ m_connectionFailed = connectionFailed;
+ m_halfClose = halfClose;
+}
+void
+Socket::SetAcceptCallback (Callback<bool, Ptr<Socket>, const Address &> connectionRequest,
+ Callback<void, Ptr<Socket>, const Address&> newConnectionCreated,
+ Callback<void, Ptr<Socket> > closeRequested)
+{
+ m_connectionRequest = connectionRequest;
+ m_newConnectionCreated = newConnectionCreated;
+ m_closeRequested = closeRequested;
+}
+void
+Socket::SetSendCallback (Callback<void, Ptr<Socket>, uint32_t> dataSent)
+{
+ m_dataSent = dataSent;
+}
+void
+Socket::SetRecvCallback (Callback<void, Ptr<Socket>, const uint8_t*, uint32_t,const Address&> receivedData,
+ Callback<void, Ptr<Socket>, uint32_t,const Address&> receivedDummyData)
+{
+ m_receivedData = receivedData;
+ m_receivedDummyData = receivedDummyData;
}
-int
-Socket::Connect(const Address & address,
- Callback<void, Ptr<Socket> > connectionSucceeded,
- Callback<void, Ptr<Socket> > connectionFailed,
- Callback<void, Ptr<Socket> > halfClose)
+void
+Socket::NotifyCloseCompleted (void)
{
- return DoConnect (address, connectionSucceeded, connectionFailed, halfClose);
-}
-int
-Socket::Accept(Callback<bool, Ptr<Socket>, const Address&> connectionRequest,
- Callback<void, Ptr<Socket>, const Address&> newConnectionCreated,
- Callback<void, Ptr<Socket> > closeRequested)
-{
- return DoAccept (connectionRequest, newConnectionCreated, closeRequested);
+ if (!m_closeCompleted.IsNull ())
+ {
+ m_closeCompleted (this);
+ }
}
-int
-Socket::Send (const uint8_t* buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent)
+void
+Socket::NotifyConnectionSucceeded (void)
{
- return DoSend (buffer, size, dataSent);
+ if (!m_connectionSucceeded.IsNull ())
+ {
+ m_connectionSucceeded (this);
+ }
}
-int
-Socket::SendTo(const Address &address,
- const uint8_t *buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent)
+void
+Socket::NotifyConnectionFailed (void)
{
- return DoSendTo (address, buffer, size, dataSent);
+ if (!m_connectionFailed.IsNull ())
+ {
+ m_connectionFailed (this);
+ }
}
void
-Socket::Recv(Callback<void, Ptr<Socket>, const uint8_t*, uint32_t,const Address&> callback)
+Socket::NotifyHalfClose (void)
+{
+ if (!m_halfClose.IsNull ())
+ {
+ m_halfClose (this);
+ }
+}
+bool
+Socket::NotifyConnectionRequest (const Address &from)
{
- DoRecv (callback);
+ if (!m_connectionRequest.IsNull ())
+ {
+ return m_connectionRequest (this, from);
+ }
+ else
+ {
+ // refuse all incomming connections by default.
+ return false;
+ }
}
void
-Socket::RecvDummy(Callback<void, Ptr<Socket>, uint32_t,const Address&> callback)
+Socket::NotifyNewConnectionCreated (Ptr<Socket> socket, const Address &from)
+{
+ if (!m_newConnectionCreated.IsNull ())
+ {
+ m_newConnectionCreated (socket, from);
+ }
+}
+void
+Socket::NotifyCloseRequested (void)
{
- DoRecvDummy (callback);
+ if (!m_closeRequested.IsNull ())
+ {
+ m_closeRequested (this);
+ }
+}
+void
+Socket::NotifyDataSent (uint32_t size)
+{
+ if (!m_dataSent.IsNull ())
+ {
+ m_dataSent (this, size);
+ }
+}
+void
+Socket::NotifyDataReceived (const Packet &p, const Address &from)
+{
+ if (!m_receivedData.IsNull ())
+ {
+ m_receivedData (this, p.PeekData (), p.GetSize (), from);
+ }
+ if (!m_receivedDummyData.IsNull ())
+ {
+ m_receivedDummyData (this, p.GetSize (), from);
+ }
}
-bool
-Socket::RefuseAllConnections (Ptr<Socket> socket, const Address& address)
-{
- return false;
-}
-void
-Socket::DummyCallbackVoidSocket (Ptr<Socket> socket)
-{}
-void
-Socket::DummyCallbackVoidSocketUi32 (Ptr<Socket> socket, uint32_t)
-{}
-void
-Socket::DummyCallbackVoidSocketUi32Address (Ptr<Socket> socket, uint32_t, const Address &)
-{}
-void
-Socket::DummyCallbackVoidSocketBufferUi32Address (Ptr<Socket> socket, const uint8_t *, uint32_t,
- const Address &)
-{}
-void
-Socket::DummyCallbackVoidSocketAddress (Ptr<Socket> socket, const Address &)
-{}
-
}//namespace ns3
--- a/src/node/socket.h Wed Aug 01 17:24:55 2007 +0200
+++ b/src/node/socket.h Wed Aug 01 18:48:24 2007 +0200
@@ -1,22 +1,24 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
-//
-// Copyright (c) 2006 Georgia Tech Research Corporation
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License version 2 as
-// published by the Free Software Foundation;
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
-// Author: George F. Riley<riley@ece.gatech.edu>
-//
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2006 Georgia Tech Research Corporation
+ * 2007 INRIA
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Authors: George F. Riley<riley@ece.gatech.edu>
+ * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
+ */
#ifndef __SOCKET_H__
#define __SOCKET_H__
@@ -30,6 +32,7 @@
namespace ns3 {
class Node;
+class Packet;
/**
* \brief Define a Socket API based on the BSD Socket API.
@@ -70,6 +73,60 @@
*/
virtual Ptr<Node> GetNode (void) const = 0;
+ /**
+ * \param closeCompleted Callback invoked when the close operation is
+ * completed.
+ */
+ void SetCloseCallback (Callback<void,Ptr<Socket> > closeCompleted);
+
+ /**
+ * \param connectionSucceeded this callback is invoked when the connection request
+ * initiated by the user is successfully completed. The callback is passed
+ * back a pointer to the same socket object.
+ * \param connectionFailed this callback is invoked when the connection request
+ * initiated by the user is unsuccessfully completed. The callback is passed
+ * back a pointer to the same socket object.
+ * \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 SetConnectCallback (Callback<void, Ptr<Socket> > connectionSucceeded,
+ Callback<void, Ptr<Socket> > connectionFailed,
+ Callback<void, Ptr<Socket> > halfClose);
+ /**
+ * \brief Accept connection requests from remote hosts
+ * \param connectionRequest Callback for connection request from peer.
+ * This user callback is passed a pointer to this socket, the
+ * ip address and the port number of the connection originator.
+ * This callback must return true to accept the incoming connection,
+ * false otherwise. If the connection is accepted, the
+ * "newConnectionCreated" callback will be invoked later to give access
+ * to the user to the socket created to match this new connection. If the
+ * user does not explicitely specify this callback, all incoming
+ * connections will be refused.
+ * \param newConnectionCreated Callback for new connection: when a new
+ * is accepted, it is created and the corresponding socket is passed
+ * back to the user through this callback. This user callback is passed
+ * a pointer to the new socket, and the ip address and port number
+ * of the connection originator.
+ * \param closeRequested Callback for connection close request from peer.
+ * XXX: when is this callback invoked ?
+ */
+ void SetAcceptCallback (Callback<bool, Ptr<Socket>, const Address &> connectionRequest,
+ Callback<void, Ptr<Socket>, const Address&> newConnectionCreated,
+ Callback<void, Ptr<Socket> > closeRequested);
+ void SetSendCallback (Callback<void, Ptr<Socket>, uint32_t> dataSent);
+ /**
+ * \brief Receive data
+ * \param receivedData Invoked whenever new data is received.
+ *
+ * If you wish to transport only dummy packets, this method is not a very
+ * efficient way to receive these dummy packets: it will trigger a memory
+ * allocation to hold the dummy memory into a buffer which can be passed
+ * to the user. Instead, consider using the RecvDummy method.
+ */
+ void SetRecvCallback (Callback<void, Ptr<Socket>, const uint8_t*, uint32_t,const Address&> receivedData,
+ Callback<void, Ptr<Socket>, uint32_t,const Address&> receivedDummyData);
+
/**
* \param address the address to try to allocate
* \returns 0 on success, -1 on failure.
@@ -85,16 +142,13 @@
*/
virtual int Bind () = 0;
-
/**
* \brief Close a socket.
- * \param closeCompleted Callback invoked when the close operation is
- * completed.
*
* After the Close call, the socket is no longer valid, and cannot
* safely be used for subsequent operations.
*/
- int Close(Callback<void, Ptr<Socket> > closeCompleted = MakeCallback (&Socket::DummyCallbackVoidSocket));
+ virtual int Close(void) = 0;
/**
* \returns zero on success, -1 on failure.
@@ -115,46 +169,10 @@
/**
* \brief Initiate a connection to a remote host
* \param address Address of remote.
- * \param connectionSucceeded this callback is invoked when the connection request
- * initiated by the user is successfully completed. The callback is passed
- * back a pointer to the same socket object.
- * \param connectionFailed this callback is invoked when the connection request
- * initiated by the user is unsuccessfully completed. The callback is passed
- * back a pointer to the same socket object.
- * \param halfClose XXX When exactly is this callback invoked ? If it invoked when the
- * other side closes the connection ? Or when I call Close ?
*/
- 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));
+ virtual int Connect(const Address &address) = 0;
/**
- * \brief Accept connection requests from remote hosts
- * \param connectionRequest Callback for connection request from peer.
- * This user callback is passed a pointer to this socket, the
- * ip address and the port number of the connection originator.
- * This callback must return true to accept the incoming connection,
- * false otherwise. If the connection is accepted, the
- * "newConnectionCreated" callback will be invoked later to give access
- * to the user to the socket created to match this new connection. If the
- * user does not explicitely specify this callback, all incoming
- * connections will be refused.
- * \param newConnectionCreated Callback for new connection: when a new
- * is accepted, it is created and the corresponding socket is passed
- * back to the user through this callback. This user callback is passed
- * a pointer to the new socket, and the ip address and port number
- * of the connection originator.
- * \param closeRequested Callback for connection close request from peer.
- * XXX: when is this callback invoked ?
- */
- int Accept(Callback<bool, Ptr<Socket>, const Address &> connectionRequest =
- MakeCallback(&Socket::RefuseAllConnections),
- Callback<void, Ptr<Socket>, const Address&> newConnectionCreated =
- MakeCallback (&Socket::DummyCallbackVoidSocketAddress),
- Callback<void, Ptr<Socket> > closeRequested = MakeCallback (&Socket::DummyCallbackVoidSocket));
-
- /**
* \brief Send data (or dummy data) to the remote host
* \param buffer Data to send (nil if dummy data).
* \param size Number of bytes to send.
@@ -162,9 +180,7 @@
* \returns -1 in case of error or the number of bytes copied in the
* internal buffer and accepted for transmission.
*/
- int Send (const uint8_t* buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent = MakeCallback (&Socket::DummyCallbackVoidSocketUi32));
+ virtual int Send (const uint8_t* buffer, uint32_t size) = 0;
/**
* \brief Send data to a specified peer.
@@ -175,60 +191,29 @@
* \returns -1 in case of error or the number of bytes copied in the
* internal buffer and accepted for transmission.
*/
- int SendTo(const Address &address,
- const uint8_t *buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent = MakeCallback (&Socket::DummyCallbackVoidSocketUi32));
-
- /**
- * \brief Receive data
- * \param receivedData Invoked whenever new data is received.
- *
- * If you wish to transport only dummy packets, this method is not a very
- * efficient way to receive these dummy packets: it will trigger a memory
- * allocation to hold the dummy memory into a buffer which can be passed
- * to the user. Instead, consider using the RecvDummy method.
- */
- void Recv(Callback<void, Ptr<Socket>, const uint8_t*, uint32_t,const Address&> receivedData =
- MakeCallback (&Socket::DummyCallbackVoidSocketBufferUi32Address));
-
- /**
- * \brief Receive data
- * \param receivedData Invoked whenever new data is received.
- *
- * This method is included because it is vastly more efficient than the
- * Recv method when you use dummy payload.
- */
- void RecvDummy(Callback<void, Ptr<Socket>, uint32_t,const Address&> receivedData =
- MakeCallback (&Socket::DummyCallbackVoidSocketUi32Address));
+ virtual int SendTo(const Address &address,const uint8_t *buffer, uint32_t size) = 0;
-private:
- 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;
- virtual int DoSend (const uint8_t* buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent) = 0;
- virtual int DoSendTo(const Address &address,
- const uint8_t *buffer,
- uint32_t size,
- Callback<void, Ptr<Socket>, uint32_t> dataSent) = 0;
- virtual void DoRecv(Callback<void, Ptr<Socket>, const uint8_t*, uint32_t,const Address&> receive) = 0;
- virtual void DoRecvDummy(Callback<void, Ptr<Socket>, uint32_t,const Address&>) = 0;
+protected:
+ void NotifyCloseCompleted (void);
+ void NotifyConnectionSucceeded (void);
+ void NotifyConnectionFailed (void);
+ void NotifyHalfClose (void);
+ bool NotifyConnectionRequest (const Address &from);
+ void NotifyNewConnectionCreated (Ptr<Socket> socket, const Address &from);
+ void NotifyCloseRequested (void);
+ void NotifyDataSent (uint32_t size);
+ void NotifyDataReceived (const Packet &p, const Address &from);
-
- static bool RefuseAllConnections (Ptr<Socket> socket, const Address& address);
- static void DummyCallbackVoidSocket (Ptr<Socket> socket);
- static void DummyCallbackVoidSocketUi32 (Ptr<Socket> socket, uint32_t);
- static void DummyCallbackVoidSocketUi32Address (Ptr<Socket> socket, uint32_t, const Address &);
- static void DummyCallbackVoidSocketBufferUi32Address (Ptr<Socket> socket, const uint8_t *, uint32_t,
- const Address &);
- static void DummyCallbackVoidSocketAddress (Ptr<Socket> socket, const Address &);
+ Callback<void,Ptr<Socket> > m_closeCompleted;
+ Callback<void, Ptr<Socket> > m_connectionSucceeded;
+ Callback<void, Ptr<Socket> > m_connectionFailed;
+ Callback<void, Ptr<Socket> > m_halfClose;
+ Callback<void, Ptr<Socket> > m_closeRequested;
+ Callback<bool, Ptr<Socket>, const Address &> m_connectionRequest;
+ Callback<void, Ptr<Socket>, const Address&> m_newConnectionCreated;
+ Callback<void, Ptr<Socket>, uint32_t> m_dataSent;
+ Callback<void, Ptr<Socket>, const uint8_t*, uint32_t,const Address&> m_receivedData;
+ Callback<void, Ptr<Socket>, uint32_t,const Address&> m_receivedDummyData;
};
} //namespace ns3