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"
43 PacketSink::GetTypeId (
void)
47 .AddConstructor<PacketSink> ()
48 .AddAttribute (
"Local",
"The Address on which to Bind the rx socket.",
50 MakeAddressAccessor (&PacketSink::m_local),
51 MakeAddressChecker ())
52 .AddAttribute (
"Protocol",
"The type id of the protocol to use for the rx socket.",
54 MakeTypeIdAccessor (&PacketSink::m_tid),
56 .AddTraceSource (
"Rx",
"A packet has been received",
62 PacketSink::PacketSink ()
69 PacketSink::~PacketSink()
74 uint32_t PacketSink::GetTotalRx ()
const
80 PacketSink::GetListeningSocket (
void)
const
86 std::list<Ptr<Socket> >
87 PacketSink::GetAcceptedSockets (
void)
const
93 void PacketSink::DoDispose (
void)
97 m_socketList.clear ();
100 Application::DoDispose ();
105 void PacketSink::StartApplication ()
111 m_socket = Socket::CreateSocket (GetNode (), m_tid);
112 m_socket->Bind (m_local);
114 m_socket->ShutdownSend ();
121 udpSocket->MulticastJoinGroup (0, m_local);
130 m_socket->SetRecvCallback (
MakeCallback (&PacketSink::HandleRead,
this));
131 m_socket->SetAcceptCallback (
134 m_socket->SetCloseCallbacks (
139 void PacketSink::StopApplication ()
142 while(!m_socketList.empty ())
144 Ptr<Socket> acceptedSocket = m_socketList.front ();
145 m_socketList.pop_front ();
146 acceptedSocket->
Close ();
160 while ((packet = socket->
RecvFrom (from)))
166 m_totalRx += packet->
GetSize ();
167 if (InetSocketAddress::IsMatchingType (from))
170 <<
"s packet sink received "
171 << packet->
GetSize () <<
" bytes from "
172 << InetSocketAddress::ConvertFrom(from).GetIpv4 ()
173 <<
" port " << InetSocketAddress::ConvertFrom (from).GetPort ()
174 <<
" total Rx " << m_totalRx <<
" bytes");
176 else if (Inet6SocketAddress::IsMatchingType (from))
179 <<
"s packet sink received "
180 << packet->
GetSize () <<
" bytes from "
181 << Inet6SocketAddress::ConvertFrom(from).GetIpv6 ()
182 <<
" port " << Inet6SocketAddress::ConvertFrom (from).GetPort ()
183 <<
" total Rx " << m_totalRx <<
" bytes");
185 m_rxTrace (packet, from);
205 m_socketList.push_back (s);