A Discrete-Event Network Simulator
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
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  */
21 #ifndef PACKET_SINK_H
22 #define PACKET_SINK_H
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>
33 namespace ns3 {
35 class Address;
36 class Socket;
37 class Packet;
71 class PacketSink : public Application
72 {
73 public:
78  static TypeId GetTypeId (void);
79  PacketSink ();
81  virtual ~PacketSink ();
86  uint64_t GetTotalRx () const;
91  Ptr<Socket> GetListeningSocket (void) const;
96  std::list<Ptr<Socket> > GetAcceptedSockets (void) const;
106  typedef void (* SeqTsSizeCallback)(Ptr<const Packet> p, const Address &from, const Address & to,
107  const SeqTsSizeHeader &header);
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
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);
147  void PacketReceived (const Ptr<Packet> &p, const Address &from, const Address &localAddress);
152  struct AddressHash
153  {
164  size_t operator() (const Address &x) const
165  {
168  return std::hash<uint32_t>()(a.GetIpv4 ().Get ());
169  }
170  };
172  std::unordered_map<Address, Ptr<Packet>, AddressHash> m_buffer;
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;
180  uint64_t m_totalRx;
183  bool m_enableSeqTsSizeHeader {false};
191 };
193 } // namespace ns3
195 #endif /* PACKET_SINK_H */
an Inet address class
Address m_local
Local address to bind to.
Definition: packet-sink.h:179
Header with a sequence, a timestamp, and a "size" attribute.
Forward calls to a chain of Callback.
uint64_t GetTotalRx() const
Definition: packet-sink.cc:91
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
Definition: packet-sink.h:186
static TypeId GetTypeId(void)
Get the type ID.
Definition: packet-sink.cc:43
std::list< Ptr< Socket > > GetAcceptedSockets(void) const
Definition: packet-sink.cc:105
a polymophic address class
Definition: address.h:90
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:188
std::unordered_map< Address, Ptr< Packet >, AddressHash > m_buffer
Buffer for received packets.
Definition: packet-sink.h:172
size_t operator()(const Address &x) const
operator ()
Definition: packet-sink.h:164
Hashing for the Address class.
Definition: packet-sink.h:152
The base class for all ns3 applications.
Definition: application.h:60
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
Definition: packet-sink.cc:176
TypeId m_tid
Protocol TypeId.
Definition: packet-sink.h:181
virtual void StopApplication(void)
Application specific shutdown code.
Definition: packet-sink.cc:160
void HandlePeerClose(Ptr< Socket > socket)
Handle an connection close.
Definition: packet-sink.cc:258
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
uint64_t m_totalRx
Total bytes received.
Definition: packet-sink.h:180
virtual ~PacketSink()
Definition: packet-sink.cc:86
void PacketReceived(const Ptr< Packet > &p, const Address &from, const Address &localAddress)
Packet received: assemble byte stream to extract SeqTsSizeHeader.
Definition: packet-sink.cc:219
bool m_enableSeqTsSizeHeader
Enable or disable the export of SeqTsSize header.
Definition: packet-sink.h:183
TracedCallback< Ptr< const Packet >, const Address &, const Address &, const SeqTsSizeHeader & > m_rxTraceWithSeqTsSize
Callbacks for tracing the packet Rx events, includes source, destination addresses, and headers.
Definition: packet-sink.h:190
std::list< Ptr< Socket > > m_socketList
the accepted sockets
Definition: packet-sink.h:177
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:77
void HandleAccept(Ptr< Socket > socket, const Address &from)
Handle an incoming connection.
Definition: packet-sink.cc:268
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
Definition: packet-sink.cc:263
Ptr< Socket > GetListeningSocket(void) const
Definition: packet-sink.cc:98
virtual void DoDispose(void)
Destructor implementation.
Definition: packet-sink.cc:111
virtual void StartApplication(void)
Application specific startup code.
Definition: packet-sink.cc:123
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:71
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
Ptr< Socket > m_socket
Listening socket.
Definition: packet-sink.h:176
a unique identifier for an interface.
Definition: type-id.h:58
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4(void) const