IBR-DTNSuite  0.10
StrictSerializer.cpp
Go to the documentation of this file.
1 /*
2  * StrictSerializer.cpp
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 
25 #include "ibrdtn/data/SDNV.h"
26 #include "ibrdtn/data/Block.h"
27 #include "ibrdtn/data/Bundle.h"
28 #include "ibrdtn/data/Dictionary.h"
29 
30 namespace dtn
31 {
32  namespace security
33  {
34  StrictSerializer::StrictSerializer(std::ostream& stream, const dtn::security::SecurityBlock::BLOCK_TYPES type, const bool with_correlator, const dtn::data::Number &correlator)
35  : DefaultSerializer(stream), _block_type(type), _with_correlator(with_correlator), _correlator(correlator)
36  {
37  }
38 
40  {
41  }
42 
44  {
45  // rebuild the dictionary
46  rebuildDictionary(bundle);
47 
48  // serialize the primary block
49  (dtn::data::DefaultSerializer&)(*this) << static_cast<const dtn::data::PrimaryBlock&>(bundle);
50 
51  // serialize all secondary blocks
53 
54  // skip all blocks before the correlator
55  for (; _with_correlator && iter != bundle.end(); ++iter)
56  {
57  const dtn::data::Block &b = (**iter);
59  {
60  const dtn::security::SecurityBlock& sb = dynamic_cast<const dtn::security::SecurityBlock&>(**iter);
62  break;
63  }
64  }
65 
66  // consume the first block. this block may have the same correlator set,
67  // we are searching for in the loop
68  (*this) << (**iter);
69  ++iter;
70 
71  // serialize the remaining block
72  for (; iter != bundle.end(); ++iter)
73  {
74  const dtn::data::Block &b = (**iter);
75  (*this) << b;
76 
77  try {
78  const dtn::security::SecurityBlock &sb = dynamic_cast<const dtn::security::SecurityBlock&>(b);
79 
81  {
82  // until the block with the second correlator is reached
83  if (_with_correlator && (sb._ciphersuite_flags & SecurityBlock::CONTAINS_CORRELATOR) && sb._correlator == _correlator) break;
84  }
85  } catch (const std::bad_cast&) { };
86  }
87 
88  return *this;
89  }
90 
92  {
93  _stream << obj.getType();
94  _stream << obj.getProcessingFlags();
95 
96  const dtn::data::Block::eid_list &eids = obj.getEIDList();
97 
98 #ifdef __DEVELOPMENT_ASSERTIONS__
99  // test: BLOCK_CONTAINS_EIDS => (_eids.size() > 0)
100  assert(!obj.get(dtn::data::Block::BLOCK_CONTAINS_EIDS) || (eids.size() > 0));
101 #endif
102 
104  {
105  _stream << dtn::data::Number(eids.size());
106  for (dtn::data::Block::eid_list::const_iterator it = eids.begin(); it != eids.end(); ++it)
107  {
109 
110  if (_compressable)
111  {
112  offsets = (*it).getCompressed();
113  }
114  else
115  {
116  offsets = _dictionary.getRef(*it);
117  }
118 
119  _stream << offsets.first;
120  _stream << offsets.second;
121  }
122  }
123 
124  // write size of the payload in the block
126 
127  dtn::data::Length slength = 0;
128  obj.serialize_strict(_stream, slength);
129 
130  return (*this);
131  }
132  }
133 }