ctkServiceTracker.cpp 10 KB

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