ctkServiceRegistration.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "ctkServiceRegistration.h"
  16. #include "ctkServiceRegistrationPrivate.h"
  17. #include "ctkPluginFrameworkContext_p.h"
  18. #include "ctkPluginPrivate_p.h"
  19. #include "ctkPluginConstants.h"
  20. #include "ctkServices_p.h"
  21. #include "ctkServiceFactory.h"
  22. #include "ctkServiceSlotEntry_p.h"
  23. #include <QMutex>
  24. #include <stdexcept>
  25. ctkServiceRegistration::ctkServiceRegistration(ctkPluginPrivate* plugin, QObject* service,
  26. const ServiceProperties& props)
  27. : d_ptr(new ctkServiceRegistrationPrivate(this, plugin, service, props))
  28. {
  29. }
  30. ctkServiceRegistration::ctkServiceRegistration(ctkServiceRegistrationPrivate& dd)
  31. : d_ptr(&dd)
  32. {
  33. }
  34. ctkServiceReference ctkServiceRegistration::getReference() const
  35. {
  36. Q_D(const ctkServiceRegistration);
  37. if (!d->available) throw std::logic_error("Service is unregistered");
  38. return d->reference;
  39. }
  40. void ctkServiceRegistration::setProperties(const ServiceProperties& props)
  41. {
  42. Q_D(ctkServiceRegistration);
  43. QMutexLocker lock(&d->eventLock);
  44. QSet<ctkServiceSlotEntry> before;
  45. // TBD, optimize the locking of services
  46. {
  47. QMutexLocker lock2(&d->plugin->fwCtx->globalFwLock);
  48. QMutexLocker lock3(&d->propsLock);
  49. if (d->available)
  50. {
  51. // NYI! Optimize the MODIFIED_ENDMATCH code
  52. int old_rank = d->properties.value(ctkPluginConstants::SERVICE_RANKING).toInt();
  53. before = d->plugin->fwCtx->listeners.getMatchingServiceSlots(d->reference);
  54. QStringList classes = d->properties.value(ctkPluginConstants::OBJECTCLASS).toStringList();
  55. qlonglong sid = d->properties.value(ctkPluginConstants::SERVICE_ID).toLongLong();
  56. d->properties = ctkServices::createServiceProperties(props, classes, sid);
  57. int new_rank = d->properties.value(ctkPluginConstants::SERVICE_RANKING).toInt();
  58. if (old_rank != new_rank)
  59. {
  60. d->plugin->fwCtx->services->updateServiceRegistrationOrder(this, classes);
  61. }
  62. }
  63. else
  64. {
  65. throw std::logic_error("Service is unregistered");
  66. }
  67. }
  68. d->plugin->fwCtx->listeners.serviceChanged(
  69. d->plugin->fwCtx->listeners.getMatchingServiceSlots(d->reference),
  70. ctkServiceEvent(ctkServiceEvent::MODIFIED, d->reference), before);
  71. d->plugin->fwCtx->listeners.serviceChanged(
  72. before,
  73. ctkServiceEvent(ctkServiceEvent::MODIFIED_ENDMATCH, d->reference));
  74. }
  75. void ctkServiceRegistration::unregister()
  76. {
  77. Q_D(ctkServiceRegistration);
  78. if (d->unregistering) return; // Silently ignore redundant unregistration.
  79. {
  80. QMutexLocker lock(&d->eventLock);
  81. if (d->unregistering) return;
  82. d->unregistering = true;
  83. if (d->available)
  84. {
  85. if (d->plugin)
  86. {
  87. d->plugin->fwCtx->services->removeServiceRegistration(this);
  88. }
  89. }
  90. else
  91. {
  92. throw std::logic_error("Service is unregistered");
  93. }
  94. }
  95. if (d->plugin)
  96. {
  97. d->plugin->fwCtx->listeners.serviceChanged(
  98. d->plugin->fwCtx->listeners.getMatchingServiceSlots(d->reference),
  99. ctkServiceEvent(ctkServiceEvent::UNREGISTERING, d->reference));
  100. }
  101. {
  102. QMutexLocker lock(&d->eventLock);
  103. {
  104. QMutexLocker lock2(&d->propsLock);
  105. d->available = false;
  106. if (d->plugin)
  107. {
  108. for (QHashIterator<ctkPlugin*, QObject*> i(d->serviceInstances); i.hasNext();)
  109. {
  110. QObject* obj = i.next().value();
  111. try
  112. {
  113. // NYI, don't call inside lock
  114. qobject_cast<ctkServiceFactory*>(d->service)->ungetService(i.key(),
  115. this,
  116. obj);
  117. }
  118. catch (const std::exception& ue)
  119. {
  120. ctkPluginFrameworkEvent pfwEvent(ctkPluginFrameworkEvent::ERROR, d->plugin->q_func(), ue);
  121. d->plugin->fwCtx->listeners
  122. .emitFrameworkEvent(pfwEvent);
  123. }
  124. }
  125. }
  126. d->plugin = 0;
  127. d->dependents.clear();
  128. d->service = 0;
  129. d->serviceInstances.clear();;
  130. d->unregistering = false;
  131. }
  132. }
  133. }
  134. bool ctkServiceRegistration::operator<(const ctkServiceRegistration& o) const
  135. {
  136. Q_D(const ctkServiceRegistration);
  137. return d->reference <(o.d_func()->reference);
  138. }