diff --git a/src/applications/packet-sink/packet-sink.cc b/src/applications/packet-sink/packet-sink.cc index 6b768978b..e090f40ac 100644 --- a/src/applications/packet-sink/packet-sink.cc +++ b/src/applications/packet-sink/packet-sink.cc @@ -88,6 +88,7 @@ void PacketSink::StartApplication() // Called at time specified by Start } m_socket->SetRecvCallback (MakeCallback(&PacketSink::Receive, this)); + m_socket->SetRecv_Callback (MakeCallback(&PacketSink::HandleRead, this)); m_socket->SetAcceptCallback ( MakeNullCallback, const Address &> (), MakeNullCallback, const Address&> (), @@ -103,6 +104,28 @@ void PacketSink::StopApplication() // Called at time specified by Stop } } +void PacketSink::HandleRead (Ptr socket) +{ + Ptr packet; + uint32_t maxSize = std::numeric_limits::max(); + uint32_t flags = 0; // no flags + while (packet = socket->Recv (maxSize, flags)) + { + NS_LOG_INFO ("Received " << packet->GetSize()); +#if 0 + // this code waits until we pass address as a Packet tag + if (InetSocketAddress::IsMatchingType (from)) + { + address = InetSocketAddress::ConvertFrom (from); + NS_LOG_INFO ("Received " << packet->GetSize() << " bytes from " << + address.GetIpv4() << " [" << address << "]---'" << + packet->PeekData() << "'"); + } + m_rxTrace (packet, from); +#endif + } +} + // This LOG output inspired by the application on Joseph Kopena's wiki void PacketSink::Receive(Ptr socket, Ptr packet, const Address &from) diff --git a/src/applications/packet-sink/packet-sink.h b/src/applications/packet-sink/packet-sink.h index 70f190ab0..576784dda 100644 --- a/src/applications/packet-sink/packet-sink.h +++ b/src/applications/packet-sink/packet-sink.h @@ -66,6 +66,8 @@ private: virtual void StopApplication (void); // Called at time specified by Stop virtual void Receive (Ptr socket, Ptr packet, const Address& from); + virtual void HandleRead (Ptr socket); + virtual void CloseConnection (Ptr socket); Ptr m_socket; // Associated socket diff --git a/src/internet-node/tcp-socket.cc b/src/internet-node/tcp-socket.cc index ef6a40dc2..e88693979 100644 --- a/src/internet-node/tcp-socket.cc +++ b/src/internet-node/tcp-socket.cc @@ -442,6 +442,14 @@ TcpSocket::Listen (uint32_t q) return 0; } +Ptr +TcpSocket::Recv (uint32_t maxSize, uint32_t flags) +{ + Ptr p = m_deliveryQueue.front (); + m_deliveryQueue.pop (); + return p; +} + void TcpSocket::ForwardUp (Ptr packet, Ipv4Address ipv4, uint16_t port) { diff --git a/src/internet-node/tcp-socket.h b/src/internet-node/tcp-socket.h index 7499e2a3e..6bf63082f 100644 --- a/src/internet-node/tcp-socket.h +++ b/src/internet-node/tcp-socket.h @@ -21,6 +21,7 @@ #define TCP_SOCKET_H #include +#include #include "ns3/callback.h" #include "ns3/traced-value.h" #include "ns3/socket.h" @@ -69,6 +70,8 @@ public: virtual int SendTo(const Address &address, Ptr p); virtual int Listen(uint32_t queueLimit); + virtual Ptr Recv (uint32_t maxSize, uint32_t flags); + private: friend class Tcp; // invoked by Tcp class @@ -170,6 +173,8 @@ private: Time m_cnTimeout; uint32_t m_cnCount; + // Temporary queue for delivering data to application + std::queue > m_deliveryQueue; }; }//namespace ns3 diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index 951a8c73f..00ba74e9a 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -513,6 +513,14 @@ UdpSocketTest::RunTests (void) return result; } +Ptr +UdpSocket::Recv (uint32_t maxSize, uint32_t flags) +{ + Ptr p = m_deliveryQueue.front (); + m_deliveryQueue.pop (); + return p; +} + static UdpSocketTest gUdpSocketTest; diff --git a/src/internet-node/udp-socket.h b/src/internet-node/udp-socket.h index 0b662926e..d976e1ad3 100644 --- a/src/internet-node/udp-socket.h +++ b/src/internet-node/udp-socket.h @@ -21,6 +21,7 @@ #define UDP_SOCKET_H #include +#include #include "ns3/callback.h" #include "ns3/socket.h" #include "ns3/ptr.h" @@ -56,7 +57,7 @@ public: virtual int Send (Ptr p); virtual int SendTo(const Address &address,Ptr p); -private: + virtual Ptr Recv (uint32_t maxSize, uint32_t flags); private: friend class Udp; @@ -79,6 +80,8 @@ private: bool m_shutdownSend; bool m_shutdownRecv; bool m_connected; + + std::queue > m_deliveryQueue; }; }//namespace ns3 diff --git a/src/node/packet-socket.cc b/src/node/packet-socket.cc index 7d3578475..fa045e3b0 100644 --- a/src/node/packet-socket.cc +++ b/src/node/packet-socket.cc @@ -305,4 +305,12 @@ PacketSocket::ForwardUp (Ptr device, Ptr packet, NotifyDataReceived (packet, address); } +Ptr +PacketSocket::Recv (uint32_t maxSize, uint32_t flags) +{ + Ptr p = m_deliveryQueue.front (); + m_deliveryQueue.pop (); + return p; +} + }//namespace ns3 diff --git a/src/node/packet-socket.h b/src/node/packet-socket.h index d94ef7030..b5282aaab 100644 --- a/src/node/packet-socket.h +++ b/src/node/packet-socket.h @@ -22,6 +22,7 @@ #define PACKET_SOCKET_H #include +#include #include "ns3/callback.h" #include "ns3/ptr.h" #include "ns3/socket.h" @@ -87,6 +88,8 @@ public: virtual int Send (Ptr p); virtual int SendTo(const Address &address,Ptr p); + virtual Ptr Recv (uint32_t maxSize, uint32_t flags); + private: void ForwardUp (Ptr device, Ptr packet, @@ -109,6 +112,8 @@ private: bool m_isSingleDevice; uint32_t m_device; Address m_destAddr; /// Default destination address + + std::queue > m_deliveryQueue; }; }//namespace ns3 diff --git a/src/node/socket.cc b/src/node/socket.cc index 41f09f879..86d5d2ec8 100644 --- a/src/node/socket.cc +++ b/src/node/socket.cc @@ -86,6 +86,13 @@ Socket::SetRecvCallback (Callback, Ptr,const Address&> m_receivedData = receivedData; } +void +Socket::SetRecv_Callback (Callback > receivedData) +{ + NS_LOG_FUNCTION_NOARGS (); + m_receivedData_ = receivedData; +} + int Socket::Listen (uint32_t queueLimit) { return 0; //XXX the base class version does nothing @@ -230,4 +237,13 @@ Socket::NotifyDataReceived (Ptr p, const Address &from) } } +void +Socket::NotifyDataRecv (void) +{ + NS_LOG_FUNCTION_NOARGS (); + if (!m_receivedData_.IsNull ()) + { + m_receivedData_ (this); + } +} }//namespace ns3 diff --git a/src/node/socket.h b/src/node/socket.h index 07a267ee7..8340ae665 100644 --- a/src/node/socket.h +++ b/src/node/socket.h @@ -157,7 +157,12 @@ public: */ void SetRecvCallback (Callback, Ptr, const Address&> receivedData); - + /** + * \brief Receive data + * \param receivedData Invoked whenever new data is received. + * + */ + void SetRecv_Callback (Callback >); /** * \param address the address to try to allocate * \returns 0 on success, -1 on failure. @@ -252,6 +257,8 @@ public: */ int SendTo (const Address &address, const uint8_t* buf, uint32_t size); + virtual Ptr Recv (uint32_t maxSize, uint32_t flags) = 0; + protected: void NotifyCloseCompleted (void); void NotifyConnectionSucceeded (void); @@ -263,6 +270,7 @@ protected: void NotifyDataSent (uint32_t size); void NotifySend (uint32_t spaceAvailable); void NotifyDataReceived (Ptr p, const Address &from); + void NotifyDataRecv (void); Callback > m_closeCompleted; Callback > m_connectionSucceeded; @@ -274,6 +282,7 @@ protected: Callback, uint32_t> m_dataSent; Callback, uint32_t > m_sendCb; Callback, Ptr,const Address&> m_receivedData; + Callback > m_receivedData_; }; } //namespace ns3