IBR-DTNSuite  0.10
BundleStreamBuf.h
Go to the documentation of this file.
1 /*
2  * BundleStreamBuf.h
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 
22 #ifndef BUNDLESTREAMBUF_H_
23 #define BUNDLESTREAMBUF_H_
24 
25 #include <ibrdtn/data/MetaBundle.h>
27 #include <iostream>
28 
29 namespace dtn
30 {
31  namespace api
32  {
34  {
35  public:
36  virtual ~BundleStreamBufCallback() { };
37  virtual void put(dtn::data::Bundle &b) = 0;
38  virtual dtn::data::MetaBundle get(const dtn::data::Timeout timeout = 0) = 0;
39  virtual void delivered(const dtn::data::MetaBundle &b) = 0;
40  };
41 
42  class BundleStreamBuf : public std::basic_streambuf<char, std::char_traits<char> >
43  {
44  public:
45  // The size of the input and output buffers.
46  static const dtn::data::Length BUFF_SIZE = 5120;
47 
48  BundleStreamBuf(BundleStreamBufCallback &callback, const dtn::data::Length chunk_size = 4096, bool wait_seq_zero = false);
49  virtual ~BundleStreamBuf();
50 
51  void setChunkSize(const dtn::data::Length &size);
52  void setTimeout(const dtn::data::Timeout &timeout);
53 
54  protected:
55  virtual int sync();
56  virtual std::char_traits<char>::int_type overflow(std::char_traits<char>::int_type = std::char_traits<char>::eof());
57  virtual std::char_traits<char>::int_type underflow();
58 
59  private:
60  class Chunk
61  {
62  public:
63  Chunk(const dtn::data::MetaBundle &m);
64  virtual ~Chunk();
65 
66  bool operator==(const Chunk& other) const;
67  bool operator<(const Chunk& other) const;
68 
69  void load();
70 
72  dtn::data::Number _seq;
73  bool _first;
74  bool _last;
75  };
76 
77  void flushPayload(bool final = false);
78 
79  static void append(ibrcommon::BLOB::Reference &ref, const char* data, const dtn::data::Length &length);
80 
81  BundleStreamBufCallback &_callback;
82 
83  // Input buffer
84  std::vector<char> _in_buf;
85  // Output buffer
86  std::vector<char> _out_buf;
87 
88  dtn::data::Length _chunk_size;
89 
90  ibrcommon::BLOB::Reference _chunk_payload;
91 
92  std::set<Chunk> _chunks;
93  dtn::data::Length _chunk_offset;
94 
95  dtn::data::Bundle _current_bundle;
96 
97  dtn::data::Number _in_seq;
98  dtn::data::Number _out_seq;
99 
100  bool _streaming;
101 
102  // is set to true, when the first bundle is still not sent
103  bool _first_chunk;
104 
105  // if true, the last chunk was received before
106  bool _last_chunk_received;
107 
108  dtn::data::Timeout _timeout_receive;
109  };
110  } /* namespace data */
111 } /* namespace dtn */
112 #endif /* BUNDLESTREAMBUF_H_ */