Collage  1.6.0
High-performance C++ library for developing object-oriented distributed applications.
dataOStream.h
1 
2 /* Copyright (c) 2007-2016, 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/types.h>
27 
28 #include <lunchbox/array.h> // used inline
29 #include <lunchbox/stdExt.h>
30 
31 #include <boost/noncopyable.hpp>
32 #include <boost/type_traits.hpp>
33 #include <map>
34 #include <set>
35 #include <vector>
36 
37 namespace co
38 {
39 namespace detail { class DataOStream; }
40 namespace DataStreamTest { class Sender; }
41 
48 class DataOStream : public boost::noncopyable
49 {
50 public:
54  CO_API void disable();
55 
57  void enableSave();
58 
60  void disableSave();
61 
63  CO_API bool hasSentData() const;
64 
66  CO_API const Connections& getConnections() const;
67 
69  DataOStream& streamDataHeader( DataOStream& os );
70 
72  void sendBody( ConnectionPtr connection, const void* data,
73  const uint64_t dataSize );
74 
76  uint64_t getCompressedDataSize() const;
78 
82  template< class T > DataOStream& operator << ( const T& value )
83  { _write( value, boost::has_trivial_copy< T >( )); return *this; }
84 
86  template< class T > DataOStream& operator << ( const Array< T > array )
87  { _writeArray( array, boost::has_trivial_copy<T>( )); return *this; }
88 
93  template< class T >
94  DataOStream& operator << ( const lunchbox::RefPtr< T >& ptr );
95 
97  template< class T >
98  DataOStream& operator << ( const lunchbox::Buffer< T >& buffer );
99 
101  template< class T >
102  DataOStream& operator << ( const lunchbox::Request<T>& request )
103  { return (*this) << request.getID(); }
104 
106  template< class T >
107  DataOStream& operator << ( const std::vector< T >& value );
108 
110  template< class K, class V >
111  DataOStream& operator << ( const std::map< K, V >& value );
112 
114  template< class T >
115  DataOStream& operator << ( const std::set< T >& value );
116 
118  template< class K, class V >
119  DataOStream& operator << ( const stde::hash_map< K, V >& value );
120 
122  template< class T >
123  DataOStream& operator << ( const stde::hash_set< T >& value );
124 
131  template< typename C >
132  void serializeChildren( const std::vector< C* >& children );
134 
135  CO_API static std::ostream& printStatistics( std::ostream& );
136  CO_API static void clearStatistics();
137 
138 protected:
139  CO_API DataOStream();
140  explicit DataOStream( DataOStream& rhs );
141  virtual CO_API ~DataOStream();
142 
144  CO_API lunchbox::Bufferb& getBuffer();
145 
147  void _setCompressor( const pression::data::CompressorInfo& info );
148 
150  CO_API void _enable();
151 
153  void flush( const bool last );
154 
158  void _setupConnections( const Nodes& receivers );
159 
160  void _setupConnections( const Connections& connections );
161 
163  void _setupConnection( NodePtr node, const bool useMulticast );
164 
166  CO_API void _setupConnection( ConnectionPtr connection );
167  friend class DataStreamTest::Sender;
168 
170  void _resend();
171 
172  void _clearConnections();
173 
177  virtual void sendData( const void* buffer, const uint64_t size,
178  const bool last ) = 0;
180 
182  virtual CO_API void reset();
183 
184 private:
185  detail::DataOStream* const _impl;
186 
188  CO_API uint64_t _getCompressedData( const uint8_t** chunks,
189  uint64_t* chunkSizes ) const;
190 
192  CO_API void _write( const void* data, uint64_t size );
193 
195  void _sendData( const void* data, const uint64_t size );
196 
198  void _resetBuffer();
199 
201  template< class T >
202  DataOStream& _writeFlatVector( const std::vector< T >& value )
203  {
204  const uint64_t nElems = value.size();
205  _write( &nElems, sizeof( nElems ));
206  if( nElems > 0 )
207  _write( &value.front(), nElems * sizeof( T ));
208  return *this;
209  }
210 
212  template< class T > void _write( const T& value, const boost::true_type& )
213  { _write( &value, sizeof( value )); }
214 
216  template< class T > void _write( const T& value, const boost::false_type& )
217  {
218  _writeSerializable( value,
219  boost::is_base_of< servus::Serializable, T >( ));
220  }
221 
223  template< class T>
224  void _writeSerializable( const T& value, const boost::true_type& );
225 
227  template< class T >
228  void _writeArray( const Array< T > array, const boost::true_type& )
229  { _write( array.data, array.getNumBytes( )); }
230 
232  template< class T >
233  void _writeArray( const Array< T > array, const boost::false_type& )
234  {
235  for( size_t i = 0; i < array.num; ++i )
236  *this << array.data[ i ];
237  }
238 
240  void _sendFooter( const void* buffer, const uint64_t size );
241 };
242 }
243 
244 #include "dataOStream.ipp" // template implementation
245 
246 #endif //CO_DATAOSTREAM_H
Defines export visibility macros for library Collage.
std::vector< ConnectionPtr > Connections
A vector of ConnectionPtr&#39;s.
Definition: types.h:125
lunchbox::RefPtr< Node > NodePtr
A reference pointer for Node pointers.
Definition: types.h:86
A std::ostream-like interface for object serialization.
Definition: dataOStream.h:48
Object-oriented network library.
Definition: barrier.h:27
std::vector< NodePtr > Nodes
A vector of NodePtr&#39;s.
Definition: types.h:104
lunchbox::RefPtr< Connection > ConnectionPtr
A reference pointer for Connection pointers.
Definition: types.h:94