diff --git a/samples/main-simple.cc b/samples/main-simple.cc index 3be286c46..7285f4946 100644 --- a/samples/main-simple.cc +++ b/samples/main-simple.cc @@ -6,6 +6,7 @@ #include "ns3/socket.h" #include "ns3/inet-socket-address.h" #include "ns3/nstime.h" +#include "ns3/packet.h" using namespace ns3; @@ -13,7 +14,7 @@ static void GenerateTraffic (Ptr socket, uint32_t size) { std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, tx bytes=" << size << std::endl; - socket->Send (0, size); + socket->Send (Packet (size)); if (size > 0) { Simulator::Schedule (Seconds (0.5), &GenerateTraffic, socket, size - 50); @@ -25,15 +26,15 @@ GenerateTraffic (Ptr socket, uint32_t size) } static void -SocketPrinter (Ptr socket, uint32_t size, const Address &from) +SocketPrinter (Ptr socket, const Packet &packet, const Address &from) { - std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << size << std::endl; + std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << packet.GetSize () << std::endl; } static void PrintTraffic (Ptr socket) { - socket->RecvDummy (MakeCallback (&SocketPrinter)); + socket->SetRecvCallback (MakeCallback (&SocketPrinter)); } void diff --git a/src/applications/onoff-application.cc b/src/applications/onoff-application.cc index 5fa2c8266..32bcd7564 100644 --- a/src/applications/onoff-application.cc +++ b/src/applications/onoff-application.cc @@ -31,6 +31,7 @@ #include "ns3/simulator.h" #include "ns3/socket-factory.h" #include "ns3/default-value.h" +#include "ns3/packet.h" #include "onoff-application.h" using namespace std; @@ -205,7 +206,7 @@ void OnOffApplication::ScheduleStopEvent() void OnOffApplication::SendPacket() { NS_ASSERT (m_sendEvent.IsExpired ()); - m_socket->Send(0, m_pktSize); + m_socket->Send(Packet (m_pktSize)); m_totBytes += m_pktSize; m_lastStartTime = Simulator::Now(); m_residualBits = 0; diff --git a/src/common/packet-metadata-test.cc b/src/common/packet-metadata-test.cc index 7f3711735..e3e61c45e 100644 --- a/src/common/packet-metadata-test.cc +++ b/src/common/packet-metadata-test.cc @@ -230,6 +230,7 @@ PacketMetadataTest::PacketMetadataTest () : Test ("PacketMetadata") { m_printer.SetPayloadPrinter (MakeCallback (&PacketMetadataTest::PrintPayload, this)); + m_printer.SetSeparator (""); } PacketMetadataTest::~PacketMetadataTest () diff --git a/src/common/packet.cc b/src/common/packet.cc index 4c1a3b87d..5159f3e2c 100644 --- a/src/common/packet.cc +++ b/src/common/packet.cc @@ -128,7 +128,7 @@ Packet::GetUid (void) const void Packet::PrintTags (std::ostream &os) const { - m_tags.Print (os); + m_tags.Print (os, " "); } void diff --git a/src/common/tags.cc b/src/common/tags.cc index 635f82b65..e2206b7af 100644 --- a/src/common/tags.cc +++ b/src/common/tags.cc @@ -119,14 +119,14 @@ Tags::Remove (uint32_t id) } void -Tags::Print (std::ostream &os) const +Tags::Print (std::ostream &os, std::string separator) const { for (struct TagData *cur = m_next; cur != 0; cur = cur->m_next) { TagRegistry::Print (cur->m_id, cur->m_data, os); if (cur->m_next != 0) { - os << " "; + os << separator; } } } @@ -347,7 +347,7 @@ TagsTest::RunTests (void) ok = false; } g_a = false; - tags.Print (std::cout); + tags.Print (std::cout, ""); if (!g_a) { ok = false; @@ -363,7 +363,7 @@ TagsTest::RunTests (void) } g_b = false; g_a = false; - tags.Print (std::cout); + tags.Print (std::cout, ""); if (!g_a || !g_b) { ok = false; @@ -372,14 +372,14 @@ TagsTest::RunTests (void) Tags other = tags; g_b = false; g_a = false; - other.Print (std::cout); + other.Print (std::cout, ""); if (!g_a || !g_b) { ok = false; } g_b = false; g_a = false; - tags.Print (std::cout); + tags.Print (std::cout, ""); if (!g_a || !g_b) { ok = false; @@ -406,7 +406,7 @@ TagsTest::RunTests (void) } g_b = false; g_a = false; - other.Print (std::cout); + other.Print (std::cout, ""); if (g_a || !g_b) { ok = false; @@ -452,7 +452,7 @@ TagsTest::RunTests (void) tagZ.z = 0; testLastTag.Add (tagZ); g_z = false; - testLastTag.Print (std::cout); + testLastTag.Print (std::cout, ""); if (!g_z) { ok = false; diff --git a/src/common/tags.h b/src/common/tags.h index ff52a49fe..4002ae4f8 100644 --- a/src/common/tags.h +++ b/src/common/tags.h @@ -52,7 +52,7 @@ public: template bool Peek (T &tag) const; - void Print (std::ostream &os) const; + void Print (std::ostream &os, std::string separator) const; uint32_t GetSerializedSize (void) const; void Serialize (Buffer::Iterator i, uint32_t size) const; uint32_t Deserialize (Buffer::Iterator i); diff --git a/src/devices/csma/csma-net-device.cc b/src/devices/csma/csma-net-device.cc index 4217f8941..a493fa12a 100644 --- a/src/devices/csma/csma-net-device.cc +++ b/src/devices/csma/csma-net-device.cc @@ -274,8 +274,12 @@ CsmaNetDevice::DoNeedsArp (void) const } bool -CsmaNetDevice::SendTo (Packet& p, const Address& dest, uint16_t protocolNumber) +CsmaNetDevice::SendTo ( + const Packet& packet, + const Address& dest, + uint16_t protocolNumber) { + Packet p = packet; NS_DEBUG ("CsmaNetDevice::SendTo (" << &p << ")"); NS_DEBUG ("CsmaNetDevice::SendTo (): UID is " << p.GetUid () << ")"); diff --git a/src/devices/csma/csma-net-device.h b/src/devices/csma/csma-net-device.h index 40595f22e..62e07f6fc 100644 --- a/src/devices/csma/csma-net-device.h +++ b/src/devices/csma/csma-net-device.h @@ -255,7 +255,7 @@ private: * \param protocolNumber -- this parameter is not used here * \return true if success, false on failure */ - virtual bool SendTo (Packet& p, const Address& dest, uint16_t protocolNumber); + virtual bool SendTo (const Packet& p, const Address& dest, uint16_t protocolNumber); /** * Start Sending a Packet Down the Wire. diff --git a/src/devices/point-to-point/point-to-point-net-device.cc b/src/devices/point-to-point/point-to-point-net-device.cc index e77508e46..a071f9246 100644 --- a/src/devices/point-to-point/point-to-point-net-device.cc +++ b/src/devices/point-to-point/point-to-point-net-device.cc @@ -117,9 +117,10 @@ void PointToPointNetDevice::SetInterframeGap(const Time& t) m_tInterframeGap = t; } -bool PointToPointNetDevice::SendTo (Packet& p, const Address& dest, +bool PointToPointNetDevice::SendTo (const Packet& packet, const Address& dest, uint16_t protocolNumber) { + Packet p = packet; NS_DEBUG ("PointToPointNetDevice::SendTo (" << &p << ", " << &dest << ")"); NS_DEBUG ("PointToPointNetDevice::SendTo (): UID is " << p.GetUid () << ")"); diff --git a/src/devices/point-to-point/point-to-point-net-device.h b/src/devices/point-to-point/point-to-point-net-device.h index b6be56afa..6112c7e19 100644 --- a/src/devices/point-to-point/point-to-point-net-device.h +++ b/src/devices/point-to-point/point-to-point-net-device.h @@ -212,7 +212,7 @@ private: * @param protocolNumber Protocol Number used to find protocol touse * @returns true if success, false on failure */ - virtual bool SendTo (Packet& p, const Address& dest, + virtual bool SendTo (const Packet& p, const Address& dest, uint16_t protocolNumber); /** * Start Sending a Packet Down the Wire. diff --git a/src/internet-node/ipv4-loopback-interface.cc b/src/internet-node/ipv4-loopback-interface.cc index 47ddc9ee2..be680a022 100644 --- a/src/internet-node/ipv4-loopback-interface.cc +++ b/src/internet-node/ipv4-loopback-interface.cc @@ -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 @@ void Ipv4LoopbackInterface::SendTo (Packet packet, Ipv4Address dest) { Ptr ipv4 = m_node->QueryInterface (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")); } }//namespace ns3 diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index 0817ae937..a2bde3224 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -57,6 +57,12 @@ UdpSocket::~UdpSocket () m_udp = 0; } +enum Socket::SocketErrno +UdpSocket::GetErrno (void) const +{ + return m_errno; +} + Ptr UdpSocket::GetNode (void) const { @@ -118,11 +124,6 @@ UdpSocket::Bind (const Address &address) return FinishBind (); } -enum Socket::SocketErrno -UdpSocket::GetErrno (void) const -{ - return m_errno; -} int UdpSocket::ShutdownSend (void) { @@ -137,73 +138,42 @@ UdpSocket::ShutdownRecv (void) } int -UdpSocket::DoClose(Callback > 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 > connectionSucceeded, - Callback > connectionFailed, - Callback > 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, const Address&> connectionRequest, - Callback, const Address&> newConnectionCreated, - Callback > 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, uint32_t> dataSent) +UdpSocket::Send (const Packet &p) { if (!m_connected) { m_errno = ERROR_NOTCONN; return -1; } - Packet p; - if (buffer == 0) - { - p = Packet (size); - } - else - { - 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, 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, uint32_t> dataSent) +UdpSocket::DoSendTo (const Packet &p, Ipv4Address ipv4, uint16_t port) { if (m_endPoint == 0) { @@ -221,46 +191,21 @@ UdpSocket::DoSendPacketTo (const Packet &p, Ipv4Address ipv4, uint16_t port, } 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, - const uint8_t *buffer, - uint32_t size, - Callback, uint32_t> dataSent) +UdpSocket::SendTo(const Address &address, const Packet &p) { if (m_connected) { m_errno = ERROR_ISCONN; return -1; } - Packet p; - if (buffer == 0) - { - p = Packet (size); - } - else - { - p = Packet (buffer, size); - } InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); Ipv4Address ipv4 = transport.GetIpv4 (); uint16_t port = transport.GetPort (); - return DoSendPacketTo (p, ipv4, port, dataSent); -} -void -UdpSocket::DoRecv(Callback, const uint8_t*, uint32_t,const Address&> callback) -{ - m_rxCallback = callback; -} -void -UdpSocket::DoRecvDummy(Callback, uint32_t,const Address&> callback) -{ - m_dummyRxCallback = callback; + return DoSendTo (p, ipv4, port); } void @@ -273,14 +218,7 @@ UdpSocket::ForwardUp (const Packet &packet, Ipv4Address ipv4, uint16_t port) Address address = InetSocketAddress (ipv4, port); 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 diff --git a/src/internet-node/udp-socket.h b/src/internet-node/udp-socket.h index 8778aff84..b3674f7ef 100644 --- a/src/internet-node/udp-socket.h +++ b/src/internet-node/udp-socket.h @@ -47,27 +47,14 @@ public: virtual Ptr 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 Packet &p); + virtual int SendTo(const Address &address,const Packet &p); private: - virtual int DoClose(Callback > closeCompleted); - virtual int DoConnect(const Address & address, - Callback > connectionSucceeded, - Callback > connectionFailed, - Callback > halfClose); - virtual int DoAccept(Callback, const Address&> connectionRequest, - Callback, const Address&> newConnectionCreated, - Callback > closeRequested); - virtual int DoSend (const uint8_t* buffer, - uint32_t size, - Callback, uint32_t> dataSent); - virtual int DoSendTo(const Address &address, - const uint8_t *buffer, - uint32_t size, - Callback, uint32_t> dataSent); - virtual void DoRecv(Callback, const uint8_t*, uint32_t,const Address&>); - virtual void DoRecvDummy(Callback, uint32_t,const Address&>); private: friend class Udp; @@ -75,10 +62,8 @@ private: 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, uint32_t> dataSent); - int DoSendPacketTo (const Packet &p, Ipv4Address daddr, uint16_t dport, - Callback, 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 m_node; diff --git a/src/node/address.cc b/src/node/address.cc index 33104083d..0cc37b1a2 100644 --- a/src/node/address.cc +++ b/src/node/address.cc @@ -147,7 +147,7 @@ std::ostream& operator<< (std::ostream& os, const Address & address) return os; } os.setf (std::ios::hex, std::ios::basefield); - std::cout.fill('0'); + os.fill('0'); for (uint8_t i=0; i < (address.m_len-1); i++) { os << std::setw(2) << (uint32_t)address.m_data[i] << ":"; @@ -155,7 +155,7 @@ std::ostream& operator<< (std::ostream& os, const Address & address) // Final byte not suffixed by ":" os << std::setw(2) << (uint32_t)address.m_data[address.m_len-1]; os.setf (std::ios::dec, std::ios::basefield); - std::cout.fill(' '); + os.fill(' '); return os; } diff --git a/src/node/eui48-address.cc b/src/node/eui48-address.cc index d8b62257b..d197a0e52 100644 --- a/src/node/eui48-address.cc +++ b/src/node/eui48-address.cc @@ -152,7 +152,7 @@ std::ostream& operator<< (std::ostream& os, const Eui48Address & address) address.CopyTo (ad); os.setf (std::ios::hex, std::ios::basefield); - std::cout.fill('0'); + os.fill('0'); for (uint8_t i=0; i < 5; i++) { os << std::setw(2) << (uint32_t)ad[i] << ":"; @@ -160,7 +160,7 @@ std::ostream& operator<< (std::ostream& os, const Eui48Address & address) // Final byte not suffixed by ":" os << std::setw(2) << (uint32_t)ad[5]; os.setf (std::ios::dec, std::ios::basefield); - std::cout.fill(' '); + os.fill(' '); return os; } diff --git a/src/node/eui64-address.cc b/src/node/eui64-address.cc index 3b91353fe..9cbbba381 100644 --- a/src/node/eui64-address.cc +++ b/src/node/eui64-address.cc @@ -155,7 +155,7 @@ std::ostream& operator<< (std::ostream& os, const Eui64Address & address) address.CopyTo (ad); os.setf (std::ios::hex, std::ios::basefield); - std::cout.fill('0'); + os.fill('0'); for (uint8_t i=0; i < 7; i++) { os << std::setw(2) << (uint32_t)ad[i] << ":"; @@ -163,7 +163,7 @@ std::ostream& operator<< (std::ostream& os, const Eui64Address & address) // Final byte not suffixed by ":" os << std::setw(2) << (uint32_t)ad[7]; os.setf (std::ios::dec, std::ios::basefield); - std::cout.fill(' '); + os.fill(' '); return os; } diff --git a/src/node/inet-socket-address.cc b/src/node/inet-socket-address.cc index 3b2ad5ba8..58b589586 100644 --- a/src/node/inet-socket-address.cc +++ b/src/node/inet-socket-address.cc @@ -72,7 +72,7 @@ InetSocketAddress::ConvertFrom (const Address &address) uint8_t buf[6]; address.CopyTo (buf); Ipv4Address ipv4 = Ipv4Address::Deserialize (buf); - uint16_t port = buf[0] | (buf[1] << 8); + uint16_t port = buf[4] | (buf[5] << 8); return InetSocketAddress (ipv4, port); } uint8_t diff --git a/src/node/net-device.cc b/src/node/net-device.cc index 4fa2e8d51..1ffe6d4b1 100644 --- a/src/node/net-device.cc +++ b/src/node/net-device.cc @@ -171,7 +171,7 @@ NetDevice::DisablePointToPoint (void) // Receive packet from above bool -NetDevice::Send(Packet& p, const Address& dest, uint16_t protocolNumber) +NetDevice::Send(const Packet& p, const Address& dest, uint16_t protocolNumber) { if (m_isUp) { diff --git a/src/node/net-device.h b/src/node/net-device.h index dd590f39c..315f16033 100644 --- a/src/node/net-device.h +++ b/src/node/net-device.h @@ -158,7 +158,7 @@ public: * * \return whether the Send operation succeeded */ - bool Send(Packet& p, const Address& dest, uint16_t protocolNumber); + bool Send(const Packet& p, const Address& dest, uint16_t protocolNumber); /** * \returns the node base class which contains this network * interface. @@ -274,7 +274,7 @@ public: * method. When the link is Up, this method is invoked to ask * subclasses to forward packets. Subclasses MUST override this method. */ - virtual bool SendTo (Packet& p, const Address &dest, uint16_t protocolNumber) = 0; + virtual bool SendTo (const Packet& p, const Address &dest, uint16_t protocolNumber) = 0; /** * \returns true if this NetDevice needs the higher-layers * to perform ARP over it, false otherwise. diff --git a/src/node/packet-socket.cc b/src/node/packet-socket.cc index 700f5f8b5..f642eee9e 100644 --- a/src/node/packet-socket.cc +++ b/src/node/packet-socket.cc @@ -52,13 +52,18 @@ PacketSocket::DoDispose (void) m_device = 0; } +enum Socket::SocketErrno +PacketSocket::GetErrno (void) const +{ + return m_errno; +} + Ptr PacketSocket::GetNode (void) const { return m_node; } - int PacketSocket::Bind (void) { @@ -111,11 +116,6 @@ PacketSocket::DoBind (const PacketSocketAddress &address) return 0; } -enum Socket::SocketErrno -PacketSocket::GetErrno (void) const -{ - return m_errno; -} int PacketSocket::ShutdownSend (void) { @@ -139,26 +139,20 @@ PacketSocket::ShutdownRecv (void) return 0; } int -PacketSocket::DoClose(ns3::Callback > 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 > connectionSucceeded, - ns3::Callback > connectionFailed, - ns3::Callback > halfClose) +PacketSocket::Connect(const Address &ad) { PacketSocketAddress address; if (m_state == STATE_CLOSED) @@ -184,33 +178,15 @@ PacketSocket::DoConnect(const Address &ad, } 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, const Address &> connectionRequest, - ns3::Callback, const Address &> newConnectionCreated, - ns3::Callback > 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, uint32_t> dataSent) +PacketSocket::Send (const Packet &p) { if (m_state == STATE_OPEN || m_state == STATE_BOUND) @@ -218,14 +194,11 @@ PacketSocket::DoSend (const uint8_t* buffer, m_errno = ERROR_NOTCONN; return -1; } - return DoSendTo (m_destAddr, buffer, size, dataSent); + return SendTo (m_destAddr, p); } int -PacketSocket::DoSendTo(const Address &address, - const uint8_t *buffer, - uint32_t size, - Callback, uint32_t> dataSent) +PacketSocket::SendTo(const Address &address, const Packet &p) { PacketSocketAddress ad; if (m_state == STATE_CLOSED) @@ -250,16 +223,6 @@ PacketSocket::DoSendTo(const Address &address, return -1; } ad = PacketSocketAddress::ConvertFrom (address); - - Packet p; - if (buffer == 0) - { - p = Packet (size); - } - else - { - p = Packet (buffer, size); - } bool error = false; Address dest = ad.GetPhysicalAddress (); @@ -282,9 +245,9 @@ PacketSocket::DoSendTo(const Address &address, } } } - if (!error && !dataSent.IsNull ()) + if (!error) { - dataSent (this, p.GetSize ()); + NotifyDataSent (p.GetSize ()); } if (error) @@ -298,18 +261,6 @@ PacketSocket::DoSendTo(const Address &address, } } -void -PacketSocket::DoRecv(ns3::Callback, const uint8_t*, uint32_t,const Address &> callback) -{ - m_rxCallback = callback; -} - -void -PacketSocket::DoRecvDummy(ns3::Callback, uint32_t, const Address &> callback) -{ - m_dummyRxCallback = callback; -} - void PacketSocket::ForwardUp (Ptr device, const Packet &packet, uint16_t protocol, const Address &from) @@ -328,14 +279,7 @@ PacketSocket::ForwardUp (Ptr device, const Packet &packet, NS_DEBUG ("PacketSocket::ForwardUp: UID is " << packet.GetUid() << " PacketSocket " << this); - 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 diff --git a/src/node/packet-socket.h b/src/node/packet-socket.h index f18279949..8dc1b9e62 100644 --- a/src/node/packet-socket.h +++ b/src/node/packet-socket.h @@ -78,27 +78,15 @@ public: virtual Ptr 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 Packet &p); + virtual int SendTo(const Address &address,const Packet &p); + private: - virtual int DoClose(Callback > closeCompleted); - virtual int DoConnect(const Address & address, - Callback > connectionSucceeded, - Callback > connectionFailed, - Callback > halfClose); - virtual int DoAccept(Callback, const Address&> connectionRequest, - Callback, const Address&> newConnectionCreated, - Callback > closeRequested); - virtual int DoSend (const uint8_t* buffer, - uint32_t size, - Callback, uint32_t> dataSent); - virtual int DoSendTo(const Address &address, - const uint8_t *buffer, - uint32_t size, - Callback, uint32_t> dataSent); - virtual void DoRecv(Callback, const uint8_t*, uint32_t,const Address&> receive); - virtual void DoRecvDummy(Callback, uint32_t,const Address&>); private: void Init (void); @@ -114,8 +102,6 @@ private: STATE_CLOSED }; Ptr m_node; - Callback,uint32_t,const Address &> m_dummyRxCallback; - Callback,uint8_t const*,uint32_t, const Address &> m_rxCallback; enum SocketErrno m_errno; bool m_shutdownSend; bool m_shutdownRecv; diff --git a/src/node/socket.cc b/src/node/socket.cc index e3e573ab6..58f83947f 100644 --- a/src/node/socket.cc +++ b/src/node/socket.cc @@ -1,79 +1,144 @@ +/* -*- 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 + * Mathieu Lacage + */ #include "socket.h" +#include "ns3/packet.h" namespace ns3 { Socket::~Socket () {} -int -Socket::Close(Callback > closeCompleted) +void +Socket::SetCloseCallback (Callback > closeCompleted) { - return DoClose (closeCompleted); -} - -int -Socket::Connect(const Address & address, - Callback > connectionSucceeded, - Callback > connectionFailed, - Callback > halfClose) -{ - return DoConnect (address, connectionSucceeded, connectionFailed, halfClose); -} -int -Socket::Accept(Callback, const Address&> connectionRequest, - Callback, const Address&> newConnectionCreated, - Callback > closeRequested) -{ - return DoAccept (connectionRequest, newConnectionCreated, closeRequested); -} -int -Socket::Send (const uint8_t* buffer, - uint32_t size, - Callback, uint32_t> dataSent) -{ - return DoSend (buffer, size, dataSent); -} -int -Socket::SendTo(const Address &address, - const uint8_t *buffer, - uint32_t size, - Callback, uint32_t> dataSent) -{ - return DoSendTo (address, buffer, size, dataSent); + m_closeCompleted = closeCompleted; } void -Socket::Recv(Callback, const uint8_t*, uint32_t,const Address&> callback) +Socket::SetConnectCallback (Callback > connectionSucceeded, + Callback > connectionFailed, + Callback > halfClose) { - DoRecv (callback); + m_connectionSucceeded = connectionSucceeded; + m_connectionFailed = connectionFailed; + m_halfClose = halfClose; } void -Socket::RecvDummy(Callback, uint32_t,const Address&> callback) +Socket::SetAcceptCallback (Callback, const Address &> connectionRequest, + Callback, const Address&> newConnectionCreated, + Callback > closeRequested) { - DoRecvDummy (callback); + m_connectionRequest = connectionRequest; + m_newConnectionCreated = newConnectionCreated; + m_closeRequested = closeRequested; +} +void +Socket::SetSendCallback (Callback, uint32_t> dataSent) +{ + m_dataSent = dataSent; +} +void +Socket::SetRecvCallback (Callback, const Packet &,const Address&> receivedData) +{ + m_receivedData = receivedData; } - +void +Socket::NotifyCloseCompleted (void) +{ + if (!m_closeCompleted.IsNull ()) + { + m_closeCompleted (this); + } +} +void +Socket::NotifyConnectionSucceeded (void) +{ + if (!m_connectionSucceeded.IsNull ()) + { + m_connectionSucceeded (this); + } +} +void +Socket::NotifyConnectionFailed (void) +{ + if (!m_connectionFailed.IsNull ()) + { + m_connectionFailed (this); + } +} +void +Socket::NotifyHalfClose (void) +{ + if (!m_halfClose.IsNull ()) + { + m_halfClose (this); + } +} bool -Socket::RefuseAllConnections (Ptr socket, const Address& address) +Socket::NotifyConnectionRequest (const Address &from) { - return false; + if (!m_connectionRequest.IsNull ()) + { + return m_connectionRequest (this, from); + } + else + { + // refuse all incomming connections by default. + return false; + } } void -Socket::DummyCallbackVoidSocket (Ptr socket) -{} -void -Socket::DummyCallbackVoidSocketUi32 (Ptr socket, uint32_t) -{} +Socket::NotifyNewConnectionCreated (Ptr socket, const Address &from) +{ + if (!m_newConnectionCreated.IsNull ()) + { + m_newConnectionCreated (socket, from); + } +} void -Socket::DummyCallbackVoidSocketUi32Address (Ptr socket, uint32_t, const Address &) -{} +Socket::NotifyCloseRequested (void) +{ + if (!m_closeRequested.IsNull ()) + { + m_closeRequested (this); + } +} void -Socket::DummyCallbackVoidSocketBufferUi32Address (Ptr socket, const uint8_t *, uint32_t, - const Address &) -{} +Socket::NotifyDataSent (uint32_t size) +{ + if (!m_dataSent.IsNull ()) + { + m_dataSent (this, size); + } +} void -Socket::DummyCallbackVoidSocketAddress (Ptr socket, const Address &) -{} +Socket::NotifyDataReceived (const Packet &p, const Address &from) +{ + if (!m_receivedData.IsNull ()) + { + m_receivedData (this, p, from); + } +} + }//namespace ns3 diff --git a/src/node/socket.h b/src/node/socket.h index 991fb01b1..e3365fcd8 100644 --- a/src/node/socket.h +++ b/src/node/socket.h @@ -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 -// +/* -*- 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 + * Mathieu Lacage + */ #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,55 @@ public: */ virtual Ptr GetNode (void) const = 0; + /** + * \param closeCompleted Callback invoked when the close operation is + * completed. + */ + void SetCloseCallback (Callback > 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 > connectionSucceeded, + Callback > connectionFailed, + Callback > 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, const Address &> connectionRequest, + Callback, const Address&> newConnectionCreated, + Callback > closeRequested); + void SetSendCallback (Callback, uint32_t> dataSent); + /** + * \brief Receive data + * \param receivedData Invoked whenever new data is received. + * + */ + void SetRecvCallback (Callback, const Packet &,const Address&> receivedData); + /** * \param address the address to try to allocate * \returns 0 on success, -1 on failure. @@ -85,16 +137,13 @@ public: */ 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 > closeCompleted = MakeCallback (&Socket::DummyCallbackVoidSocket)); + virtual int Close(void) = 0; /** * \returns zero on success, -1 on failure. @@ -115,120 +164,48 @@ public: /** * \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 > connectionSucceeded = MakeCallback(&Socket::DummyCallbackVoidSocket), - Callback > connectionFailed = MakeCallback(&Socket::DummyCallbackVoidSocket), - Callback > 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, const Address &> connectionRequest = - MakeCallback(&Socket::RefuseAllConnections), - Callback, const Address&> newConnectionCreated = - MakeCallback (&Socket::DummyCallbackVoidSocketAddress), - Callback > 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. + * \param p packet to send * \param dataSent Data sent callback. * \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, uint32_t> dataSent = MakeCallback (&Socket::DummyCallbackVoidSocketUi32)); + virtual int Send (const Packet &p) = 0; /** * \brief Send data to a specified peer. * \param address IP Address of remote host - * \param buffer Data to send (nil if dummy data). - * \param size Number of bytes to send. + * \param p packet to send * \param dataSent Data sent callback. * \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, 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, 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, uint32_t,const Address&> receivedData = - MakeCallback (&Socket::DummyCallbackVoidSocketUi32Address)); + virtual int SendTo(const Address &address,const Packet &p) = 0; -private: - virtual int DoClose(Callback > closeCompleted) = 0; - virtual int DoConnect(const Address & address, - Callback > connectionSucceeded, - Callback > connectionFailed, - Callback > halfClose) = 0; - virtual int DoAccept(Callback, const Address&> connectionRequest, - Callback, const Address&> newConnectionCreated, - Callback > closeRequested) = 0; - virtual int DoSend (const uint8_t* buffer, - uint32_t size, - Callback, uint32_t> dataSent) = 0; - virtual int DoSendTo(const Address &address, - const uint8_t *buffer, - uint32_t size, - Callback, uint32_t> dataSent) = 0; - virtual void DoRecv(Callback, const uint8_t*, uint32_t,const Address&> receive) = 0; - virtual void DoRecvDummy(Callback, 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, const Address &from); + void NotifyCloseRequested (void); + void NotifyDataSent (uint32_t size); + void NotifyDataReceived (const Packet &p, const Address &from); - - static bool RefuseAllConnections (Ptr socket, const Address& address); - static void DummyCallbackVoidSocket (Ptr socket); - static void DummyCallbackVoidSocketUi32 (Ptr socket, uint32_t); - static void DummyCallbackVoidSocketUi32Address (Ptr socket, uint32_t, const Address &); - static void DummyCallbackVoidSocketBufferUi32Address (Ptr socket, const uint8_t *, uint32_t, - const Address &); - static void DummyCallbackVoidSocketAddress (Ptr socket, const Address &); + Callback > m_closeCompleted; + Callback > m_connectionSucceeded; + Callback > m_connectionFailed; + Callback > m_halfClose; + Callback > m_closeRequested; + Callback, const Address &> m_connectionRequest; + Callback, const Address&> m_newConnectionCreated; + Callback, uint32_t> m_dataSent; + Callback, const Packet &,const Address&> m_receivedData; }; } //namespace ns3