IBR-DTNSuite  0.10
ExtensionBlock.cpp
Go to the documentation of this file.
1 /*
2  * ExtensionBlock.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 
23 #include "ibrdtn/data/Exceptions.h"
26 
27 namespace dtn
28 {
29  namespace data
30  {
31  ExtensionBlock::FactoryList *ExtensionBlock::factories = NULL;
32 
34  { }
35 
37  { }
38 
40  {
41  static ibrcommon::Mutex mutex;
42  ibrcommon::MutexLock l(mutex);
43 
44  if (ExtensionBlock::factories == NULL)
45  {
47  }
48  }
49 
51  {
52  std::map<block_t, ExtensionBlock::Factory*>::iterator iter = fmap.find(type);
53 
54  if (iter != fmap.end())
55  {
56  return *(iter->second);
57  }
58 
59  throw ibrcommon::Exception("Factory not available");
60  }
61 
63  {
64  try {
65  get(type);
66  throw ibrcommon::Exception("extension block type already taken");
67  } catch (const ibrcommon::Exception&) {
68  fmap[type] = f;
69  }
70  }
71 
73  {
74  fmap.erase(type);
75  }
76 
78  : _type(type)
79  {
81  ExtensionBlock::factories->add(type, this);
82  }
83 
85  {
87  }
88 
90  {
92  return ExtensionBlock::factories->get(type);
93  }
94 
96  : Block(0), _blobref(ibrcommon::BLOB::create())
97  {
98  }
99 
101  : Block(0), _blobref(ref)
102  {
103  }
104 
106  {
107  }
108 
110  {
111  return _blobref;
112  }
113 
115  {
116  _blocktype = type;
117  }
118 
120  {
121  ibrcommon::BLOB::Reference blobref = _blobref;
122  ibrcommon::BLOB::iostream io = blobref.iostream();
123  return io.size();
124  }
125 
126  std::ostream& ExtensionBlock::serialize(std::ostream &stream, Length &) const
127  {
128  ibrcommon::BLOB::Reference blobref = _blobref;
129  ibrcommon::BLOB::iostream io = blobref.iostream();
130 
131  try {
132  ibrcommon::BLOB::copy(stream, *io, io.size());
133  } catch (const ibrcommon::IOException &ex) {
135  }
136 
137  return stream;
138  }
139 
140  std::istream& ExtensionBlock::deserialize(std::istream &stream, const Length &length)
141  {
142  // lock the BLOB
143  ibrcommon::BLOB::iostream io = _blobref.iostream();
144 
145  // clear the blob
146  io.clear();
147 
148  // check if the blob is ready
149  if (!(*io).good()) throw dtn::SerializationFailedException("could not open BLOB for payload");
150 
151  try {
152  ibrcommon::BLOB::copy(*io, stream, length);
153  } catch (const ibrcommon::IOException &ex) {
155  }
156 
157  // set block not processed bit
159 
160  return stream;
161  }
162  }
163 }