A Discrete-Event Network Simulator
API
packet-sink.h
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 
21 #ifndef PACKET_SINK_H
22 #define PACKET_SINK_H
23 
24 #include "ns3/application.h"
25 #include "ns3/event-id.h"
26 #include "ns3/ptr.h"
27 #include "ns3/traced-callback.h"
28 #include "ns3/address.h"
29 #include "ns3/inet-socket-address.h"
30 #include "ns3/seq-ts-size-header.h"
31 #include <unordered_map>
32 
33 namespace ns3 {
34 
35 class Address;
36 class Socket;
37 class Packet;
38 
71 class PacketSink : public Application
72 {
73 public:
78  static TypeId GetTypeId (void);
79  PacketSink ();
80 
81  virtual ~PacketSink ();
82 
86  uint64_t GetTotalRx () const;
87 
91  Ptr<Socket> GetListeningSocket (void) const;
92 
96  std::list<Ptr<Socket> > GetAcceptedSockets (void) const;
97 
106  typedef void (* SeqTsSizeCallback)(Ptr<const Packet> p, const Address &from, const Address & to,
107  const SeqTsSizeHeader &header);
108 
109 protected:
110  virtual void DoDispose (void);
111 private:
112  // inherited from Application base class.
113  virtual void StartApplication (void); // Called at time specified by Start
114  virtual void StopApplication (void); // Called at time specified by Stop
115 
120  void HandleRead (Ptr<Socket> socket);
126  void HandleAccept (Ptr<Socket> socket, const Address& from);
131  void HandlePeerClose (Ptr<Socket> socket);
136  void HandlePeerError (Ptr<Socket> socket);
137 
147  void PacketReceived (const Ptr<Packet> &p, const Address &from, const Address &localAddress);
148 
152  struct AddressHash
153  {
164  size_t operator() (const Address &x) const
165  {
168  return std::hash<uint32_t>()(a.GetIpv4 ().Get ());
169  }
170  };
171 
172  std::unordered_map<Address, Ptr<Packet>, AddressHash> m_buffer;
173 
174  // In the case of TCP, each socket accept returns a new socket, so the
175  // listening socket is stored separately from the accepted sockets
177  std::list<Ptr<Socket> > m_socketList;
178 
180  uint16_t m_localPort;
181  uint64_t m_totalRx;
183 
184  bool m_enableSeqTsSizeHeader {false};
185 
192 };
193 
194 } // namespace ns3
195 
196 #endif /* PACKET_SINK_H */
197 
a polymophic address class
Definition: address.h:91
The base class for all ns3 applications.
Definition: application.h:61
an Inet address class
Ipv4Address GetIpv4(void) const
static bool IsMatchingType(const Address &address)
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
uint32_t Get(void) const
Get the host-order 32-bit IP address.
Receive and consume traffic generated to an IP address and port.
Definition: packet-sink.h:72
virtual void DoDispose(void)
Destructor implementation.
Definition: packet-sink.cc:113
std::unordered_map< Address, Ptr< Packet >, AddressHash > m_buffer
Buffer for received packets.
Definition: packet-sink.h:172
Ptr< Socket > GetListeningSocket(void) const
Definition: packet-sink.cc:100
TypeId m_tid
Protocol TypeId.
Definition: packet-sink.h:182
static TypeId GetTypeId(void)
Get the type ID.
Definition: packet-sink.cc:45
uint16_t m_localPort
Local port to bind to.
Definition: packet-sink.h:180
Address m_local
Local address to bind to (address and port)
Definition: packet-sink.h:179
std::list< Ptr< Socket > > GetAcceptedSockets(void) const
Definition: packet-sink.cc:107
virtual void StartApplication(void)
Application specific startup code.
Definition: packet-sink.cc:125
std::list< Ptr< Socket > > m_socketList
the accepted sockets
Definition: packet-sink.h:177
virtual void StopApplication(void)
Application specific shutdown code.
Definition: packet-sink.cc:175
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
Definition: packet-sink.cc:191
uint64_t GetTotalRx() const
Definition: packet-sink.cc:93
void HandleAccept(Ptr< Socket > socket, const Address &from)
Handle an incoming connection.
Definition: packet-sink.cc:301
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
Definition: packet-sink.cc:296
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
Definition: packet-sink.h:187
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_rxTraceWithAddresses
Callback for tracing the packet Rx events, includes source and destination addresses.
Definition: packet-sink.h:189
void(* SeqTsSizeCallback)(Ptr< const Packet > p, const Address &from, const Address &to, const SeqTsSizeHeader &header)
TracedCallback signature for a reception with addresses and SeqTsSizeHeader.
Definition: packet-sink.h:106
uint64_t m_totalRx
Total bytes received.
Definition: packet-sink.h:181
bool m_enableSeqTsSizeHeader
Enable or disable the export of SeqTsSize header.
Definition: packet-sink.h:184
TracedCallback< Ptr< const Packet >, const Address &, const Address &, const SeqTsSizeHeader & > m_rxTraceWithSeqTsSize
Callbacks for tracing the packet Rx events, includes source, destination addresses,...
Definition: packet-sink.h:191
void HandlePeerClose(Ptr< Socket > socket)
Handle an connection close.
Definition: packet-sink.cc:291
Ptr< Socket > m_socket
Listening socket.
Definition: packet-sink.h:176
void PacketReceived(const Ptr< Packet > &p, const Address &from, const Address &localAddress)
Packet received: assemble byte stream to extract SeqTsSizeHeader.
Definition: packet-sink.cc:252
virtual ~PacketSink()
Definition: packet-sink.cc:88
Header with a sequence, a timestamp, and a "size" attribute.
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:77
Every class exported by the ns3 library is enclosed in the ns3 namespace.
list x
Random number samples.
Hashing for the Address class.
Definition: packet-sink.h:153
size_t operator()(const Address &x) const
operator ()
Definition: packet-sink.h:164