IBR-DTNSuite  0.12
PayloadBlock.cpp
Go to the documentation of this file.
1 /*
2  * PayloadBlock.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"
25 #include <ibrcommon/Logger.h>
26 
27 namespace dtn
28 {
29  namespace data
30  {
32 
34  : Block(PayloadBlock::BLOCK_TYPE), _blobref(ibrcommon::BLOB::create())
35  {
36  }
37 
39  : Block(PayloadBlock::BLOCK_TYPE), _blobref(ref)
40  {
41  }
42 
44  {
45  }
46 
48  {
49  return _blobref;
50  }
51 
53  {
54  return _blobref.size();
55  }
56 
57  std::ostream& PayloadBlock::serialize(std::ostream &stream, Length &length) const
58  {
59  ibrcommon::BLOB::Reference blobref = _blobref;
60  ibrcommon::BLOB::iostream io = blobref.iostream();
61 
62  try {
63  ibrcommon::BLOB::copy(stream, *io, io.size());
64  length += io.size();
65  } catch (const ibrcommon::IOException &ex) {
67  }
68 
69  return stream;
70  }
71 
72  std::ostream& PayloadBlock::serialize(std::ostream &stream, const Length &clip_offset, const Length &clip_length) const
73  {
74  ibrcommon::BLOB::Reference blobref = _blobref;
75  ibrcommon::BLOB::iostream io = blobref.iostream();
76 
77  try {
78  (*io).seekg(clip_offset, std::ios::beg);
79  ibrcommon::BLOB::copy(stream, *io, clip_length);
80  } catch (const ibrcommon::IOException &ex) {
82  }
83 
84  return stream;
85  }
86 
87  std::istream& PayloadBlock::deserialize(std::istream &stream, const Length &length)
88  {
89  // lock the BLOB
90  ibrcommon::BLOB::iostream io = _blobref.iostream();
91 
92  // clear the blob
93  io.clear();
94 
95  // check if the blob is ready
96  if (!(*io).good()) throw dtn::SerializationFailedException("could not open BLOB for payload");
97 
98  // set block not processed bit to false
100 
101  try {
102  ibrcommon::BLOB::copy(*io, stream, length);
103  } catch (const ibrcommon::IOException &ex) {
104  throw dtn::PayloadReceptionInterrupted(length, ex.what());
105  }
106 
107  return stream;
108  }
109  }
110 }