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 using namespace std;
36 
37 namespace ns3 {
38 
39 NS_LOG_COMPONENT_DEFINE ("PacketSink");
40 NS_OBJECT_ENSURE_REGISTERED (PacketSink);
41 
42 TypeId
43 PacketSink::GetTypeId (void)
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.",
53  TypeIdValue (UdpSocketFactory::GetTypeId ()),
54  MakeTypeIdAccessor (&PacketSink::m_tid),
55  MakeTypeIdChecker ())
56  .AddTraceSource ("Rx", "A packet has been received",
57  MakeTraceSourceAccessor (&PacketSink::m_rxTrace))
58  ;
59  return tid;
60 }
61 
62 PacketSink::PacketSink ()
63 {
64  NS_LOG_FUNCTION (this);
65  m_socket = 0;
66  m_totalRx = 0;
67 }
68 
69 PacketSink::~PacketSink()
70 {
71  NS_LOG_FUNCTION (this);
72 }
73 
74 uint32_t PacketSink::GetTotalRx () const
75 {
76  return m_totalRx;
77 }
78 
80 PacketSink::GetListeningSocket (void) const
81 {
82  NS_LOG_FUNCTION (this);
83  return m_socket;
84 }
85 
86 std::list<Ptr<Socket> >
87 PacketSink::GetAcceptedSockets (void) const
88 {
89  NS_LOG_FUNCTION (this);
90  return m_socketList;
91 }
92 
93 void PacketSink::DoDispose (void)
94 {
95  NS_LOG_FUNCTION (this);
96  m_socket = 0;
97  m_socketList.clear ();
98 
99  // chain up
100  Application::DoDispose ();
101 }
102 
103 
104 // Application Methods
105 void PacketSink::StartApplication () // Called at time specified by Start
106 {
107  NS_LOG_FUNCTION (this);
108  // Create the socket if not already
109  if (!m_socket)
110  {
111  m_socket = Socket::CreateSocket (GetNode (), m_tid);
112  m_socket->Bind (m_local);
113  m_socket->Listen ();
114  m_socket->ShutdownSend ();
115  if (addressUtils::IsMulticast (m_local))
116  {
117  Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket);
118  if (udpSocket)
119  {
120  // equivalent to setsockopt (MCAST_JOIN_GROUP)
121  udpSocket->MulticastJoinGroup (0, m_local);
122  }
123  else
124  {
125  NS_FATAL_ERROR ("Error: joining multicast on a non-UDP socket");
126  }
127  }
128  }
129 
130  m_socket->SetRecvCallback (MakeCallback (&PacketSink::HandleRead, this));
131  m_socket->SetAcceptCallback (
132  MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
133  MakeCallback (&PacketSink::HandleAccept, this));
134  m_socket->SetCloseCallbacks (
135  MakeCallback (&PacketSink::HandlePeerClose, this),
136  MakeCallback (&PacketSink::HandlePeerError, this));
137 }
138 
139 void PacketSink::StopApplication () // Called at time specified by Stop
140 {
141  NS_LOG_FUNCTION (this);
142  while(!m_socketList.empty ()) //these are accepted sockets, close them
143  {
144  Ptr<Socket> acceptedSocket = m_socketList.front ();
145  m_socketList.pop_front ();
146  acceptedSocket->Close ();
147  }
148  if (m_socket)
149  {
150  m_socket->Close ();
151  m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
152  }
153 }
154 
155 void PacketSink::HandleRead (Ptr<Socket> socket)
156 {
157  NS_LOG_FUNCTION (this << socket);
158  Ptr<Packet> packet;
159  Address from;
160  while ((packet = socket->RecvFrom (from)))
161  {
162  if (packet->GetSize () == 0)
163  { //EOF
164  break;
165  }
166  m_totalRx += packet->GetSize ();
167  if (InetSocketAddress::IsMatchingType (from))
168  {
169  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds ()
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");
175  }
176  else if (Inet6SocketAddress::IsMatchingType (from))
177  {
178  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds ()
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");
184  }
185  m_rxTrace (packet, from);
186  }
187 }
188 
189 
190 void PacketSink::HandlePeerClose (Ptr<Socket> socket)
191 {
192  NS_LOG_FUNCTION (this << socket);
193 }
194 
195 void PacketSink::HandlePeerError (Ptr<Socket> socket)
196 {
197  NS_LOG_FUNCTION (this << socket);
198 }
199 
200 
201 void PacketSink::HandleAccept (Ptr<Socket> s, const Address& from)
202 {
203  NS_LOG_FUNCTION (this << s << from);
204  s->SetRecvCallback (MakeCallback (&PacketSink::HandleRead, this));
205  m_socketList.push_back (s);
206 }
207 
208 } // Namespace ns3