IBR-DTNSuite  0.8
daemon/src/api/BundleStreamBuf.h
Go to the documentation of this file.
00001 /*
00002  * BundleStreamBuf.h
00003  *
00004  *  Created on: 17.11.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef BUNDLESTREAMBUF_H_
00009 #define BUNDLESTREAMBUF_H_
00010 
00011 #include <ibrdtn/data/MetaBundle.h>
00012 #include <ibrcommon/thread/Conditional.h>
00013 #include <iostream>
00014 
00015 namespace dtn
00016 {
00017         namespace api
00018         {
00019                 class BundleStreamBufCallback
00020                 {
00021                 public:
00022                         virtual ~BundleStreamBufCallback() { };
00023                         virtual void put(dtn::data::Bundle &b) = 0;
00024                         virtual dtn::data::MetaBundle get(size_t timeout = 0) = 0;
00025                         virtual void delivered(const dtn::data::MetaBundle &b) = 0;
00026                 };
00027 
00028                 class BundleStreamBuf : public std::basic_streambuf<char, std::char_traits<char> >
00029                 {
00030                 public:
00031                         // The size of the input and output buffers.
00032                         static const size_t BUFF_SIZE = 5120;
00033 
00034                         BundleStreamBuf(BundleStreamBufCallback &callback, size_t chunk_size = 4096, bool wait_seq_zero = false);
00035                         virtual ~BundleStreamBuf();
00036 
00037                         void setChunkSize(size_t size);
00038                         void setTimeout(size_t timeout);
00039 
00040                 protected:
00041                         virtual int sync();
00042                         virtual std::char_traits<char>::int_type overflow(std::char_traits<char>::int_type = std::char_traits<char>::eof());
00043                         virtual std::char_traits<char>::int_type underflow();
00044 
00045                 private:
00046                         class Chunk
00047                         {
00048                         public:
00049                                 Chunk(const dtn::data::MetaBundle &m);
00050                                 virtual ~Chunk();
00051 
00052                                 bool operator==(const Chunk& other) const;
00053                                 bool operator<(const Chunk& other) const;
00054 
00055                                 void load();
00056 
00057                                 dtn::data::MetaBundle _meta;
00058                                 size_t _seq;
00059                                 bool _first;
00060                                 bool _last;
00061                         };
00062 
00063                         void flushPayload(bool final = false);
00064 
00065                         static void append(ibrcommon::BLOB::Reference &ref, const char* data, size_t length);
00066 
00067                         BundleStreamBufCallback &_callback;
00068 
00069                         // Input buffer
00070                         char *_in_buf;
00071                         // Output buffer
00072                         char *_out_buf;
00073 
00074                         size_t _chunk_size;
00075 
00076                         ibrcommon::BLOB::Reference _chunk_payload;
00077 
00078                         std::set<Chunk> _chunks;
00079                         size_t _chunk_offset;
00080 
00081                         dtn::data::Bundle _current_bundle;
00082 
00083                         size_t _in_seq;
00084                         size_t _out_seq;
00085 
00086                         bool _streaming;
00087 
00088                         // is set to true, when the first bundle is still not sent
00089                         bool _first_chunk;
00090 
00091                         // if true, the last chunk was received before
00092                         bool _last_chunk_received;
00093 
00094                         size_t _timeout_receive;
00095                 };
00096         } /* namespace data */
00097 } /* namespace dtn */
00098 #endif /* BUNDLESTREAMBUF_H_ */