IBR-DTNSuite  0.10
Bundle.h
Go to the documentation of this file.
1 /*
2  * Bundle.h
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 #ifndef BUNDLE_H_
23 #define BUNDLE_H_
24 
25 #include "ibrdtn/data/Number.h"
26 #include "ibrdtn/data/EID.h"
27 #include "ibrdtn/data/Block.h"
31 #include <ibrcommon/refcnt_ptr.h>
32 #include <ibrcommon/Iterator.h>
33 #include <ostream>
34 #ifdef __DEVELOPMENT_ASSERTIONS__
35 #include <cassert>
36 #endif
37 #include <set>
38 #include <map>
39 #include <typeinfo>
40 #include <list>
41 #include <iterator>
42 #include <algorithm>
43 
44 namespace dtn
45 {
46  namespace data
47  {
48  class BundleBuilder;
49  class MetaBundle;
50  class BundleID;
51 
52  class Bundle : public PrimaryBlock
53  {
54  friend class BundleBuilder;
55 
56  public:
58  {
59  public:
60  NoSuchBlockFoundException() : ibrcommon::Exception("No block found with this Block ID.")
61  {
62  };
63  };
64 
65  class block_elem : public refcnt_ptr<Block>
66  {
67  public:
69  bool operator==(const dtn::data::block_t &type) const {
70  return (**this) == type;
71  }
72  };
73 
74  typedef std::list<block_elem> block_list;
75 
76  typedef block_list::iterator iterator;
77  typedef block_list::const_iterator const_iterator;
78 
81 
82  iterator begin();
83  iterator end();
84  const_iterator begin() const;
85  const_iterator end() const;
86 
87  iterator find(block_t blocktype);
88  const_iterator find(block_t blocktype) const;
89 
90  iterator find(const Block &block);
91  const_iterator find(const Block &block) const;
92 
93  Bundle();
94  virtual ~Bundle();
95 
96  bool operator==(const BundleID& other) const;
97  bool operator==(const MetaBundle& other) const;
98 
99  bool operator==(const Bundle& other) const;
100  bool operator!=(const Bundle& other) const;
101  bool operator<(const Bundle& other) const;
102  bool operator>(const Bundle& other) const;
103 
104  template<class T>
105  T& find();
106 
107  template<class T>
108  const T& find() const;
109 
110  template<class T>
111  T& push_front();
112 
113  template<class T>
114  T& push_back();
115 
116  template<class T>
117  T& insert(iterator before);
118 
122 
126 
127  void erase(iterator it);
129 
130  void remove(const Block &block);
131 
132  void clear();
133 
134  std::string toString() const;
135 
136  Size size() const;
137 
138  bool allEIDsInCBHE() const;
139 
140  private:
141  block_list _blocks;
142  };
143 
144  template<class T>
146  {
147  iterator it = this->find(T::BLOCK_TYPE);
148  if (it == this->end()) throw NoSuchBlockFoundException();
149  return dynamic_cast<T&>(**it);
150  }
151 
152  template<class T>
153  const T& Bundle::find() const
154  {
155  const_iterator it = this->find(T::BLOCK_TYPE);
156  if (it == this->end()) throw NoSuchBlockFoundException();
157  return dynamic_cast<const T&>(**it);
158  }
159 
160  template<class T>
162  {
163  T *tmpblock = new T();
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 
179  template<class T>
181  {
182  if (size() > 0) {
183  // remove the last block bit
184  iterator last = end();
185  last--;
186  (**last).set(dtn::data::Block::LAST_BLOCK, false);
187  }
188 
189  T *tmpblock = new T();
190  block_elem block( static_cast<dtn::data::Block*>(tmpblock) );
191  _blocks.push_back(block);
192 
193  // set the last block bit
194  block->set(dtn::data::Block::LAST_BLOCK, true);
195 
196  return (*tmpblock);
197  }
198 
199  template<class T>
201  {
202  if (size() > 0) {
203  // remove the last block bit
204  iterator last = end();
205  last--;
206  (**last).set(dtn::data::Block::LAST_BLOCK, false);
207  }
208 
209  T *tmpblock = new T();
210  block_elem block( static_cast<dtn::data::Block*>(tmpblock) );
211  _blocks.insert(before, block);
212 
213  // set the last block bit
214  iterator last = end();
215  last--;
216  (**last).set(dtn::data::Block::LAST_BLOCK, true);
217 
218  return (*tmpblock);
219  }
220  }
221 }
222 
223 #endif /* BUNDLE_H_ */