A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
packet-sink.cc
Go to the documentation of this file.
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  ;
39 NS_OBJECT_ENSURE_REGISTERED (PacketSink)
40  ;
41 
42 TypeId
44 {
45  static TypeId tid = TypeId ("ns3::PacketSink")
47  .AddConstructor<PacketSink> ()
48  .AddAttribute ("Local", "The Address on which to Bind the rx socket.",
49  AddressValue (),
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),
55  MakeTypeIdChecker ())
56  .AddTraceSource ("Rx", "A packet has been received",
58  ;
59  return tid;
60 }
61 
63 {
64  NS_LOG_FUNCTION (this);
65  m_socket = 0;
66  m_totalRx = 0;
67 }
68 
70 {
71  NS_LOG_FUNCTION (this);
72 }
73 
74 uint32_t PacketSink::GetTotalRx () const
75 {
76  NS_LOG_FUNCTION (this);
77  return m_totalRx;
78 }
79 
82 {
83  NS_LOG_FUNCTION (this);
84  return m_socket;
85 }
86 
87 std::list<Ptr<Socket> >
89 {
90  NS_LOG_FUNCTION (this);
91  return m_socketList;
92 }
93 
95 {
96  NS_LOG_FUNCTION (this);
97  m_socket = 0;
98  m_socketList.clear ();
99 
100  // chain up
102 }
103 
104 
105 // Application Methods
106 void PacketSink::StartApplication () // Called at time specified by Start
107 {
108  NS_LOG_FUNCTION (this);
109  // Create the socket if not already
110  if (!m_socket)
111  {
113  m_socket->Bind (m_local);
114  m_socket->Listen ();
117  {
118  Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket);
119  if (udpSocket)
120  {
121  // equivalent to setsockopt (MCAST_JOIN_GROUP)
122  udpSocket->MulticastJoinGroup (0, m_local);
123  }
124  else
125  {
126  NS_FATAL_ERROR ("Error: joining multicast on a non-UDP socket");
127  }
128  }
129  }
130 
133  MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
138 }
139 
140 void PacketSink::StopApplication () // Called at time specified by Stop
141 {
142  NS_LOG_FUNCTION (this);
143  while(!m_socketList.empty ()) //these are accepted sockets, close them
144  {
145  Ptr<Socket> acceptedSocket = m_socketList.front ();
146  m_socketList.pop_front ();
147  acceptedSocket->Close ();
148  }
149  if (m_socket)
150  {
151  m_socket->Close ();
153  }
154 }
155 
157 {
158  NS_LOG_FUNCTION (this << socket);
159  Ptr<Packet> packet;
160  Address from;
161  while ((packet = socket->RecvFrom (from)))
162  {
163  if (packet->GetSize () == 0)
164  { //EOF
165  break;
166  }
167  m_totalRx += packet->GetSize ();
169  {
170  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds ()
171  << "s packet sink received "
172  << packet->GetSize () << " bytes from "
174  << " port " << InetSocketAddress::ConvertFrom (from).GetPort ()
175  << " total Rx " << m_totalRx << " bytes");
176  }
177  else if (Inet6SocketAddress::IsMatchingType (from))
178  {
179  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds ()
180  << "s packet sink received "
181  << packet->GetSize () << " bytes from "
183  << " port " << Inet6SocketAddress::ConvertFrom (from).GetPort ()
184  << " total Rx " << m_totalRx << " bytes");
185  }
186  m_rxTrace (packet, from);
187  }
188 }
189 
190 
192 {
193  NS_LOG_FUNCTION (this << socket);
194 }
195 
197 {
198  NS_LOG_FUNCTION (this << socket);
199 }
200 
201 
203 {
204  NS_LOG_FUNCTION (this << s << from);
206  m_socketList.push_back (s);
207 }
208 
209 } // Namespace ns3
static TypeId GetTypeId(void)
Get the type ID.
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
Ipv4Address GetIpv4(void) const
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Address m_local
Local address to bind to.
Definition: packet-sink.h:129
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
uint32_t m_totalRx
Total bytes received.
Definition: packet-sink.h:130
virtual int ShutdownSend(void)=0
bool IsMulticast(const Address &ad)
Address family-independent test for a multicast address.
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
Definition: packet-sink.h:134
uint32_t GetSize(void) const
Definition: packet.h:650
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:94
#define NS_LOG_INFO(msg)
Definition: log.h:298
static TypeId GetTypeId(void)
Get the type ID.
Definition: packet-sink.cc:43
Callback< R > MakeNullCallback(void)
Definition: callback.h:1395
#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.
The base class for all ns3 applications.
Definition: application.h:61
Ptr< SampleEmitter > s
Ptr< Node > GetNode() const
Definition: application.cc:104
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
Definition: packet-sink.cc:156
hold objects of type ns3::TypeId
TypeId m_tid
Protocol TypeId.
Definition: packet-sink.h:131
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
virtual void StopApplication(void)
Application specific shutdown code.
Definition: packet-sink.cc:140
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:71
void HandlePeerClose(Ptr< Socket > socket)
Handle an connection close.
Definition: packet-sink.cc:191
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: application.cc:83
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)
virtual ~PacketSink()
Definition: packet-sink.cc:69
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
uint32_t GetTotalRx() const
Definition: packet-sink.cc:74
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:104
std::list< Ptr< Socket > > GetAcceptedSockets(void) const
Definition: packet-sink.cc:88
std::list< Ptr< Socket > > m_socketList
the accepted sockets
Definition: packet-sink.h:127
hold objects of type ns3::Address
void HandleAccept(Ptr< Socket > socket, const Address &from)
Handle an incoming connection.
Definition: packet-sink.cc:202
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
Definition: packet-sink.cc:196
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)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: packet-sink.cc:94
uint16_t GetPort(void) const
virtual void StartApplication(void)
Application specific startup code.
Definition: packet-sink.cc:106
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.
Ptr< Socket > m_socket
Listening socket.
Definition: packet-sink.h:126
virtual int Close(void)=0
Close a socket.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
static bool IsMatchingType(const Address &address)
Ptr< Socket > GetListeningSocket(void) const
Definition: packet-sink.cc:81