20 #include "ns3/address.h"
21 #include "ns3/address-utils.h"
23 #include "ns3/inet-socket-address.h"
24 #include "ns3/inet6-socket-address.h"
26 #include "ns3/socket.h"
27 #include "ns3/udp-socket.h"
28 #include "ns3/simulator.h"
29 #include "ns3/socket-factory.h"
30 #include "ns3/packet.h"
31 #include "ns3/trace-source-accessor.h"
32 #include "ns3/udp-socket-factory.h"
33 #include "packet-sink.h"
38 NS_OBJECT_ENSURE_REGISTERED (PacketSink);
41 PacketSink::GetTypeId (
void)
43 static TypeId tid = TypeId (
"ns3::PacketSink")
44 .SetParent<Application> ()
45 .AddConstructor<PacketSink> ()
46 .AddAttribute (
"Local",
"The Address on which to Bind the rx socket.",
48 MakeAddressAccessor (&PacketSink::m_local),
49 MakeAddressChecker ())
50 .AddAttribute (
"Protocol",
"The type id of the protocol to use for the rx socket.",
51 TypeIdValue (UdpSocketFactory::GetTypeId ()),
52 MakeTypeIdAccessor (&PacketSink::m_tid),
54 .AddTraceSource (
"Rx",
"A packet has been received",
60 PacketSink::PacketSink ()
67 PacketSink::~PacketSink()
85 std::list<Ptr<Socket> >
96 m_socketList.clear ();
111 m_socket->
Bind (m_local);
114 if (addressUtils::IsMulticast (m_local))
120 udpSocket->MulticastJoinGroup (0, m_local);
141 while(!m_socketList.empty ())
143 Ptr<Socket> acceptedSocket = m_socketList.front ();
144 m_socketList.pop_front ();
145 acceptedSocket->
Close ();
159 while ((packet = socket->
RecvFrom (from)))
165 m_totalRx += packet->
GetSize ();
169 <<
"s packet sink received "
170 << packet->
GetSize () <<
" bytes from "
173 <<
" total Rx " << m_totalRx <<
" bytes");
178 <<
"s packet sink received "
179 << packet->
GetSize () <<
" bytes from "
182 <<
" total Rx " << m_totalRx <<
" bytes");
184 m_rxTrace (packet, from);
189 void PacketSink::HandlePeerClose (Ptr<Socket> socket)
194 void PacketSink::HandlePeerError (Ptr<Socket> socket)
200 void PacketSink::HandleAccept (Ptr<Socket> s,
const Address& from)
203 s->SetRecvCallback (
MakeCallback (&PacketSink::HandleRead,
this));
204 m_socketList.push_back (s);
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
Ipv4Address GetIpv4(void) const
#define NS_LOG_FUNCTION(parameters)
virtual int ShutdownSend(void)=0
#define NS_LOG_COMPONENT_DEFINE(name)
uint32_t GetSize(void) const
void SetCloseCallbacks(Callback< void, Ptr< Socket > > normalClose, Callback< void, Ptr< Socket > > errorClose)
Detect socket recv() events such as graceful shutdown or error.
#define NS_FATAL_ERROR(msg)
fatal error handling
a polymophic address class
virtual int Listen(void)=0
Listen for incoming connections.
Ptr< Node > GetNode() const
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
virtual void StopApplication(void)
Application specific shutdown code.
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
virtual void DoDispose(void)
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
static InetSocketAddress ConvertFrom(const Address &address)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Callback< R > MakeNullCallback(void)
uint32_t GetTotalRx() const
void SetAcceptCallback(Callback< bool, Ptr< Socket >, const Address & > connectionRequest, Callback< void, Ptr< Socket >, const Address & > newConnectionCreated)
Accept connection requests from remote hosts.
std::list< Ptr< Socket > > GetAcceptedSockets(void) const
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
uint16_t GetPort(void) const
Get the port.
virtual void DoDispose(void)
uint16_t GetPort(void) const
virtual void StartApplication(void)
Application specific startup code.
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
virtual int Close(void)=0
Close a socket.
static bool IsMatchingType(const Address &address)
Ptr< Socket > GetListeningSocket(void) const