Collage
0.6.1
|
00001 00002 /* Copyright (c) 2007-2012, Stefan Eilemann <eile@equalizergraphics.com> 00003 * 2010, Cedric Stalder <cedric.stalder@gmail.com> 00004 * 00005 * This library is free software; you can redistribute it and/or modify it under 00006 * the terms of the GNU Lesser General Public License version 2.1 as published 00007 * by the Free Software Foundation. 00008 * 00009 * This library is distributed in the hope that it will be useful, but WITHOUT 00010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00011 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00012 * details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public License 00015 * along with this library; if not, write to the Free Software Foundation, Inc., 00016 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00017 */ 00018 00019 #ifndef CO_DATAOSTREAM_H 00020 #define CO_DATAOSTREAM_H 00021 00022 #include <co/api.h> 00023 #include <co/types.h> 00024 #include <lunchbox/buffer.h> // member 00025 #include <lunchbox/nonCopyable.h> // base class 00026 00027 #include <iostream> 00028 #include <vector> 00029 00030 //#define EQ_INSTRUMENT_DATAOSTREAM 00031 00032 namespace co 00033 { 00034 namespace DataStreamTest 00035 { 00036 class Sender; 00037 } 00038 00044 class DataOStream : public lunchbox::NonCopyable 00045 { 00046 public: 00049 CO_API DataOStream(); 00050 virtual CO_API ~DataOStream(); 00051 00053 CO_API void disable(); 00054 00056 void disable( const Packet& packet ); 00057 00059 void enableSave(); 00060 00062 void disableSave(); 00063 00065 bool hasSentData() const { return _dataSent; } 00067 00071 template< typename T > DataOStream& operator << ( const T& value ) 00072 { write( &value, sizeof( value )); return *this; } 00073 00075 template< typename T > 00076 DataOStream& operator << ( const std::vector< T >& value ); 00077 00079 CO_API void write( const void* data, uint64_t size ); 00080 00087 template< typename C > 00088 void serializeChildren( const std::vector< C* >& children ); 00090 00091 protected: 00093 void _initCompressor( const uint32_t compressor ); 00094 00096 CO_API void _enable(); 00097 00099 void _flush(); 00100 00105 void _setupConnections( const Nodes& receivers ); 00106 00108 void _setupConnection( NodePtr node, const bool useMulticast ); 00109 00111 void _resend(); 00112 00113 void _send( const Packet& packet ); 00114 00115 void _clearConnections() { _connections.clear(); } 00116 00120 virtual void sendData( const void* buffer, const uint64_t size, 00121 const bool last ) = 0; 00122 00123 template< typename P > 00124 void sendPacket( P& packet, const void* buffer, const uint64_t size, 00125 const bool last ); 00127 00129 virtual CO_API void reset(); 00130 00131 private: 00132 enum CompressorState 00133 { 00134 STATE_UNCOMPRESSED, 00135 STATE_PARTIAL, 00136 STATE_COMPLETE, 00137 STATE_UNCOMPRESSIBLE 00138 }; 00139 CompressorState _compressorState; 00140 00142 lunchbox::Bufferb _buffer; 00143 00145 uint64_t _bufferStart; 00146 00148 uint64_t _dataSize; 00149 00151 Connections _connections; 00152 friend class DataStreamTest::Sender; 00153 00155 CPUCompressor* const _compressor; 00156 00158 bool _enabled; 00159 00161 bool _dataSent; 00162 00164 bool _save; 00165 00166 bool _disable(); 00167 00169 void _sendData( const void* data, const uint64_t size ); 00170 00172 void _resetBuffer(); 00173 00175 template< typename T > 00176 DataOStream& _writeFlatVector( const std::vector< T >& value ) 00177 { 00178 const uint64_t nElems = value.size(); 00179 write( &nElems, sizeof( nElems )); 00180 if( nElems > 0 ) 00181 write( &value.front(), nElems * sizeof( T )); 00182 return *this; 00183 } 00185 void _sendFooter( const void* buffer, const uint64_t size ); 00186 00191 CO_API uint64_t _getCompressedData( void** chunks, 00192 uint64_t* chunkSizes ) const; 00193 00195 void _compress( void* src, const uint64_t size, 00196 const CompressorState result ); 00197 }; 00198 00199 std::ostream& operator << ( std::ostream& os, 00200 const DataOStream& dataOStream ); 00201 00202 } 00203 00204 #include <co/object.h> 00205 #include <co/objectVersion.h> 00206 00207 namespace co 00208 { 00212 template<> 00213 inline DataOStream& DataOStream::operator << ( const std::string& str ) 00214 { 00215 const uint64_t nElems = str.length(); 00216 write( &nElems, sizeof( nElems )); 00217 if ( nElems > 0 ) 00218 write( str.c_str(), nElems ); 00219 00220 return *this; 00221 } 00222 00224 template<> inline DataOStream& 00225 DataOStream::operator << ( const Object* const& object ) 00226 { 00227 LBASSERT( !object || object->isAttached( )); 00228 (*this) << ObjectVersion( object ); 00229 return *this; 00230 } 00231 00234 template< typename T > inline DataOStream& 00235 DataOStream::operator << ( const std::vector< T >& value ) 00236 { 00237 const uint64_t nElems = value.size(); 00238 (*this) << nElems; 00239 for( uint64_t i = 0; i < nElems; ++i ) 00240 (*this) << value[i]; 00241 return *this; 00242 } 00243 00244 template< typename C > inline void 00245 DataOStream::serializeChildren( const std::vector<C*>& children ) 00246 { 00247 const uint64_t nElems = children.size(); 00248 (*this) << nElems; 00249 00250 for( typename std::vector< C* >::const_iterator i = children.begin(); 00251 i != children.end(); ++i ) 00252 { 00253 C* child = *i; 00254 (*this) << ObjectVersion( child ); 00255 LBASSERTINFO( !child || child->isAttached(), 00256 "Found unmapped object during serialization" ); 00257 } 00258 } 00262 template<> inline DataOStream& 00263 DataOStream::operator << ( const std::vector< uint8_t >& value ) 00264 { return _writeFlatVector( value ); } 00265 00267 template<> inline DataOStream& 00268 DataOStream::operator << ( const std::vector< uint16_t >& value ) 00269 { return _writeFlatVector( value ); } 00270 00272 template<> inline DataOStream& 00273 DataOStream::operator << ( const std::vector< int16_t >& value ) 00274 { return _writeFlatVector( value ); } 00275 00277 template<> inline DataOStream& 00278 DataOStream::operator << ( const std::vector< uint32_t >& value ) 00279 { return _writeFlatVector( value ); } 00280 00282 template<> inline DataOStream& 00283 DataOStream::operator << ( const std::vector< int32_t >& value ) 00284 { return _writeFlatVector( value ); } 00285 00287 template<> inline DataOStream& 00288 DataOStream::operator << ( const std::vector< uint64_t >& value ) 00289 { return _writeFlatVector( value ); } 00290 00292 template<> inline DataOStream& 00293 DataOStream::operator << ( const std::vector< int64_t >& value ) 00294 { return _writeFlatVector( value ); } 00295 00297 template<> inline DataOStream& 00298 DataOStream::operator << ( const std::vector< float >& value ) 00299 { return _writeFlatVector( value ); } 00300 00302 template<> inline DataOStream& 00303 DataOStream::operator << ( const std::vector< double >& value ) 00304 { return _writeFlatVector( value ); } 00305 00307 template<> inline DataOStream& 00308 DataOStream::operator << ( const std::vector< ObjectVersion >& value ) 00309 { return _writeFlatVector( value ); } 00311 } 00312 #endif //CO_DATAOSTREAM_H