IBR-DTNSuite  0.12
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  {
33  Bundle::Bundle(bool zero_timestamp)
34  : PrimaryBlock(zero_timestamp)
35  {
36  // if the timestamp is not set, add a ageblock
37  if (timestamp == 0)
38  {
39  // add a new ageblock
40  push_front<dtn::data::AgeBlock>();
41  }
42  }
43 
45  {
46  clear();
47  }
48 
50  {
51  return _blocks.begin();
52  }
53 
55  {
56  return _blocks.end();
57  }
58 
60  {
61  return _blocks.begin();
62  }
63 
65  {
66  return _blocks.end();
67  }
68 
69  bool Bundle::operator==(const BundleID& other) const
70  {
71  return other == (const PrimaryBlock&)(*this);
72  }
73 
74  bool Bundle::operator==(const MetaBundle& other) const
75  {
76  return other == (const PrimaryBlock&)(*this);
77  }
78 
79  bool Bundle::operator!=(const Bundle& other) const
80  {
81  return (const PrimaryBlock&)(*this) != (const PrimaryBlock&)other;
82  }
83 
84  bool Bundle::operator==(const Bundle& other) const
85  {
86  return (const PrimaryBlock&)(*this) == (const PrimaryBlock&)other;
87  }
88 
89  bool Bundle::operator<(const Bundle& other) const
90  {
91  return (const PrimaryBlock&)(*this) < (const PrimaryBlock&)other;
92  }
93 
94  bool Bundle::operator>(const Bundle& other) const
95  {
96  return (const PrimaryBlock&)(*this) > (const PrimaryBlock&)other;
97  }
98 
99  void Bundle::remove(const dtn::data::Block &block)
100  {
101  for (iterator it = begin(); it != end(); ++it)
102  {
103  dtn::data::Block *b = (*it).getPointer();
104  if (b == &block)
105  {
106  erase(it);
107  return;
108  }
109  }
110  }
111 
113  {
114  for (iterator it = b; it != e;)
115  {
116  _blocks.erase(it++);
117  }
118 
119  if (size() > 0) {
120  // set the last block bit
121  iterator last = end();
122  --last;
123  (**last).set(dtn::data::Block::LAST_BLOCK, true);
124  }
125  }
126 
128  {
129  _blocks.erase(it);
130 
131  if (size() > 0) {
132  // set the last block bit
133  iterator last = end();
134  --last;
135  (**last).set(dtn::data::Block::LAST_BLOCK, true);
136  }
137  }
138 
140  {
141  _blocks.clear();
142  }
143 
145  {
146  if (size() > 0) {
147  // remove the last block bit
148  iterator last = end();
149  --last;
150  (**last).set(dtn::data::Block::LAST_BLOCK, false);
151  }
152 
154  block_elem block( static_cast<dtn::data::Block*>(tmpblock) );
155 
156  _blocks.insert(before, block);
157 
158  // set the last block bit
159  iterator last = end();
160  --last;
161  (**last).set(dtn::data::Block::LAST_BLOCK, true);
162 
163  return (*tmpblock);
164  }
165 
167  {
169  block_elem block( static_cast<dtn::data::Block*>(tmpblock) );
170  _blocks.push_front(block);
171 
172  // if this was the first element
173  if (size() == 1)
174  {
175  // set the last block bit
176  iterator last = end();
177  --last;
178  (**last).set(dtn::data::Block::LAST_BLOCK, true);
179  }
180 
181  return (*tmpblock);
182  }
183 
185  {
186  if (size() > 0) {
187  // remove the last block bit
188  iterator last = end();
189  --last;
190  (**last).set(dtn::data::Block::LAST_BLOCK, false);
191  }
192 
194  block_elem block( static_cast<dtn::data::Block*>(tmpblock) );
195  _blocks.push_back(block);
196 
197  // set the last block bit
198  block->set(dtn::data::Block::LAST_BLOCK, true);
199 
200  return (*tmpblock);
201  }
202 
204  {
205  block_elem block( factory.create() );
206  _blocks.push_front(block);
207 
208  // if this was the first element
209  if (size() == 1)
210  {
211  // set the last block bit
212  iterator last = end();
213  --last;
214  (**last).set(dtn::data::Block::LAST_BLOCK, true);
215  }
216 
217  return (*block);
218  }
219 
221  {
222  if (size() > 0) {
223  // remove the last block bit
224  iterator last = end();
225  --last;
226  (**last).set(dtn::data::Block::LAST_BLOCK, false);
227  }
228 
229  block_elem block( factory.create() );
230  _blocks.push_back(block);
231 
232  // set the last block bit
233  block->set(dtn::data::Block::LAST_BLOCK, true);
234 
235  return (*block);
236  }
237 
239  {
240  if (size() > 0) {
241  // remove the last block bit
242  iterator last = end();
243  --last;
244  (**last).set(dtn::data::Block::LAST_BLOCK, false);
245  }
246 
247  block_elem block( factory.create() );
248  _blocks.insert(before, block);
249 
250  // set the last block bit
251  iterator last = end();
252  --last;
253  (**last).set(dtn::data::Block::LAST_BLOCK, true);
254 
255  return (*block);
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  try {
295  const dtn::data::PayloadBlock &payload = find<dtn::data::PayloadBlock>();
296  return payload.getLength();
297  } catch (const NoSuchBlockFoundException&) {
298  return 0;
299  }
300  }
301 
303  {
304  return std::find(begin(), end(), blocktype);
305  }
306 
308  {
309  return std::find(begin(), end(), blocktype);
310  }
311 
313  {
314  for (iterator it = begin(); it != end(); ++it)
315  {
316  if ((&**it) == &block) return it;
317  }
318 
319  return end();
320  }
321 
323  {
324  for (const_iterator it = begin(); it != end(); ++it)
325  {
326  if ((&**it) == &block) return it;
327  }
328 
329  return end();
330  }
331 
332  }
333 }