Permalink
Browse files

Implement #111: Support for custom command handler registration

[ ] May break build
[ ] May break existing applications (see CHANGES.txt)
[ ] Bugfix
[x] New Feature
[ ] Cleanup
[ ] Optimization
[ ] Documentation
  • Loading branch information...
1 parent 97efd20 commit 125567587e8c6e4fbe5f1f4ac149a94b268726e8 @tribal-tec tribal-tec committed Apr 13, 2012
Showing with 88 additions and 3 deletions.
  1. +2 −0 libs/co/commands.h
  2. +1 −1 libs/co/files.cmake
  3. +55 −0 libs/co/localNode.cpp
  4. +19 −1 libs/co/localNode.h
  5. +11 −1 libs/co/nodePackets.h
View
@@ -60,6 +60,8 @@ namespace co
CMD_NODE_OBJECT_PUSH,
CMD_NODE_PING,
CMD_NODE_PING_REPLY,
+ CMD_NODE_UUID_COMMAND_RCV,
+ CMD_NODE_UUID_COMMAND_CMD,
CMD_NODE_CUSTOM = 40 // some buffer for binary-compatible patches
};
View
@@ -33,6 +33,7 @@ set(CO_PUBLIC_HEADERS
localNode.h
log.h
node.h
+ nodePackets.h
nodeType.h
object.h
objectFactory.h
@@ -62,7 +63,6 @@ set(CO_HEADERS
eventConnection.h
fullMasterCM.h
masterCM.h
- nodePackets.h
nullCM.h
objectCM.h
objectDataIStream.h
View
@@ -56,6 +56,8 @@ typedef stde::hash_map< uint128_t, NodePtr > NodeHash;
typedef NodeHash::const_iterator NodeHashCIter;
typedef stde::hash_map< uint128_t, LocalNode::PushHandler > HandlerHash;
typedef HandlerHash::const_iterator HandlerHashCIter;
+typedef std::pair< LocalNode::CommandHandler, CommandQueue* > CommandPair;
+typedef stde::hash_map< uint128_t, CommandPair > FunctionTable;
}
namespace detail
@@ -159,6 +161,8 @@ class LocalNode
ReceiverThread* receiverThread;
CommandThread* commandThread;
+
+ FunctionTable fTable;
};
}
@@ -208,6 +212,10 @@ LocalNode::LocalNode( )
CmdFunc( this, &LocalNode::_cmdPing ), queue );
registerCommand( CMD_NODE_PING_REPLY,
CmdFunc( this, &LocalNode::_cmdDiscard ), 0 );
+ registerCommand( CMD_NODE_UUID_COMMAND_RCV,
+ CmdFunc( this, &LocalNode::_cmdUUIDRcv ), 0 );
+ registerCommand( CMD_NODE_UUID_COMMAND_CMD,
+ CmdFunc( this, &LocalNode::_cmdUUIDCmd ), queue );
}
LocalNode::~LocalNode( )
@@ -772,6 +780,18 @@ void LocalNode::registerPushHandler( const uint128_t& groupID,
(*_impl->pushHandlers)[ groupID ] = handler;
}
+bool LocalNode::registerCustomCommand( const uint128_t& command,
+ const CommandHandler& func,
+ CommandQueue* queue )
+{
+ if( _impl->fTable.find( command ) != _impl->fTable.end( ))
+ return false;
+
+ _impl->fTable.insert( std::make_pair( command,
+ std::make_pair( func, queue )));
+ return true;
+}
+
LocalNode::SendToken LocalNode::acquireSendToken( NodePtr node )
{
LBASSERT( !inCommandThread( ));
@@ -1934,4 +1954,39 @@ bool LocalNode::_cmdPing( Command& command )
return true;
}
+bool LocalNode::_cmdUUIDRcv( Command& command )
+{
+ UUIDPacket* packet = command.getModifiable< UUIDPacket >();
+
+ FunctionTable::const_iterator i = _impl->fTable.find( packet->custom );
+ if( i == _impl->fTable.end( ))
+ return false;
+
+ if( i->second.second )
+ {
+ packet->command = CMD_NODE_UUID_COMMAND_CMD;
+ dispatchCommand( command );
+ return true;
+ }
+ // else
+
+ const CommandHandler& func = i->second.first;
+ LBCHECK( func( command ));
+
+ return true;
+}
+
+bool LocalNode::_cmdUUIDCmd( Command& command )
+{
+ const UUIDPacket* packet = command.get< UUIDPacket >();
+
+ FunctionTable::const_iterator i = _impl->fTable.find( packet->custom );
+ if( i == _impl->fTable.end( ))
+ return false;
+
+ const CommandHandler& func = i->second.first;
+ LBCHECK( func( command ));
+ return true;
+}
+
}
View
@@ -24,6 +24,7 @@
#include <co/objectHandler.h> // base class
#include <co/objectVersion.h> // VERSION_FOO used inline
#include <lunchbox/requestHandler.h> // base class
+#include <boost/function/function1.hpp>
#include <boost/function/function4.hpp>
namespace co
@@ -45,12 +46,15 @@ namespace detail { class LocalNode; class ReceiverThread; class CommandThread; }
typedef NodePtr SendToken; //!< An acquired send token
- /** Function signature for push handlers*/
+ /** Function signature for push handlers. */
typedef boost::function< void( const uint128_t&, //!< groupID
const uint128_t&, //!< objectType
const uint128_t&, //!< objectID
DataIStream& ) > PushHandler;
+ /** Function signature for custom command handlers. */
+ typedef boost::function< bool( Command& ) > CommandHandler;
+
/**
* @name State Changes
*
@@ -300,6 +304,18 @@ namespace detail { class LocalNode; class ReceiverThread; class CommandThread; }
CO_API void registerPushHandler( const uint128_t& groupID,
const PushHandler& handler );
+ /**
+ * Register a custom command handler handled by this node.
+ *
+ * @param command the unique identifier of the custom command
+ * @param func the handler function for the custom command
+ * @param queue the queue where the command should be inserted to
+ * @return true on successful registering, false otherwise
+ */
+ CO_API bool registerCustomCommand( const uint128_t& command,
+ const CommandHandler& func,
+ CommandQueue* queue );
+
/** @internal swap the existing object by a new object and keep
the cm, id and instanceID. */
CO_API void swapObject( Object* oldObject, Object* newObject );
@@ -466,6 +482,8 @@ namespace detail { class LocalNode; class ReceiverThread; class CommandThread; }
bool _cmdAddListener( Command& command );
bool _cmdRemoveListener( Command& command );
bool _cmdPing( Command& command );
+ bool _cmdUUIDRcv( Command& command );
+ bool _cmdUUIDCmd( Command& command );
bool _cmdDiscard( Command& ) { return true; }
//@}
View
@@ -459,7 +459,17 @@ namespace co
command = CMD_NODE_PING_REPLY;
size = sizeof( NodePingReplyPacket );
}
- };
+ };
+
+ struct UUIDPacket : public NodePacket
+ {
+ UUIDPacket()
+ {
+ command = CMD_NODE_UUID_COMMAND_RCV;
+ size = sizeof( UUIDPacket );
+ }
+ uint128_t custom;
+ };
//------------------------------------------------------------
inline std::ostream& operator << ( std::ostream& os,

0 comments on commit 1255675

Please sign in to comment.