ctkCMEventDispatcher.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCMEventDispatcher_p.h"
  16. #include <service/log/ctkLogService.h>
  17. #include <service/cm/ctkConfigurationListener.h>
  18. #include <QRunnable>
  19. class _DispatchEventRunnable : public QRunnable
  20. {
  21. public:
  22. _DispatchEventRunnable(ctkServiceTracker<ctkConfigurationListener*>* tracker,
  23. ctkLogService* log, const ctkConfigurationEvent& event,
  24. const ctkServiceReference& ref)
  25. : tracker(tracker), log(log), event(event), ref(ref)
  26. {
  27. }
  28. void run()
  29. {
  30. ctkConfigurationListener* listener = tracker->getService(ref);
  31. if (listener == 0)
  32. {
  33. return;
  34. }
  35. try
  36. {
  37. listener->configurationEvent(event);
  38. }
  39. catch (const std::exception& e)
  40. {
  41. log->log(ctkLogService::LOG_ERROR, e.what());
  42. }
  43. catch (...)
  44. {
  45. log->log(ctkLogService::LOG_ERROR, "Unspecified exception");
  46. }
  47. }
  48. private:
  49. ctkServiceTracker<ctkConfigurationListener*>* tracker;
  50. ctkLogService* log;
  51. ctkConfigurationEvent event;
  52. ctkServiceReference ref;
  53. };
  54. ctkCMEventDispatcher::ctkCMEventDispatcher(ctkPluginContext* context, ctkLogService* log)
  55. : tracker(context), queue("ctkConfigurationListener Event Queue"), log(log)
  56. {
  57. }
  58. void ctkCMEventDispatcher::start()
  59. {
  60. tracker.open();
  61. }
  62. void ctkCMEventDispatcher::stop()
  63. {
  64. tracker.close();
  65. {
  66. QMutexLocker lock(&mutex);
  67. configAdminReference = 0;
  68. }
  69. }
  70. void ctkCMEventDispatcher::setServiceReference(const ctkServiceReference& reference)
  71. {
  72. QMutexLocker lock(&mutex);
  73. if (!configAdminReference)
  74. {
  75. configAdminReference = reference;
  76. }
  77. }
  78. void ctkCMEventDispatcher::dispatchEvent(ctkConfigurationEvent::Type type, const QString& factoryPid, const QString& pid)
  79. {
  80. const ctkConfigurationEvent event = createConfigurationEvent(type, factoryPid, pid);
  81. if (event.isNull())
  82. return;
  83. QList<ctkServiceReference> refs = tracker.getServiceReferences();
  84. foreach (ctkServiceReference ref, refs)
  85. {
  86. queue.put(new _DispatchEventRunnable(&tracker, log, event, ref));
  87. }
  88. }
  89. ctkConfigurationEvent ctkCMEventDispatcher::createConfigurationEvent(ctkConfigurationEvent::Type type, const QString& factoryPid, const QString& pid)
  90. {
  91. if (!configAdminReference)
  92. {
  93. return ctkConfigurationEvent();
  94. }
  95. return ctkConfigurationEvent(configAdminReference, type, factoryPid, pid);
  96. }