IBR-DTNSuite  0.8
ibrdtn/ibrdtn/data/Serializer.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* 
00003  * File:   Serializer.h
00004  * Author: morgenro
00005  *
00006  * Created on 30. Mai 2010, 15:31
00007  */
00008 
00009 #ifndef _SERIALIZER_H
00010 #define _SERIALIZER_H
00011 
00012 #include <iostream>
00013 #include "ibrdtn/data/Dictionary.h"
00014 #include "ibrdtn/data/PrimaryBlock.h"
00015 #include "ibrdtn/data/Exceptions.h"
00016 #include "ibrdtn/data/BundleFragment.h"
00017 
00018 namespace dtn
00019 {
00020         namespace data
00021         {
00022                 class Bundle;
00023                 class Block;
00024                 class PrimaryBlock;
00025                 class PayloadBlock;
00026                 class MetaBundle;
00027 
00028                 class Serializer
00029                 {
00030                 public:
00031                         virtual ~Serializer() {};
00032 
00033                         virtual Serializer &operator<<(const dtn::data::Bundle &obj) = 0;
00034                         virtual Serializer &operator<<(const dtn::data::PrimaryBlock &obj) = 0;
00035                         virtual Serializer &operator<<(const dtn::data::Block &obj) = 0;
00036                         virtual Serializer &operator<<(const dtn::data::BundleFragment &obj)
00037                         {
00038                                 (*this) << obj._bundle;
00039                                 return (*this);
00040                         };
00041 
00042                         virtual size_t getLength(const dtn::data::Bundle &obj) = 0;
00043                         virtual size_t getLength(const dtn::data::PrimaryBlock &obj) const = 0;
00044                         virtual size_t getLength(const dtn::data::Block &obj) const = 0;
00045                 };
00046 
00047                 class Deserializer
00048                 {
00049                 public:
00050                         virtual ~Deserializer() {};
00051 
00052                         virtual Deserializer &operator>>(dtn::data::Bundle &obj) = 0;
00053                         virtual Deserializer &operator>>(dtn::data::PrimaryBlock &obj) = 0;
00054                         virtual Deserializer &operator>>(dtn::data::Block &obj) = 0;
00055                 };
00056 
00057                 class Validator
00058                 {
00059                 public:
00060                         class RejectedException : public dtn::SerializationFailedException
00061                         {
00062                         public:
00063                                 RejectedException(string what = "A validate method has the bundle rejected.") throw() : dtn::SerializationFailedException(what)
00064                                 {
00065                                 };
00066                         };
00067 
00068                         virtual void validate(const dtn::data::PrimaryBlock&) const throw (RejectedException) = 0;
00069                         virtual void validate(const dtn::data::Block&, const size_t) const throw (RejectedException) = 0;
00070                         virtual void validate(const dtn::data::Bundle&) const throw (RejectedException) = 0;
00071                 };
00072 
00073                 class AcceptValidator : public Validator
00074                 {
00075                 public:
00076                         AcceptValidator();
00077                         virtual ~AcceptValidator();
00078 
00079                         virtual void validate(const dtn::data::PrimaryBlock&) const throw (RejectedException);
00080                         virtual void validate(const dtn::data::Block&, const size_t) const throw (RejectedException);
00081                         virtual void validate(const dtn::data::Bundle&) const throw (RejectedException);
00082                 };
00083 
00084                 class DefaultSerializer : public Serializer
00085                 {
00086                 public:
00091                         DefaultSerializer(std::ostream &stream);
00092 
00099                         DefaultSerializer(std::ostream &stream, const Dictionary &d);
00100 
00104                         virtual ~DefaultSerializer() {};
00105 
00106                         virtual Serializer &operator<<(const dtn::data::Bundle &obj);
00107                         virtual Serializer &operator<<(const dtn::data::PrimaryBlock &obj);
00108                         virtual Serializer &operator<<(const dtn::data::Block &obj);
00109                         virtual Serializer &operator<<(const dtn::data::BundleFragment &obj);
00110 
00111                         virtual size_t getLength(const dtn::data::Bundle &obj);
00112                         virtual size_t getLength(const dtn::data::PrimaryBlock &obj) const;
00113                         virtual size_t getLength(const dtn::data::Block &obj) const;
00114 
00115                 protected:
00116                         Serializer &serialize(const dtn::data::PayloadBlock& obj, size_t clip_offset, size_t clip_length);
00117                         void rebuildDictionary(const dtn::data::Bundle &obj);
00118                         bool isCompressable(const dtn::data::Bundle &obj) const;
00119                         std::ostream &_stream;
00120 
00121                         Dictionary _dictionary;
00122                         bool _compressable;
00123                 };
00124 
00125                 class DefaultDeserializer : public Deserializer
00126                 {
00127                 public:
00132                         DefaultDeserializer(std::istream &stream);
00133 
00141                         DefaultDeserializer(std::istream &stream, Validator &v);
00142 
00150                         DefaultDeserializer(std::istream &stream, const Dictionary &d);
00151 
00156                         virtual ~DefaultDeserializer() {};
00157 
00158                         virtual Deserializer &operator>>(dtn::data::Bundle &obj);
00159                         virtual Deserializer &operator>>(dtn::data::PrimaryBlock &obj);
00160                         virtual Deserializer &operator>>(dtn::data::Block &obj);
00161                         virtual Deserializer &operator>>(dtn::data::MetaBundle &obj);
00162 
00168                         void setFragmentationSupport(bool val);
00169 
00170                 protected:
00171                         std::istream &_stream;
00172                         Validator &_validator;
00173                         AcceptValidator _default_validator;
00174 
00175                 private:
00176                         Dictionary _dictionary;
00177                         bool _compressed;
00178                         bool _fragmentation;
00179                 };
00180 
00181                 class SeparateSerializer : public DefaultSerializer
00182                 {
00183                 public:
00184                         SeparateSerializer(std::ostream &stream);
00185                         virtual ~SeparateSerializer();
00186 
00187                         virtual Serializer &operator<<(const dtn::data::Block &obj);
00188                         virtual size_t getLength(const dtn::data::Block &obj) const;
00189                 };
00190 
00191                 class SeparateDeserializer : public DefaultDeserializer
00192                 {
00193                 public:
00194                         SeparateDeserializer(std::istream &stream, Bundle &b);
00195                         virtual ~SeparateDeserializer();
00196 
00197                         dtn::data::Block& readBlock();
00198 
00199                         virtual Deserializer &operator>>(dtn::data::Block &obj);
00200 
00201                 private:
00202                         Bundle &_bundle;
00203                 };
00204         }
00205 }
00206 
00207 
00208 #endif  /* _SERIALIZER_H */
00209