14#include <ns3/callback.h>
15#include <ns3/double.h>
16#include <ns3/inet-socket-address.h>
17#include <ns3/inet6-socket-address.h>
19#include <ns3/packet.h>
20#include <ns3/pointer.h>
21#include <ns3/simulator.h>
22#include <ns3/socket.h>
23#include <ns3/tcp-socket-factory.h>
24#include <ns3/uinteger.h>
34 : m_state(NOT_STARTED),
36 m_objectBytesToBeReceived(0),
39 m_embeddedObjectsToBeRequested(0),
41 m_numberEmbeddedObjectsRequested(0),
53 TypeId(
"ns3::ThreeGppHttpClient")
55 .AddConstructor<ThreeGppHttpClient>()
58 "Variable collection, which is used to control e.g. timing and HTTP request size.",
62 .AddAttribute(
"RemoteServerAddress",
63 "The address of the destination server.",
67 .AddAttribute(
"RemoteServerPort",
68 "The destination port of the outbound packets.",
73 "The Type of Service used to send packets. "
74 "All 8 bits of the TOS byte are set (including ECN bits).",
78 .AddTraceSource(
"RxPage",
79 "A page has been received.",
81 "ns3::ThreeGppHttpClient::RxPageTracedCallback")
83 "ConnectionEstablished",
84 "Connection to the destination web server has been established.",
86 "ns3::ThreeGppHttpClient::TracedCallback")
87 .AddTraceSource(
"ConnectionClosed",
88 "Connection to the destination web server is closed.",
90 "ns3::ThreeGppHttpClient::TracedCallback")
92 "General trace for sending a packet of any kind.",
94 "ns3::Packet::TracedCallback")
96 "TxMainObjectRequest",
97 "Sent a request for a main object.",
99 "ns3::Packet::TracedCallback")
101 "TxEmbeddedObjectRequest",
102 "Sent a request for an embedded object.",
104 "ns3::Packet::TracedCallback")
105 .AddTraceSource(
"RxMainObjectPacket",
106 "A packet of main object has been received.",
108 "ns3::Packet::TracedCallback")
109 .AddTraceSource(
"RxMainObject",
110 "Received a whole main object. Header is included.",
112 "ns3::ThreeGppHttpClient::TracedCallback")
114 "RxEmbeddedObjectPacket",
115 "A packet of embedded object has been received.",
117 "ns3::Packet::TracedCallback")
118 .AddTraceSource(
"RxEmbeddedObject",
119 "Received a whole embedded object. Header is included.",
121 "ns3::ThreeGppHttpClient::TracedCallback")
122 .AddTraceSource(
"Rx",
123 "General trace for receiving a packet of any kind.",
125 "ns3::Packet::PacketAddressTracedCallback")
126 .AddTraceSource(
"RxDelay",
127 "General trace of delay for receiving a complete object.",
129 "ns3::Application::DelayAddressCallback")
132 "General trace of round trip delay time for receiving a complete object.",
134 "ns3::Application::DelayAddressCallback")
135 .AddTraceSource(
"StateTransition",
136 "Trace fired upon every HTTP client state transition.",
138 "ns3::Application::StateTransitionCallback");
167 return "NOT_STARTED";
171 return "EXPECTING_MAIN_OBJECT";
173 return "PARSING_MAIN_OBJECT";
175 return "EXPECTING_EMBEDDED_OBJECT";
182 return "FATAL_ERROR";
275 <<
" error code: " << socket->GetErrno() <<
".");
293 <<
" error code: " << socket->GetErrno() <<
".");
307 while ((packet = socket->RecvFrom(from)))
309 if (packet->GetSize() == 0)
318 NS_LOG_INFO(
this <<
" A packet of " << packet->GetSize() <<
" bytes"
325 NS_LOG_INFO(
this <<
" A packet of " << packet->GetSize() <<
" bytes"
361 "'RemoteServerAddress' attribute not properly set");
364 int ret [[maybe_unused]];
373 << inetSocket <<
".");
381 int ret [[maybe_unused]];
390 << inet6Socket <<
".");
431 packet->AddHeader(header);
436 NS_LOG_DEBUG(
this <<
" Send() packet " << packet <<
" of " << packet->GetSize() <<
" bytes,"
437 <<
" return value= " << actualBytes <<
".");
438 if (actualBytes !=
static_cast<int>(
packetSize))
440 NS_LOG_ERROR(
this <<
" Failed to send request for embedded object,"
442 <<
" waiting for another Tx opportunity.");
474 packet->AddHeader(header);
479 NS_LOG_DEBUG(
this <<
" Send() packet " << packet <<
" of " << packet->GetSize()
481 <<
" return value= " << actualBytes <<
".");
483 if (actualBytes !=
static_cast<int>(
packetSize))
485 NS_LOG_ERROR(
this <<
" Failed to send request for embedded object,"
487 <<
" waiting for another Tx opportunity.");
497 NS_LOG_WARN(
this <<
" No embedded object to be requested.");
532 <<
" remains from this chunk of main object.");
540 NS_LOG_INFO(
this <<
" Finished receiving a main object.");
591 <<
" remains from this chunk of embedded object");
599 NS_LOG_INFO(
this <<
" Finished receiving an embedded object.");
617 <<
" more embedded object(s) to be requested.");
628 NS_LOG_INFO(
this <<
" Finished receiving a web page.");
651 bool firstPacket =
false;
660 packet->RemoveHeader(httpHeader);
670 uint32_t contentSize = packet->GetSize();
679 <<
" (" << contentSize <<
" bytes of content)"
681 <<
" the content that we expected to receive"
707 NS_LOG_INFO(
this <<
" The parsing of this main object"
708 <<
" will complete in " << parsingTime.
As(
Time::S) <<
".");
730 <<
" embedded object(s) in the main object.");
747 NS_LOG_INFO(
this <<
" Finished receiving a web page.");
767 NS_LOG_INFO(
this <<
" Client will finish reading this web page in "
788 NS_LOG_INFO(
this <<
" Canceling RequestMainObject() which is due in "
795 NS_LOG_INFO(
this <<
" Canceling RequestEmbeddedObject() which is due in "
803 NS_LOG_INFO(
this <<
" Canceling ParseMainObject() which is due in "
821 <<
" if the previous object"
823 <<
" is not completely received yet.");
828 NS_LOG_INFO(
this <<
" HttpClient " << oldState <<
" --> " << newState <<
".");
a polymophic address class
The base class for all ns3 applications.
void DoDispose() override
Destructor implementation.
Ptr< Node > GetNode() const
This class can be used to hold variables of floating point type such as 'double' or 'float'.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
uint16_t GetPort() const
Get the port.
static bool IsMatchingType(const Address &addr)
If the address match.
Ipv6Address GetIpv6() const
Get the IPv6 address.
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address ConvertFrom(const Address &address)
static bool IsMatchingType(const Address &address)
Describes an IPv6 address.
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.
static bool IsMatchingType(const Address &address)
If the Address matches the type.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
void AddHeader(const Header &header)
Add header to this packet.
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
static bool IsFinished()
Check if the simulation should finish.
static Time Now()
Return the current simulation virtual time.
static bool IsExpired(const EventId &id)
Check if an event has already run or been cancelled.
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SetConnectCallback(Callback< void, Ptr< Socket > > connectionSucceeded, Callback< void, Ptr< Socket > > connectionFailed)
Specify callbacks to allow the caller to determine if the connection succeeds of fails.
virtual Socket::SocketErrno GetErrno() const =0
Get last error number.
void SetIpTos(uint8_t ipTos)
Manually set IP Type of Service field.
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
void SetCloseCallbacks(Callback< void, Ptr< Socket > > normalClose, Callback< void, Ptr< Socket > > errorClose)
Detect socket recv() events such as graceful shutdown or error.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
static TypeId GetTypeId()
Get the type ID.
ns3::TracedCallback< const Time &, const Address & > m_rxRttTrace
The RxRtt trace source.
Time m_pageLoadStartTs
The time stamp when the page started loading.
ns3::TracedCallback< Ptr< const ThreeGppHttpClient >, const Time &, uint32_t, uint32_t > m_rxPageTrace
The RxPage trace source.
void ReceiveMainObject(Ptr< Packet > packet, const Address &from)
Receive a packet of main object from the destination web server.
ns3::TracedCallback< Ptr< const ThreeGppHttpClient > > m_connectionClosedTrace
The ConnectionClosed trace source.
ns3::TracedCallback< const Time &, const Address & > m_rxDelayTrace
The RxDelay trace source.
ns3::TracedCallback< Ptr< const ThreeGppHttpClient >, Ptr< const Packet > > m_rxEmbeddedObjectTrace
The TxEmbeddedObject trace source.
Ptr< ThreeGppHttpVariables > m_httpVariables
The Variables attribute.
uint32_t m_embeddedObjectsToBeRequested
Determined after parsing the main object.
EventId m_eventParseMainObject
An event of ParseMainObject(), scheduled to trigger after parsing time has elapsed.
void SwitchToState(State_t state)
Change the state of the client.
uint32_t m_numberEmbeddedObjectsRequested
Number of embedded objects to requested in the current page.
void ConnectionFailedCallback(Ptr< Socket > socket)
Invoked when m_socket cannot establish a connection with the web server.
ThreeGppHttpClient()
Creates a new instance of HTTP client application.
ns3::TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
The Rx trace source.
void EnterParsingTime()
Becomes idle for a randomly determined amount of time, and then triggers ParseMainObject().
State_t m_state
The current state of the client application. Begins with NOT_STARTED.
void FinishReceivingPage()
Finish receiving a page.
Ptr< Socket > m_socket
The socket for sending and receiving packets to/from the web server.
void RequestEmbeddedObject()
Send a request object for an embedded object to the destination web server.
void ParseMainObject()
Randomly determines the number of embedded objects in the main object.
ns3::TracedCallback< Ptr< const ThreeGppHttpClient >, Ptr< const Packet > > m_rxMainObjectTrace
The TxMainObject trace source.
State_t GetState() const
Returns the current state of the application.
void EnterReadingTime()
Becomes idle for a randomly determined amount of time, and then triggers RequestMainObject().
void DoDispose() override
Destructor implementation.
void StartApplication() override
Application specific startup code.
Ptr< Packet > m_constructedPacket
The packet constructed of one or more parts with ThreeGppHttpHeader.
uint32_t m_objectBytesToBeReceived
According to the content length specified by the ThreeGppHttpHeader.
void ConnectionSucceededCallback(Ptr< Socket > socket)
Invoked when a connection is established successfully on m_socket.
static TypeId GetTypeId()
Returns the object TypeId.
void ErrorCloseCallback(Ptr< Socket > socket)
Invoked when connection between m_socket and the web sever is terminated.
EventId m_eventRequestMainObject
An event of either RequestMainObject() or OpenConnection(), scheduled to trigger after a connection h...
uint16_t m_remoteServerPort
The RemoteServerPort attribute.
uint32_t m_numberBytesPage
Number of bytes received for the current page.
Address m_remoteServerAddress
The RemoteServerAddress attribute. The address of the web server.
void CancelAllPendingEvents()
Cancels m_eventRequestMainObject, m_eventRequestEmbeddedObject, and m_eventParseMainObject.
Ptr< Socket > GetSocket() const
Returns a pointer to the associated socket.
ns3::TracedCallback< Ptr< const Packet > > m_txTrace
The Tx trace source.
uint8_t m_tos
The Tos attribute.
Time m_objectClientTs
The client time stamp of the ThreeGppHttpHeader from the last received packet.
State_t
The possible states of the application.
@ CONNECTING
Sent the server a connection request and waiting for the server to be accept it.
@ NOT_STARTED
Before StartApplication() is invoked.
@ READING
User reading a web page that has just been received.
@ EXPECTING_MAIN_OBJECT
Sent the server a request for a main object and waiting to receive the packets.
@ STOPPED
After StopApplication() is invoked.
@ PARSING_MAIN_OBJECT
Parsing a main object that has just been received.
@ EXPECTING_EMBEDDED_OBJECT
Sent the server a request for an embedded object and waiting to receive the packets.
ns3::TracedCallback< Ptr< const Packet > > m_txMainObjectRequestTrace
The TxMainObjectRequest trace source.
void StopApplication() override
Application specific shutdown code.
ns3::TracedCallback< Ptr< const Packet > > m_rxMainObjectPacketTrace
The TxMainObjectPacket trace source.
ns3::TracedCallback< const std::string &, const std::string & > m_stateTransitionTrace
The StateTransition trace source.
void RequestMainObject()
Send a request object for a main object to the destination web server.
Time m_objectServerTs
The server time stamp of the ThreeGppHttpHeader from the last received packet.
void NormalCloseCallback(Ptr< Socket > socket)
Invoked when connection between m_socket and the web sever is terminated.
ns3::TracedCallback< Ptr< const ThreeGppHttpClient > > m_connectionEstablishedTrace
The ConnectionEstablished trace source.
ns3::TracedCallback< Ptr< const Packet > > m_txEmbeddedObjectRequestTrace
The TxEmbeddedObjectRequest trace source.
void ReceivedDataCallback(Ptr< Socket > socket)
Invoked when m_socket receives some packet data.
ns3::TracedCallback< Ptr< const Packet > > m_rxEmbeddedObjectPacketTrace
The TxEmbeddedObjectPacket trace source.
void Receive(Ptr< Packet > packet)
Simulate a consumption of the received packet by subtracting the packet size from the internal counte...
void ReceiveEmbeddedObject(Ptr< Packet > packet, const Address &from)
Receive a packet of embedded object from the destination web server.
void OpenConnection()
Initialize m_socket to connect to the destination web server at m_remoteServerAddress and m_remoteSer...
std::string GetStateString() const
Returns the current state of the application in string format.
EventId m_eventRequestEmbeddedObject
An event of either RequestEmbeddedObject() or OpenConnection().
Container of various random variables to assist in generating web browsing traffic pattern.
Simulation virtual time values and global simulation resolution.
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
bool IsZero() const
Exactly equivalent to t == 0.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Hold an unsigned integer type.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Callback< R, Args... > MakeNullCallback()
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeUintegerChecker()
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Ptr< const AttributeChecker > MakeAddressChecker()
Ptr< const AttributeAccessor > MakeAddressAccessor(T1 a1)
static const uint32_t packetSize
Packet size generated at the AP.