IBR-DTNSuite  0.12
Serializer.h
Go to the documentation of this file.
1 /*
2  * Serializer.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef _SERIALIZER_H
23 #define _SERIALIZER_H
24 
25 #include <iostream>
26 #include "ibrdtn/data/Dictionary.h"
28 #include "ibrdtn/data/Exceptions.h"
30 
31 namespace dtn
32 {
33  namespace data
34  {
35  class Bundle;
36  class Block;
37  class PrimaryBlock;
38  class PayloadBlock;
39  class MetaBundle;
40 
41  class Serializer
42  {
43  public:
44  virtual ~Serializer() {};
45 
46  virtual Serializer &operator<<(const dtn::data::Bundle &obj) = 0;
48  {
49  (*this) << obj._bundle;
50  return (*this);
51  };
52 
53  virtual Length getLength(const dtn::data::Bundle &obj) = 0;
54  };
55 
57  {
58  public:
59  virtual ~Deserializer() {};
60 
61  virtual Deserializer &operator>>(dtn::data::Bundle &obj) = 0;
62  };
63 
64  class Validator
65  {
66  public:
68  {
69  public:
70  RejectedException(string what = "A validate method has the bundle rejected.") throw() : dtn::SerializationFailedException(what)
71  {
72  };
73  };
74 
75  virtual ~Validator() {};
76 
77  virtual void validate(const dtn::data::PrimaryBlock&) const throw (RejectedException) = 0;
78  virtual void validate(const dtn::data::Block&, const dtn::data::Number&) const throw (RejectedException) = 0;
79  virtual void validate(const dtn::data::PrimaryBlock&, const dtn::data::Block&, const dtn::data::Number&) const throw (RejectedException) = 0;
80  virtual void validate(const dtn::data::Bundle&) const throw (RejectedException) = 0;
81  };
82 
83  class AcceptValidator : public Validator
84  {
85  public:
87  virtual ~AcceptValidator();
88 
89  virtual void validate(const dtn::data::PrimaryBlock&) const throw (RejectedException);
90  virtual void validate(const dtn::data::Block&, const dtn::data::Number&) const throw (RejectedException);
91  virtual void validate(const dtn::data::PrimaryBlock&, const dtn::data::Block&, const dtn::data::Number&) const throw (RejectedException);
92  virtual void validate(const dtn::data::Bundle&) const throw (RejectedException);
93  };
94 
96  {
97  public:
102  DefaultSerializer(std::ostream &stream);
103 
110  DefaultSerializer(std::ostream &stream, const Dictionary &d);
111 
115  virtual ~DefaultSerializer() {};
116 
117  virtual Serializer &operator<<(const dtn::data::Bundle &obj);
118  virtual Serializer &operator<<(const dtn::data::PrimaryBlock &obj);
119  virtual Serializer &operator<<(const dtn::data::Block &obj);
120  virtual Serializer &operator<<(const dtn::data::BundleFragment &obj);
121 
122  virtual Length getLength(const dtn::data::Bundle &obj);
123  virtual Length getLength(const dtn::data::PrimaryBlock &obj) const;
124  virtual Length getLength(const dtn::data::Block &obj) const;
125 
126  protected:
127  Serializer &serialize(const dtn::data::PayloadBlock& obj, const Length &clip_offset, const Length &clip_length);
128  void rebuildDictionary(const dtn::data::Bundle &obj);
129  bool isCompressable(const dtn::data::Bundle &obj) const;
130  std::ostream &_stream;
131 
134  };
135 
137  {
138  public:
143  DefaultDeserializer(std::istream &stream);
144 
152  DefaultDeserializer(std::istream &stream, Validator &v);
153 
158  virtual ~DefaultDeserializer() {};
159 
163  virtual Deserializer &read(const dtn::data::PrimaryBlock &bundle, dtn::data::Block &obj);
165 
171  void setFragmentationSupport(bool val);
172 
173  protected:
174  std::istream &_stream;
177 
178  private:
179  Dictionary _dictionary;
180  bool _compressed;
181  bool _fragmentation;
182  };
183 
185  {
186  public:
187  SeparateSerializer(std::ostream &stream);
188  virtual ~SeparateSerializer();
189 
190  virtual Serializer &operator<<(const dtn::data::Block &obj);
191  virtual Length getLength(const dtn::data::Block &obj) const;
192  };
193 
195  {
196  public:
197  SeparateDeserializer(std::istream &stream, Bundle &b);
198  virtual ~SeparateDeserializer();
199 
200  dtn::data::Block& readBlock();
201 
202  private:
203  Bundle &_bundle;
204  };
205  }
206 }
207 
208 
209 #endif /* _SERIALIZER_H */
210