A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
packet-sink.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright 2007 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tom Henderson (tomhend@u.washington.edu)
19  */
20 #include "ns3/address.h"
21 #include "ns3/address-utils.h"
22 #include "ns3/log.h"
23 #include "ns3/inet-socket-address.h"
24 #include "ns3/inet6-socket-address.h"
25 #include "ns3/node.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"
34 
35 namespace ns3 {
36 
37 NS_LOG_COMPONENT_DEFINE ("PacketSink");
38 NS_OBJECT_ENSURE_REGISTERED (PacketSink);
39 
40 TypeId
41 PacketSink::GetTypeId (void)
42 {
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.",
47  AddressValue (),
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),
53  MakeTypeIdChecker ())
54  .AddTraceSource ("Rx", "A packet has been received",
55  MakeTraceSourceAccessor (&PacketSink::m_rxTrace))
56  ;
57  return tid;
58 }
59 
60 PacketSink::PacketSink ()
61 {
62  NS_LOG_FUNCTION (this);
63  m_socket = 0;
64  m_totalRx = 0;
65 }
66 
67 PacketSink::~PacketSink()
68 {
69  NS_LOG_FUNCTION (this);
70 }
71 
72 uint32_t PacketSink::GetTotalRx () const
73 {
74  NS_LOG_FUNCTION (this);
75  return m_totalRx;
76 }
77 
80 {
81  NS_LOG_FUNCTION (this);
82  return m_socket;
83 }
84 
85 std::list<Ptr<Socket> >
87 {
88  NS_LOG_FUNCTION (this);
89  return m_socketList;
90 }
91 
93 {
94  NS_LOG_FUNCTION (this);
95  m_socket = 0;
96  m_socketList.clear ();
97 
98  // chain up
100 }
101 
102 
103 // Application Methods
104 void PacketSink::StartApplication () // Called at time specified by Start
105 {
106  NS_LOG_FUNCTION (this);
107  // Create the socket if not already
108  if (!m_socket)
109  {
110  m_socket = Socket::CreateSocket (GetNode (), m_tid);
111  m_socket->Bind (m_local);
112  m_socket->Listen ();
113  m_socket->ShutdownSend ();
114  if (addressUtils::IsMulticast (m_local))
115  {
116  Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket);
117  if (udpSocket)
118  {
119  // equivalent to setsockopt (MCAST_JOIN_GROUP)
120  udpSocket->MulticastJoinGroup (0, m_local);
121  }
122  else
123  {
124  NS_FATAL_ERROR ("Error: joining multicast on a non-UDP socket");
125  }
126  }
127  }
128 
129  m_socket->SetRecvCallback (MakeCallback (&PacketSink::HandleRead, this));
130  m_socket->SetAcceptCallback (
131  MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
132  MakeCallback (&PacketSink::HandleAccept, this));
133  m_socket->SetCloseCallbacks (
134  MakeCallback (&PacketSink::HandlePeerClose, this),
135  MakeCallback (&PacketSink::HandlePeerError, this));
136 }
137 
138 void PacketSink::StopApplication () // Called at time specified by Stop
139 {
140  NS_LOG_FUNCTION (this);
141  while(!m_socketList.empty ()) //these are accepted sockets, close them
142  {
143  Ptr<Socket> acceptedSocket = m_socketList.front ();
144  m_socketList.pop_front ();
145  acceptedSocket->Close ();
146  }
147  if (m_socket)
148  {
149  m_socket->Close ();
150  m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
151  }
152 }
153 
154 void PacketSink::HandleRead (Ptr<Socket> socket)
155 {
156  NS_LOG_FUNCTION (this << socket);
157  Ptr<Packet> packet;
158  Address from;
159  while ((packet = socket->RecvFrom (from)))
160  {
161  if (packet->GetSize () == 0)
162  { //EOF
163  break;
164  }
165  m_totalRx += packet->GetSize ();
167  {
168  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds ()
169  << "s packet sink received "
170  << packet->GetSize () << " bytes from "
172  << " port " << InetSocketAddress::ConvertFrom (from).GetPort ()
173  << " total Rx " << m_totalRx << " bytes");
174  }
175  else if (Inet6SocketAddress::IsMatchingType (from))
176  {
177  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds ()
178  << "s packet sink received "
179  << packet->GetSize () << " bytes from "
181  << " port " << Inet6SocketAddress::ConvertFrom (from).GetPort ()
182  << " total Rx " << m_totalRx << " bytes");
183  }
184  m_rxTrace (packet, from);
185  }
186 }
187 
188 
189 void PacketSink::HandlePeerClose (Ptr<Socket> socket)
190 {
191  NS_LOG_FUNCTION (this << socket);
192 }
193 
194 void PacketSink::HandlePeerError (Ptr<Socket> socket)
195 {
196  NS_LOG_FUNCTION (this << socket);
197 }
198 
199 
200 void PacketSink::HandleAccept (Ptr<Socket> s, const Address& from)
201 {
202  NS_LOG_FUNCTION (this << s << from);
203  s->SetRecvCallback (MakeCallback (&PacketSink::HandleRead, this));
204  m_socketList.push_back (s);
205 }
206 
207 } // Namespace ns3
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
Ipv4Address GetIpv4(void) const
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
virtual int ShutdownSend(void)=0
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
uint32_t GetSize(void) const
Definition: packet.h:620
void SetCloseCallbacks(Callback< void, Ptr< Socket > > normalClose, Callback< void, Ptr< Socket > > errorClose)
Detect socket recv() events such as graceful shutdown or error.
Definition: socket.cc:93
#define NS_LOG_INFO(msg)
Definition: log.h:264
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
a polymophic address class
Definition: address.h:86
virtual int Listen(void)=0
Listen for incoming connections.
Ptr< Node > GetNode() const
Definition: application.cc:103
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
virtual void StopApplication(void)
Application specific shutdown code.
Definition: packet-sink.cc:138
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
Definition: socket.cc:70
virtual void DoDispose(void)
Definition: application.cc:82
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)
Definition: callback.h:781
static Time Now(void)
Definition: simulator.cc:179
uint32_t GetTotalRx() const
Definition: packet-sink.cc:72
void SetAcceptCallback(Callback< bool, Ptr< Socket >, const Address & > connectionRequest, Callback< void, Ptr< Socket >, const Address & > newConnectionCreated)
Accept connection requests from remote hosts.
Definition: socket.cc:103
std::list< Ptr< Socket > > GetAcceptedSockets(void) const
Definition: packet-sink.cc:86
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)
Definition: packet-sink.cc:92
uint16_t GetPort(void) const
virtual void StartApplication(void)
Application specific startup code.
Definition: packet-sink.cc:104
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
Definition: packet-sink.cc:79