IBR-DTNSuite  0.10
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  ibrcommon::BLOB::Reference blobref = _blobref;
55  ibrcommon::BLOB::iostream io = blobref.iostream();
56  return io.size();
57  }
58 
59  std::ostream& PayloadBlock::serialize(std::ostream &stream, Length &length) const
60  {
61  ibrcommon::BLOB::Reference blobref = _blobref;
62  ibrcommon::BLOB::iostream io = blobref.iostream();
63 
64  try {
65  ibrcommon::BLOB::copy(stream, *io, io.size());
66  length += io.size();
67  } catch (const ibrcommon::IOException &ex) {
69  }
70 
71  return stream;
72  }
73 
74  std::ostream& PayloadBlock::serialize(std::ostream &stream, const Length &clip_offset, const Length &clip_length) const
75  {
76  ibrcommon::BLOB::Reference blobref = _blobref;
77  ibrcommon::BLOB::iostream io = blobref.iostream();
78 
79  try {
80  (*io).seekg(clip_offset, std::ios::beg);
81  ibrcommon::BLOB::copy(stream, *io, clip_length);
82  } catch (const ibrcommon::IOException &ex) {
84  }
85 
86  return stream;
87  }
88 
89  std::istream& PayloadBlock::deserialize(std::istream &stream, const Length &length)
90  {
91  // lock the BLOB
92  ibrcommon::BLOB::iostream io = _blobref.iostream();
93 
94  // clear the blob
95  io.clear();
96 
97  // check if the blob is ready
98  if (!(*io).good()) throw dtn::SerializationFailedException("could not open BLOB for payload");
99 
100  // set block not processed bit to false
102 
103  try {
104  ibrcommon::BLOB::copy(*io, stream, length);
105  } catch (const ibrcommon::IOException &ex) {
106  throw dtn::PayloadReceptionInterrupted(length, ex.what());
107  }
108 
109  return stream;
110  }
111  }
112 }