A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
three-gpp-http-server.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 Magister Solutions
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Budiarto Herman <budiarto.herman@magister.fi>
7 *
8 */
9
10#ifndef THREE_GPP_HTTP_SERVER_H
11#define THREE_GPP_HTTP_SERVER_H
12
13#include "sink-application.h"
15
16#include "ns3/address.h"
17#include "ns3/event-id.h"
18#include "ns3/nstime.h"
19#include "ns3/ptr.h"
20#include "ns3/simple-ref-count.h"
21#include "ns3/traced-callback.h"
22
23#include <map>
24#include <optional>
25#include <ostream>
26
27namespace ns3
28{
29
30class Packet;
33
34/**
35 * @ingroup http
36 * Model application which simulates the traffic of a web server. This
37 * application works in conjunction with ThreeGppHttpClient applications.
38 *
39 * The application works by responding to requests. Each request is a small
40 * packet of data which contains ThreeGppHttpHeader. The value of the *content
41 * type* field of the header determines the type of object that the client is
42 * requesting. The possible type is either a *main object* or an *embedded
43 * object*.
44 *
45 * The application is responsible to generate the right type of object and send
46 * it back to the client. The size of each object to be sent is randomly
47 * determined (see ThreeGppHttpVariables). Each object may be sent as multiple packets
48 * due to limited socket buffer space.
49 *
50 * To assist with the transmission, the application maintains several instances
51 * of ThreeGppHttpServerTxBuffer. Each instance keeps track of the object type to be
52 * served and the number of bytes left to be sent.
53 *
54 * The application accepts connection request from clients. Every connection is
55 * kept open until the client disconnects.
56 */
58{
59 public:
60 static constexpr uint16_t HTTP_DEFAULT_PORT{80}; //!< default HTTP port
61
62 /**
63 * Creates a new instance of HTTP server application.
64 *
65 * After creation, the application must be further configured through
66 * attributes. To avoid having to do this process manually, please use
67 * ThreeGppHttpServerHelper.
68 *
69 * Upon creation, the application randomly determines the MTU size that it
70 * will use (either 536 or 1460 bytes). The chosen size will be used while
71 * creating the listener socket.
72 */
74
75 /**
76 * Returns the object TypeId.
77 * @return The object TypeId.
78 */
79 static TypeId GetTypeId();
80
81 /**
82 * Sets the maximum transmission unit (MTU) size used by the application.
83 *
84 * This overrides the MTU size which is randomly determined once the
85 * application is created. Values other than the standard 536 and 1460 bytes
86 * can be set using this method.
87 *
88 * @param mtuSize MTU size in bytes.
89 */
90 void SetMtuSize(uint32_t mtuSize);
91
92 /**
93 * Returns a pointer to the listening socket.
94 * @return Pointer to the listening socket
95 */
96 Ptr<Socket> GetSocket() const;
97
98 /// The possible states of the application.
100 {
101 NOT_STARTED = 0, ///< Before StartApplication() is invoked.
102 STARTED, ///< Passively listening and responding to requests.
103 STOPPED ///< After StopApplication() is invoked.
104 };
105
106 /**
107 * Returns the current state of the application.
108 * @return The current state of the application.
109 */
110 State_t GetState() const;
111
112 /**
113 * Returns the current state of the application in string format.
114 * @return The current state of the application in string format.
115 */
116 std::string GetStateString() const;
117
118 /**
119 * Returns the given state in string format.
120 * @param state An arbitrary state of an application.
121 * @return The given state equivalently expressed in string format.
122 */
123 static std::string GetStateString(State_t state);
124
125 /**
126 * Common callback signature for `MainObject` and `EmbeddedObject` trace
127 * sources.
128 * @param size Size of the generated object in bytes.
129 */
131
132 /**
133 * Callback signature for `ConnectionEstablished` trace source.
134 * @param httpServer Pointer to this instance of ThreeGppHttpServer, which is where
135 * the trace originated.
136 * @param socket Pointer to the socket where the connection is established.
137 */
139 Ptr<Socket> socket);
140
141 protected:
142 void DoDispose() override;
143
144 private:
145 void DoStartApplication() override;
146 void DoStopApplication() override;
147 void SetLocal(const Address& addr) override;
148 void SetPort(uint32_t port) override;
149
150 // SOCKET CALLBACK METHODS
151
152 /**
153 * Invoked when #m_socket receives a connection request.
154 * @param socket Pointer to the socket where the event originates from.
155 * @param address The address of the remote client where the connection
156 * request comes from.
157 * @return Always true, to indicate to the other end that the connection
158 * request is accepted.
159 */
160 bool ConnectionRequestCallback(Ptr<Socket> socket, const Address& address);
161 /**
162 * Invoked when a new connection has been established.
163 * @param socket Pointer to the socket that maintains the connection to the
164 * remote client. This socket will be saved to the Tx buffer.
165 * @param address The address the connection is incoming from.
166 */
167 void NewConnectionCreatedCallback(Ptr<Socket> socket, const Address& address);
168 /**
169 * Invoked when a connection with a web client is terminated. The
170 * corresponding socket will be removed from Tx buffer.
171 * @param socket Pointer to the socket where the event originates from.
172 */
173 void NormalCloseCallback(Ptr<Socket> socket);
174 /**
175 * Invoked when a connection with a web client is terminated. The
176 * corresponding socket will be removed from Tx buffer.
177 * @param socket Pointer to the socket where the event originates from.
178 */
179 void ErrorCloseCallback(Ptr<Socket> socket);
180 /**
181 * Invoked when #m_socket receives some packet data. It will check the
182 * packet for ThreeGppHttpHeader. It also fires the `Rx` trace source.
183 *
184 * Depending on the type of object requested, the method will trigger
185 * ServeMainObject() or ServeEmbeddedObject() after some delays.
186 *
187 * @param socket Pointer to the socket where the event originates from.
188 */
190 /**
191 * Invoked when more buffer space for transmission is added to a socket. The
192 * method will invoke ServeFromTxBuffer() to start some transmission using
193 * the socket.
194 * @param socket Pointer to the socket where the event originates from.
195 * @param availableBufferSize The number of bytes available in the socket's
196 * transmission buffer.
197 */
198 void SendCallback(Ptr<Socket> socket, uint32_t availableBufferSize);
199
200 // TX-RELATED METHODS
201
202 /**
203 * Generates a new main object and push it into the Tx buffer.
204 *
205 * The size of the object is randomly determined by ThreeGppHttpVariables.
206 * Fires the `MainObject` trace source. It then immediately triggers
207 * ServeFromTxBuffer() to send the object.
208 *
209 * @param socket Pointer to the socket which is associated with the
210 * destination client.
211 */
212 void ServeNewMainObject(Ptr<Socket> socket);
213 /**
214 * Generates a new embedded object and push it into the Tx buffer.
215 *
216 * The size of the object is randomly determined by ThreeGppHttpVariables.
217 * Fires the `EmbeddedObject` trace source. It then immediately triggers
218 * ServeFromTxBuffer() to send the object.
219 *
220 * @param socket Pointer to the socket which is associated with the
221 * destination client.
222 */
224 /**
225 * Creates a packet out of a pending object in the Tx buffer send it over the
226 * given socket. If the socket capacity is smaller than the object size, then
227 * the method only convert a part of the object into a packet.
228 *
229 * ThreeGppHttpHeader will be attached in the beginning of each application
230 * layer packet - if a packet is split, then then the following parts will
231 * not have the header. The method fires the `Tx` trace source after sending
232 * the packet to the socket.
233 *
234 * This method is invoked when a new object is generated by
235 * ServeNewMainObject() or ServeNewEmbeddedObject(). It's also invoked when
236 * the socket informs (through SendCallback()) that more buffer space for
237 * transmission has become available.
238 *
239 * @param socket Pointer to the socket which is associated with the
240 * destination client.
241 * @return Size of the packet sent (in bytes).
242 */
244
245 /**
246 * Change the state of the server. Fires the `StateTransition` trace source.
247 * @param state The new state.
248 */
249 void SwitchToState(State_t state);
250
251 /// The current state of the client application. Begins with NOT_STARTED.
253 /// Pointer to the transmission buffer.
255
256 // ATTRIBUTES
257
258 /// The `Variables` attribute.
260 /// The `LocalPort` attribute.
261 /// Note: this is a trick needed because we have 2 ports attributes, Port and LocalPort, where
262 /// the latter is deprecated and hence this will actually go away once we can remove deprecated
263 /// attributes and code.
264 std::optional<uint16_t> m_optPort;
265 /// The `Tos` attribute.
266 uint8_t m_tos;
267 /// The `Mtu` attribute.
269
270 // TRACE SOURCES
271
272 /// The `ConnectionEstablished` trace source.
274 /// The `MainObject` trace source.
276 /// The `EmbeddedObject` trace source.
278 /// The `Tx` trace source.
280 /// The `Rx` trace source with the local address.
282 /// The `RxDelay` trace source.
284 /// The `StateTransition` trace source.
286
287}; // end of `class ThreeGppHttpServer`
288
289/**
290 * @internal
291 * @ingroup http
292 * Transmission buffer used by an HTTP server instance.
293 *
294 * The class handles the sockets which face the connected HTTP clients. An
295 * individual buffer is allocated for each socket. The buffer indicates the
296 * length (in bytes) and the type of the data within, i.e., it does *not*
297 * contain the actual packet data.
298 *
299 * Types of data are expressed using the ThreeGppHttpHeader::ContentType_t type. Only one
300 * type of data can be active for one client at a time, i.e., the current
301 * content of a buffer has to be removed before a different type of data can
302 * be added.
303 */
304class ThreeGppHttpServerTxBuffer : public SimpleRefCount<ThreeGppHttpServerTxBuffer>
305{
306 public:
307 /// Create an empty instance of transmission buffer.
309
310 // SOCKET MANAGEMENT
311
312 /**
313 * This method is typically used before calling other methods. For example,
314 * AddSocket() requires that the given socket does not exist among the stored
315 * buffers. On the other hand, all the other methods that accept a pointer to
316 * a socket as an argument require the existence of a buffer allocated to the
317 * given socket.
318 * @param socket Pointer to the socket to be found.
319 * @return True if the given socket is found within the buffer.
320 */
321 bool IsSocketAvailable(Ptr<Socket> socket) const;
322
323 /**
324 * Add a new socket and create an empty transmission buffer for it. After the
325 * method is completed, IsSocketAvailable() for the same pointer of socket
326 * shall return true.
327 * @param socket Pointer to the new socket to be added (must not exist in the
328 * buffer).
329 * @warning Must be called only when IsSocketAvailable() for the given socket
330 * is false.
331 */
332 void AddSocket(Ptr<Socket> socket);
333
334 /**
335 * Remove a socket and its associated transmission buffer, and then unset the
336 * socket's callbacks to prevent further interaction with the socket. If the
337 * socket has a pending transmission event, it will be canceled.
338 *
339 * This method is useful for discarding a socket which is already closed,
340 * e.g., by the HTTP client. This is due to the fact that double closing of a
341 * socket may introduce undefined behaviour.
342 *
343 * After the method is completed, IsSocketAvailable() for the same pointer of
344 * socket shall return false.
345 *
346 * @param socket Pointer to the socket to be removed.
347 * @warning Must be called only when IsSocketAvailable() for the given socket
348 * is true.
349 */
350 void RemoveSocket(Ptr<Socket> socket);
351
352 /**
353 * Close and remove a socket and its associated transmission buffer, and then
354 * unset the socket's callback to prevent further interaction with the
355 * socket.
356 *
357 * This method is similar with RemoveSocket(), except that the latter does
358 * not close the socket.
359 *
360 * After the method is completed, IsSocketAvailable() for the same pointer of
361 * socket shall return false.
362 *
363 * @param socket Pointer to the socket to be closed and removed.
364 * @warning Must be called only when IsSocketAvailable() for the given socket
365 * is true.
366 */
367 void CloseSocket(Ptr<Socket> socket);
368
369 /**
370 * Close and remove all stored sockets, hence clearing the buffer.
371 */
372 void CloseAllSockets();
373
374 // BUFFER MANAGEMENT
375
376 /**
377 * @param socket Pointer to the socket which is associated with the
378 * transmission buffer of interest.
379 * @return True if the current length of the transmission buffer is zero,
380 * i.e., no pending packet.
381 * @warning Must be called only when IsSocketAvailable() for the given socket
382 * is true.
383 */
384 bool IsBufferEmpty(Ptr<Socket> socket) const;
385
386 /**
387 * @param socket Pointer to the socket which is associated with the
388 * transmission buffer of interest
389 * @return The client time stamp that comes from the last request packet
390 * received by the given socket. It indicates the time the request
391 * packet was transmitted by the client.
392 */
393 Time GetClientTs(Ptr<Socket> socket) const;
394
395 /**
396 * Returns ThreeGppHttpHeader::NOT_SET when the buffer is new and never been filled
397 * with any data before. Otherwise, returns either ThreeGppHttpHeader::MAIN_OBJECT
398 * or ThreeGppHttpHeader::EMBEDDED_OBJECT.
399 * @param socket Pointer to the socket which is associated with the
400 * transmission buffer of interest
401 * @return The content type of the current data inside the transmission
402 * buffer.
403 * @warning Must be called only when IsSocketAvailable() for the given socket
404 * is true.
405 */
407
408 /**
409 * @param socket Pointer to the socket which is associated with the
410 * transmission buffer of interest
411 * @return The length (in bytes) of the current data inside the transmission
412 * buffer.
413 * @warning Must be called only when IsSocketAvailable() for the given socket
414 * is true.
415 */
416 uint32_t GetBufferSize(Ptr<Socket> socket) const;
417
418 /**
419 * @param socket pointer to the socket which is associated with the
420 * transmission buffer of interest
421 * @return true if the buffer content has been read since it is written
422 *
423 * @warning Must be called only when IsSocketAvailable() for the given socket
424 * is true.
425 *
426 * This method returns true after WriteNewObject() method is called. It
427 * becomes false after DepleteBufferSize() method is called.
428 */
429 bool HasTxedPartOfObject(Ptr<Socket> socket) const;
430
431 /**
432 * Writes a data representing a new main object or embedded object to the
433 * transmission buffer.
434 *
435 * The stored data can be later consumed wholly of partially by
436 * DepleteBufferSize() method.
437 *
438 * @param socket Pointer to the socket which is associated with the
439 * transmission buffer of interest.
440 * @param contentType The content-type of the data to be written (must not
441 * equal to ThreeGppHttpHeader:NOT_SET).
442 * @param objectSize The length (in bytes) of the new object to be created
443 * (must be greater than zero).
444 * @warning Must be called only when both IsSocketAvailable() and
445 * IsBufferEmpty() for the given socket are true.
446 */
447 void WriteNewObject(Ptr<Socket> socket,
449 uint32_t objectSize);
450
451 /**
452 * Informs about a pending transmission event associated with the socket, so
453 * that it would be automatically canceled in case the socket is closed.
454 *
455 * The method also indicates the time stamp given by the client. The time
456 * stamp will be included in every packet sent.
457 *
458 * @param socket pointer to the socket which is associated with the
459 * transmission buffer of interest
460 * @param eventId the event to be recorded, e.g., the return value of
461 * Simulator::Schedule function
462 * @param clientTs client time stamp
463 *
464 * @warning Must be called only when IsSocketAvailable() for the given socket
465 * is true.
466 */
467 void RecordNextServe(Ptr<Socket> socket, const EventId& eventId, const Time& clientTs);
468
469 /**
470 * Decrements a buffer size by a given amount.
471 *
472 * The content type of the object to be consumed can be inquired beforehand
473 * by the GetBufferContentType() method.
474 *
475 * If the method has consumed all the remaining bytes within the buffer,
476 * IsBufferEmpty() for the buffer shall return true.
477 *
478 * @param socket Pointer to the socket which is associated with the
479 * transmission buffer of interest.
480 * @param amount The length (in bytes) to be consumed (must be greater than
481 * zero).
482 *
483 * @warning Must be called only when IsSocketAvailable() for the given socket
484 * is true. In addition, the requested amount must be larger than
485 * the current buffer size, which can be checked by calling the
486 * GetBufferSize() method.
487 */
488 void DepleteBufferSize(Ptr<Socket> socket, uint32_t amount);
489
490 /**
491 * Tell the buffer to close the associated socket once the buffer becomes
492 * empty.
493 * @param socket Pointer to the socket which is associated with the
494 * transmission buffer of interest.
495 * @warning Must be called only when IsSocketAvailable() for the given socket
496 * is true.
497 */
498 void PrepareClose(Ptr<Socket> socket);
499
500 private:
501 /**
502 * Set of fields representing a single transmission buffer, which will be
503 * associated with a socket.
504 */
506 {
507 /**
508 * Pending transmission event which will be automatically canceled when the
509 * associated socket is closed.
510 */
512 /**
513 * The client time stamp that comes from the request packet. This value
514 * will be set in ThreeGppHttpHeader of every corresponding response packet sent, to
515 * be used by the client to compute round trip delay time (RTT).
516 */
518 /**
519 * The content type of the current data inside the transmission buffer.
520 * Accessible using the GetBufferContentType() method.
521 */
523 /**
524 * The length (in bytes) of the current data inside the transmission
525 * buffer. Accessible using the GetBufferSize() method.
526 */
528 /**
529 * True if the remote end has issued a request to close, which means that
530 * this socket will immediately closes itself once the buffer becomes
531 * empty.
532 */
534 /**
535 * @brief True if the buffer content has been read since it is written.
536 * Accessible using the HasTxedPartOfObject() method.
537 */
539 };
540
541 /// Collection of accepted sockets and its individual transmission buffer.
542 std::map<Ptr<Socket>, TxBuffer_t> m_txBuffer;
543
544}; // end of `class ThreeGppHttpServerTxBuffer`
545
546} // namespace ns3
547
548#endif /* THREE_GPP_HTTP_SERVER_H */
a polymophic address class
Definition address.h:111
An identifier for simulation events.
Definition event-id.h:44
network packets
Definition packet.h:228
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
SinkApplication(uint16_t defaultPort=0)
Constructor.
ContentType_t
The possible types of content (default = NOT_SET).
static constexpr uint16_t HTTP_DEFAULT_PORT
default HTTP port
State_t
The possible states of the application.
@ NOT_STARTED
Before StartApplication() is invoked.
@ STOPPED
After StopApplication() is invoked.
@ STARTED
Passively listening and responding to requests.
uint32_t ServeFromTxBuffer(Ptr< Socket > socket)
Creates a packet out of a pending object in the Tx buffer send it over the given socket.
State_t m_state
The current state of the client application. Begins with NOT_STARTED.
void(* ThreeGppHttpObjectCallback)(uint32_t size)
Common callback signature for MainObject and EmbeddedObject trace sources.
Ptr< ThreeGppHttpVariables > m_httpVariables
The Variables attribute.
void DoStartApplication() override
Application specific startup code for child subclasses.
void SetLocal(const Address &addr) override
set the local address
TracedCallback< uint32_t > m_embeddedObjectTrace
The EmbeddedObject trace source.
void ReceivedDataCallback(Ptr< Socket > socket)
Invoked when m_socket receives some packet data.
void ServeNewMainObject(Ptr< Socket > socket)
Generates a new main object and push it into the Tx buffer.
TracedCallback< uint32_t > m_mainObjectTrace
The MainObject trace source.
uint32_t m_mtuSize
The Mtu attribute.
TracedCallback< const Time &, const Address & > m_rxDelayTrace
The RxDelay trace source.
State_t GetState() const
Returns the current state of the application.
Ptr< ThreeGppHttpServerTxBuffer > m_txBuffer
Pointer to the transmission buffer.
void(* ConnectionEstablishedCallback)(Ptr< const ThreeGppHttpServer > httpServer, Ptr< Socket > socket)
Callback signature for ConnectionEstablished trace source.
bool ConnectionRequestCallback(Ptr< Socket > socket, const Address &address)
Invoked when m_socket receives a connection request.
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_rxTraceWithAddresses
The Rx trace source with the local address.
void ServeNewEmbeddedObject(Ptr< Socket > socket)
Generates a new embedded object and push it into the Tx buffer.
TracedCallback< const std::string &, const std::string & > m_stateTransitionTrace
The StateTransition trace source.
void ErrorCloseCallback(Ptr< Socket > socket)
Invoked when a connection with a web client is terminated.
void SendCallback(Ptr< Socket > socket, uint32_t availableBufferSize)
Invoked when more buffer space for transmission is added to a socket.
uint8_t m_tos
The Tos attribute.
Ptr< Socket > GetSocket() const
Returns a pointer to the listening socket.
TracedCallback< Ptr< const ThreeGppHttpServer >, Ptr< Socket > > m_connectionEstablishedTrace
The ConnectionEstablished trace source.
static TypeId GetTypeId()
Returns the object TypeId.
std::string GetStateString() const
Returns the current state of the application in string format.
ThreeGppHttpServer()
Creates a new instance of HTTP server application.
TracedCallback< Ptr< const Packet > > m_txTrace
The Tx trace source.
void NormalCloseCallback(Ptr< Socket > socket)
Invoked when a connection with a web client is terminated.
void DoDispose() override
Destructor implementation.
void DoStopApplication() override
Application specific shutdown code for child subclasses.
void NewConnectionCreatedCallback(Ptr< Socket > socket, const Address &address)
Invoked when a new connection has been established.
void SetMtuSize(uint32_t mtuSize)
Sets the maximum transmission unit (MTU) size used by the application.
std::optional< uint16_t > m_optPort
The LocalPort attribute.
void SwitchToState(State_t state)
Change the state of the server.
void SetPort(uint32_t port) override
set the server port
void DepleteBufferSize(Ptr< Socket > socket, uint32_t amount)
Decrements a buffer size by a given amount.
ThreeGppHttpHeader::ContentType_t GetBufferContentType(Ptr< Socket > socket) const
Returns ThreeGppHttpHeader::NOT_SET when the buffer is new and never been filled with any data before...
uint32_t GetBufferSize(Ptr< Socket > socket) const
void CloseAllSockets()
Close and remove all stored sockets, hence clearing the buffer.
bool HasTxedPartOfObject(Ptr< Socket > socket) const
void PrepareClose(Ptr< Socket > socket)
Tell the buffer to close the associated socket once the buffer becomes empty.
Time GetClientTs(Ptr< Socket > socket) const
void CloseSocket(Ptr< Socket > socket)
Close and remove a socket and its associated transmission buffer, and then unset the socket's callbac...
ThreeGppHttpServerTxBuffer()
Create an empty instance of transmission buffer.
void RecordNextServe(Ptr< Socket > socket, const EventId &eventId, const Time &clientTs)
Informs about a pending transmission event associated with the socket, so that it would be automatica...
std::map< Ptr< Socket >, TxBuffer_t > m_txBuffer
Collection of accepted sockets and its individual transmission buffer.
void WriteNewObject(Ptr< Socket > socket, ThreeGppHttpHeader::ContentType_t contentType, uint32_t objectSize)
Writes a data representing a new main object or embedded object to the transmission buffer.
bool IsBufferEmpty(Ptr< Socket > socket) const
bool IsSocketAvailable(Ptr< Socket > socket) const
This method is typically used before calling other methods.
void AddSocket(Ptr< Socket > socket)
Add a new socket and create an empty transmission buffer for it.
void RemoveSocket(Ptr< Socket > socket)
Remove a socket and its associated transmission buffer, and then unset the socket's callbacks to prev...
Container of various random variables to assist in generating web browsing traffic pattern.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition type-id.h:49
uint16_t port
Definition dsdv-manet.cc:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Set of fields representing a single transmission buffer, which will be associated with a socket.
ThreeGppHttpHeader::ContentType_t txBufferContentType
The content type of the current data inside the transmission buffer.
uint32_t txBufferSize
The length (in bytes) of the current data inside the transmission buffer.
EventId nextServe
Pending transmission event which will be automatically canceled when the associated socket is closed.
bool isClosing
True if the remote end has issued a request to close, which means that this socket will immediately c...
bool hasTxedPartOfObject
True if the buffer content has been read since it is written.
Time clientTs
The client time stamp that comes from the request packet.