25 #include <ns3/simulator.h> 26 #include <ns3/callback.h> 27 #include <ns3/config.h> 28 #include <ns3/pointer.h> 29 #include <ns3/uinteger.h> 30 #include <ns3/three-gpp-http-variables.h> 31 #include <ns3/packet.h> 32 #include <ns3/socket.h> 33 #include <ns3/tcp-socket.h> 34 #include <ns3/tcp-socket-factory.h> 35 #include <ns3/inet-socket-address.h> 36 #include <ns3/inet6-socket-address.h> 37 #include <ns3/unused.h> 53 : m_state (NOT_STARTED),
61 NS_LOG_INFO (
this <<
" MTU size for this server application is " 72 .AddConstructor<ThreeGppHttpServer> ()
73 .AddAttribute (
"Variables",
74 "Variable collection, which is used to control e.g. processing and " 75 "object generation delays.",
78 MakePointerChecker<ThreeGppHttpVariables> ())
79 .AddAttribute (
"LocalAddress",
80 "The local address of the server, " 81 "i.e., the address on which to bind the Rx socket.",
85 .AddAttribute (
"LocalPort",
86 "Port on which the application listen for incoming packets.",
89 MakeUintegerChecker<uint16_t> ())
91 "Maximum transmission unit (in bytes) of the TCP sockets " 92 "used in this application, excluding the compulsory 40 " 93 "bytes TCP header. Typical values are 1460 and 536 bytes. " 94 "The attribute is read-only because the value is randomly " 99 MakeUintegerChecker<uint32_t> ())
100 .AddTraceSource (
"ConnectionEstablished",
101 "Connection to a remote web client has been established.",
103 "ns3::HttpServer::ConnectionEstablishedCallback")
104 .AddTraceSource (
"MainObject",
105 "A main object has been generated.",
107 "ns3::HttpServer::HttpObjectCallback")
108 .AddTraceSource (
"EmbeddedObject",
109 "An embedded object has been generated.",
111 "ns3::HttpServer::HttpObjectCallback")
112 .AddTraceSource (
"Tx",
113 "A packet has been sent.",
115 "ns3::Packet::TracedCallback")
116 .AddTraceSource (
"Rx",
117 "A packet has been received.",
119 "ns3::Packet::PacketAddressTracedCallback")
120 .AddTraceSource (
"RxDelay",
121 "A packet has been received with delay information.",
123 "ns3::Application::DelayAddressCallback")
124 .AddTraceSource (
"StateTransition",
125 "Trace fired upon every HTTP client state transition.",
127 "ns3::Application::StateTransitionCallback")
169 return "NOT_STARTED";
179 return "FATAL_ERROR";
215 if (attrInfo.
name ==
"SegmentSize")
235 <<
" / " << inetSocket <<
".");
248 <<
" / " << inet6Socket <<
".");
256 NS_LOG_DEBUG (
this <<
" Listen () return value= " << ret
283 <<
" for StartApplication().");
364 <<
" when the server instance is still running.");
367 else if (
m_txBuffer->IsSocketAvailable (socket))
401 <<
" when the server instance is still running.");
404 else if (
m_txBuffer->IsSocketAvailable (socket))
419 while ((packet = socket->
RecvFrom (from)))
426 #ifdef NS3_LOG_ENABLE 452 Time processingDelay;
457 NS_LOG_INFO (
this <<
" Will finish generating a main object" 458 <<
" in " << processingDelay.GetSeconds () <<
" seconds.");
467 processingDelay =
m_httpVariables->GetEmbeddedObjectGenerationDelay ();
468 NS_LOG_INFO (
this <<
" Will finish generating an embedded object" 469 <<
" in " << processingDelay.GetSeconds () <<
" seconds.");
494 const uint32_t txBufferSize =
m_txBuffer->GetBufferSize (socket);
497 #ifdef NS3_LOG_ENABLE 499 if (actualSent < txBufferSize)
501 switch (
m_txBuffer->GetBufferContentType (socket))
504 NS_LOG_INFO (
this <<
" Transmission of main object is suspended" 505 <<
" after " << actualSent <<
" bytes.");
508 NS_LOG_INFO (
this <<
" Transmission of embedded object is suspended" 509 <<
" after " << actualSent <<
" bytes.");
518 switch (
m_txBuffer->GetBufferContentType (socket))
521 NS_LOG_INFO (
this <<
" Finished sending a whole main object.");
524 NS_LOG_INFO (
this <<
" Finished sending a whole embedded object.");
548 NS_LOG_INFO (
this <<
" Main object to be served is " 549 << objectSize <<
" bytes.");
555 if (actualSent < objectSize)
557 NS_LOG_INFO (
this <<
" Transmission of main object is suspended" 558 <<
" after " << actualSent <<
" bytes.");
562 NS_LOG_INFO (
this <<
" Finished sending a whole main object.");
573 NS_LOG_INFO (
this <<
" Embedded object to be served is " 574 << objectSize <<
" bytes.");
580 if (actualSent < objectSize)
582 NS_LOG_INFO (
this <<
" Transmission of embedded object is suspended" 583 <<
" after " << actualSent <<
" bytes.");
587 NS_LOG_INFO (
this <<
" Finished sending a whole embedded object.");
599 NS_LOG_LOGIC (
this <<
" Tx buffer is empty. Not sending anything.");
602 bool firstPartOfObject = !
m_txBuffer->HasTxedPartOfObject (socket);
606 <<
" bytes available for Tx.");
609 const uint32_t txBufferSize =
m_txBuffer->GetBufferSize (socket);
613 uint32_t contentSize =
std::min (txBufferSize, socketSize - 22);
618 NS_LOG_LOGIC (
this <<
" Socket size leads to packet size of zero; not sending anything.");
623 if (firstPartOfObject)
635 NS_LOG_INFO (
this <<
" Created packet " << packet <<
" of " 637 <<
" The corresponding request came " 643 NS_LOG_INFO (
this <<
" Created packet " << packet <<
" of " 644 <<
packetSize <<
" bytes to be appended to a previous packet.");
648 const int actualBytes = socket->
Send (packet);
651 <<
" return value= " << actualBytes <<
".");
654 if (actualBytes == static_cast<int> (
packetSize))
657 m_txBuffer->DepleteBufferSize (socket, contentSize);
658 NS_LOG_INFO (
this <<
" Remaining object to be sent " 659 <<
m_txBuffer->GetBufferSize (socket) <<
" bytes.");
665 <<
" GetErrNo= " << socket->
GetErrno () <<
"," 666 <<
" suspending transmission" 667 <<
" and waiting for another Tx opportunity.");
681 NS_LOG_INFO (
this <<
" ThreeGppHttpServer " << oldState
682 <<
" --> " << newState <<
".");
698 std::map<Ptr<Socket>,
TxBuffer_t>::const_iterator it;
709 this <<
" Cannot add socket " << socket
710 <<
" because it has already been added before.");
726 std::map<Ptr<Socket>,
TxBuffer_t>::iterator it;
729 "Socket " << socket <<
" cannot be found.");
733 NS_LOG_INFO (
this <<
" Canceling a serving event which is due in " 753 std::map<Ptr<Socket>,
TxBuffer_t>::iterator it;
756 "Socket " << socket <<
" cannot be found.");
760 NS_LOG_INFO (
this <<
" Canceling a serving event which is due in " 766 if (it->second.txBufferSize > 0)
769 << it->second.txBufferSize <<
" bytes of transmission" 770 <<
" is still pending in the corresponding Tx buffer.");
788 std::map<Ptr<Socket>,
TxBuffer_t>::iterator it;
793 NS_LOG_INFO (
this <<
" Canceling a serving event which is due in " 813 std::map<Ptr<Socket>,
TxBuffer_t>::const_iterator it;
816 "Socket " << socket <<
" cannot be found.");
817 return (it->second.txBufferSize == 0);
824 std::map<Ptr<Socket>,
TxBuffer_t>::const_iterator it;
827 "Socket " << socket <<
" cannot be found.");
828 return it->second.clientTs;
835 std::map<Ptr<Socket>,
TxBuffer_t>::const_iterator it;
838 "Socket " << socket <<
" cannot be found.");
839 return it->second.txBufferContentType;
846 std::map<Ptr<Socket>,
TxBuffer_t>::const_iterator it;
849 "Socket " << socket <<
" cannot be found.");
850 return it->second.txBufferSize;
857 std::map<Ptr<Socket>,
TxBuffer_t>::const_iterator it;
860 "Socket " << socket <<
" cannot be found");
861 return it->second.hasTxedPartOfObject;
873 "Unable to write an object without a proper Content-Type.");
875 "Unable to write a zero-sized object.");
877 std::map<Ptr<Socket>,
TxBuffer_t>::iterator it;
880 "Socket " << socket <<
" cannot be found.");
882 "Cannot write to Tx buffer of socket " << socket
883 <<
" until the previous content has been completely sent.");
884 it->second.txBufferContentType = contentType;
885 it->second.txBufferSize = objectSize;
886 it->second.hasTxedPartOfObject =
false;
893 const Time &clientTs)
897 std::map<Ptr<Socket>,
TxBuffer_t>::iterator it;
900 "Socket " << socket <<
" cannot be found.");
901 it->second.nextServe = eventId;
902 it->second.clientTs = clientTs;
913 std::map<Ptr<Socket>,
TxBuffer_t>::iterator it;
916 "Socket " << socket <<
" cannot be found.");
918 "The requested amount is larger than the current buffer size.");
919 it->second.txBufferSize -= amount;
920 it->second.hasTxedPartOfObject =
true;
922 if (it->second.isClosing && (it->second.txBufferSize == 0))
938 std::map<Ptr<Socket>,
TxBuffer_t>::iterator it;
941 "Socket " << socket <<
" cannot be found.");
942 it->second.isClosing =
true;
static bool IsMatchingType(const Address &address)
If the Address matches the type.
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Simulation virtual time values and global simulation resolution.
TracedCallback< Ptr< const Packet > > m_txTrace
The Tx trace source.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint32_t txBufferSize
The length (in bytes) of the current data inside the transmission buffer.
State_t
The possible states of the application.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
void NormalCloseCallback(Ptr< Socket > socket)
Invoked when a connection with a web client is terminated.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
void AddSocket(Ptr< Socket > socket)
Add a new socket and create an empty transmission buffer for it.
bool ConnectionRequestCallback(Ptr< Socket > socket, const Address &address)
Invoked when m_initialSocket receives a connection request.
virtual int ShutdownSend(void)=0
ThreeGppHttpHeader::ContentType_t txBufferContentType
The content type of the current data inside the 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...
Time GetClientTs(Ptr< Socket > socket) const
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
bool IsBufferEmpty(Ptr< Socket > socket) const
virtual void DoDispose()
Destructor implementation.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_UNUSED(x)
Mark a local variable as unused.
Set of fields representing a single transmission buffer, which will be associated with a socket...
void SetCloseCallbacks(Callback< void, Ptr< Socket > > normalClose, Callback< void, Ptr< Socket > > errorClose)
Detect socket recv() events such as graceful shutdown or error.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
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...
Callback< R > MakeNullCallback(void)
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
The Rx trace source.
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.
ThreeGppHttpServerTxBuffer()
Create an empty instance of transmission buffer.
virtual enum Socket::SocketErrno GetErrno(void) const =0
Get last error number.
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...
a polymophic address class
ThreeGppHttpServer()
Creates a new instance of HTTP server application.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
void SendCallback(Ptr< Socket > socket, uint32_t availableBufferSize)
Invoked when more buffer space for transmission is added to a socket.
The attribute can be read.
State_t m_state
The current state of the client application. Begins with NOT_STARTED.
virtual int Listen(void)=0
Listen for incoming connections.
Ptr< ThreeGppHttpVariables > m_httpVariables
The Variables attribute.
TracedCallback< uint32_t > m_mainObjectTrace
The MainObject trace source.
static TypeId GetTypeId(void)
Get the type ID.
Ptr< const AttributeAccessor > MakeAddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
TracedCallback< const Time &, const Address & > m_rxDelayTrace
The RxDelay trace source.
The base class for all ns3 applications.
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.
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...
bool isClosing
True if the remote end has issued a request to close, which means that this socket will immediately c...
Hold an unsigned integer type.
State_t GetState() const
Returns the current state of the application.
uint16_t m_localPort
The LocalPort attribute.
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
void CloseSocket(Ptr< Socket > socket)
Close and remove a socket and its associated transmission buffer, and then unset the socket's callbac...
void SetRecvCallback(Callback< void, Ptr< Socket > >)
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...
Ptr< Socket > m_initialSocket
The listening socket, for receiving connection requests from clients.
static bool IsMatchingType(const Address &address)
Ptr< Node > GetNode() const
static TypeId GetTypeId(void)
Get the type ID.
virtual void DoDispose(void)
Destructor implementation.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
bool hasTxedPartOfObject
True if the buffer content has been read since it is written.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Hold objects of type Ptr<T>.
static bool IsExpired(const EventId &id)
Check if an event has already run or been cancelled.
TracedCallback< Ptr< const ThreeGppHttpServer >, Ptr< Socket > > m_connectionEstablishedTrace
The ConnectionEstablished trace source.
uint16_t GetPort(void) const
Container of various random variables to assist in generating web browsing traffic pattern...
virtual void StopApplication()
Application specific shutdown code.
Ptr< T > Create(void)
Create class instances by constructors with varying numbers of arguments and return them by Ptr...
Ptr< const AttributeChecker > MakeAddressChecker(void)
uint32_t GetBufferSize(Ptr< Socket > socket) const
std::string GetStateString() const
Returns the current state of the application in string format.
static Time Now(void)
Return the current simulation virtual time.
Address m_localAddress
The LocalAddress attribute.
void SetSendCallback(Callback< void, Ptr< Socket >, uint32_t > sendCb)
Notify application when space in transmit buffer is added.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
Passively listening and responding to requests.
bool IsSocketAvailable(Ptr< Socket > socket) const
This method is typically used before calling other methods.
uint32_t m_mtuSize
The Mtu attribute.
void CloseAllSockets()
Close and remove all stored sockets, hence clearing the buffer.
void RemoveSocket(Ptr< Socket > socket)
Remove a socket and its associated transmission buffer, and then unset the socket's callbacks to prev...
Before StartApplication() is invoked.
Describes an IPv6 address.
Ipv4 addresses are stored in host order in this class.
AttributeValue implementation for Address.
std::map< Ptr< Socket >, TxBuffer_t > m_txBuffer
Collection of accepted sockets and its individual transmission buffer.
An identifier for simulation events.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
std::size_t GetAttributeN(void) const
Get the number of attributes.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
bool HasTxedPartOfObject(Ptr< Socket > socket) const
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
TracedCallback< uint32_t > m_embeddedObjectTrace
The EmbeddedObject trace source.
struct TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
void DepleteBufferSize(Ptr< Socket > socket, uint32_t amount)
Decrements a buffer size by a given amount.
static bool IsMatchingType(const Address &addr)
If the address match.
void SetAcceptCallback(Callback< bool, Ptr< Socket >, const Address &> connectionRequest, Callback< void, Ptr< Socket >, const Address &> newConnectionCreated)
Accept connection requests from remote hosts.
void PrepareClose(Ptr< Socket > socket)
Tell the buffer to close the associated socket once the buffer becomes empty.
static bool IsFinished(void)
Check if the simulation should finish.
Ptr< T > CreateObject(void)
Create an object by type, with varying number of constructor parameters.
Ptr< Socket > GetSocket() const
Returns a pointer to the listening socket.
void ServeNewEmbeddedObject(Ptr< Socket > socket)
Generates a new embedded object and push it into the Tx buffer.
static const uint32_t packetSize
void ErrorCloseCallback(Ptr< Socket > socket)
Invoked when a connection with a web client is terminated.
static Ipv4Address ConvertFrom(const Address &address)
After StopApplication() is invoked.
uint32_t ServeFromTxBuffer(Ptr< Socket > socket)
Creates a packet out of a pending object in the Tx buffer send it over the given socket.
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.
TracedCallback< const std::string &, const std::string & > m_stateTransitionTrace
The StateTransition trace source.
uint16_t GetPort(void) const
Get the port.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SwitchToState(State_t state)
Change the state of the server.
virtual int Close(void)=0
Close a socket.
virtual uint32_t GetTxAvailable(void) const =0
Returns the number of bytes which can be sent in a single call to Send.
static TypeId GetTypeId()
Returns the object TypeId.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Ptr< ThreeGppHttpServerTxBuffer > m_txBuffer
Pointer to the transmission buffer.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
static bool IsMatchingType(const Address &address)
void AddHeader(const Header &header)
Add header to this packet.
virtual void StartApplication()
Application specific startup code.
Ipv4Address GetIpv4(void) const
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.