IBR-DTNSuite  0.8
ibrdtn/ibrdtn/api/PlainSerializer.h
Go to the documentation of this file.
00001 /*
00002  * PlainSerializer.h
00003  *
00004  *  Created on: 16.06.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef PLAINSERIALIZER_H_
00009 #define PLAINSERIALIZER_H_
00010 
00011 #include <ibrdtn/data/Serializer.h>
00012 #include <ibrdtn/data/Bundle.h>
00013 #include <ibrdtn/data/PrimaryBlock.h>
00014 #include <ibrdtn/data/Block.h>
00015 
00016 namespace dtn
00017 {
00018         namespace api
00019         {
00020                 class PlainSerializer : public dtn::data::Serializer
00021                 {
00022                 public:
00023                         PlainSerializer(std::ostream &stream, bool skip_payload = false);
00024                         virtual ~PlainSerializer();
00025 
00026                         dtn::data::Serializer &operator<<(const dtn::data::Bundle &obj);
00027                         dtn::data::Serializer &operator<<(const dtn::data::PrimaryBlock &obj);
00028                         dtn::data::Serializer &operator<<(const dtn::data::Block &obj);
00029 
00030                         dtn::data::Serializer &serialize(ibrcommon::BLOB::iostream &obj, size_t limit = 0);
00031 
00032                         size_t getLength(const dtn::data::Bundle &obj);
00033                         size_t getLength(const dtn::data::PrimaryBlock &obj) const;
00034                         size_t getLength(const dtn::data::Block &obj) const;
00035 
00036                 private:
00037                         std::ostream &_stream;
00038                         bool _skip_payload;
00039                 };
00040 
00041                 class PlainDeserializer : public dtn::data::Deserializer
00042                 {
00043                 public:
00044                         class BlockInserter;
00045                         class UnknownBlockException;
00046                         class BlockNotProcessableException;
00047 
00048                         PlainDeserializer(std::istream &stream);
00049                         virtual ~PlainDeserializer();
00050 
00051                         dtn::data::Deserializer &operator>>(dtn::data::Bundle &obj);
00052                         dtn::data::Deserializer &operator>>(dtn::data::PrimaryBlock &obj);
00053                         dtn::data::Deserializer &operator>>(dtn::data::Block &obj);
00054                         dtn::data::Deserializer &operator>>(ibrcommon::BLOB::iostream &obj);
00055 
00061                         dtn::data::Block& readBlock(BlockInserter inserter, bool payload_is_adm);
00062 
00063                 private:
00067                         dtn::data::Deserializer &operator>>(std::ostream &stream);
00068 
00069                         std::istream &_stream;
00070 
00071                 public:
00072 
00073                         class BlockInserter
00074                         {
00075                         public:
00076                                 enum POSITION
00077                                 {
00078                                         FRONT,
00079                                         MIDDLE,
00080                                         END
00081                                 };
00082 
00083                                 BlockInserter(dtn::data::Bundle &bundle, POSITION alignment, int pos = 0);
00084 
00085                                 template <class T>
00086                                 T& insert();
00087                                 POSITION getAlignment() const;
00088 
00089                                 dtn::data::Block &insert(dtn::data::ExtensionBlock::Factory &f);
00090 
00091                         private:
00092                                 dtn::data::Bundle *_bundle;
00093                                 POSITION _alignment;
00094                                 int _pos;
00095                         };
00096 
00097                         class PlainDeserializerException : public ibrcommon::Exception
00098                         {
00099                                 public:
00100                                         PlainDeserializerException(string what = "") throw() : Exception(what)
00101                                         {
00102                                         }
00103                         };
00104 
00105                         class UnknownBlockException : public PlainDeserializerException
00106                         {
00107                                 public:
00108                                         UnknownBlockException(string what = "unknown block") throw() : PlainDeserializerException(what)
00109                                         {
00110                                         }
00111                         };
00112 
00113                         class BlockNotProcessableException : public PlainDeserializerException
00114                         {
00115                                 public:
00116                                         BlockNotProcessableException(string what = "block not processable") throw() : PlainDeserializerException(what)
00117                                         {
00118                                         }
00119                         };
00120 
00121                 };
00122 
00123                 template <class T>
00124                 T& PlainDeserializer::BlockInserter::insert()
00125                 {
00126                         switch (_alignment)
00127                         {
00128                         case FRONT:
00129                                 return _bundle->push_front<T>();
00130                         case END:
00131                                 return _bundle->push_back<T>();
00132                         default:
00133                                 if(_pos <= 0)
00134                                         return _bundle->push_front<T>();
00135 
00136                                 try
00137                                 {
00138                                         dtn::data::Block &prev_block = _bundle->getBlock(_pos-1);
00139                                         return _bundle->insert<T>(prev_block);
00140                                 }
00141                                 catch (const std::exception &ex)
00142                                 {
00143                                         return _bundle->push_back<T>();
00144                                 }
00145                         }
00146                 }
00147         }
00148 }
00149 
00150 #endif /* PLAINSERIALIZER_H_ */