Collage  1.7.0
High-performance C++ library for developing object-oriented distributed applications.
dataOStream.h
1 
2 /* Copyright (c) 2007-2017, Stefan Eilemann <eile@equalizergraphics.com>
3  * Cedric Stalder <cedric.stalder@gmail.com>
4  * Daniel Nachbaur <danielnachbaur@gmail.com>
5  *
6  * This file is part of Collage <https://github.com/Eyescale/Collage>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 2.1 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifndef CO_DATAOSTREAM_H
23 #define CO_DATAOSTREAM_H
24 
25 #include <co/api.h>
26 #include <co/global.h>
27 #include <co/types.h>
28 
29 #include <lunchbox/array.h> // used inline
30 
31 #include <boost/noncopyable.hpp>
32 #include <boost/type_traits.hpp>
33 #include <map>
34 #include <set>
35 #include <unordered_map>
36 #include <unordered_set>
37 #include <vector>
38 
39 namespace co
40 {
41 namespace detail
42 {
43 class DataOStream;
44 }
45 namespace DataStreamTest
46 {
47 class Sender;
48 }
49 
56 class DataOStream : public boost::noncopyable
57 {
58 public:
62  CO_API void disable();
63 
65  void enableSave();
66 
68  void disableSave();
69 
71  CO_API bool hasSentData() const;
72 
74  CO_API const Connections& getConnections() const;
75 
77  DataOStream& streamDataHeader(DataOStream& os);
78 
80  void sendBody(ConnectionPtr connection, const void* data,
81  const uint64_t dataSize);
82 
84  uint64_t getCompressedDataSize() const;
86 
90  template <class T>
91  DataOStream& operator<<(const T& value)
92  {
93  _write(value, boost::has_trivial_copy<T>());
94  return *this;
95  }
96 
98  template <class T>
99  DataOStream& operator<<(const Array<T> array)
100  {
101  _writeArray(array, boost::has_trivial_copy<T>());
102  return *this;
103  }
104 
109  template <class T>
110  DataOStream& operator<<(const lunchbox::RefPtr<T>& ptr);
111 
113  template <class T>
114  DataOStream& operator<<(const lunchbox::Buffer<T>& buffer);
115 
117  template <class T>
118  DataOStream& operator<<(const lunchbox::Request<T>& request)
119  {
120  return (*this) << request.getID();
121  }
122 
124  template <class T>
125  DataOStream& operator<<(const std::vector<T>& value);
126 
128  template <class K, class V>
129  DataOStream& operator<<(const std::map<K, V>& value);
130 
132  template <class T>
133  DataOStream& operator<<(const std::set<T>& value);
134 
136  template <class K, class V>
137  DataOStream& operator<<(const std::unordered_map<K, V>& value);
138 
140  template <class T>
141  DataOStream& operator<<(const std::unordered_set<T>& value);
142 
149  template <typename C>
150  void serializeChildren(const std::vector<C*>& children);
152 
153  CO_API static std::ostream& printStatistics(std::ostream&);
154  CO_API static void clearStatistics();
155 
156 protected:
157  CO_API explicit DataOStream(
158  size_t chunkSize = Global::getObjectBufferSize());
159  explicit DataOStream(DataOStream& rhs);
160  virtual CO_API ~DataOStream();
161 
163  CO_API lunchbox::Bufferb& getBuffer();
164 
166  void _setCompressor(const pression::data::CompressorInfo& info);
167 
169  CO_API void _enable();
170 
172  void flush(const bool last);
173 
177  void _setupConnections(const Nodes& receivers);
178 
179  void _setupConnections(const Connections& connections);
180 
182  void _setupConnection(NodePtr node, const bool useMulticast);
183 
185  CO_API void _setupConnection(ConnectionPtr connection);
186  friend class DataStreamTest::Sender;
187 
189  void _resend();
190 
191  void _clearConnections();
192 
196  virtual void sendData(const void* buffer, const uint64_t size,
197  const bool last) = 0;
199 
201  virtual CO_API void reset();
202 
203 private:
204  detail::DataOStream* const _impl;
205 
207  CO_API uint64_t _getCompressedData(const uint8_t** chunks,
208  uint64_t* chunkSizes) const;
209 
211  CO_API void _write(const void* data, uint64_t size);
212 
214  void _sendData(const void* data, const uint64_t size);
215 
217  void _resetBuffer();
218 
220  template <class T>
221  DataOStream& _writeFlatVector(const std::vector<T>& value)
222  {
223  const uint64_t nElems = value.size();
224  _write(&nElems, sizeof(nElems));
225  if (nElems > 0)
226  _write(&value.front(), nElems * sizeof(T));
227  return *this;
228  }
229 
231  template <class T>
232  void _write(const T& value, const boost::true_type&)
233  {
234  _write(&value, sizeof(value));
235  }
236 
238  template <class T>
239  void _write(const T& value, const boost::false_type&)
240  {
241  _writeSerializable(value, boost::is_base_of<servus::Serializable, T>());
242  }
243 
245  template <class T>
246  void _writeSerializable(const T& value, const boost::true_type&);
247 
249  template <class T>
250  void _writeArray(const Array<T> array, const boost::true_type&)
251  {
252  _write(array.data, array.getNumBytes());
253  }
254 
256  template <class T>
257  void _writeArray(const Array<T> array, const boost::false_type&)
258  {
259  for (size_t i = 0; i < array.num; ++i)
260  *this << array.data[i];
261  }
262 
264  void _sendFooter(const void* buffer, const uint64_t size);
265 };
266 }
267 
268 #include "dataOStream.ipp" // template implementation
269 
270 #endif // CO_DATAOSTREAM_H
Defines export visibility macros for library Collage.
DataOStream & operator<<(const T &value)
Write a plain data item by copying it to the stream.
Definition: dataOStream.h:91
static CO_API uint32_t getObjectBufferSize()
A std::ostream-like interface for object serialization.
Definition: dataOStream.h:56
Object-oriented network library.
Definition: barrier.h:27
std::vector< NodePtr > Nodes
A vector of NodePtr&#39;s.
Definition: types.h:103
lunchbox::RefPtr< Connection > ConnectionPtr
A reference pointer for Connection pointers.
Definition: types.h:93
std::vector< ConnectionPtr > Connections
A vector of ConnectionPtr&#39;s.
Definition: types.h:124
lunchbox::RefPtr< Node > NodePtr
A reference pointer for Node pointers.
Definition: types.h:85