Collage  1.5.0
High-performance C++ library for developing object-oriented distributed applications.
localNode.h
1 
2 /* Copyright (c) 2005-2016, Stefan Eilemann <eile@equalizergraphics.com>
3  * Cedric Stalder <cedric.stalder@gmail.com>
4  * Daniel Nachbaur <danielnachbaur@gmail.com>
5  *
6  * This file is part of Collage <https://github.com/Eyescale/Collage>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 2.1 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifndef CO_LOCALNODE_H
23 #define CO_LOCALNODE_H
24 
25 #include <co/node.h> // base class
26 #include <co/objectHandler.h> // base class
27 #include <co/objectVersion.h> // VERSION_FOO used inline
28 #include <lunchbox/requestHandler.h> // base class
29 
30 #include <boost/function/function1.hpp>
31 #include <boost/function/function4.hpp>
32 
33 namespace co
34 {
35 namespace detail { class LocalNode; class ReceiverThread; class CommandThread; }
36 
44 class LocalNode : public lunchbox::RequestHandler, public Node,
45  public ObjectHandler
46 {
47 public:
52  enum Counter
53  {
55  COUNTER_ALL // must be last
56  };
57 
59  CO_API explicit LocalNode( const uint32_t type = co::NODETYPE_NODE );
60 
91  CO_API virtual bool initLocal( const int argc, char** argv );
92 
105  CO_API virtual bool listen();
106 
116  CO_API virtual bool close();
117 
119  virtual bool exitLocal() { return close(); }
120 
137  CO_API bool connect( NodePtr node );
138 
151  CO_API NodePtr connect( const NodeID& nodeID );
152 
165  CO_API NodePtr connectObjectMaster( const uint128_t& id );
166 
174  CO_API virtual bool disconnect( NodePtr node );
176 
195  CO_API bool launch( NodePtr node, const std::string& command );
196 
202  CO_API NodePtr syncLaunch( const uint128_t& nodeID, int64_t timeout );
204 
219  CO_API bool registerObject( Object* object ) override;
220 
230  CO_API void deregisterObject( Object* object ) override;
231 
276  CO_API f_bool_t mapObject( Object* object, const uint128_t& id,
277  NodePtr master,
278  const uint128_t& version = VERSION_OLDEST );
279 
281  f_bool_t mapObject( Object* object, const ObjectVersion& v )
282  { return mapObject( object, v.identifier, 0, v.version ); }
283 
285  f_bool_t mapObject( Object* object, const uint128_t& id,
286  const uint128_t& version = VERSION_OLDEST )
287  { return mapObject( object, id, 0, version ); }
288 
290  CO_API uint32_t mapObjectNB( Object* object, const uint128_t& id,
291  const uint128_t& version = VERSION_OLDEST );
292 
294  CO_API uint32_t mapObjectNB( Object* object, const uint128_t& id,
295  const uint128_t& version,
296  NodePtr master ) override;
297 
299  CO_API bool mapObjectSync( const uint32_t requestID ) override;
300 
321  CO_API f_bool_t syncObject( Object* object, const uint128_t& id,
322  NodePtr master,
323  const uint32_t instanceID = CO_INSTANCE_ALL ) override;
330  CO_API void unmapObject( Object* object ) override;
331 
333  CO_API void disableInstanceCache();
334 
336  CO_API void expireInstanceData( const int64_t age );
337 
355  CO_API void enableSendOnRegister();
356 
358  CO_API void disableSendOnRegister();
359 
380  CO_API virtual void objectPush( const uint128_t& groupID,
381  const uint128_t& objectType,
382  const uint128_t& objectID,
383  DataIStream& istream );
384 
386  typedef boost::function< void( const uint128_t&,
387  const uint128_t&,
388  const uint128_t&,
400  CO_API void registerPushHandler( const uint128_t& groupID,
401  const PushHandler& handler );
402 
403 
405  typedef boost::function< bool( CustomICommand& ) > CommandHandler;
406 
421  CO_API bool registerCommandHandler( const uint128_t& command,
422  const CommandHandler& func,
423  CommandQueue* queue );
424 
427  CO_API void swapObject( Object* oldObject, Object* newObject );
429 
441  CO_API NodePtr getNode( const NodeID& id ) const;
442 
444  CO_API Nodes getNodes( const bool addSelf = true ) const;
445 
448 
454  CO_API bool inCommandThread() const;
455 
456  CO_API int64_t getTime64() const;
457  CO_API ssize_t getCounter( const Counter counter ) const;
458 
459 
467 
469  CO_API void addListener( ConnectionPtr connection );
470 
472  CO_API void removeListeners( const Connections& connections );
473 
480  CO_API void flushCommands();
481 
483  CO_API BufferPtr allocBuffer( const uint64_t size );
484 
496  CO_API bool dispatchCommand( ICommand& command ) override;
497 
498 
500  typedef lunchbox::RefPtr< co::SendToken > SendToken;
501 
510  CO_API SendToken acquireSendToken( NodePtr toNode );
511 
513  CO_API void releaseSendToken( SendToken token );
514 
516  CO_API Zeroconf getZeroconf();
518 
520  CO_API void ackRequest( NodePtr node, const uint32_t requestID );
521 
523  CO_API void ping( NodePtr remoteNode );
524 
530  CO_API bool pingIdleNodes();
531 
536  CO_API void setAffinity( const int32_t affinity );
537 
539  CO_API void addConnection( ConnectionPtr connection );
540 
541 protected:
543  CO_API ~LocalNode() override;
544 
557  CO_API bool connect( NodePtr node, ConnectionPtr connection );
558 
560  virtual void notifyConnect( NodePtr ) {}
561 
563  virtual void notifyDisconnect( NodePtr ) {}
564 
573  CO_API virtual NodePtr createNode( const uint32_t type );
574 
575 private:
576  detail::LocalNode* const _impl;
577 
578  friend class detail::ReceiverThread;
579  bool _startCommandThread( const int32_t threadID );
580  void _runReceiverThread();
581 
582  friend class detail::CommandThread;
583  bool _notifyCommandThreadIdle();
584 
585  void _cleanup();
586  void _closeNode( NodePtr node );
587  void _addConnection( ConnectionPtr connection );
588  void _removeConnection( ConnectionPtr connection );
589 
590  lunchbox::Request< void > _removeListener( ConnectionPtr connection );
591 
592  uint32_t _connect( NodePtr node );
593  NodePtr _connect( const NodeID& nodeID );
594  uint32_t _connect( NodePtr node, ConnectionPtr connection );
595  NodePtr _connect( const NodeID& nodeID, NodePtr peer );
596  NodePtr _connectFromZeroconf( const NodeID& nodeID );
597  bool _connectSelf();
598 
599  bool _setupPeer( const std::string& setupOpts );
600 
601  void _handleConnect();
602  void _handleDisconnect();
603  bool _handleData();
604  BufferPtr _readHead( ConnectionPtr connection );
605  ICommand _setupCommand( ConnectionPtr, ConstBufferPtr );
606  bool _readTail( ICommand&, BufferPtr, ConnectionPtr );
607  void _initService();
608  void _exitService();
609 
610  friend class ObjectStore;
611  template< typename T >
612  void _registerCommand( const uint32_t command, const CommandFunc< T >& func,
613  CommandQueue* destinationQueue )
614  {
615  registerCommand( command, func, destinationQueue );
616  }
617 
618  void _dispatchCommand( ICommand& command );
619  void _redispatchCommands();
620 
622  bool _cmdAckRequest( ICommand& command );
623  bool _cmdStopRcv( ICommand& command );
624  bool _cmdStopCmd( ICommand& command );
625  bool _cmdSetAffinity( ICommand& command );
626  bool _cmdConnect( ICommand& command );
627  bool _cmdConnectReply( ICommand& command );
628  bool _cmdConnectAck( ICommand& command );
629  bool _cmdID( ICommand& command );
630  bool _cmdDisconnect( ICommand& command );
631  bool _cmdGetNodeData( ICommand& command );
632  bool _cmdGetNodeDataReply( ICommand& command );
633  bool _cmdAcquireSendToken( ICommand& command );
634  bool _cmdAcquireSendTokenReply( ICommand& command );
635  bool _cmdReleaseSendToken( ICommand& command );
636  bool _cmdAddListener( ICommand& command );
637  bool _cmdRemoveListener( ICommand& command );
638  bool _cmdPing( ICommand& command );
639  bool _cmdCommand( ICommand& command );
640  bool _cmdCommandAsync( ICommand& command );
641  bool _cmdAddConnection( ICommand& command );
642  bool _cmdDiscard( ICommand& ) { return true; }
644 
645  LB_TS_VAR( _cmdThread )
646  LB_TS_VAR( _rcvThread )
647 };
648 
649 inline std::ostream& operator << ( std::ostream& os, const LocalNode& node )
650 {
651  os << static_cast< const Node& >( node );
652  return os;
653 }
654 }
655 #endif // CO_LOCALNODE_H
CO_API void deregisterObject(Object *object) override
Deregister a distributed object.
Num of mapObjects served for other nodes.
Definition: localNode.h:54
std::vector< ConnectionPtr > Connections
A vector of ConnectionPtr's.
Definition: types.h:124
CO_API bool connect(NodePtr node)
Connect a remote node (proxy) to this listening node.
CO_API Zeroconf getZeroconf()
lunchbox::RefPtr< co::SendToken > SendToken
A handle for a send token acquired by acquireSendToken().
Definition: localNode.h:500
lunchbox::RefPtr< ConnectionDescription > ConnectionDescriptionPtr
A reference pointer for ConnectionDescription pointers.
Definition: types.h:95
CO_API void unmapObject(Object *object) override
Unmap a mapped object.
uint128_t NodeID
A unique identifier for nodes.
Definition: types.h:82
A distributed object.
Definition: object.h:45
virtual CO_API void objectPush(const uint128_t &groupID, const uint128_t &objectType, const uint128_t &objectID, DataIStream &istream)
Handler for an Object::push() operation.
STL namespace.
virtual CO_API bool initLocal(const int argc, char **argv)
Initialize the node.
CO_API NodePtr syncLaunch(const uint128_t &nodeID, int64_t timeout)
Wait for a launched node to connect.
A helper struct bundling an object identifier and version.
Definition: objectVersion.h:47
CO_API void removeListeners(const Connections &connections)
Remove listening connections from this listening node.
A thread-safe, blocking queue for ICommand buffers.
Definition: commandQueue.h:33
CO_API bool dispatchCommand(ICommand &command) override
Dispatches a command to the registered command queue.
void registerCommand(const uint32_t command, const CommandFunc< T > &func, CommandQueue *queue)
Register a command member function for a command.
Definition: dispatcher.h:94
virtual CO_API NodePtr createNode(const uint32_t type)
Factory method to create a new node.
Proxy node representing a remote LocalNode.
Definition: node.h:42
f_bool_t mapObject(Object *object, const ObjectVersion &v)
Convenience wrapper for mapObject().
Definition: localNode.h:281
CO_API Nodes getNodes(const bool addSelf=true) const
CO_API void registerPushHandler(const uint128_t &groupID, const PushHandler &handler)
Register a custom handler for Object::push operations.
CO_API CommandQueue * getCommandThreadQueue()
Return the command queue to the command thread.
uint128_t identifier
the object identifier
Definition: objectVersion.h:95
CO_API void enableSendOnRegister()
Enable sending instance data after registration.
lunchbox::RefPtr< Node > NodePtr
A reference pointer for Node pointers.
Definition: types.h:85
f_bool_t mapObject(Object *object, const uint128_t &id, const uint128_t &version=VERSION_OLDEST)
Definition: localNode.h:285
Counter
Counters are monotonically increasing performance variables for operations performed by a LocalNode i...
Definition: localNode.h:52
Object-oriented network library.
Definition: barrier.h:27
virtual bool exitLocal()
Close a listening node.
Definition: localNode.h:119
CO_API LocalNode(const uint32_t type=co::NODETYPE_NODE)
Construct a new local node of the given type.
Interface for entities which map and register objects.
Definition: objectHandler.h:29
CO_API bool registerObject(Object *object) override
Register a distributed object.
boost::function< bool(CustomICommand &) > CommandHandler
Function signature for custom command handlers.
Definition: localNode.h:405
CO_API SendToken acquireSendToken(NodePtr toNode)
Acquire a send token from the given node.
virtual CO_API bool disconnect(NodePtr node)
Disconnect a connected node.
virtual CO_API bool close()
Close a listening node.
CO_API bool inCommandThread() const
A zeroconf communicator.
Definition: zeroconf.h:46
A class managing received commands.
Definition: iCommand.h:43
Node specialization for a local node.
Definition: localNode.h:44
CO_API bool launch(NodePtr node, const std::string &command)
Launch a remote process using the given command.
CO_API ~LocalNode() override
Destruct this local node.
uint128_t version
the object version
Definition: objectVersion.h:96
CO_API f_bool_t syncObject(Object *object, const uint128_t &id, NodePtr master, const uint32_t instanceID=CO_INSTANCE_ALL) override
Synchronize the local object with a remote object.
CO_API NodePtr connectObjectMaster(const uint128_t &id)
Find and connect the node where the given object is registered.
A std::istream-like input data stream for binary data.
Definition: dataIStream.h:41
CO_API void disableInstanceCache()
Disable the instance cache of a stopped local node.
CO_API ConnectionPtr addListener(ConnectionDescriptionPtr desc)
Add a listening connection to this listening node.
CO_API void disableSendOnRegister()
Disable sending data of newly registered objects.
CO_API bool registerCommandHandler(const uint128_t &command, const CommandHandler &func, CommandQueue *queue)
Register a custom command handler handled by this node.
boost::function< void(const uint128_t &, const uint128_t &, const uint128_t &, DataIStream &) > PushHandler
Function signature for push handlers.
Definition: localNode.h:389
CO_API void setAffinity(const int32_t affinity)
Bind this, the receiver and the command thread to the given lunchbox::Thread affinity.
CO_API bool pingIdleNodes()
Request updates from all nodes above keep-alive timeout.
std::vector< NodePtr > Nodes
A vector of NodePtr's.
Definition: types.h:103
A plain co::Node.
Definition: nodeType.h:31
CO_API void releaseSendToken(SendToken token)
CO_API NodePtr getNode(const NodeID &id) const
Get a node by identifier.
virtual CO_API bool listen()
Open all connections and put this node into the listening state.
CO_API void ping(NodePtr remoteNode)
Request keep-alive update from the remote node.
CO_API f_bool_t mapObject(Object *object, const uint128_t &id, NodePtr master, const uint128_t &version=VERSION_OLDEST)
Map a distributed object.
CO_API bool mapObjectSync(const uint32_t requestID) override
lunchbox::RefPtr< Connection > ConnectionPtr
A reference pointer for Connection pointers.
Definition: types.h:93
CO_API uint32_t mapObjectNB(Object *object, const uint128_t &id, const uint128_t &version=VERSION_OLDEST)