RINASim  October 2016
Documentation of framework for OMNeT++
SDU.cc
Go to the documentation of this file.
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2014-2016 Brno University of Technology, PRISTINE project
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
29 #include <SDU.h>
30 
31 SDU::SDU(const char *name, int kind) : SDU_Base(name, kind)
32 {
33  fSeqNum = seqNum = fOffset = fSize = offset = size = 0;
34 
35 }
36 
37 SDU::SDU(const SDU& other): SDU_Base(other){
38  copy(other);
39 }
40 
41 void SDU::copy(const SDU& other){
42  mUserDataType::const_iterator it;
43 
44  for(it = other.mUserData_var.begin(); it != other.mUserData_var.end(); ++it){
45  cPacket *cdap = (*it)->dup();
46  take(cdap);
47 
48  mUserData_var.push_back(cdap);
49  }
50 }
51 
52 /*
53  * This size returns size depending on @var dataType_var
54  */
55 unsigned int SDU::getSize() const
56 {
57 // if (dataType_var == SDU_COMPLETE_TYPE)
58  {
59  return size;
60  }
61 // else if (dataType_var == SDU_FRAGMENT_TYPE)
62  {
63  return fSize;
64 // }else{
65  return 0; //PANIC!
66  }
67 }
68 /*
69  * This method returns the absolute size of the SDU.
70  */
71 unsigned int SDU::getAbsoluteSize() const
72 {
73  return size;
74 
75 }
76 
77 unsigned int SDU::getRestSize()const
78 {
79  return size - offset;
80 }
81 
82 //void SDU::setSize(unsigned int size)
83 //{
84 // if(userData !=NULL){
85 // unsigned char *tmp = new unsigned char[size];
86 // memcpy(tmp,userData, (this->size_var < size) ? this->size_var : size);
87 // this->size_var = size;
88 // free(userData);
89 // userData = tmp;
90 // }else{
91 // this->size_var = size;
92 // userData = new unsigned char[size];
93 // }
94 //
95 //}
96 
97 //unsigned char* SDU::getUserData() const
98 //{
99 // return &userData[offset_var];
100 //}
101 
102 //const unsigned char* SDU::getUserData(unsigned int size)
103 //{
104 // offset_var += size;
105 // return &userData[offset_var - size];
106 //}
107 
108 //void SDU::setUserData(unsigned char* userData, unsigned int size)
109 //{
110 // if(this->userData != NULL){
111 // free(this->userData);
112 // this->offset_var = 0;
113 // }
114 // this->size_var = size;
115 // this->userData = new unsigned char[size];
116 // memcpy(this->userData, userData, size);
118 //}
119 
120 bool SDU::addUserData(cPacket* msg){
121 
122 // if(msg->getSize() > MAXSDUSIZE){
123 // return false;
124 // }
125 
126  //TODO A1 check current SDU size
127  take(msg);
128  this->mUserData_var.push_back(msg);
129  this->size += msg->getByteLength();
130 
131  return true;
132 }
133 
134 cPacket* SDU::getUserData(){
135 
136  if (!mUserData_var.empty())
137  {
138  cPacket* cdap;
139  cdap = mUserData_var.front();
140 
141  mUserData_var.erase(mUserData_var.begin());
142  drop(cdap);
143  return cdap;
144  }
145  else
146  {
147  return NULL;
148  }
149 }
154 std::vector<SDU*> SDU::fragment(unsigned int size){
155 
156  std::vector<SDU*> frags;
157  SDU* tmp;
158  for(unsigned int i = 0; i* size < this->size; i++){
159  tmp = this->genFragment(size, i, i*size);
160  frags.push_back(tmp);
161  }
162 
163  return frags;
164 }
165 
166 SDU* SDU::genFragment(unsigned int size, unsigned int fSeqNum, unsigned int fOffset){
167  SDU* tmp = this->dup();
168  tmp = new SDU(*this);
169  tmp->setFragment(size, fSeqNum, fOffset);
170  return tmp;
171 
172 }
173 
174 void SDU::setFragment(unsigned int fSize, unsigned int fSeqNum, unsigned int fOffset){
175  this->fSize = fSize;
176  this->fSeqNum = fSeqNum;
177  this->fOffset = fOffset;
178 // dataType_var = SDU_FRAGMENT_TYPE;
179  if(fSeqNum == 0){
180  this->fragType = SDU_FRAG_FIRST;
181  }else if(fSize + fOffset == this->size){
182  this->fragType = SDU_FRAG_LAST;
183  }else{
184  this->fragType = SDU_FRAG_MIDDLE;
185  }
186 }
187 
189 {
190  std::vector<cPacket*>::iterator it;
191  for(it = mUserData_var.begin(); it != mUserData_var.end();){
192  if((*it) != NULL){
193  drop((*it));
194  delete (*it);
195  }
196  it = mUserData_var.erase(it);
197 
198  }
199 }
200 
void setFragment(unsigned int size, unsigned int fSeqNum, unsigned int fOffset)
Definition: SDU.cc:174
virtual SDU * dup() const
Definition: SDU.h:72
Definition: SDU_m.h:88
bool addUserData(cPacket *msg)
Definition: SDU.cc:120
unsigned int getAbsoluteSize() const
Definition: SDU.cc:71
virtual ~SDU()
Definition: SDU.cc:188
void copy(const SDU &other)
Definition: SDU.cc:41
SDU(const char *name=NULL, int kind=0)
Definition: SDU.cc:31
unsigned int fSeqNum
Definition: SDU_m.h:96
unsigned int getSize() const
Definition: SDU.cc:55
int fragType
Definition: SDU_m.h:97
cPacket * getUserData()
Definition: SDU.cc:134
unsigned int size
Definition: SDU_m.h:91
mUserDataType mUserData_var
Definition: SDU.h:54
SDU * genFragment(unsigned int size, unsigned int fSeqNum, unsigned int fOffset)
Definition: SDU.cc:166
Definition: SDU.h:47
std::vector< SDU * > fragment(unsigned int size)
Definition: SDU.cc:154
unsigned int seqNum
Definition: SDU_m.h:95
unsigned int offset
Definition: SDU_m.h:92
unsigned int fSize
Definition: SDU_m.h:93
unsigned int fOffset
Definition: SDU_m.h:94
unsigned int getRestSize() const
Definition: SDU.cc:77