00001 /* 00002 * Bundle.h 00003 * 00004 * Created on: 29.05.2009 00005 * Author: morgenro 00006 */ 00007 00008 #ifndef BUNDLE_H_ 00009 #define BUNDLE_H_ 00010 00011 00012 #include "ibrdtn/data/Dictionary.h" 00013 #include "ibrdtn/data/PrimaryBlock.h" 00014 #include "ibrdtn/data/Block.h" 00015 #include "ibrdtn/data/PayloadBlock.h" 00016 #include "ibrdtn/data/EID.h" 00017 #include "ibrdtn/data/CustodySignalBlock.h" 00018 #include "ibrdtn/data/StatusReportBlock.h" 00019 #include "ibrcommon/refcnt_ptr.h" 00020 #include <ostream> 00021 #include <cassert> 00022 #include <set> 00023 #include <map> 00024 #include <typeinfo> 00025 00026 namespace dtn 00027 { 00028 namespace data 00029 { 00030 class ExtensionBlockFactory; 00031 00032 class Bundle : public PrimaryBlock 00033 { 00034 friend class DefaultSerializer; 00035 friend class DefaultDeserializer; 00036 00037 public: 00038 static std::map<char, ExtensionBlockFactory*>& getExtensionBlockFactories(); 00039 00040 class NoSuchBlockFoundException : public ibrcommon::Exception 00041 { 00042 public: 00043 NoSuchBlockFoundException() : ibrcommon::Exception("No block found with this Block ID.") 00044 { 00045 }; 00046 }; 00047 00048 class BlockList 00049 { 00050 friend class DefaultSerializer; 00051 friend class DefaultDeserializer; 00052 00053 public: 00054 BlockList(); 00055 ~BlockList(); 00056 00057 void push_front(Block *block); 00058 void push_back(Block *block); 00059 void insert(Block *block, const Block *before); 00060 void remove(const Block *block); 00061 void clear(); 00062 00063 const std::set<dtn::data::EID> getEIDs() const; 00064 00065 template<class T> T& get(); 00066 template<class T> const T& get() const; 00067 00068 template<class T> 00069 const std::list<const T*> getList() const; 00070 00071 const std::list<const Block*> getList() const; 00072 00073 private: 00074 std::list<refcnt_ptr<Block> > _blocks; 00075 }; 00076 00077 Bundle(); 00078 virtual ~Bundle(); 00079 00080 bool operator==(const Bundle& other) const; 00081 bool operator!=(const Bundle& other) const; 00082 bool operator<(const Bundle& other) const; 00083 bool operator>(const Bundle& other) const; 00084 00085 const std::list<const dtn::data::Block*> getBlocks() const; 00086 00087 template<class T> 00088 T& getBlock(); 00089 00090 template<class T> 00091 const T& getBlock() const; 00092 00093 template<class T> 00094 const std::list<const T*> getBlocks() const; 00095 00096 template<class T> 00097 T& push_front(); 00098 00099 template<class T> 00100 T& push_back(); 00101 00102 template<class T> 00103 T& insert(const dtn::data::Block &before); 00104 00105 dtn::data::PayloadBlock& push_front(ibrcommon::BLOB::Reference &ref); 00106 dtn::data::PayloadBlock& push_back(ibrcommon::BLOB::Reference &ref); 00107 dtn::data::PayloadBlock& insert(const dtn::data::Block &before, ibrcommon::BLOB::Reference &ref); 00108 00109 dtn::data::Block& push_back(dtn::data::ExtensionBlockFactory &factory); 00110 00111 void remove(const dtn::data::Block &block); 00112 void clearBlocks(); 00113 00114 string toString() const; 00115 00116 private: 00117 BlockList _blocks; 00118 }; 00119 00120 template<class T> 00121 const std::list<const T*> Bundle::getBlocks() const 00122 { 00123 return _blocks.getList<T>(); 00124 } 00125 00126 template<class T> 00127 T& Bundle::getBlock() 00128 { 00129 return _blocks.get<T>(); 00130 } 00131 00132 template<class T> 00133 const T& Bundle::getBlock() const 00134 { 00135 return _blocks.get<T>(); 00136 } 00137 00138 template<> 00139 inline CustodySignalBlock& Bundle::BlockList::get<CustodySignalBlock>() 00140 { 00141 try { 00142 // copy all blocks to the list 00143 for (std::list<refcnt_ptr<Block> >::iterator iter = _blocks.begin(); iter != _blocks.end(); iter++) 00144 { 00145 if ((*iter)->getType() == PayloadBlock::BLOCK_TYPE) 00146 { 00147 Block *b = (*iter).getPointer(); 00148 return dynamic_cast<CustodySignalBlock&>(*b); 00149 } 00150 } 00151 } catch (std::bad_cast) { 00152 00153 } 00154 00155 throw NoSuchBlockFoundException(); 00156 } 00157 00158 template<> 00159 inline const CustodySignalBlock& Bundle::BlockList::get<const CustodySignalBlock>() const 00160 { 00161 try { 00162 // copy all blocks to the list 00163 for (std::list<refcnt_ptr<Block> >::const_iterator iter = _blocks.begin(); iter != _blocks.end(); iter++) 00164 { 00165 if ((*iter)->getType() == PayloadBlock::BLOCK_TYPE) 00166 { 00167 const Block *b = (*iter).getPointer(); 00168 return dynamic_cast<const CustodySignalBlock&>(*b); 00169 } 00170 } 00171 } catch (std::bad_cast) { 00172 00173 } 00174 00175 throw NoSuchBlockFoundException(); 00176 } 00177 00178 template<> 00179 inline StatusReportBlock& Bundle::BlockList::get<StatusReportBlock> () 00180 { 00181 try { 00182 // copy all blocks to the list 00183 for (std::list<refcnt_ptr<Block> >::iterator iter = _blocks.begin(); iter != _blocks.end(); iter++) 00184 { 00185 if ((*iter)->getType() == PayloadBlock::BLOCK_TYPE) 00186 { 00187 Block *b = (*iter).getPointer(); 00188 return dynamic_cast<StatusReportBlock&>(*b); 00189 } 00190 } 00191 } catch (std::bad_cast) { 00192 00193 } 00194 00195 throw NoSuchBlockFoundException(); 00196 } 00197 00198 template<> 00199 inline const StatusReportBlock& Bundle::BlockList::get<const StatusReportBlock>() const 00200 { 00201 try { 00202 // copy all blocks to the list 00203 for (std::list<refcnt_ptr<Block> >::const_iterator iter = _blocks.begin(); iter != _blocks.end(); iter++) 00204 { 00205 if ((*iter)->getType() == PayloadBlock::BLOCK_TYPE) 00206 { 00207 const Block *b = (*iter).getPointer(); 00208 return dynamic_cast<const StatusReportBlock&>(*b); 00209 } 00210 } 00211 } catch (std::bad_cast) { 00212 00213 } 00214 00215 throw NoSuchBlockFoundException(); 00216 } 00217 00218 template<class T> 00219 const T& Bundle::BlockList::get() const 00220 { 00221 try { 00222 // copy all blocks to the list 00223 for (std::list<refcnt_ptr<Block> >::const_iterator iter = _blocks.begin(); iter != _blocks.end(); iter++) 00224 { 00225 if ((*iter)->getType() == T::BLOCK_TYPE) 00226 { 00227 const Block *b = (*iter).getPointer(); 00228 return dynamic_cast<const T&>(*b); 00229 } 00230 } 00231 } catch (std::bad_cast) { 00232 00233 } 00234 00235 throw NoSuchBlockFoundException(); 00236 } 00237 00238 template<class T> 00239 T& Bundle::BlockList::get() 00240 { 00241 try { 00242 // copy all blocks to the list 00243 for (std::list<refcnt_ptr<Block> >::iterator iter = _blocks.begin(); iter != _blocks.end(); iter++) 00244 { 00245 if ((*iter)->getType() == T::BLOCK_TYPE) 00246 { 00247 Block *b = (*iter).getPointer(); 00248 return dynamic_cast<T&>(*b); 00249 } 00250 } 00251 } catch (std::bad_cast) { 00252 00253 } 00254 00255 throw NoSuchBlockFoundException(); 00256 } 00257 00258 template<class T> 00259 const std::list<const T*> Bundle::BlockList::getList() const 00260 { 00261 // create a list of blocks 00262 std::list<const T*> ret; 00263 00264 // copy all blocks to the list 00265 for (std::list<refcnt_ptr<Block> >::const_iterator iter = _blocks.begin(); iter != _blocks.end(); iter++) 00266 { 00267 if ((*(*iter)).getType() == T::BLOCK_TYPE) 00268 { 00269 const T* obj = dynamic_cast<const T*>((*iter).getPointer()); 00270 00271 if (obj != NULL) 00272 { 00273 ret.push_back( obj ); 00274 } 00275 } 00276 } 00277 00278 return ret; 00279 } 00280 00281 template<class T> 00282 T& Bundle::push_front() 00283 { 00284 T *tmpblock = new T(); 00285 dtn::data::Block *block = dynamic_cast<dtn::data::Block*>(tmpblock); 00286 assert(block != NULL); 00287 _blocks.push_front(block); 00288 return (*tmpblock); 00289 } 00290 00291 template<class T> 00292 T& Bundle::push_back() 00293 { 00294 T *tmpblock = new T(); 00295 dtn::data::Block *block = dynamic_cast<dtn::data::Block*>(tmpblock); 00296 assert(block != NULL); 00297 _blocks.push_back(block); 00298 return (*tmpblock); 00299 } 00300 00301 template<class T> 00302 T& Bundle::insert(const dtn::data::Block &before) 00303 { 00304 T *tmpblock = new T(*this); 00305 dtn::data::Block *block = dynamic_cast<dtn::data::Block*>(tmpblock); 00306 assert(block != NULL); 00307 _blocks.insert(block, &before); 00308 return (*tmpblock); 00309 } 00310 } 00311 } 00312 00313 #endif /* BUNDLE_H_ */
1.7.1