LCOV - code coverage report
Current view: top level - co - iCommand.cpp (source / functions) Hit Total Coverage
Test: Collage Lines: 78 101 77.2 %
Date: 2016-06-07 01:19:44 Functions: 26 32 81.2 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2006-2013, Stefan Eilemann <eile@equalizergraphics.com>
       3             :  *                    2012, Daniel Nachbaur <danielnachbaur@gmail.com>
       4             :  *
       5             :  * This file is part of Collage <https://github.com/Eyescale/Collage>
       6             :  *
       7             :  * This library is free software; you can redistribute it and/or modify it under
       8             :  * the terms of the GNU Lesser General Public License version 2.1 as published
       9             :  * by the Free Software Foundation.
      10             :  *
      11             :  * This library is distributed in the hope that it will be useful, but WITHOUT
      12             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      13             :  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
      14             :  * details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public License
      17             :  * along with this library; if not, write to the Free Software Foundation, Inc.,
      18             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      19             :  */
      20             : 
      21             : #include "iCommand.h"
      22             : 
      23             : #include "buffer.h"
      24             : #include "localNode.h"
      25             : #include "node.h"
      26             : #include <pression/plugins/compressorTypes.h>
      27             : 
      28             : namespace co
      29             : {
      30             : namespace detail
      31             : {
      32     6311520 : class ICommand
      33             : {
      34             : public:
      35     1214309 :     ICommand()
      36             :         : func( 0, 0 )
      37             :         , buffer( 0 )
      38             :         , size( 0 )
      39             :         , type( COMMANDTYPE_INVALID )
      40             :         , cmd( CMD_INVALID )
      41     1214309 :         , consumed( false )
      42     1214303 :     {}
      43             : 
      44      121567 :     ICommand( LocalNodePtr local_, NodePtr remote_, ConstBufferPtr buffer_ )
      45             :         : local( local_ )
      46             :         , remote( remote_ )
      47             :         , func( 0, 0 )
      48             :         , buffer( buffer_ )
      49             :         , size( 0 )
      50             :         , type( COMMANDTYPE_INVALID )
      51             :         , cmd( CMD_INVALID )
      52      121567 :         , consumed( false )
      53      121567 :     {}
      54             : 
      55      381735 :     void clear()
      56             :     {
      57      381735 :         *this = ICommand();
      58      381735 :     }
      59             : 
      60             :     LocalNodePtr local; //!< The node receiving the command
      61             :     NodePtr remote; //!< The node sending the command
      62             :     co::Dispatcher::Func func;
      63             :     ConstBufferPtr buffer;
      64             :     uint64_t size;
      65             :     uint32_t type;
      66             :     uint32_t cmd;
      67             :     bool consumed;
      68             : };
      69             : } // detail namespace
      70             : 
      71      832560 : ICommand::ICommand()
      72             :     : DataIStream( false )
      73      832560 :     , _impl( new detail::ICommand )
      74             : {
      75      832568 : }
      76             : 
      77      121567 : ICommand::ICommand( LocalNodePtr local, NodePtr remote, ConstBufferPtr buffer,
      78             :                     const bool swap_ )
      79             :     : DataIStream( swap_ )
      80      121567 :     , _impl( new detail::ICommand( local, remote, buffer ))
      81             : {
      82      121567 :     if( _impl->buffer )
      83             :     {
      84      121566 :         LBASSERT( buffer->getSize() >= sizeof( _impl->size ) +
      85             :                   sizeof( _impl->type ) + sizeof( _impl->cmd ));
      86             : 
      87      121566 :         *this >> _impl->size >> _impl->type >> _impl->cmd;
      88             :     }
      89      121567 : }
      90             : 
      91     1983401 : ICommand::ICommand( const ICommand& rhs )
      92             :     : DataIStream( rhs )
      93     1983401 :     , _impl( new detail::ICommand( *rhs._impl ))
      94             : {
      95     1983403 :     _impl->consumed = false;
      96     1983403 :     _skipHeader();
      97     1983402 : }
      98             : 
      99      641944 : ICommand& ICommand::operator = ( const ICommand& rhs )
     100             : {
     101      641944 :     if( this != &rhs )
     102             :     {
     103      641950 :         DataIStream::operator = ( rhs );
     104      641948 :         *_impl = *rhs._impl;
     105      641955 :         _impl->consumed = false;
     106      641955 :         _skipHeader();
     107             :     }
     108      641935 :     return *this;
     109             : }
     110             : 
     111     5859756 : ICommand::~ICommand()
     112             : {
     113     2922637 :     delete _impl;
     114     2936352 : }
     115             : 
     116      381735 : void ICommand::clear()
     117             : {
     118      381735 :     _impl->clear();
     119      381735 : }
     120             : 
     121     2620023 : void ICommand::_skipHeader()
     122             : {
     123             :     const size_t headerSize = sizeof( _impl->size ) + sizeof( _impl->type ) +
     124     2620023 :                               sizeof( _impl->cmd );
     125     2620023 :     if( isValid() && getRemainingBufferSize() >= headerSize )
     126     2591915 :         getRemainingBuffer( headerSize );
     127     2601793 : }
     128             : 
     129       72273 : uint32_t ICommand::getType() const
     130             : {
     131       72273 :     return _impl->type;
     132             : }
     133             : 
     134      715823 : uint32_t ICommand::getCommand() const
     135             : {
     136      715823 :     return _impl->cmd;
     137             : }
     138             : 
     139      654052 : uint64_t ICommand::getSize() const
     140             : {
     141      654052 :     return _impl->size;
     142             : }
     143             : 
     144       49425 : void ICommand::setType( const CommandType type )
     145             : {
     146       49425 :     _impl->type = type;
     147       49425 : }
     148             : 
     149       49555 : void ICommand::setCommand( const uint32_t cmd )
     150             : {
     151       49555 :     _impl->cmd = cmd;
     152       49555 : }
     153             : 
     154      693436 : void ICommand::setDispatchFunction( const Dispatcher::Func& func )
     155             : {
     156      693436 :     _impl->func = func;
     157      693436 : }
     158             : 
     159           0 : ConstBufferPtr ICommand::getBuffer() const
     160             : {
     161           0 :     LBASSERT( _impl->buffer );
     162           0 :     return _impl->buffer;
     163             : }
     164             : 
     165           0 : size_t ICommand::nRemainingBuffers() const
     166             : {
     167           0 :     return _impl->buffer ? 1 : 0;
     168             : }
     169             : 
     170           0 : uint128_t ICommand::getVersion() const
     171             : {
     172           0 :     return VERSION_NONE;
     173             : }
     174             : 
     175     2714014 : bool ICommand::getNextBuffer( uint32_t& compressor, uint32_t& nChunks,
     176             :                               const void** chunkData, uint64_t& size )
     177             : {
     178     2714014 :     if( _impl->consumed ) // 2nd call
     179           0 :         _impl->buffer = 0;
     180             : 
     181     2714014 :     if( !_impl->buffer )
     182           0 :         return false;
     183             : 
     184     2714763 :     _impl->consumed = true;
     185     2714763 :     *chunkData = _impl->buffer->getData();
     186     2711116 :     size = reinterpret_cast< const uint64_t* >( *chunkData )[ 0 ];
     187     2711116 :     compressor = EQ_COMPRESSOR_NONE;
     188     2711116 :     nChunks = 1;
     189     2711116 :     return true;
     190             : }
     191             : 
     192      221607 : NodePtr ICommand::getRemoteNode() const
     193             : {
     194      221607 :     return _impl->remote;
     195             : }
     196             : 
     197           0 : LocalNodePtr ICommand::getLocalNode() const
     198             : {
     199           0 :     return _impl->local;
     200             : }
     201             : 
     202     4426214 : bool ICommand::isValid() const
     203             : {
     204    13253164 :     return _impl->buffer && !_impl->buffer->isEmpty() &&
     205    17636796 :            _impl->type != COMMANDTYPE_INVALID && _impl->cmd != CMD_INVALID &&
     206     8808079 :            _impl->size > 0;
     207             : }
     208             : 
     209      693400 : bool ICommand::operator()()
     210             : {
     211      693400 :     LBASSERT( _impl->func.isValid( ));
     212      693396 :     Dispatcher::Func func = _impl->func;
     213      693396 :     _impl->func.clear();
     214      693395 :     return func( *this );
     215             : }
     216             : 
     217           0 : std::ostream& operator << ( std::ostream& os, const ICommand& command )
     218             : {
     219           0 :     ConstBufferPtr buffer = command.getBuffer();
     220           0 :     if( buffer )
     221           0 :         os << lunchbox::disableFlush << "command< type "
     222           0 :            << uint32_t( command.getType( )) << " cmd " << command.getCommand()
     223           0 :            << " size " << command.getSize() << '/' << buffer->getSize() << '/'
     224           0 :            << buffer->getMaxSize() << " from " << command.getNode() << " to "
     225           0 :            << command.getLocalNode() << " >" << lunchbox::enableFlush;
     226             :     else
     227           0 :         os << "command< empty >";
     228             : 
     229           0 :     if( command._impl->func.isValid( ))
     230           0 :         os << ' ' << command._impl->func << std::endl;
     231           0 :     return os;
     232             : }
     233          66 : }

Generated by: LCOV version 1.11