|
A Discrete-Event Network Simulator
|
API
|
Go to the documentation of this file.
25 #include <ns3/simulator.h>
26 #include <ns3/callback.h>
27 #include <ns3/pointer.h>
28 #include <ns3/uinteger.h>
29 #include <ns3/double.h>
30 #include <ns3/three-gpp-http-variables.h>
31 #include <ns3/packet.h>
32 #include <ns3/socket.h>
33 #include <ns3/tcp-socket-factory.h>
34 #include <ns3/inet-socket-address.h>
35 #include <ns3/inet6-socket-address.h>
36 #include <ns3/unused.h>
48 : m_state (NOT_STARTED),
50 m_objectBytesToBeReceived (0),
53 m_embeddedObjectsToBeRequested (0),
66 .AddConstructor<ThreeGppHttpClient> ()
67 .AddAttribute (
"Variables",
68 "Variable collection, which is used to control e.g. timing and HTTP request size.",
71 MakePointerChecker<ThreeGppHttpVariables> ())
72 .AddAttribute (
"RemoteServerAddress",
73 "The address of the destination server.",
77 .AddAttribute (
"RemoteServerPort",
78 "The destination port of the outbound packets.",
81 MakeUintegerChecker<uint16_t> ())
82 .AddTraceSource (
"ConnectionEstablished",
83 "Connection to the destination web server has been established.",
85 "ns3::ThreeGppHttpClient::TracedCallback")
86 .AddTraceSource (
"ConnectionClosed",
87 "Connection to the destination web server is closed.",
89 "ns3::ThreeGppHttpClient::TracedCallback")
90 .AddTraceSource (
"Tx",
91 "General trace for sending a packet of any kind.",
93 "ns3::Packet::TracedCallback")
94 .AddTraceSource (
"TxMainObjectRequest",
95 "Sent a request for a main object.",
97 "ns3::Packet::TracedCallback")
98 .AddTraceSource (
"TxEmbeddedObjectRequest",
99 "Sent a request for an embedded object.",
101 "ns3::Packet::TracedCallback")
102 .AddTraceSource (
"RxMainObjectPacket",
103 "A packet of main object has been received.",
105 "ns3::Packet::TracedCallback")
106 .AddTraceSource (
"RxMainObject",
107 "Received a whole main object. Header is included.",
109 "ns3::ThreeGppHttpClient::TracedCallback")
110 .AddTraceSource (
"RxEmbeddedObjectPacket",
111 "A packet of embedded object has been received.",
113 "ns3::Packet::TracedCallback")
114 .AddTraceSource (
"RxEmbeddedObject",
115 "Received a whole embedded object. Header is included.",
117 "ns3::ThreeGppHttpClient::TracedCallback")
118 .AddTraceSource (
"Rx",
119 "General trace for receiving a packet of any kind.",
121 "ns3::Packet::PacketAddressTracedCallback")
122 .AddTraceSource (
"RxDelay",
123 "General trace of delay for receiving a complete object.",
125 "ns3::Application::DelayAddressCallback")
126 .AddTraceSource (
"RxRtt",
127 "General trace of round trip delay time for receiving a complete object.",
129 "ns3::Application::DelayAddressCallback")
130 .AddTraceSource (
"StateTransition",
131 "Trace fired upon every HTTP client state transition.",
133 "ns3::Application::StateTransitionCallback")
166 return "NOT_STARTED";
172 return "EXPECTING_MAIN_OBJECT";
175 return "PARSING_MAIN_OBJECT";
178 return "EXPECTING_EMBEDDED_OBJECT";
188 return "FATAL_ERROR";
221 <<
" for StartApplication().");
258 <<
" for ConnectionSucceeded().");
277 <<
" for ConnectionFailed().");
291 NS_LOG_ERROR (
this <<
" Connection has been terminated,"
292 <<
" error code: " << socket->
GetErrno () <<
".");
310 NS_LOG_ERROR (
this <<
" Connection has been terminated,"
311 <<
" error code: " << socket->
GetErrno () <<
".");
326 while ((packet = socket->
RecvFrom (from)))
333 #ifdef NS3_LOG_ENABLE
363 <<
" for ReceivedData().");
396 <<
" / " << inetSocket <<
".");
398 NS_LOG_DEBUG (
this <<
" Connect() return value= " << ret
412 <<
" / " << inet6Socket <<
".");
414 NS_LOG_DEBUG (
this <<
" Connect() return value= " << ret
439 <<
" for OpenConnection().");
465 <<
" of " << packet->
GetSize () <<
" bytes,"
466 <<
" return value= " << actualBytes <<
".");
467 if (actualBytes !=
static_cast<int> (
packetSize))
469 NS_LOG_ERROR (
this <<
" Failed to send request for embedded object,"
471 <<
" waiting for another Tx opportunity.");
481 <<
" for RequestMainObject().");
510 <<
" of " << packet->
GetSize () <<
" bytes,"
511 <<
" return value= " << actualBytes <<
".");
513 if (actualBytes !=
static_cast<int> (
packetSize))
515 NS_LOG_ERROR (
this <<
" Failed to send request for embedded object,"
517 <<
" waiting for another Tx opportunity.");
527 NS_LOG_WARN (
this <<
" No embedded object to be requested.");
533 <<
" for RequestEmbeddedObject().");
564 <<
" remains from this chunk of main object.");
572 NS_LOG_INFO (
this <<
" Finished receiving a main object.");
595 <<
" for ReceiveMainObject().");
625 <<
" remains from this chunk of embedded object");
633 NS_LOG_INFO (
this <<
" Finished receiving an embedded object.");
651 <<
" more embedded object(s) to be requested.");
662 NS_LOG_INFO (
this <<
" Finished receiving a web page.");
672 <<
" for ReceiveEmbeddedObject().");
686 bool firstPacket =
false;
705 uint32_t contentSize = packet->
GetSize ();
713 <<
" (" << contentSize <<
" bytes of content)"
715 <<
" the content that we expected to receive"
742 NS_LOG_INFO (
this <<
" The parsing of this main object"
743 <<
" will complete in "
752 <<
" for EnterParsingTime().");
767 <<
" embedded object(s) in the main object.");
784 NS_LOG_INFO (
this <<
" Finished receiving a web page.");
792 <<
" for ParseMainObject().");
806 NS_LOG_INFO (
this <<
" Client will finish reading this web page in "
817 <<
" for EnterReadingTime().");
829 NS_LOG_INFO (
this <<
" Canceling RequestMainObject() which is due in "
837 NS_LOG_INFO (
this <<
" Canceling RequestEmbeddedObject() which is due in "
845 NS_LOG_INFO (
this <<
" Canceling ParseMainObject() which is due in "
865 <<
" if the previous object"
867 <<
" is not completely received yet.");
873 <<
" --> " << newState <<
".");
a unique identifier for an interface.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
static bool IsMatchingType(const Address &addr)
If the address match.
static bool IsExpired(const EventId &id)
Check if an event has already run or been cancelled.
static bool IsMatchingType(const Address &address)
If the Address matches the type.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
ns3::TracedCallback< Ptr< const ThreeGppHttpClient > > m_connectionClosedTrace
The ConnectionClosed trace source.
static bool IsMatchingType(const Address &address)
Ptr< ThreeGppHttpVariables > m_httpVariables
The Variables attribute.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
void Receive(Ptr< Packet > packet)
Simulate a consumption of the received packet by subtracting the packet size from the internal counte...
static TypeId GetTypeId(void)
Get the type ID.
State_t
The possible states of the application.
static bool IsMatchingType(const Address &address)
EventId m_eventRequestMainObject
An event of either RequestMainObject() or OpenConnection(), scheduled to trigger after a connection h...
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
ns3::TracedCallback< Ptr< const ThreeGppHttpClient >, Ptr< const Packet > > m_rxEmbeddedObjectTrace
The TxEmbeddedObject trace source.
ns3::TracedCallback< Ptr< const Packet > > m_txTrace
The Tx trace source.
void AddHeader(const Header &header)
Add header to this packet.
ns3::TracedCallback< Ptr< const Packet > > m_rxMainObjectPacketTrace
The TxMainObjectPacket trace source.
uint16_t GetPort(void) const
void SetCloseCallbacks(Callback< void, Ptr< Socket > > normalClose, Callback< void, Ptr< Socket > > errorClose)
Detect socket recv() events such as graceful shutdown or error.
Ptr< Socket > m_socket
The socket for sending and receiving packets to/from the web server.
Ptr< const AttributeAccessor > MakeAddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
ns3::TracedCallback< Ptr< const Packet > > m_txEmbeddedObjectRequestTrace
The TxEmbeddedObjectRequest trace source.
static Time Now(void)
Return the current simulation virtual time.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::TracedCallback< const Time &, const Address & > m_rxRttTrace
The RxRtt trace source.
Ptr< Packet > m_constructedPacket
The packet constructed of one or more parts with ThreeGppHttpHeader.
void RequestMainObject()
Send a request object for a main object to the destination web server.
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
@ EXPECTING_EMBEDDED_OBJECT
Sent the server a request for an embedded object and waiting to receive the packets.
ns3::TracedCallback< const Time &, const Address & > m_rxDelayTrace
The RxDelay trace source.
ns3::TracedCallback< Ptr< const ThreeGppHttpClient >, Ptr< const Packet > > m_rxMainObjectTrace
The TxMainObject trace source.
AttributeValue implementation for Address.
void SwitchToState(State_t state)
Change the state of the client.
EventId m_eventRequestEmbeddedObject
An event of either RequestEmbeddedObject() or OpenConnection().
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
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.
Ipv4 addresses are stored in host order in this class.
ns3::TracedCallback< Ptr< const Packet > > m_txMainObjectRequestTrace
The TxMainObjectRequest trace source.
Describes an IPv6 address.
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
Hold objects of type Ptr<T>.
uint16_t m_remoteServerPort
The RemoteServerPort attribute.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
@ CONNECTING
Sent the server a connection request and waiting for the server to be accept it.
virtual void DoDispose(void)
Destructor implementation.
TimeWithUnit As(const enum Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
void ConnectionFailedCallback(Ptr< Socket > socket)
Invoked when m_socket cannot establish a connection with the web server.
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Callback< R, Ts... > MakeNullCallback(void)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
void CancelAllPendingEvents()
Cancels m_eventRequestMainObject, m_eventRequestEmbeddedObject, and m_eventParseMainObject.
void ParseMainObject()
Randomly determines the number of embedded objects in the main object.
uint16_t GetPort(void) const
Get the port.
uint32_t m_embeddedObjectsToBeRequested
Determined after parsing the main object.
void ReceiveMainObject(Ptr< Packet > packet, const Address &from)
Receive a packet of main object from the destination web server.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Ptr< const AttributeChecker > MakeAddressChecker(void)
@ STOPPED
After StopApplication() is invoked.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
void ConnectionSucceededCallback(Ptr< Socket > socket)
Invoked when a connection is established successfully on m_socket.
Time m_objectClientTs
The client time stamp of the ThreeGppHttpHeader from the last received packet.
a polymophic address class
bool IsZero(void) const
Exactly equivalent to t == 0.
virtual void DoDispose()
Destructor implementation.
Time m_objectServerTs
The server time stamp of the ThreeGppHttpHeader from the last received packet.
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...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
Ipv4Address GetIpv4(void) const
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
#define NS_UNUSED(x)
Mark a local variable as unused.
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.
ThreeGppHttpClient()
Creates a new instance of HTTP client application.
Ptr< Socket > GetSocket() const
Returns a pointer to the associated socket.
Simulation virtual time values and global simulation resolution.
virtual int Close(void)=0
Close a socket.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
void ErrorCloseCallback(Ptr< Socket > socket)
Invoked when connection between m_socket and the web sever is terminated.
Address m_remoteServerAddress
The RemoteServerAddress attribute. The address of the web server.
void NormalCloseCallback(Ptr< Socket > socket)
Invoked when connection between m_socket and the web sever is terminated.
ns3::TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
The Rx trace source.
uint32_t m_objectBytesToBeReceived
According to the content length specified by the ThreeGppHttpHeader.
EventId m_eventParseMainObject
An event of ParseMainObject(), scheduled to trigger after parsing time has elapsed.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
ns3::TracedCallback< Ptr< const Packet > > m_rxEmbeddedObjectPacketTrace
The TxEmbeddedObjectPacket trace source.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
@ NOT_STARTED
Before StartApplication() is invoked.
static TypeId GetTypeId()
Returns the object TypeId.
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Container of various random variables to assist in generating web browsing traffic pattern.
virtual void StopApplication()
Application specific shutdown code.
virtual enum Socket::SocketErrno GetErrno(void) const =0
Get last error number.
static const uint32_t packetSize
@ READING
User reading a web page that has just been received.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
void OpenConnection()
Initialize m_socket to connect to the destination web server at m_remoteServerAddress and m_remoteSer...
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
ns3::TracedCallback< const std::string &, const std::string & > m_stateTransitionTrace
The StateTransition trace source.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
static bool IsFinished(void)
Check if the simulation should finish.
@ EXPECTING_MAIN_OBJECT
Sent the server a request for a main object and waiting to receive the packets.
void EnterReadingTime()
Becomes idle for a randomly determined amount of time, and then triggers RequestMainObject().
Hold an unsigned integer type.
Ptr< Node > GetNode() const
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...
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
The base class for all ns3 applications.
@ PARSING_MAIN_OBJECT
Parsing a main object that has just been received.
State_t GetState() const
Returns the current state of the application.
void EnterParsingTime()
Becomes idle for a randomly determined amount of time, and then triggers ParseMainObject().
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
void ReceivedDataCallback(Ptr< Socket > socket)
Invoked when m_socket receives some packet data.
ns3::TracedCallback< Ptr< const ThreeGppHttpClient > > m_connectionEstablishedTrace
The ConnectionEstablished trace source.
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
static Ipv4Address ConvertFrom(const Address &address)
std::string GetStateString() const
Returns the current state of the application in string format.
void ReceiveEmbeddedObject(Ptr< Packet > packet, const Address &from)
Receive a packet of embedded object from the destination web server.
State_t m_state
The current state of the client application. Begins with NOT_STARTED.
virtual void StartApplication()
Application specific startup code.
void RequestEmbeddedObject()
Send a request object for an embedded object to the destination web server.