IBR-DTNSuite 0.6

ibrdtn/ibrdtn/data/Bundle.h

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