IBR-DTNSuite  0.8
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                                 BlockList& operator=(const BlockList &ref);
00067 
00068                                 void push_front(Block *block);
00069                                 void push_back(Block *block);
00070                                 void insert(Block *block, const Block *before);
00071                                 void remove(const Block *block);
00072                                 void clear();
00073 
00074                                 const std::set<dtn::data::EID> getEIDs() const;
00075 
00076                                 Block& get(int index);
00077                                 const Block& get(int index) const;
00078                                 template<class T> T& get();
00079                                 template<class T> const T& get() const;
00080 
00081                                 template<class T>
00082                                 const std::list<const T*> getList() const;
00083 
00084                                 const std::list<const Block*> getList() const;
00085 
00086                                 size_t size() const;
00087 
00088                         private:
00089                                 std::list<refcnt_ptr<Block> > _blocks;
00090                         };
00091 
00092                         Bundle();
00093                         virtual ~Bundle();
00094 
00095                         bool operator==(const Bundle& other) const;
00096                         bool operator!=(const Bundle& other) const;
00097                         bool operator<(const Bundle& other) const;
00098                         bool operator>(const Bundle& other) const;
00099 
00100                         const std::list<const dtn::data::Block*> getBlocks() const;
00101 
00102                         dtn::data::Block& getBlock(int index);
00103                         const dtn::data::Block& getBlock(int index) const;
00104 
00105                         template<class T>
00106                         T& getBlock();
00107 
00108                         template<class T>
00109                         const T& getBlock() const;
00110 
00111                         template<class T>
00112                         const std::list<const T*> getBlocks() const;
00113 
00114                         template<class T>
00115                         T& push_front();
00116 
00117                         template<class T>
00118                         T& push_back();
00119 
00120                         template<class T>
00121                         T& insert(const dtn::data::Block &before);
00122 
00123                         dtn::data::PayloadBlock& push_front(ibrcommon::BLOB::Reference &ref);
00124                         dtn::data::PayloadBlock& push_back(ibrcommon::BLOB::Reference &ref);
00125                         dtn::data::PayloadBlock& insert(const dtn::data::Block &before, ibrcommon::BLOB::Reference &ref);
00126 
00127                         dtn::data::Block& push_front(dtn::data::ExtensionBlock::Factory &factory);
00128                         dtn::data::Block& push_back(dtn::data::ExtensionBlock::Factory &factory);
00129                         dtn::data::Block& insert(dtn::data::ExtensionBlock::Factory &factory, const dtn::data::Block &before);
00130 
00131                         void remove(const dtn::data::Block &block);
00132                         void clearBlocks();
00133 
00134                         string toString() const;
00135 
00136                         size_t blockCount() const;
00137 
00138                 private:
00139                         BlockList _blocks;
00140                 };
00141 
00142                 template<class T>
00143                 const std::list<const T*> Bundle::getBlocks() const
00144                 {
00145                         return _blocks.getList<T>();
00146                 }
00147 
00148                 template<class T>
00149                 T& Bundle::getBlock()
00150                 {
00151                         return _blocks.get<T>();
00152                 }
00153 
00154                 template<class T>
00155                 const T& Bundle::getBlock() const
00156                 {
00157                         return _blocks.get<T>();
00158                 }
00159 
00160                 template<>
00161                 CustodySignalBlock& Bundle::BlockList::get<CustodySignalBlock>();
00162 
00163                 template<>
00164                 const CustodySignalBlock& Bundle::BlockList::get<const CustodySignalBlock>() const;
00165 
00166                 template<>
00167                 StatusReportBlock& Bundle::BlockList::get<StatusReportBlock> ();
00168 
00169                 template<>
00170                 const StatusReportBlock& Bundle::BlockList::get<const StatusReportBlock>() const;
00171 
00172                 template<class T>
00173                 const T& Bundle::BlockList::get() const
00174                 {
00175                         try {
00176                                 // copy all blocks to the list
00177                                 for (std::list<refcnt_ptr<Block> >::const_iterator iter = _blocks.begin(); iter != _blocks.end(); iter++)
00178                                 {
00179                                         if ((*iter)->getType() == T::BLOCK_TYPE)
00180                                         {
00181                                                 const Block *b = (*iter).getPointer();
00182                                                 return dynamic_cast<const T&>(*b);
00183                                         }
00184                                 }
00185                         } catch (const std::bad_cast&) {
00186 
00187                         }
00188 
00189                         throw NoSuchBlockFoundException();
00190                 }
00191 
00192                 template<class T>
00193                 T& Bundle::BlockList::get()
00194                 {
00195                         try {
00196                                 // copy all blocks to the list
00197                                 for (std::list<refcnt_ptr<Block> >::iterator iter = _blocks.begin(); iter != _blocks.end(); iter++)
00198                                 {
00199                                         if ((*iter)->getType() == T::BLOCK_TYPE)
00200                                         {
00201                                                 Block *b = (*iter).getPointer();
00202                                                 return dynamic_cast<T&>(*b);
00203                                         }
00204                                 }
00205                         } catch (const std::bad_cast&) {
00206 
00207                         }
00208 
00209                         throw NoSuchBlockFoundException();
00210                 }
00211 
00212                 template<class T>
00213                 const std::list<const T*> Bundle::BlockList::getList() const
00214                 {
00215                         // create a list of blocks
00216                         std::list<const T*> ret;
00217 
00218                         // copy all blocks to the list
00219                         for (std::list<refcnt_ptr<Block> >::const_iterator iter = _blocks.begin(); iter != _blocks.end(); iter++)
00220                         {
00221                                 if ((*(*iter)).getType() == T::BLOCK_TYPE)
00222                                 {
00223                                         const T* obj = dynamic_cast<const T*>((*iter).getPointer());
00224 
00225                                         if (obj != NULL)
00226                                         {
00227                                                 ret.push_back( obj );
00228                                         }
00229                                 }
00230                         }
00231 
00232                         return ret;
00233                 }
00234 
00235                 template<class T>
00236                 T& Bundle::push_front()
00237                 {
00238                         T *tmpblock = new T();
00239                         dtn::data::Block *block = dynamic_cast<dtn::data::Block*>(tmpblock);
00240 
00241 #ifdef __DEVELOPMENT_ASSERTIONS__
00242                         assert(block != NULL);
00243 #endif
00244 
00245                         _blocks.push_front(block);
00246                         return (*tmpblock);
00247                 }
00248 
00249                 template<class T>
00250                 T& Bundle::push_back()
00251                 {
00252                         T *tmpblock = new T();
00253                         dtn::data::Block *block = dynamic_cast<dtn::data::Block*>(tmpblock);
00254 
00255 #ifdef __DEVELOPMENT_ASSERTIONS__
00256                         assert(block != NULL);
00257 #endif
00258 
00259                         _blocks.push_back(block);
00260                         return (*tmpblock);
00261                 }
00262 
00263                 template<class T>
00264                 T& Bundle::insert(const dtn::data::Block &before)
00265                 {
00266                         T *tmpblock = new T();
00267                         dtn::data::Block *block = dynamic_cast<dtn::data::Block*>(tmpblock);
00268 
00269 #ifdef __DEVELOPMENT_ASSERTIONS__
00270                         assert(block != NULL);
00271 #endif
00272 
00273                         _blocks.insert(block, &before);
00274                         return (*tmpblock);
00275                 }
00276         }
00277 }
00278 
00279 #endif /* BUNDLE_H_ */