IBR-DTNSuite  0.10
NativeSerializer.cpp
Go to the documentation of this file.
1 /*
2  * NativeSerializer.cpp
3  *
4  * Copyright (C) 2013 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 #include "api/NativeSerializer.h"
23 
24 namespace dtn
25 {
26  namespace api
27  {
29  : _output_buf(4096), _callback(cb)
30  {
31  setp(&_output_buf[0], &_output_buf[4096 - 1]);
32  }
33 
35 
36  }
37 
39  {
40  int ret = std::char_traits<char>::eq_int_type(this->overflow(
41  std::char_traits<char>::eof()), std::char_traits<char>::eof()) ? -1
42  : 0;
43 
44  return ret;
45  }
46 
47  std::char_traits<char>::int_type NativeCallbackStream::overflow(std::char_traits<char>::int_type c)
48  {
49  char *ibegin = &_output_buf[0];
50  char *iend = pptr();
51 
52  // mark the buffer as free
53  setp(&_output_buf[0], &_output_buf[4096 - 1]);
54 
55  if (!std::char_traits<char>::eq_int_type(c, std::char_traits<char>::eof()))
56  {
57  *iend++ = std::char_traits<char>::to_char_type(c);
58  }
59 
60  // if there is nothing to send, just return
61  if ((iend - ibegin) == 0)
62  {
63  return std::char_traits<char>::not_eof(c);
64  }
65 
66  _callback.payload(&_output_buf[0], (iend - ibegin));
67 
68  return std::char_traits<char>::not_eof(c);
69  }
70 
72  : _callback(cb), _mode(mode) {
73 
74  }
75 
77  }
78 
80  {
81  _callback.beginBundle(obj);
82  if (_mode != BUNDLE_HEADER)
83  {
84  for (dtn::data::Bundle::const_iterator it = obj.begin(); it != obj.end(); ++it) {
85  const dtn::data::Block &block = (**it);
86  _callback.beginBlock(block, block.getLength());
87 
88  if (_mode == BUNDLE_FULL)
89  {
90  dtn::data::Length len = 0;
91  NativeCallbackStream streambuf(_callback);
92  std::ostream stream(&streambuf);
93  block.serialize(stream, len);
94  stream << std::flush;
95  }
96 
97  _callback.endBlock();
98  }
99  }
100  _callback.endBundle();
101  return (*this);
102  }
103  } /* namespace api */
104 } /* namespace dtn */