A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
source-application.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 DERONNE SOFTWARE ENGINEERING
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Sébastien Deronne <sebastien.deronne@gmail.com>
7 */
8
9#ifndef SOURCE_APPLICATION_H
10#define SOURCE_APPLICATION_H
11
12#include "ns3/address.h"
13#include "ns3/application.h"
14#include "ns3/traced-callback.h"
15
16namespace ns3
17{
18
19class Packet;
20class Socket;
21
22/**
23 * @ingroup applications
24 * @brief Base class for source applications.
25 *
26 * This class can be used as a base class for source applications.
27 * A source application is one that primarily sources new data towards a single remote client
28 * address and port, and may also receive data (such as an HTTP server).
29 *
30 * The main purpose of this base class application public API is to provide a uniform way to
31 * configure remote and local addresses.
32 *
33 * Unlike the SinkApplication, the SourceApplication does not expose an individual Port attribute.
34 * Instead, the port values are embedded in the Local and Remote address attributes, which should be
35 * configured to an InetSocketAddress or Inet6SocketAddress value that contains the desired port
36 * number.
37 */
39{
40 public:
41 /**
42 * @brief Get the type ID.
43 * @return the object TypeId
44 */
45 static TypeId GetTypeId();
46
47 /**
48 * @brief Constructor
49 * @param allowPacketSocket flag whether the application should allow the use of packet sockets
50 */
51 explicit SourceApplication(bool allowPacketSocket = true);
52 ~SourceApplication() override;
53
54 /**
55 * @brief set the remote address
56 * @param addr remote address
57 */
58 virtual void SetRemote(const Address& addr);
59
60 /**
61 * @brief get the remote address
62 * @return the remote address
63 */
64 Address GetRemote() const;
65
66 /**
67 * @brief Get the socket this application is attached to.
68 * @return pointer to associated socket
69 */
70 Ptr<Socket> GetSocket() const;
71
72 protected:
73 void DoDispose() override;
74
75 /**
76 * @brief Close the socket
77 * @return true if the socket was closed, false if there was no socket to close
78 */
79 bool CloseSocket();
80
81 /// Traced Callback: transmitted packets.
83
84 /**
85 * TracedCallback signature for connection success/failure event.
86 *
87 * @param [in] socket The socket for which connection succeeded/failed.
88 * @param [in] local The local address.
89 * @param [in] remote The remote address.
90 */
91 typedef void (*ConnectionEventCallback)(Ptr<Socket> socket,
92 const Address& local,
93 const Address& remote);
94
95 /// Traced Callback: connection success event.
97
98 /// Traced Callback: connection failure event.
100
102
103 TypeId m_protocolTid; //!< Protocol TypeId value
104
105 Address m_peer; //!< Peer address
106 Address m_local; //!< Local address to bind to
107 uint8_t m_tos; //!< The packets Type of Service
108
109 bool m_connected{false}; //!< flag whether socket is connected
110
111 private:
112 void StartApplication() override;
113 void StopApplication() override;
114
115 /**
116 * @brief Handle a Connection Succeed event
117 * @param socket the connected socket
118 */
119 void ConnectionSucceeded(Ptr<Socket> socket);
120
121 /**
122 * @brief Handle a Connection Failed event
123 * @param socket the not connected socket
124 */
125 void ConnectionFailed(Ptr<Socket> socket);
126
127 /**
128 * @brief Application specific startup code for child subclasses
129 */
130 virtual void DoStartApplication();
131
132 /**
133 * @brief Application specific shutdown code for child subclasses
134 */
135 virtual void DoStopApplication();
136
137 /**
138 * @brief Application specific code for child subclasses upon a Connection Succeed event
139 * @param socket the connected socket
140 */
141 virtual void DoConnectionSucceeded(Ptr<Socket> socket);
142
143 /**
144 * @brief Application specific code for child subclasses upon a Connection Failed event
145 * @param socket the not connected socket
146 */
147 virtual void DoConnectionFailed(Ptr<Socket> socket);
148
149 /**
150 * @brief Cancel all pending events.
151 */
152 virtual void CancelEvents() = 0;
153
154 bool m_allowPacketSocket; //!< Allow use of packet socket
155};
156
157} // namespace ns3
158
159#endif /* SOURCE_APPLICATION_H */
a polymophic address class
Definition address.h:111
network packets
Definition packet.h:228
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
A low-level Socket API based loosely on the BSD Socket API.
Definition socket.h:57
TracedCallback< Ptr< Socket >, const Address &, const Address & > m_connectionFailure
Traced Callback: connection failure event.
TypeId m_protocolTid
Protocol TypeId value.
void StopApplication() override
Application specific shutdown code.
Address m_local
Local address to bind to.
Address GetRemote() const
get the remote address
SourceApplication(bool allowPacketSocket=true)
Constructor.
void ConnectionFailed(Ptr< Socket > socket)
Handle a Connection Failed event.
Ptr< Socket > m_socket
Socket.
TracedCallback< Ptr< Socket >, const Address &, const Address & > m_connectionSuccess
Traced Callback: connection success event.
void DoDispose() override
Destructor implementation.
Ptr< Socket > GetSocket() const
Get the socket this application is attached to.
void ConnectionSucceeded(Ptr< Socket > socket)
Handle a Connection Succeed event.
virtual void DoConnectionFailed(Ptr< Socket > socket)
Application specific code for child subclasses upon a Connection Failed event.
virtual void DoStopApplication()
Application specific shutdown code for child subclasses.
virtual void SetRemote(const Address &addr)
set the remote address
virtual void CancelEvents()=0
Cancel all pending events.
TracedCallback< Ptr< const Packet > > m_txTrace
Traced Callback: transmitted packets.
uint8_t m_tos
The packets Type of Service.
void StartApplication() override
Application specific startup code.
bool m_connected
flag whether socket is connected
bool m_allowPacketSocket
Allow use of packet socket.
static TypeId GetTypeId()
Get the type ID.
void(* ConnectionEventCallback)(Ptr< Socket > socket, const Address &local, const Address &remote)
TracedCallback signature for connection success/failure event.
Address m_peer
Peer address.
bool CloseSocket()
Close the socket.
virtual void DoStartApplication()
Application specific startup code for child subclasses.
virtual void DoConnectionSucceeded(Ptr< Socket > socket)
Application specific code for child subclasses upon a Connection Succeed event.
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition type-id.h:49
Every class exported by the ns3 library is enclosed in the ns3 namespace.