ctkServiceTracker.tpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 "ctkServiceTrackerPrivate.h"
  16. #include "ctkTrackedService_p.h"
  17. #include "ctkServiceException.h"
  18. #include "ctkPluginConstants.h"
  19. #include "ctkPluginContext.h"
  20. #include <QVarLengthArray>
  21. #include <QDebug>
  22. #include <stdexcept>
  23. #include <limits>
  24. //----------------------------------------------------------------------------
  25. template<class S, class T>
  26. ctkServiceTracker<S,T>::~ctkServiceTracker()
  27. {
  28. }
  29. //----------------------------------------------------------------------------
  30. template<class S, class T>
  31. ctkServiceTracker<S,T>::ctkServiceTracker(ctkPluginContext* context,
  32. const ctkServiceReference& reference,
  33. ServiceTrackerCustomizer* customizer)
  34. : d_ptr(new ServiceTrackerPrivate(this, context, reference, customizer))
  35. {
  36. }
  37. //----------------------------------------------------------------------------
  38. template<class S, class T>
  39. ctkServiceTracker<S,T>::ctkServiceTracker(ctkPluginContext* context, const QString& clazz,
  40. ServiceTrackerCustomizer* customizer)
  41. : d_ptr(new ServiceTrackerPrivate(this, context, clazz, customizer))
  42. {
  43. }
  44. //----------------------------------------------------------------------------
  45. template<class S, class T>
  46. ctkServiceTracker<S,T>::ctkServiceTracker(ctkPluginContext* context, const ctkLDAPSearchFilter& filter,
  47. ServiceTrackerCustomizer* customizer)
  48. : d_ptr(new ServiceTrackerPrivate(this, context, filter, customizer))
  49. {
  50. }
  51. //----------------------------------------------------------------------------
  52. template<class S, class T>
  53. ctkServiceTracker<S,T>::ctkServiceTracker(ctkPluginContext *context, ctkServiceTrackerCustomizer<T> *customizer)
  54. : d_ptr(new ServiceTrackerPrivate(this, context, qobject_interface_iid<S>(), customizer))
  55. {
  56. const char* clazz = qobject_interface_iid<S>();
  57. if (clazz == 0) throw ctkServiceException("The service interface class has no Q_DECLARE_INTERFACE macro");
  58. }
  59. //----------------------------------------------------------------------------
  60. template<class S, class T>
  61. void ctkServiceTracker<S,T>::open()
  62. {
  63. Q_D(ServiceTracker);
  64. QSharedPointer<TrackedService> t;
  65. {
  66. QMutexLocker lock(&d->mutex);
  67. if (d->trackedService)
  68. {
  69. return;
  70. }
  71. if (d->DEBUG)
  72. {
  73. qDebug() << "ctkServiceTracker<S,T>::open: " << d->filter;
  74. }
  75. t = QSharedPointer<TrackedService>(
  76. new TrackedService(this, d->customizer));
  77. {
  78. QMutexLocker lockT(t.data());
  79. try {
  80. d->context->connectServiceListener(t.data(), "serviceChanged", d->listenerFilter);
  81. QList<ctkServiceReference> references;
  82. if (!d->trackClass.isEmpty())
  83. {
  84. references = d->getInitialReferences(d->trackClass, QString());
  85. }
  86. else
  87. {
  88. if (!d->trackReference.getPlugin().isNull())
  89. {
  90. references.push_back(d->trackReference);
  91. }
  92. else
  93. { /* user supplied filter */
  94. references = d->getInitialReferences(QString(),
  95. (d->listenerFilter.isNull()) ? d->filter.toString() : d->listenerFilter);
  96. }
  97. }
  98. /* set tracked with the initial references */
  99. t->setInitial(references);
  100. }
  101. catch (const std::invalid_argument& e)
  102. {
  103. throw std::runtime_error(std::string("unexpected std::invalid_argument exception: ")
  104. + e.what());
  105. }
  106. }
  107. d->trackedService = t;
  108. }
  109. /* Call tracked outside of synchronized region */
  110. t->trackInitial(); /* process the initial references */
  111. }
  112. //----------------------------------------------------------------------------
  113. template<class S, class T>
  114. void ctkServiceTracker<S,T>::close()
  115. {
  116. Q_D(ServiceTracker);
  117. QSharedPointer<TrackedService> outgoing;
  118. QList<ctkServiceReference> references;
  119. {
  120. QMutexLocker lock(&d->mutex);
  121. outgoing = d->trackedService;
  122. if (outgoing.isNull())
  123. {
  124. return;
  125. }
  126. if (d->DEBUG)
  127. {
  128. qDebug() << "ctkServiceTracker<S,T>::close:" << d->filter;
  129. }
  130. outgoing->close();
  131. references = getServiceReferences();
  132. d->trackedService.clear();;
  133. try
  134. {
  135. d->context->disconnectServiceListener(outgoing.data(), "serviceChanged");
  136. }
  137. catch (const std::logic_error& /*e*/)
  138. {
  139. /* In case the context was stopped. */
  140. }
  141. }
  142. d->modified(); /* clear the cache */
  143. {
  144. QMutexLocker lockT(outgoing.data());
  145. outgoing->wakeAll(); /* wake up any waiters */
  146. }
  147. foreach (ctkServiceReference ref, references)
  148. {
  149. outgoing->untrack(ref, ctkServiceEvent());
  150. }
  151. if (d->DEBUG)
  152. {
  153. QMutexLocker lock(&d->mutex);
  154. if ((d->cachedReference.getPlugin().isNull()) && (d->cachedService == 0))
  155. {
  156. qDebug() << "ctkServiceTracker<S,T>::close[cached cleared]:"
  157. << d->filter;
  158. }
  159. }
  160. }
  161. //----------------------------------------------------------------------------
  162. template<class S, class T>
  163. T ctkServiceTracker<S,T>::waitForService(unsigned long timeout)
  164. {
  165. Q_D(ServiceTracker);
  166. T object = getService();
  167. while (object == 0)
  168. {
  169. QSharedPointer<TrackedService> t = d->tracked();
  170. if (t.isNull())
  171. { /* if ServiceTracker is not open */
  172. return 0;
  173. }
  174. {
  175. QMutexLocker lockT(t.data());
  176. if (t->size() == 0)
  177. {
  178. t->wait(timeout);
  179. }
  180. }
  181. object = getService();
  182. if (timeout > 0)
  183. {
  184. return object;
  185. }
  186. }
  187. return object;
  188. }
  189. //----------------------------------------------------------------------------
  190. template<class S, class T>
  191. QList<ctkServiceReference> ctkServiceTracker<S,T>::getServiceReferences() const
  192. {
  193. Q_D(const ServiceTracker);
  194. QSharedPointer<TrackedService> t = d->tracked();
  195. if (t.isNull())
  196. { /* if ServiceTracker is not open */
  197. return QList<ctkServiceReference>();
  198. }
  199. {
  200. QMutexLocker lockT(t.data());
  201. return d->getServiceReferences_unlocked(t.data());
  202. }
  203. }
  204. //----------------------------------------------------------------------------
  205. template<class S, class T>
  206. ctkServiceReference ctkServiceTracker<S,T>::getServiceReference() const
  207. {
  208. Q_D(const ServiceTracker);
  209. ctkServiceReference reference(0);
  210. {
  211. QMutexLocker lock(&d->mutex);
  212. reference = d->cachedReference;
  213. }
  214. if (!reference.getPlugin().isNull())
  215. {
  216. if (d->DEBUG)
  217. {
  218. qDebug() << "ctkServiceTracker<S,T>::getServiceReference[cached]:"
  219. << d->filter;
  220. }
  221. return reference;
  222. }
  223. if (d->DEBUG)
  224. {
  225. qDebug() << "ctkServiceTracker<S,T>::getServiceReference:" << d->filter;
  226. }
  227. QList<ctkServiceReference> references = getServiceReferences();
  228. int length = references.size();
  229. if (length == 0)
  230. { /* if no service is being tracked */
  231. throw ctkServiceException("No service is being tracked");
  232. }
  233. int index = 0;
  234. if (length > 1)
  235. { /* if more than one service, select highest ranking */
  236. QVarLengthArray<int, 10> rankings(length);
  237. int count = 0;
  238. int maxRanking = std::numeric_limits<int>::min();
  239. for (int i = 0; i < length; i++)
  240. {
  241. bool ok = false;
  242. int ranking = references[i].getProperty(ctkPluginConstants::SERVICE_RANKING).toInt(&ok);
  243. if (!ok) ranking = 0;
  244. rankings[i] = ranking;
  245. if (ranking > maxRanking)
  246. {
  247. index = i;
  248. maxRanking = ranking;
  249. count = 1;
  250. }
  251. else
  252. {
  253. if (ranking == maxRanking)
  254. {
  255. count++;
  256. }
  257. }
  258. }
  259. if (count > 1)
  260. { /* if still more than one service, select lowest id */
  261. qlonglong minId = std::numeric_limits<qlonglong>::max();
  262. for (int i = 0; i < length; i++)
  263. {
  264. if (rankings[i] == maxRanking)
  265. {
  266. qlonglong id = references[i].getProperty(ctkPluginConstants::SERVICE_ID).toLongLong();
  267. if (id < minId)
  268. {
  269. index = i;
  270. minId = id;
  271. }
  272. }
  273. }
  274. }
  275. }
  276. {
  277. QMutexLocker lock(&d->mutex);
  278. d->cachedReference = references[index];
  279. return d->cachedReference;
  280. }
  281. }
  282. //----------------------------------------------------------------------------
  283. template<class S, class T>
  284. T ctkServiceTracker<S,T>::getService(const ctkServiceReference& reference) const
  285. {
  286. Q_D(const ServiceTracker);
  287. QSharedPointer<TrackedService> t = d->tracked();
  288. if (t.isNull())
  289. { /* if ServiceTracker is not open */
  290. return 0;
  291. }
  292. {
  293. QMutexLocker lockT(t.data());
  294. return t->getCustomizedObject(reference);
  295. }
  296. }
  297. //----------------------------------------------------------------------------
  298. template<class S, class T>
  299. QList<T> ctkServiceTracker<S,T>::getServices() const
  300. {
  301. Q_D(const ServiceTracker);
  302. QSharedPointer<TrackedService> t = d->tracked();
  303. if (t.isNull())
  304. { /* if ServiceTracker is not open */
  305. return QList<T>();
  306. }
  307. {
  308. QMutexLocker lockT(t.data());
  309. QList<ctkServiceReference> references = d->getServiceReferences_unlocked(t.data());
  310. QList<T> objects;
  311. foreach (ctkServiceReference ref, references)
  312. {
  313. //objects << getService(ref);
  314. objects << t->getCustomizedObject(ref);
  315. }
  316. return objects;
  317. }
  318. }
  319. //----------------------------------------------------------------------------
  320. template<class S, class T>
  321. T ctkServiceTracker<S,T>::getService() const
  322. {
  323. Q_D(const ServiceTracker);
  324. T service = d->cachedService;
  325. if (service != 0)
  326. {
  327. if (d->DEBUG)
  328. {
  329. qDebug() << "ctkServiceTracker<S,T>::getService[cached]:"
  330. << d->filter;
  331. }
  332. return service;
  333. }
  334. if (d->DEBUG)
  335. {
  336. qDebug() << "ctkServiceTracker<S,T>::getService:" << d->filter;
  337. }
  338. try
  339. {
  340. ctkServiceReference reference = getServiceReference();
  341. if (reference.getPlugin().isNull())
  342. {
  343. return 0;
  344. }
  345. return d->cachedService = getService(reference);
  346. }
  347. catch (const ctkServiceException&)
  348. {
  349. return 0;
  350. }
  351. }
  352. //----------------------------------------------------------------------------
  353. template<class S, class T>
  354. void ctkServiceTracker<S,T>::remove(const ctkServiceReference& reference)
  355. {
  356. Q_D(ServiceTracker);
  357. QSharedPointer<TrackedService> t = d->tracked();
  358. if (t.isNull())
  359. { /* if ServiceTracker is not open */
  360. return;
  361. }
  362. t->untrack(reference, ctkServiceEvent());
  363. }
  364. //----------------------------------------------------------------------------
  365. template<class S, class T>
  366. int ctkServiceTracker<S,T>::size() const
  367. {
  368. Q_D(const ServiceTracker);
  369. QSharedPointer<TrackedService> t = d->tracked();
  370. if (t.isNull())
  371. { /* if ServiceTracker is not open */
  372. return 0;
  373. }
  374. {
  375. QMutexLocker lockT(t.data());
  376. return t->size();
  377. }
  378. }
  379. //----------------------------------------------------------------------------
  380. template<class S, class T>
  381. int ctkServiceTracker<S,T>::getTrackingCount() const
  382. {
  383. Q_D(const ServiceTracker);
  384. QSharedPointer<TrackedService> t = d->tracked();
  385. if (t.isNull())
  386. { /* if ServiceTracker is not open */
  387. return -1;
  388. }
  389. {
  390. QMutexLocker lockT(t.data());
  391. return t->getTrackingCount();
  392. }
  393. }
  394. //----------------------------------------------------------------------------
  395. template<class S, class T>
  396. QMap<ctkServiceReference, T> ctkServiceTracker<S,T>::getTracked() const
  397. {
  398. QMap<ctkServiceReference, T> map;
  399. Q_D(const ServiceTracker);
  400. QSharedPointer<TrackedService> t = d->tracked();
  401. if (t.isNull())
  402. { /* if ServiceTracker is not open */
  403. return map;
  404. }
  405. {
  406. QMutexLocker lockT(t.data());
  407. return t->copyEntries(map);
  408. }
  409. }
  410. //----------------------------------------------------------------------------
  411. template<class S, class T>
  412. bool ctkServiceTracker<S,T>::isEmpty() const
  413. {
  414. Q_D(const ServiceTracker);
  415. QSharedPointer<TrackedService> t = d->tracked();
  416. if (t.isNull())
  417. { /* if ServiceTracker is not open */
  418. return true;
  419. }
  420. {
  421. QMutexLocker lockT(t.data());
  422. return t->isEmpty();
  423. }
  424. }
  425. //----------------------------------------------------------------------------
  426. template<class S, class T>
  427. T ctkServiceTracker<S,T>::addingService(const ctkServiceReference& reference)
  428. {
  429. Q_D(ServiceTracker);
  430. return qobject_cast<T>(d->context->getService(reference));
  431. }
  432. //----------------------------------------------------------------------------
  433. template<class S, class T>
  434. void ctkServiceTracker<S,T>::modifiedService(const ctkServiceReference& reference, T service)
  435. {
  436. Q_UNUSED(reference)
  437. Q_UNUSED(service)
  438. /* do nothing */
  439. }
  440. //----------------------------------------------------------------------------
  441. template<class S, class T>
  442. void ctkServiceTracker<S,T>::removedService(const ctkServiceReference& reference, T service)
  443. {
  444. Q_UNUSED(service)
  445. Q_D(ServiceTracker);
  446. d->context->ungetService(reference);
  447. }