ctkServiceRegistration.cpp 4.9 KB

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