Equalizer 1.0
|
00001 00002 /* Copyright (c) 2007-2011, 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 <co/base/buffer.h> // member 00025 00026 #include <iostream> 00027 #include <vector> 00028 00029 //#define EQ_INSTRUMENT_DATAOSTREAM 00030 00031 namespace co 00032 { 00033 namespace DataStreamTest 00034 { 00035 class Sender; 00036 } 00037 00043 class DataOStream : public base::NonCopyable 00044 { 00045 public: 00048 CO_API DataOStream(); 00049 virtual CO_API ~DataOStream(); 00050 00052 CO_API void disable(); 00053 00055 void enableSave(); 00056 00058 void disableSave(); 00059 00061 bool hasSentData() const { return _dataSent; } 00063 00067 template< typename T > DataOStream& operator << ( const T& value ) 00068 { write( &value, sizeof( value )); return *this; } 00069 00071 template< typename T > 00072 DataOStream& operator << ( const std::vector< T >& value ); 00073 00075 CO_API void write( const void* data, uint64_t size ); 00076 00083 template< typename C > 00084 void serializeChildren( const std::vector< C* >& children ); 00086 00087 protected: 00089 void _initCompressor( const uint32_t compressor ); 00090 00092 CO_API void _enable(); 00093 00095 void _flush(); 00096 00101 void _setupConnections( const Nodes& receivers ); 00102 00104 void _setupConnection( NodePtr node, const bool useMulticast ); 00105 00107 void _resend(); 00108 00112 virtual void sendData( const void* buffer, const uint64_t size, 00113 const bool last ) = 0; 00114 00115 template< typename P > 00116 void sendPacket( P& packet, const void* buffer, const uint64_t size, 00117 const bool last ); 00119 00121 virtual CO_API void reset(); 00122 00123 private: 00124 enum CompressorState 00125 { 00126 STATE_UNCOMPRESSED, 00127 STATE_PARTIAL, 00128 STATE_COMPLETE, 00129 STATE_UNCOMPRESSIBLE, 00130 }; 00131 CompressorState _compressorState; 00132 00134 base::Bufferb _buffer; 00135 00137 uint64_t _bufferStart; 00138 00140 uint64_t _dataSize; 00141 00143 Connections _connections; 00144 friend class DataStreamTest::Sender; 00145 00147 base::CPUCompressor* const _compressor; 00148 00150 bool _enabled; 00151 00153 bool _dataSent; 00154 00156 bool _save; 00157 00159 void _sendData( const void* data, const uint64_t size ); 00160 00162 void _resetBuffer(); 00163 00165 template< typename T > 00166 DataOStream& _writeFlatVector( const std::vector< T >& value ) 00167 { 00168 const uint64_t nElems = value.size(); 00169 write( &nElems, sizeof( nElems )); 00170 if( nElems > 0 ) 00171 write( &value.front(), nElems * sizeof( T )); 00172 return *this; 00173 } 00175 void _sendFooter( const void* buffer, const uint64_t size ); 00176 00181 CO_API uint64_t _getCompressedData( void** chunks, 00182 uint64_t* chunkSizes ) const; 00183 00185 void _compress( void* src, const uint64_t size, 00186 const CompressorState result ); 00187 }; 00188 00189 std::ostream& operator << ( std::ostream& os, 00190 const DataOStream& dataOStream ); 00191 00192 } 00193 00194 #include <co/object.h> 00195 #include <co/objectVersion.h> 00196 00197 namespace co 00198 { 00202 template<> 00203 inline DataOStream& DataOStream::operator << ( const std::string& str ) 00204 { 00205 const uint64_t nElems = str.length(); 00206 write( &nElems, sizeof( nElems )); 00207 if ( nElems > 0 ) 00208 write( str.c_str(), nElems ); 00209 00210 return *this; 00211 } 00212 00214 template<> inline DataOStream& 00215 DataOStream::operator << ( const Object* const& object ) 00216 { 00217 EQASSERT( !object || object->isAttached( )); 00218 (*this) << ObjectVersion( object ); 00219 return *this; 00220 } 00221 00224 template< typename T > inline DataOStream& 00225 DataOStream::operator << ( const std::vector< T >& value ) 00226 { 00227 const uint64_t nElems = value.size(); 00228 (*this) << nElems; 00229 for( uint64_t i = 0; i < nElems; ++i ) 00230 (*this) << value[i]; 00231 return *this; 00232 } 00233 00234 template< typename C > inline void 00235 DataOStream::serializeChildren( const std::vector<C*>& children ) 00236 { 00237 const uint64_t nElems = children.size(); 00238 (*this) << nElems; 00239 00240 for( typename std::vector< C* >::const_iterator i = children.begin(); 00241 i != children.end(); ++i ) 00242 { 00243 C* child = *i; 00244 (*this) << ObjectVersion( child ); 00245 EQASSERTINFO( !child || child->isAttached(), 00246 "Found unmapped object during serialization" ); 00247 } 00248 } 00252 template<> inline DataOStream& 00253 DataOStream::operator << ( const std::vector< uint8_t >& value ) 00254 { return _writeFlatVector( value ); } 00255 00257 template<> inline DataOStream& 00258 DataOStream::operator << ( const std::vector< uint16_t >& value ) 00259 { return _writeFlatVector( value ); } 00260 00262 template<> inline DataOStream& 00263 DataOStream::operator << ( const std::vector< int16_t >& value ) 00264 { return _writeFlatVector( value ); } 00265 00267 template<> inline DataOStream& 00268 DataOStream::operator << ( const std::vector< uint32_t >& value ) 00269 { return _writeFlatVector( value ); } 00270 00272 template<> inline DataOStream& 00273 DataOStream::operator << ( const std::vector< int32_t >& value ) 00274 { return _writeFlatVector( value ); } 00275 00277 template<> inline DataOStream& 00278 DataOStream::operator << ( const std::vector< uint64_t >& value ) 00279 { return _writeFlatVector( value ); } 00280 00282 template<> inline DataOStream& 00283 DataOStream::operator << ( const std::vector< int64_t >& value ) 00284 { return _writeFlatVector( value ); } 00285 00287 template<> inline DataOStream& 00288 DataOStream::operator << ( const std::vector< float >& value ) 00289 { return _writeFlatVector( value ); } 00290 00292 template<> inline DataOStream& 00293 DataOStream::operator << ( const std::vector< double >& value ) 00294 { return _writeFlatVector( value ); } 00295 00297 template<> inline DataOStream& 00298 DataOStream::operator << ( const std::vector< ObjectVersion >& value ) 00299 { return _writeFlatVector( value ); } 00301 } 00302 #endif //CO_DATAOSTREAM_H