00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "ns3/address.h"
00021 #include "ns3/log.h"
00022 #include "ns3/inet-socket-address.h"
00023 #include "ns3/node.h"
00024 #include "ns3/socket.h"
00025 #include "ns3/simulator.h"
00026 #include "ns3/socket-factory.h"
00027 #include "ns3/packet.h"
00028 #include "ns3/trace-source-accessor.h"
00029 #include "ns3/udp-socket-factory.h"
00030 #include "packet-sink.h"
00031
00032 using namespace std;
00033
00034 namespace ns3 {
00035
00036 NS_LOG_COMPONENT_DEFINE ("PacketSink");
00037 NS_OBJECT_ENSURE_REGISTERED (PacketSink);
00038
00039 TypeId
00040 PacketSink::GetTypeId (void)
00041 {
00042 static TypeId tid = TypeId ("ns3::PacketSink")
00043 .SetParent<Application> ()
00044 .AddConstructor<PacketSink> ()
00045 .AddAttribute ("Local", "The Address on which to Bind the rx socket.",
00046 AddressValue (),
00047 MakeAddressAccessor (&PacketSink::m_local),
00048 MakeAddressChecker ())
00049 .AddAttribute ("Protocol", "The type id of the protocol to use for the rx socket.",
00050 TypeIdValue (UdpSocketFactory::GetTypeId ()),
00051 MakeTypeIdAccessor (&PacketSink::m_tid),
00052 MakeTypeIdChecker ())
00053 .AddTraceSource ("Rx", "A packet has been received",
00054 MakeTraceSourceAccessor (&PacketSink::m_rxTrace))
00055 ;
00056 return tid;
00057 }
00058
00059 PacketSink::PacketSink ()
00060 {
00061 NS_LOG_FUNCTION (this);
00062 m_socket = 0;
00063 }
00064
00065 PacketSink::~PacketSink()
00066 {
00067 NS_LOG_FUNCTION (this);
00068 }
00069
00070 void
00071 PacketSink::DoDispose (void)
00072 {
00073 NS_LOG_FUNCTION (this);
00074 m_socket = 0;
00075
00076
00077 Application::DoDispose ();
00078 }
00079
00080
00081
00082 void PacketSink::StartApplication()
00083 {
00084 NS_LOG_FUNCTION (this);
00085
00086 if (!m_socket)
00087 {
00088 m_socket = Socket::CreateSocket (GetNode(), m_tid);
00089 m_socket->Bind (m_local);
00090 m_socket->Listen ();
00091 }
00092
00093 m_socket->SetRecvCallback (MakeCallback(&PacketSink::HandleRead, this));
00094 m_socket->SetAcceptCallback (
00095 MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
00096 MakeCallback(&PacketSink::HandleAccept, this));
00097 }
00098
00099 void PacketSink::StopApplication()
00100 {
00101 NS_LOG_FUNCTION (this);
00102 while(!m_socketList.empty())
00103 {
00104 Ptr<Socket> acceptedSocket = m_socketList.front();
00105 m_socketList.pop_front();
00106 acceptedSocket->Close();
00107 }
00108 if (m_socket)
00109 {
00110 m_socket->Close ();
00111 m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
00112 }
00113 }
00114
00115 void PacketSink::HandleRead (Ptr<Socket> socket)
00116 {
00117 NS_LOG_FUNCTION (this << socket);
00118 Ptr<Packet> packet;
00119 Address from;
00120 while (packet = socket->RecvFrom (from))
00121 {
00122 if (packet->GetSize() == 0)
00123 {
00124 break;
00125 }
00126 if (InetSocketAddress::IsMatchingType (from))
00127 {
00128 InetSocketAddress address = InetSocketAddress::ConvertFrom (from);
00129 NS_LOG_INFO ("Received " << packet->GetSize() << " bytes from " <<
00130 address.GetIpv4() << " [" << address << "]---'" <<
00131 packet->PeekData() << "'");
00132 }
00133 m_rxTrace (packet, from);
00134 }
00135 }
00136
00137 void PacketSink::HandleAccept (Ptr<Socket> s, const Address& from)
00138 {
00139 NS_LOG_FUNCTION (this << s << from);
00140 s->SetRecvCallback (MakeCallback(&PacketSink::HandleRead, this));
00141 m_socketList.push_back (s);
00142 }
00143
00144 }