dispatcher.cpp

00001 
00002 /* Copyright (c) 2005-2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #include "dispatcher.h"
00019 
00020 #include "command.h"
00021 #include "commandQueue.h"
00022 #include "node.h"
00023 #include "packets.h"
00024 
00025 #include <eq/base/log.h>
00026 
00027 using namespace std;
00028 
00029 namespace eq
00030 {
00031 namespace net
00032 {
00033 
00034 Dispatcher::Dispatcher()
00035 {}
00036 
00037 Dispatcher::Dispatcher( const Dispatcher& from )
00038 {}
00039 
00040 Dispatcher::~Dispatcher()
00041 {
00042 }
00043 
00044 //===========================================================================
00045 // command handling
00046 //===========================================================================
00047 void Dispatcher::_registerCommand( const uint32_t command,
00048                                    const CommandFunc< Dispatcher >& func,
00049                                    CommandQueue* destinationQueue )
00050 {
00051     EQASSERT( _vTable.size() == _qTable.size( ));
00052 
00053     if( _vTable.size() <= command )
00054     {
00055         while( _vTable.size() < command )
00056         {
00057             _vTable.push_back( 
00058                 CommandFunc< Dispatcher >( this, &Dispatcher::_cmdUnknown ));
00059             _qTable.push_back( 0 );
00060         }
00061 
00062         _vTable.push_back( func );
00063         _qTable.push_back( destinationQueue );
00064 
00065         EQASSERT( _vTable.size() == command + 1 );
00066     }
00067     else
00068     {
00069         _vTable[command] = func;
00070         _qTable[command] = destinationQueue;
00071     }
00072 }
00073 
00074 
00075 bool Dispatcher::dispatchCommand( Command& command )
00076 {
00077     EQVERB << "dispatch " << static_cast< ObjectPacket* >( command.getPacket( ))
00078            << ", " << typeid( *this ).name() << endl;
00079 
00080     const uint32_t which = command->command;
00081 #ifndef NDEBUG
00082     if( which >= _qTable.size( ))
00083     {
00084         EQABORT( "Command " << which
00085                  << " higher than number of registered command handlers ("
00086                  << _qTable.size() << ") for object of type "
00087                  << typeid(*this).name() << endl );
00088         return false;
00089     }
00090 #endif
00091 
00092     CommandQueue* queue = _qTable[which];
00093     if( queue )
00094         queue->push( command );
00095     else
00096     {
00097 #ifdef NDEBUG // OPT
00098         _vTable[which]( command );
00099 #else
00100         const CommandResult result = _vTable[which]( command );
00101 #  ifdef EQ_SEND_TOKEN
00102         return( result == COMMAND_HANDLED );
00103 #  else
00104         EQASSERTINFO( result == COMMAND_HANDLED, result );
00105 #  endif
00106 #endif
00107     }
00108 
00109     return true;
00110 }
00111 
00112 CommandResult Dispatcher::invokeCommand( Command& command )
00113 {
00114     const uint32_t which = command->command;
00115 #ifndef NDEBUG
00116     if( which >= _vTable.size( ))
00117     {
00118         EQERROR << "Command " << which
00119                 << " higher than number of registered command handlers ("
00120                 << _vTable.size() << ") for object of type "
00121                 << typeid(*this).name() << endl;
00122         return COMMAND_ERROR;
00123     }
00124 #endif
00125     return _vTable[which]( command );
00126 }
00127 
00128 CommandResult Dispatcher::_cmdUnknown( Command& command )
00129 {
00130     EQERROR << "Unknown " << command << " for " << typeid(*this).name()
00131             << " @" << static_cast< void* >( this ) << endl;
00132     return COMMAND_ERROR;
00133 }
00134 
00135 }
00136 }
Generated on Mon Aug 10 18:58:32 2009 for Equalizer 0.9 by  doxygen 1.5.8