00001
00002
00003
00004
00005 #include "base.h"
00006
00007 #include "command.h"
00008 #include "commandQueue.h"
00009 #include "node.h"
00010 #include "packets.h"
00011
00012 #include <eq/base/log.h>
00013
00014 using namespace eq::base;
00015 using namespace std;
00016
00017 namespace eq
00018 {
00019 namespace net
00020 {
00021
00022
00023
00024
00025 void Base::_registerCommand( const uint32_t command,
00026 const CommandFunc<Base>& func,
00027 CommandQueue* destinationQueue )
00028 {
00029 EQASSERT( _vTable.size() == _qTable.size( ));
00030
00031 if( _vTable.size() <= command )
00032 {
00033 while( _vTable.size() < command )
00034 {
00035 _vTable.push_back( CommandFunc<Base>( this, &Base::_cmdUnknown ));
00036 _qTable.push_back( 0 );
00037 }
00038
00039 _vTable.push_back( func );
00040 _qTable.push_back( destinationQueue );
00041
00042 EQASSERT( _vTable.size() == command + 1 );
00043 }
00044 else
00045 {
00046 _vTable[command] = func;
00047 _qTable[command] = destinationQueue;
00048 }
00049 }
00050
00051
00052 bool Base::dispatchCommand( Command& command )
00053 {
00054 EQVERB << "dispatch " << static_cast< ObjectPacket* >( command.getPacket( ))
00055 << ", " << typeid( *this ).name() << endl;
00056
00057 const uint32_t which = command->command;
00058 #ifndef NDEBUG
00059 if( which >= _qTable.size( ))
00060 {
00061 EQASSERTINFO( 0, "Command " << which
00062 << " higher than number of registered command handlers ("
00063 << _qTable.size() << ") for object of type "
00064 << typeid(*this).name() << endl );
00065 return false;
00066 }
00067 EQASSERT( !command.isDispatched( ));
00068 #endif
00069
00070 CommandQueue* queue = _qTable[which];
00071 if( queue )
00072 queue->push( command );
00073 else
00074 {
00075 #ifdef NDEBUG // OPT
00076 _vTable[which]( command );
00077 #else
00078 const CommandResult result = _vTable[which]( command );
00079 EQASSERT( result == COMMAND_HANDLED );
00080 #endif
00081 }
00082
00083 return true;
00084 }
00085
00086 CommandResult Base::invokeCommand( Command& command )
00087 {
00088 const uint32_t which = command->command;
00089 #ifndef NDEBUG
00090 if( which >= _vTable.size( ))
00091 {
00092 EQERROR << "Command " << which
00093 << " higher than number of registered command handlers ("
00094 << _vTable.size() << ") for object of type "
00095 << typeid(*this).name() << endl;
00096 return COMMAND_ERROR;
00097 }
00098 EQASSERT( command.isDispatched( ));
00099 #endif
00100 return _vTable[which]( command );
00101 }
00102
00103 CommandResult Base::_cmdUnknown( Command& command )
00104 {
00105 EQERROR << "Unknown " << command << " for " << typeid(*this).name()
00106 << " @" << static_cast< void* >( this ) << endl;
00107 return COMMAND_ERROR;
00108 }
00109
00110 }
00111 }