A Discrete-Event Network Simulator
API
three-gpp-http-header.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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 #include "three-gpp-http-header.h"
22 
23 #include <ns3/log.h>
24 #include <ns3/packet.h>
25 
26 #include <sstream>
27 
28 NS_LOG_COMPONENT_DEFINE("ThreeGppHttpHeader");
29 
30 namespace ns3
31 {
32 
33 NS_OBJECT_ENSURE_REGISTERED(ThreeGppHttpHeader);
34 
36  : Header(),
37  m_contentType(NOT_SET),
38  m_contentLength(0),
39  m_clientTs(0),
40  m_serverTs(0)
41 {
42  NS_LOG_FUNCTION(this);
43 }
44 
45 // static
46 TypeId
48 {
49  static TypeId tid =
50  TypeId("ns3::ThreeGppHttpHeader").SetParent<Header>().AddConstructor<ThreeGppHttpHeader>();
51  return tid;
52 }
53 
54 TypeId
56 {
57  return GetTypeId();
58 }
59 
60 uint32_t
62 {
63  return 2 + 4 + 8 + 8;
64 }
65 
66 void
68 {
69  NS_LOG_FUNCTION(this << &start);
70  start.WriteU16(m_contentType);
71  start.WriteU32(m_contentLength);
72  start.WriteU64(m_clientTs);
73  start.WriteU64(m_serverTs);
74 }
75 
76 uint32_t
78 {
79  NS_LOG_FUNCTION(this << &start);
80  uint32_t bytesRead = 0;
81 
82  // First block of 2 bytes (content type)
83  m_contentType = start.ReadU16();
84  bytesRead += 2;
85 
86  // Second block of 4 bytes (content length)
87  m_contentLength = start.ReadU32();
88  bytesRead += 4;
89 
90  // Third block of 8 bytes (client time stamp)
91  m_clientTs = start.ReadU64();
92  bytesRead += 8;
93 
94  // Fourth block of 8 bytes (server time stamp)
95  m_serverTs = start.ReadU64();
96  bytesRead += 8;
97 
98  return bytesRead;
99 }
100 
101 void
102 ThreeGppHttpHeader::Print(std::ostream& os) const
103 {
104  NS_LOG_FUNCTION(this << &os);
105  os << "(Content-Type: " << m_contentType << " Content-Length: " << m_contentLength
106  << " Client TS: " << TimeStep(m_clientTs).As(Time::S)
107  << " Server TS: " << TimeStep(m_serverTs).As(Time::S) << ")";
108 }
109 
110 std::string
112 {
113  NS_LOG_FUNCTION(this);
114  std::ostringstream oss;
115  Print(oss);
116  return oss.str();
117 }
118 
119 void
121 {
122  NS_LOG_FUNCTION(this << static_cast<uint16_t>(contentType));
123  switch (contentType)
124  {
125  case NOT_SET:
126  m_contentType = 0;
127  break;
128  case MAIN_OBJECT:
129  m_contentType = 1;
130  break;
131  case EMBEDDED_OBJECT:
132  m_contentType = 2;
133  break;
134  default:
135  NS_FATAL_ERROR("Unknown Content-Type: " << contentType);
136  break;
137  }
138 }
139 
142 {
143  ContentType_t ret;
144  switch (m_contentType)
145  {
146  case 0:
147  ret = NOT_SET;
148  break;
149  case 1:
150  ret = MAIN_OBJECT;
151  break;
152  case 2:
153  ret = EMBEDDED_OBJECT;
154  break;
155  default:
156  NS_FATAL_ERROR("Unknown Content-Type: " << m_contentType);
157  break;
158  }
159  return ret;
160 }
161 
162 void
164 {
165  NS_LOG_FUNCTION(this << contentLength);
166  m_contentLength = contentLength;
167 }
168 
169 uint32_t
171 {
172  return m_contentLength;
173 }
174 
175 void
177 {
178  NS_LOG_FUNCTION(this << clientTs.As(Time::S));
179  m_clientTs = clientTs.GetTimeStep();
180 }
181 
182 Time
184 {
185  return TimeStep(m_clientTs);
186 }
187 
188 void
190 {
191  NS_LOG_FUNCTION(this << serverTs.As(Time::S));
192  m_serverTs = serverTs.GetTimeStep();
193 }
194 
195 Time
197 {
198  return TimeStep(m_serverTs);
199 }
200 
201 } // namespace ns3
iterator in a Buffer instance
Definition: buffer.h:100
Protocol header serialization and deserialization.
Definition: header.h:44
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
void SetClientTs(Time clientTs)
void SetServerTs(Time serverTs)
uint32_t GetSerializedSize() const override
void SetContentLength(uint32_t contentLength)
void Serialize(Buffer::Iterator start) const override
void SetContentType(ContentType_t contentType)
ContentType_t
The possible types of content (default = NOT_SET).
@ NOT_SET
Integer equivalent = 0.
@ EMBEDDED_OBJECT
Integer equivalent = 2.
@ MAIN_OBJECT
Integer equivalent = 1.
ThreeGppHttpHeader()
Creates an empty instance.
uint64_t m_clientTs
" Client time stamp field (in time step unit).
uint32_t m_contentLength
" Content length field (in bytes unit).
void Print(std::ostream &os) const override
uint64_t m_serverTs
" Server time stamp field (in time step unit).
ContentType_t GetContentType() const
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint16_t m_contentType
" Content type field in integer format.
static TypeId GetTypeId()
Returns the object TypeId.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
@ S
second
Definition: nstime.h:116
int64_t GetTimeStep() const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:445
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.