Equalizer  1.2.1
dataOStream.h
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 disable( const Packet& packet );
00056 
00058         void enableSave();
00059 
00061         void disableSave();
00062 
00064         bool hasSentData() const { return _dataSent; }
00066 
00070         template< typename T > DataOStream& operator << ( const T& value )
00071             { write( &value, sizeof( value )); return *this; }
00072 
00074         template< typename T >
00075         DataOStream& operator << ( const std::vector< T >& value );
00076 
00078         CO_API void write( const void* data, uint64_t size );
00079 
00086         template< typename C >
00087         void serializeChildren( const std::vector< C* >& children );
00089  
00090     protected:
00092         void _initCompressor( const uint32_t compressor );
00093 
00095         CO_API void _enable();
00096 
00098         void _flush();
00099 
00104         void _setupConnections( const Nodes& receivers );
00105 
00107         void _setupConnection( NodePtr node, const bool useMulticast );
00108 
00110         void _resend();
00111 
00112         void _send( const Packet& packet );
00113 
00114         void _clearConnections() { _connections.clear(); }
00115 
00119         virtual void sendData( const void* buffer, const uint64_t size,
00120                                const bool last ) = 0;
00121 
00122         template< typename P >
00123         void sendPacket( P& packet, const void* buffer, const uint64_t size,
00124                          const bool last );
00126 
00128         virtual CO_API void reset();
00129 
00130     private:        
00131         enum CompressorState
00132         {
00133             STATE_UNCOMPRESSED,
00134             STATE_PARTIAL,
00135             STATE_COMPLETE,
00136             STATE_UNCOMPRESSIBLE,
00137         };
00138         CompressorState _compressorState;
00139         
00141         base::Bufferb  _buffer;
00142 
00144         uint64_t _bufferStart;
00145 
00147         uint64_t _dataSize;
00148 
00150         Connections _connections;
00151         friend class DataStreamTest::Sender;
00152 
00154         base::CPUCompressor* const _compressor;
00155 
00157         bool _enabled;
00158 
00160         bool _dataSent;
00161 
00163         bool _save;
00164 
00165         bool _disable();
00166 
00168         void _sendData( const void* data, const uint64_t size );
00169         
00171         void _resetBuffer();
00172 
00174         template< typename T > 
00175         DataOStream& _writeFlatVector( const std::vector< T >& value )
00176         {
00177             const uint64_t nElems = value.size();
00178             write( &nElems, sizeof( nElems ));
00179             if( nElems > 0 )
00180                 write( &value.front(), nElems * sizeof( T ));
00181             return *this;
00182         }
00184         void _sendFooter( const void* buffer, const uint64_t size );
00185 
00190         CO_API uint64_t _getCompressedData( void** chunks,
00191                                             uint64_t* chunkSizes ) const;
00192 
00194         void _compress( void* src, const uint64_t size,
00195                         const CompressorState result );
00196     };
00197 
00198     std::ostream& operator << ( std::ostream& os,
00199                                 const DataOStream& dataOStream );
00200 
00201 }
00202 
00203 #include <co/object.h>
00204 #include <co/objectVersion.h>
00205 
00206 namespace co
00207 {
00211     template<>
00212     inline DataOStream& DataOStream::operator << ( const std::string& str )
00213     { 
00214         const uint64_t nElems = str.length();
00215         write( &nElems, sizeof( nElems ));
00216         if ( nElems > 0 )
00217             write( str.c_str(), nElems );
00218 
00219         return *this;
00220     }
00221 
00223     template<> inline DataOStream& 
00224     DataOStream::operator << ( const Object* const& object )
00225     {
00226         EQASSERT( !object || object->isAttached( ));
00227         (*this) << ObjectVersion( object );
00228         return *this;
00229     }
00230  
00233     template< typename T > inline DataOStream& 
00234     DataOStream::operator << ( const std::vector< T >& value )
00235     {
00236         const uint64_t nElems = value.size();
00237         (*this) << nElems;
00238         for( uint64_t i = 0; i < nElems; ++i )
00239             (*this) << value[i];
00240         return *this;
00241     }
00242  
00243     template< typename C > inline void
00244     DataOStream::serializeChildren( const std::vector<C*>& children )
00245     {
00246         const uint64_t nElems = children.size();
00247         (*this) << nElems;
00248 
00249         for( typename std::vector< C* >::const_iterator i = children.begin();
00250              i != children.end(); ++i )
00251         {
00252             C* child = *i;
00253             (*this) << ObjectVersion( child );
00254             EQASSERTINFO( !child || child->isAttached(),
00255                           "Found unmapped object during serialization" );
00256         }
00257     }
00261     template<> inline DataOStream& 
00262     DataOStream::operator << ( const std::vector< uint8_t >& value )
00263     { return _writeFlatVector( value ); }
00264 
00266     template<> inline DataOStream& 
00267     DataOStream::operator << ( const std::vector< uint16_t >& value )
00268     { return _writeFlatVector( value ); }
00269 
00271     template<> inline DataOStream&
00272     DataOStream::operator << ( const std::vector< int16_t >& value )
00273     { return _writeFlatVector( value ); }
00274 
00276     template<> inline DataOStream& 
00277     DataOStream::operator << ( const std::vector< uint32_t >& value )
00278     { return _writeFlatVector( value ); }
00279 
00281     template<> inline DataOStream&
00282     DataOStream::operator << ( const std::vector< int32_t >& value )
00283     { return _writeFlatVector( value ); }
00284 
00286     template<> inline DataOStream&
00287     DataOStream::operator << ( const std::vector< uint64_t >& value )
00288     { return _writeFlatVector( value ); }
00289 
00291     template<> inline DataOStream&
00292     DataOStream::operator << ( const std::vector< int64_t >& value )
00293     { return _writeFlatVector( value ); }
00294 
00296     template<> inline DataOStream&
00297     DataOStream::operator << ( const std::vector< float >& value )
00298     { return _writeFlatVector( value ); }
00299 
00301     template<> inline DataOStream&
00302     DataOStream::operator << ( const std::vector< double >& value )
00303     { return _writeFlatVector( value ); }
00304 
00306     template<> inline DataOStream&
00307     DataOStream::operator << ( const std::vector< ObjectVersion >& value )
00308     { return _writeFlatVector( value ); }
00310 }
00311 #endif //CO_DATAOSTREAM_H
Generated on Fri Jun 8 2012 15:44:30 for Equalizer 1.2.1 by  doxygen 1.8.0