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 #include "streaming/StreamBundle.h"
23 #include <ibrdtn/api/Client.h>
24 #include <ibrdtn/data/Number.h>
25 #include <ibrdtn/data/Bundle.h>
26 #include <iostream>
27 #include <set>
28 #include <vector>
29 
30 #ifndef BUNDLESTREAMBUF_H_
31 #define BUNDLESTREAMBUF_H_
32 
33 class BundleStreamBuf : public std::basic_streambuf<char, std::char_traits<char> >
34 {
35 public:
36  BundleStreamBuf(dtn::api::Client &client, StreamBundle &chunk, size_t min_buffer, size_t max_buffer, bool wait_seq_zero = false);
37  virtual ~BundleStreamBuf();
38 
42  void received(const dtn::data::Bundle &b);
43 
47  void flush();
48 
52  void setRequestAck(bool val);
53 
58 
59 protected:
60  virtual int sync();
61  virtual std::char_traits<char>::int_type overflow(std::char_traits<char>::int_type = std::char_traits<char>::eof());
62  virtual std::char_traits<char>::int_type underflow();
63  std::char_traits<char>::int_type __underflow();
64 
65  void __flush();
66 
67  size_t __get_next_buffer_size() const;
68 
69 private:
70  class Chunk
71  {
72  public:
73  Chunk(const dtn::data::Bundle &b);
74  virtual ~Chunk();
75 
76  bool operator==(const Chunk& other) const;
77  bool operator<(const Chunk& other) const;
78 
79  dtn::data::Bundle _bundle;
80  dtn::data::Number _seq;
81  };
82 
83  // Input buffer
84  std::vector<char> _in_buf;
85  // Output buffer
86  std::vector<char> _out_buf;
87 
88  dtn::api::Client &_client;
89  StreamBundle &_chunk;
90 
91  // minimum buffer size
92  size_t _min_buf_size;
93 
94  // maximum buffer size
95  size_t _max_buf_size;
96 
97  ibrcommon::Conditional _chunks_cond;
98  std::set<Chunk> _chunks;
99  size_t _chunk_offset;
100 
101  dtn::data::Number _in_seq;
102  bool _streaming;
103  bool _request_ack;
104  bool _flush_request;
105 
106  dtn::data::Timeout _receive_timeout;
107 };
108 
109 #endif /* BUNDLESTREAMBUF_H_ */