A Discrete-Event Network Simulator
API
wifi-information-element-vector.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 IITP RAS
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: Pavel Boyko <boyko@iitp.ru>
19  */
20 
22 #include "ns3/packet.h"
23 #include <algorithm>
24 
25 namespace ns3 {
26 
27 NS_OBJECT_ENSURE_REGISTERED (WifiInformationElementVector);
28 
30  : m_maxSize (1500)
31 {
32 }
33 
35 {
36  for (IE_VECTOR::iterator i = m_elements.begin (); i != m_elements.end (); i++)
37  {
38  *i = 0;
39  }
40  m_elements.clear ();
41 }
42 
43 TypeId
45 {
46  static TypeId tid = TypeId ("ns3::WifiInformationElementVector")
47  .SetParent<Header> ()
48  .SetGroupName ("Wifi")
49  .AddConstructor<WifiInformationElementVector> ();
50  return tid;
51 }
52 
53 TypeId
55 {
56  return GetTypeId ();
57 }
58 
59 uint32_t
61 {
62  return GetSize ();
63 }
64 
65 void
67 {
68  for (IE_VECTOR::const_iterator i = m_elements.begin (); i != m_elements.end (); i++)
69  {
70  start = (*i)->Serialize (start);
71  }
72 }
73 
74 uint32_t
76 {
78  uint32_t size = start.GetSize ();
79  while (size > 0)
80  {
81  uint32_t deserialized = DeserializeSingleIe (i);
82  i.Next (deserialized);
83  size -= deserialized;
84  }
85  return i.GetDistanceFrom (start);
86 }
87 
88 uint32_t
90 {
92  uint8_t id = i.ReadU8 ();
93  //unused: uint8_t length = i.ReadU8 ();
94  //but need side effects of read:
95  i.ReadU8 ();
96  Ptr<WifiInformationElement> newElement;
97  switch (id)
98  {
99  default:
100  NS_FATAL_ERROR ("Information element " << (uint16_t) id << " is not implemented");
101  return 0;
102  }
103  /* unreachable: b/c switch is guaranteed to return from this function
104  if (GetSize () + length > m_maxSize)
105  {
106  NS_FATAL_ERROR ("Check max size for information element!");
107  }
108  newElement->DeserializeInformationField (i, length);
109  i.Next (length);
110  m_elements.push_back (newElement);
111  return i.GetDistanceFrom (start);
112  */
113 }
114 
115 void
116 WifiInformationElementVector::Print (std::ostream & os) const
117 {
118  for (IE_VECTOR::const_iterator i = m_elements.begin (); i != m_elements.end (); i++)
119  {
120  os << "(";
121  (*i)->Print (os);
122  os << ")";
123  }
124 }
125 
126 void
128 {
129  m_maxSize = size;
130 }
131 
134 {
135  return m_elements.begin ();
136 }
137 
140 {
141  return m_elements.end ();
142 }
143 
144 bool
146 {
147  if (element->GetInformationFieldSize () + 2 + GetSize () > m_maxSize)
148  {
149  return false;
150  }
151  m_elements.push_back (element);
152  return true;
153 }
154 
157 {
158  for (IE_VECTOR::const_iterator i = m_elements.begin (); i != m_elements.end (); i++)
159  {
160  if ((*i)->ElementId () == id)
161  {
162  return (*i);
163  }
164  }
165  return 0;
166 }
167 
168 
169 namespace {
170 
172 {
173  bool
175  {
176  return ((*PeekPointer (a)) < (*PeekPointer (b)));
177  }
178 };
179 
180 }
181 
182 
183 uint32_t
185 {
186  uint32_t size = 0;
187  for (IE_VECTOR::const_iterator i = m_elements.begin (); i != m_elements.end (); i++)
188  {
189  size += ((*i)->GetInformationFieldSize () + 2);
190  }
191  return size;
192 }
193 
194 bool
196 {
197  if (m_elements.size () != a.m_elements.size ())
198  {
199  NS_ASSERT (false);
200  return false;
201  }
202  //In principle we could bypass some of the faffing about (and speed
203  //the comparison) by simply serialising each IE vector into a
204  //buffer and memcmp'ing the two.
205  //
206  //I'm leaving it like this, however, so that there is the option of
207  //having individual Information Elements implement slightly more
208  //flexible equality operators.
209  WifiInformationElementVector::IE_VECTOR::const_iterator j = a.m_elements.begin ();
210  for (WifiInformationElementVector::IE_VECTOR::const_iterator i = m_elements.begin (); i
211  != m_elements.end (); i++, j++)
212  {
213  if (!(*(*i) == *(*j)))
214  {
215  return false;
216  }
217  }
218 
219  return true;
220 }
221 
222 } //namespace ns3
Protocol header serialization and deserialization.
Definition: header.h:42
IE_VECTOR m_elements
Information element vector.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual uint8_t GetInformationFieldSize() const =0
Length of serialized information (i.e., the length of the body of the IE, not including the Element I...
def start()
Definition: core.py:1482
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:562
virtual bool operator==(const WifiInformationElementVector &a) const
Check if the given WifiInformationElementVectors are equivalent.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
bool AddInformationElement(Ptr< WifiInformationElement > element)
add an IE, if maxSize has exceeded, returns false
uint32_t GetDistanceFrom(Iterator const &o) const
Definition: buffer.cc:783
iterator in a Buffer instance
Definition: buffer.h:98
Iterator End()
Returns End of the vector.
std::vector< Ptr< WifiInformationElement > >::iterator Iterator
As soon as this is a vector, we define an Iterator.
Ptr< WifiInformationElement > FindFirst(WifiInformationElementId id) const
vector of pointers to information elements is the body of IeVector
void Next(void)
go forward by one byte
Definition: buffer.h:844
virtual uint32_t DeserializeSingleIe(Buffer::Iterator start)
Needed when you try to deserialize a lonely IE inside other header.
uint16_t m_maxSize
Size in bytes (actually, max packet length)
virtual void Print(std::ostream &os) const
Iterator Begin()
Returns Begin of the vector.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void Serialize(Buffer::Iterator start) const
virtual uint32_t Deserialize(Buffer::Iterator start)
Information element vectorImplements a vector of WifiInformationElements.
uint32_t GetSize() const
Current number of bytes.
uint8_t ReadU8(void)
Definition: buffer.h:1020
TypeId GetInstanceTypeId() const
Get the most derived TypeId for this Object.
uint8_t WifiInformationElementId
This type is used to represent an Information Element ID.
void SetMaxSize(uint16_t size)
Set maximum size to control overflow of the max packet length.
uint32_t GetSize(void) const
Definition: buffer.cc:1158
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904