IBR-DTNSuite  0.10
Bundle.cpp
Go to the documentation of this file.
1 /*
2  * Bundle.cpp
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #include "ibrdtn/data/Bundle.h"
23 #include "ibrdtn/data/Serializer.h"
24 #include "ibrdtn/data/AgeBlock.h"
25 #include "ibrdtn/data/MetaBundle.h"
26 #include "ibrdtn/data/BundleID.h"
27 #include <algorithm>
28 
29 namespace dtn
30 {
31  namespace data
32  {
34  {
35  // if the timestamp is not set, add a ageblock
36  if (timestamp == 0)
37  {
38  // add a new ageblock
39  push_front<dtn::data::AgeBlock>();
40  }
41  }
42 
44  {
45  clear();
46  }
47 
49  {
50  return _blocks.begin();
51  }
52 
54  {
55  return _blocks.end();
56  }
57 
59  {
60  return _blocks.begin();
61  }
62 
64  {
65  return _blocks.end();
66  }
67 
68  bool Bundle::operator==(const BundleID& other) const
69  {
70  return other == (*this);
71  }
72 
73  bool Bundle::operator==(const MetaBundle& other) const
74  {
75  return other == (*this);
76  }
77 
78  bool Bundle::operator!=(const Bundle& other) const
79  {
80  return PrimaryBlock(*this) != other;
81  }
82 
83  bool Bundle::operator==(const Bundle& other) const
84  {
85  return PrimaryBlock(*this) == other;
86  }
87 
88  bool Bundle::operator<(const Bundle& other) const
89  {
90  return PrimaryBlock(*this) < other;
91  }
92 
93  bool Bundle::operator>(const Bundle& other) const
94  {
95  return PrimaryBlock(*this) > other;
96  }
97 
98  void Bundle::remove(const dtn::data::Block &block)
99  {
100  for (iterator it = begin(); it != end(); ++it)
101  {
102  dtn::data::Block *b = (*it).getPointer();
103  if (b == &block)
104  {
105  erase(it);
106  return;
107  }
108  }
109  }
110 
112  {
113  for (iterator it = b; it != e;)
114  {
115  _blocks.erase(it++);
116  }
117 
118  // set the last block bit
119  iterator last = end();
120  --last;
121  (**last).set(dtn::data::Block::LAST_BLOCK, true);
122  }
123 
125  {
126  _blocks.erase(it);
127 
128  // set the last block bit
129  iterator last = end();
130  --last;
131  (**last).set(dtn::data::Block::LAST_BLOCK, true);
132  }
133 
135  {
136  _blocks.clear();
137  }
138 
140  {
141  if (size() > 0) {
142  // remove the last block bit
143  iterator last = end();
144  --last;
145  (**last).set(dtn::data::Block::LAST_BLOCK, false);
146  }
147 
149  block_elem block( static_cast<dtn::data::Block*>(tmpblock) );
150 
151  _blocks.insert(before, block);
152 
153  // set the last block bit
154  iterator last = end();
155  --last;
156  (**last).set(dtn::data::Block::LAST_BLOCK, true);
157 
158  return (*tmpblock);
159  }
160 
162  {
164  block_elem block( static_cast<dtn::data::Block*>(tmpblock) );
165  _blocks.push_front(block);
166 
167  // if this was the first element
168  if (size() == 1)
169  {
170  // set the last block bit
171  iterator last = end();
172  --last;
173  (**last).set(dtn::data::Block::LAST_BLOCK, true);
174  }
175 
176  return (*tmpblock);
177  }
178 
180  {
181  if (size() > 0) {
182  // remove the last block bit
183  iterator last = end();
184  --last;
185  (**last).set(dtn::data::Block::LAST_BLOCK, false);
186  }
187 
189  block_elem block( static_cast<dtn::data::Block*>(tmpblock) );
190  _blocks.push_back(block);
191 
192  // set the last block bit
193  block->set(dtn::data::Block::LAST_BLOCK, true);
194 
195  return (*tmpblock);
196  }
197 
199  {
200  block_elem block( factory.create() );
201  _blocks.push_front(block);
202 
203  // if this was the first element
204  if (size() == 1)
205  {
206  // set the last block bit
207  iterator last = end();
208  --last;
209  (**last).set(dtn::data::Block::LAST_BLOCK, true);
210  }
211 
212  return (*block);
213  }
214 
216  {
217  if (size() > 0) {
218  // remove the last block bit
219  iterator last = end();
220  --last;
221  (**last).set(dtn::data::Block::LAST_BLOCK, false);
222  }
223 
224  block_elem block( factory.create() );
225  _blocks.push_back(block);
226 
227  // set the last block bit
228  block->set(dtn::data::Block::LAST_BLOCK, true);
229 
230  return (*block);
231  }
232 
234  {
235  if (size() > 0) {
236  // remove the last block bit
237  iterator last = end();
238  --last;
239  (**last).set(dtn::data::Block::LAST_BLOCK, false);
240  }
241 
242  block_elem block( factory.create() );
243  _blocks.insert(before, block);
244 
245  // set the last block bit
246  iterator last = end();
247  --last;
248  (**last).set(dtn::data::Block::LAST_BLOCK, true);
249 
250  return (*block);
251  }
252 
253  string Bundle::toString() const
254  {
255  return PrimaryBlock::toString();
256  }
257 
259  {
260  return _blocks.size();
261  }
262 
264  {
269  )
270  {
271  for( const_iterator it = begin(); it != end(); ++it ) {
272  if( (**it).get( Block::BLOCK_CONTAINS_EIDS ) )
273  {
274  std::list< EID > blockEIDs = (**it).getEIDList();
275  for( std::list< EID >::const_iterator itBlockEID = blockEIDs.begin(); itBlockEID != blockEIDs.end(); ++itBlockEID )
276  {
277  if( ! itBlockEID->isCompressable() )
278  {
279  return false;
280  }
281  }
282  }
283  }
284  return true;
285  }
286  else
287  {
288  return false;
289  }
290  }
291 
293  {
294  return std::find(begin(), end(), blocktype);
295  }
296 
298  {
299  return std::find(begin(), end(), blocktype);
300  }
301 
303  {
304  for (iterator it = begin(); it != end(); ++it)
305  {
306  if ((&**it) == &block) return it;
307  }
308 
309  return end();
310  }
311 
313  {
314  for (const_iterator it = begin(); it != end(); ++it)
315  {
316  if ((&**it) == &block) return it;
317  }
318 
319  return end();
320  }
321 
322  }
323 }