ctkEventAdminService.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "ctkEventAdminService_p.h"
  16. #include "handler/ctkEASlotHandler_p.h"
  17. #include <ctkPluginConstants.h>
  18. ctkEventAdminService::ctkEventAdminService(ctkPluginContext* context,
  19. HandlerTasksInterface* managers,
  20. ctkEADefaultThreadPool* syncPool,
  21. ctkEADefaultThreadPool* asyncPool,
  22. int timeout,
  23. const QStringList& ignoreTimeout)
  24. : impl(managers, syncPool, asyncPool, timeout, ignoreTimeout),
  25. context(context)
  26. {
  27. }
  28. ctkEventAdminService::~ctkEventAdminService()
  29. {
  30. qDeleteAll(slotHandler);
  31. }
  32. void ctkEventAdminService::postEvent(const ctkEvent& event)
  33. {
  34. impl.postEvent(event);
  35. }
  36. void ctkEventAdminService::sendEvent(const ctkEvent& event)
  37. {
  38. impl.sendEvent(event);
  39. }
  40. void ctkEventAdminService::publishSignal(const QObject* publisher, const char* signal,
  41. const QString& topic,
  42. Qt::ConnectionType type)
  43. {
  44. if (topic.isEmpty())
  45. {
  46. throw std::invalid_argument("topic must not be empty");
  47. }
  48. // check if the signal was already registered under the given topic
  49. if (signalPublisher.contains(publisher))
  50. {
  51. const QList<ctkEASignalPublisher*>& signalPublishers = signalPublisher[publisher];
  52. for(int i = 0; i < signalPublishers.size(); ++i)
  53. {
  54. if (signalPublishers[i]->getSignalName() == signal &&
  55. signalPublishers[i]->getTopicName() == topic)
  56. {
  57. return;
  58. }
  59. }
  60. }
  61. QList<ctkEASignalPublisher*>& signalList = signalPublisher[publisher];
  62. signalList.push_back(new ctkEASignalPublisher(this, signal, topic));
  63. if (type == Qt::DirectConnection)
  64. {
  65. connect(publisher, signal, signalList.back(), SLOT(publishSyncSignal(ctkDictionary)), Qt::DirectConnection);
  66. }
  67. else if (type == Qt::QueuedConnection)
  68. {
  69. connect(publisher, signal, signalList.back(), SLOT(publishAsyncSignal(ctkDictionary)), Qt::DirectConnection);
  70. }
  71. else
  72. {
  73. throw std::invalid_argument("type must be either Qt::DirectConnection or Qt::QueuedConnection");
  74. }
  75. }
  76. void ctkEventAdminService::unpublishSignal(const QObject* publisher, const char* signal,
  77. const QString& topic)
  78. {
  79. if (!signalPublisher.contains(publisher)) return;
  80. if (signal == 0 && topic.isEmpty())
  81. {
  82. // unpublish everything from the given publisher
  83. // this automatically disconnects signals
  84. qDeleteAll(signalPublisher.take(publisher));
  85. }
  86. else
  87. {
  88. QList<ctkEASignalPublisher*>& list = signalPublisher[publisher];
  89. if (signal == 0)
  90. {
  91. for (int i = 0; i < list.size(); )
  92. {
  93. if (list[i]->getTopicName() == topic)
  94. {
  95. // this automatically disconnects the signals
  96. delete list.takeAt(i);
  97. }
  98. else
  99. {
  100. ++i;
  101. }
  102. }
  103. }
  104. else {
  105. for (int i = 0; i < list.size(); )
  106. {
  107. if (list[i]->getSignalName() == signal)
  108. {
  109. if (topic.isEmpty() || list[i]->getTopicName() == topic)
  110. {
  111. // this automatically disconnects the signals
  112. delete list.takeAt(i);
  113. }
  114. }
  115. else
  116. {
  117. ++i;
  118. }
  119. }
  120. }
  121. if (list.isEmpty())
  122. {
  123. signalPublisher.remove(publisher);
  124. }
  125. }
  126. }
  127. qlonglong ctkEventAdminService::subscribeSlot(const QObject* subscriber, const char* member, const ctkDictionary& properties)
  128. {
  129. ctkEASlotHandler* handler = new ctkEASlotHandler();
  130. connect(handler, SIGNAL(eventOccured(ctkEvent)), subscriber, member, Qt::DirectConnection);
  131. ctkServiceRegistration reg = context->registerService<ctkEventHandler>(handler, properties);
  132. handler->reg = reg;
  133. qlonglong id = reg.getReference().getProperty(ctkPluginConstants::SERVICE_ID).toLongLong();
  134. slotHandler.insert(id, handler);
  135. return id;
  136. }
  137. void ctkEventAdminService::unsubscribeSlot(qlonglong subscriptionId)
  138. {
  139. ctkEASlotHandler* handler = slotHandler.take(subscriptionId);
  140. if (handler)
  141. {
  142. handler->reg.unregister();
  143. delete handler;
  144. }
  145. }
  146. bool ctkEventAdminService::updateProperties(qlonglong subscriptionId, const ctkDictionary& properties)
  147. {
  148. ctkEASlotHandler* handler = slotHandler.value(subscriptionId);
  149. if (handler)
  150. {
  151. handler->updateProperties(properties);
  152. return true;
  153. }
  154. return false;
  155. }
  156. void ctkEventAdminService::stop()
  157. {
  158. impl.stop();
  159. }
  160. void ctkEventAdminService::update(HandlerTasksInterface* managers, int timeout,
  161. const QStringList& ignoreTimeout)
  162. {
  163. impl.update(managers, timeout, ignoreTimeout);
  164. }