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