ctkEventBusManager.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * ctkEventBusManager.h
  3. * ctkEventBus
  4. *
  5. * Created by Paolo Quadrani on 27/03/09.
  6. * Copyright 2010 B3C. All rights reserved.
  7. *
  8. * See Licence at: http://tiny.cc/QXJ4D
  9. *
  10. */
  11. #ifndef CTKEVENTBUSMANAGER_H
  12. #define CTKEVENTBUSMANAGER_H
  13. // Includes list
  14. #include "ctkEventDefinitions.h"
  15. #include "ctkEventDispatcherLocal.h"
  16. #include "ctkEventDispatcherRemote.h"
  17. #include "ctkBusEvent.h"
  18. namespace ctkEventBus {
  19. // Class forwarding list
  20. /**
  21. Class name: ctkEventBusManager
  22. This singletone provides the access point of the Communication Bus for MAF3 framework.
  23. ctkEventBusManager defines also the mafId maf.local.eventBus.globalUpdate to be used as generic update notification mechanism that the event bus can send to all the observers.
  24. */
  25. class org_commontk_eventbus_EXPORT ctkEventBusManager : public QObject {
  26. Q_OBJECT
  27. public:
  28. /// Return an instance of the event bus.
  29. static ctkEventBusManager *instance();
  30. /// Add a new event property (observer or event) to the event bus hash.
  31. /** Return true if observer has beed added correctly, false otherwise.
  32. This method check before adding a new observer that it has not already been inserted into the events' Hash with the same id and callback signature.*/
  33. bool addEventProperty(ctkBusEvent &props) const;
  34. /// Remove the event property from the event bus hash.
  35. bool removeEventProperty(ctkBusEvent &props) const;
  36. /// Remove the object passed as argument from the observer's hash.
  37. /** This method allows to remove from the observer's hash the object
  38. passed as argument. If no topic is specified, the observer will be removed from all
  39. the topic it was listening, otherwise it will be disconnected only from the given topic.*/
  40. void removeObserver(const QObject *obj, const QString topic = "", bool qt_disconnect = true);
  41. /// Remove the object passed as argument from the signal emitter's hash.
  42. /** This method allows to remove from the signal emitter's hash the object
  43. passed as argument. If no topic is specified, the emitter will be removed from all
  44. the topic it was emitting signals, otherwise it will be removed only from the given topic.*/
  45. void removeSignal(const QObject *obj, QString topic = "", bool qt_disconnect = true);
  46. /// Notify events associated to the given id locally to the application.
  47. void notifyEvent(ctkBusEvent &event_dictionary, ctkEventArgumentsList *argList = NULL, ctkGenericReturnArgument *returnArg = NULL) const;
  48. /// Notify event associated to the given id locally to the application.
  49. void notifyEvent(const QString topic, ctkEventType ev_type = ctkEventTypeLocal, ctkEventArgumentsList *argList = NULL, ctkGenericReturnArgument *returnArg = NULL) const;
  50. /// Enable/Disable event logging to allow dumping events notification into the selected logging output stream.
  51. void enableEventLogging(bool enable = true);
  52. /// When logging is enabled, allows logging events releted to specific id (require a valid topic).
  53. void logEventTopic(const QString topic);
  54. /// When enabled, allows logging all events. It reset the value for m_LogEventId to -1 (the default)
  55. void logAllEvents();
  56. /// Destroy the singleton instance. To be called at the end of the application.
  57. void shutdown();
  58. /// initialize NetworkConnectors
  59. void initializeNetworkConnectors();
  60. /// Retrieve if the signal has been registered previously.
  61. bool isLocalSignalPresent(const QString topic) const;
  62. /// Plug a new network connector into the connector hash for the given network protocol (protocol eg. "XMLRPC") (connector_type eg. "ctkEventBus::ctkNetworkConnectorQXMLRPC").
  63. void plugNetworkConnector(const QString &protocol, ctkNetworkConnector *connector);
  64. /// Create the server for remote communication according to the given protocol and listen port.
  65. bool createServer(const QString &communication_protocol, unsigned int listen_port);
  66. /// Allow to start server listening.
  67. void startListen();
  68. /// Create the client for remote communication according to the given protocol, server host and port.
  69. bool createClient(const QString &communication_protocol, const QString &server_host, unsigned int port);
  70. public Q_SLOTS:
  71. /// Intercepts objects deletation and detach them from the event bus.
  72. void detachObjectFromBus();
  73. private:
  74. /// Object constructor.
  75. ctkEventBusManager();
  76. /// Object destructor.
  77. ~ctkEventBusManager();
  78. ctkEventDispatcherLocal *m_LocalDispatcher; ///< Dispatcher class which dispatches events locally to the application.
  79. ctkEventDispatcherRemote *m_RemoteDispatcher; ///< Dispatcher class dispatches events remotely to another applications or via network.
  80. bool m_EnableEventLogging; ///< Flag to enable/disable logging for event sent.
  81. QString m_LogEventTopic; ///< Store the current Event_Id to track through the logger.
  82. ctkNetworkConnectorHash m_NetworkConnectorHash; ///< Hash table used to store the association of network protocols and network connector types.
  83. bool m_SkipDetach; ///< lifesafe variable to avoid the detach from eventbus.
  84. };
  85. } // namespace ctkEventBus
  86. #endif // CTKEVENTBUSMANAGER