A Discrete-Event Network Simulator
API
three-gpp-http-header.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015 Magister Solutions
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Budiarto Herman <budiarto.herman@magister.fi>
19  *
20  */
21 
22 #include <ns3/log.h>
23 #include <ns3/packet.h>
24 #include <sstream>
25 #include "three-gpp-http-header.h"
26 
27 NS_LOG_COMPONENT_DEFINE ("ThreeGppHttpHeader");
28 
29 namespace ns3 {
30 
31 NS_OBJECT_ENSURE_REGISTERED (ThreeGppHttpHeader);
32 
34  : Header (),
35  m_contentType (NOT_SET),
36  m_contentLength (0),
37  m_clientTs (0),
38  m_serverTs (0)
39 {
40  NS_LOG_FUNCTION (this);
41 }
42 
43 
44 // static
45 TypeId
47 {
48  static TypeId tid = TypeId ("ns3::ThreeGppHttpHeader")
49  .SetParent<Header> ()
50  .AddConstructor<ThreeGppHttpHeader> ()
51  ;
52  return tid;
53 }
54 
55 
56 TypeId
58 {
59  return GetTypeId ();
60 }
61 
62 
63 uint32_t
65 {
66  return 2 + 4 + 8 + 8;
67 }
68 
69 
70 void
72 {
73  NS_LOG_FUNCTION (this << &start);
74  start.WriteU16 (m_contentType);
75  start.WriteU32 (m_contentLength);
76  start.WriteU64 (m_clientTs);
77  start.WriteU64 (m_serverTs);
78 }
79 
80 
81 uint32_t
83 {
84  NS_LOG_FUNCTION (this << &start);
85  uint32_t bytesRead = 0;
86 
87  // First block of 2 bytes (content type)
88  m_contentType = start.ReadU16 ();
89  bytesRead += 2;
90 
91  // Second block of 4 bytes (content length)
92  m_contentLength = start.ReadU32 ();
93  bytesRead += 4;
94 
95  // Third block of 8 bytes (client time stamp)
96  m_clientTs = start.ReadU64 ();
97  bytesRead += 8;
98 
99  // Fourth block of 8 bytes (server time stamp)
100  m_serverTs = start.ReadU64 ();
101  bytesRead += 8;
102 
103  return bytesRead;
104 }
105 
106 
107 void
108 ThreeGppHttpHeader::Print (std::ostream &os) const
109 {
110  NS_LOG_FUNCTION (this << &os);
111  os << "(Content-Type: " << m_contentType
112  << " Content-Length: " << m_contentLength
113  << " Client TS: " << TimeStep (m_clientTs).GetSeconds ()
114  << " Server TS: " << TimeStep (m_serverTs).GetSeconds () << ")";
115 }
116 
117 
118 std::string
120 {
121  NS_LOG_FUNCTION (this);
122  std::ostringstream oss;
123  Print (oss);
124  return oss.str ();
125 }
126 
127 
128 void
130 {
131  NS_LOG_FUNCTION (this << static_cast<uint16_t> (contentType));
132  switch (contentType)
133  {
134  case NOT_SET:
135  m_contentType = 0;
136  break;
137  case MAIN_OBJECT:
138  m_contentType = 1;
139  break;
140  case EMBEDDED_OBJECT:
141  m_contentType = 2;
142  break;
143  default:
144  NS_FATAL_ERROR ("Unknown Content-Type: " << contentType);
145  break;
146  }
147 }
148 
149 
152 {
153  ContentType_t ret;
154  switch (m_contentType)
155  {
156  case 0:
157  ret = NOT_SET;
158  break;
159  case 1:
160  ret = MAIN_OBJECT;
161  break;
162  case 2:
163  ret = EMBEDDED_OBJECT;
164  break;
165  default:
166  NS_FATAL_ERROR ("Unknown Content-Type: " << m_contentType);
167  break;
168  }
169  return ret;
170 }
171 
172 
173 void
174 ThreeGppHttpHeader::SetContentLength (uint32_t contentLength)
175 {
176  NS_LOG_FUNCTION (this << contentLength);
177  m_contentLength = contentLength;
178 }
179 
180 
181 uint32_t
183 {
184  return m_contentLength;
185 }
186 
187 
188 void
190 {
191  NS_LOG_FUNCTION (this << clientTs.GetSeconds ());
192  m_clientTs = clientTs.GetTimeStep ();
193 }
194 
195 
196 Time
198 {
199  return TimeStep (m_clientTs);
200 }
201 
202 
203 void
205 {
206  NS_LOG_FUNCTION (this << serverTs.GetSeconds ());
207  m_serverTs = serverTs.GetTimeStep ();
208 }
209 
210 
211 Time
213 {
214  return TimeStep (m_serverTs);
215 }
216 
217 
218 } // namespace ns3
Protocol header serialization and deserialization.
Definition: header.h:42
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
ContentType_t GetContentType() const
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual uint32_t GetSerializedSize() const
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
def start()
Definition: core.py:1855
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
virtual void Print(std::ostream &os) const
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
virtual void Serialize(Buffer::Iterator start) const
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
iterator in a Buffer instance
Definition: buffer.h:98
ThreeGppHttpHeader()
Creates an empty instance .
void SetServerTs(Time serverTs)
virtual TypeId GetInstanceTypeId() const
Get the most derived TypeId for this Object.
virtual uint32_t Deserialize(Buffer::Iterator start)
ContentType_t
The possible types of content (default = NOT_SET).
void SetContentLength(uint32_t contentLength)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static TypeId GetTypeId()
Returns the object TypeId.
Time TimeStep(uint64_t ts)
Definition: nstime.h:1119
uint32_t m_contentLength
" Content length field (in bytes unit).
void SetClientTs(Time clientTs)
uint64_t m_clientTs
" Client time stamp field (in time step unit).
uint64_t m_serverTs
" Server time stamp field (in time step unit).
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
void SetContentType(ContentType_t contentType)
uint16_t m_contentType
" Content type field in integer format.
int64_t GetTimeStep(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:391