123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- /*=============================================================================
- Library: CTK
- Copyright (c) German Cancer Research Center,
- Division of Medical and Biological Informatics
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =============================================================================*/
- #include "ctkServiceTracker.h"
- #include "ctkServiceTrackerPrivate.h"
- #include "ctkTrackedService_p.h"
- #include "ctkServiceException.h"
- #include "ctkPluginConstants.h"
- #include <QVarLengthArray>
- #include <QDebug>
- #include <stdexcept>
- #include <limits>
- ctkServiceTracker::~ctkServiceTracker()
- {
- }
- ctkServiceTracker::ctkServiceTracker(ctkPluginContext* context,
- const ctkServiceReference& reference,
- ctkServiceTrackerCustomizer* customizer)
- : d_ptr(new ctkServiceTrackerPrivate(this, context, reference, customizer))
- {
- }
- ctkServiceTracker::ctkServiceTracker(ctkPluginContext* context, const QString& clazz,
- ctkServiceTrackerCustomizer* customizer)
- : d_ptr(new ctkServiceTrackerPrivate(this, context, clazz, customizer))
- {
- }
- ctkServiceTracker::ctkServiceTracker(ctkPluginContext* context, const ctkLDAPSearchFilter& filter,
- ctkServiceTrackerCustomizer* customizer)
- : d_ptr(new ctkServiceTrackerPrivate(this, context, filter, customizer))
- {
- }
- void ctkServiceTracker::open()
- {
- Q_D(ctkServiceTracker);
- QSharedPointer<ctkTrackedService> t;
- {
- QMutexLocker lock(&d->mutex);
- if (d->trackedService)
- {
- return;
- }
- if (d->DEBUG)
- {
- qDebug() << "ctkServiceTracker::open: " << d->filter;
- }
- t = QSharedPointer<ctkTrackedService>(new ctkTrackedService(this, d->customizer));
- {
- QMutexLocker lockT(t.data());
- try {
- d->context->connectServiceListener(t.data(), "serviceChanged", d->listenerFilter);
- QList<ctkServiceReference> references;
- if (!d->trackClass.isEmpty())
- {
- references = d->getInitialReferences(d->trackClass, QString());
- }
- else
- {
- if (!d->trackReference.isNull())
- {
- if (d->trackReference.getPlugin())
- {
- references.push_back(d->trackReference);
- }
- }
- else
- { /* user supplied filter */
- references = d->getInitialReferences(QString(),
- (d->listenerFilter.isNull()) ? d->filter.toString() : d->listenerFilter);
- }
- }
- /* set tracked with the initial references */
- t->setInitial(references);
- }
- catch (const std::invalid_argument& e)
- {
- throw std::runtime_error(std::string("unexpected std::invalid_argument exception: ")
- + e.what());
- }
- }
- d->trackedService = t;
- }
- /* Call tracked outside of synchronized region */
- t->trackInitial(); /* process the initial references */
- }
- void ctkServiceTracker::close()
- {
- Q_D(ctkServiceTracker);
- QSharedPointer<ctkTrackedService> outgoing;
- QList<ctkServiceReference> references;
- {
- QMutexLocker lock(&d->mutex);
- outgoing = d->trackedService;
- if (outgoing.isNull())
- {
- return;
- }
- if (d->DEBUG)
- {
- qDebug() << "ctkServiceTracker::close:" << d->filter;
- }
- outgoing->close();
- references = getServiceReferences();
- d->trackedService.clear();;
- try
- {
- d->context->disconnectServiceListener(outgoing.data(), "serviceChanged");
- }
- catch (const std::logic_error& e)
- {
- /* In case the context was stopped. */
- }
- }
- d->modified(); /* clear the cache */
- {
- QMutexLocker lockT(outgoing.data());
- outgoing->wakeAll(); /* wake up any waiters */
- }
- foreach (ctkServiceReference ref, references)
- {
- outgoing->untrack(ref, ctkServiceEvent());
- }
- if (d->DEBUG)
- {
- QMutexLocker lock(&d->mutex);
- if ((d->cachedReference.isNull()) && (d->cachedService == 0))
- {
- qDebug() << "ctkServiceTracker::close[cached cleared]:"
- << d->filter;
- }
- }
- }
- QObject* ctkServiceTracker::waitForService(unsigned long timeout)
- {
- Q_D(ctkServiceTracker);
- QObject* object = getService();
- while (object == 0)
- {
- QSharedPointer<ctkTrackedService> t = d->tracked();
- if (t.isNull())
- { /* if ServiceTracker is not open */
- return 0;
- }
- {
- QMutexLocker lockT(t.data());
- if (t->size() == 0)
- {
- t->wait(timeout);
- }
- }
- object = getService();
- if (timeout > 0)
- {
- return object;
- }
- }
- return object;
- }
- QList<ctkServiceReference> ctkServiceTracker::getServiceReferences() const
- {
- Q_D(const ctkServiceTracker);
- QSharedPointer<ctkTrackedService> t = d->tracked();
- if (t.isNull())
- { /* if ServiceTracker is not open */
- return QList<ctkServiceReference>();
- }
- {
- QMutexLocker lockT(t.data());
- if (t->size() == 0)
- {
- return QList<ctkServiceReference>();
- }
- return t->getTracked();
- }
- }
- ctkServiceReference ctkServiceTracker::getServiceReference() const
- {
- Q_D(const ctkServiceTracker);
- ctkServiceReference reference(0);
- {
- QMutexLocker lock(&d->mutex);
- reference = d->cachedReference;
- }
- if (!reference.isNull())
- {
- if (d->DEBUG)
- {
- qDebug() << "ctkServiceTracker::getServiceReference[cached]:"
- << d->filter;
- }
- return reference;
- }
- if (d->DEBUG)
- {
- qDebug() << "ctkServiceTracker::getServiceReference:" << d->filter;
- }
- QList<ctkServiceReference> references = getServiceReferences();
- int length = references.size();
- if (length == 0)
- { /* if no service is being tracked */
- throw ctkServiceException("No service is being tracked");
- }
- int index = 0;
- if (length > 1)
- { /* if more than one service, select highest ranking */
- QVarLengthArray<int, 10> rankings(length);
- int count = 0;
- int maxRanking = std::numeric_limits<int>::min();
- for (int i = 0; i < length; i++)
- {
- bool ok = false;
- int ranking = references[i].getProperty(ctkPluginConstants::SERVICE_RANKING).toInt(&ok);
- if (!ok) ranking = 0;
- rankings[i] = ranking;
- if (ranking > maxRanking)
- {
- index = i;
- maxRanking = ranking;
- count = 1;
- }
- else
- {
- if (ranking == maxRanking)
- {
- count++;
- }
- }
- }
- if (count > 1)
- { /* if still more than one service, select lowest id */
- qlonglong minId = std::numeric_limits<qlonglong>::max();
- for (int i = 0; i < length; i++)
- {
- if (rankings[i] == maxRanking)
- {
- qlonglong id = references[i].getProperty(ctkPluginConstants::SERVICE_ID).toLongLong();
- if (id < minId)
- {
- index = i;
- minId = id;
- }
- }
- }
- }
- }
- {
- QMutexLocker lock(&d->mutex);
- d->cachedReference = references[index];
- return d->cachedReference;
- }
- }
- QObject* ctkServiceTracker::getService(const ctkServiceReference& reference) const
- {
- Q_D(const ctkServiceTracker);
- QSharedPointer<ctkTrackedService> t = d->tracked();
- if (t.isNull())
- { /* if ServiceTracker is not open */
- return 0;
- }
- {
- QMutexLocker lockT(t.data());
- return t->getCustomizedObject(reference);
- }
- }
- QList<QObject*> ctkServiceTracker::getServices() const
- {
- Q_D(const ctkServiceTracker);
- QSharedPointer<ctkTrackedService> t = d->tracked();
- if (t.isNull())
- { /* if ServiceTracker is not open */
- return QList<QObject*>();
- }
- {
- QMutexLocker lockT(t.data());
- QList<ctkServiceReference> references = getServiceReferences();
- QList<QObject*> objects;
- foreach (ctkServiceReference ref, references)
- {
- objects << getService(ref);
- }
- return objects;
- }
- }
- QObject* ctkServiceTracker::getService() const
- {
- Q_D(const ctkServiceTracker);
- QObject* service = d->cachedService;
- if (service != 0)
- {
- if (d->DEBUG)
- {
- qDebug() << "ctkServiceTracker::getService[cached]:"
- << d->filter;
- }
- return service;
- }
- if (d->DEBUG)
- {
- qDebug() << "ctkServiceTracker::getService:" << d->filter;
- }
- try
- {
- ctkServiceReference reference = getServiceReference();
- if (reference.isNull())
- {
- return 0;
- }
- return d->cachedService = getService(reference);
- }
- catch (const ctkServiceException&)
- {
- return 0;
- }
- }
- void ctkServiceTracker::remove(const ctkServiceReference& reference)
- {
- Q_D(ctkServiceTracker);
- QSharedPointer<ctkTrackedService> t = d->tracked();
- if (t.isNull())
- { /* if ServiceTracker is not open */
- return;
- }
- t->untrack(reference, ctkServiceEvent());
- }
- int ctkServiceTracker::size() const
- {
- Q_D(const ctkServiceTracker);
- QSharedPointer<ctkTrackedService> t = d->tracked();
- if (t.isNull())
- { /* if ServiceTracker is not open */
- return 0;
- }
- {
- QMutexLocker lockT(t.data());
- return t->size();
- }
- }
- int ctkServiceTracker::getTrackingCount() const
- {
- Q_D(const ctkServiceTracker);
- QSharedPointer<ctkTrackedService> t = d->tracked();
- if (t.isNull())
- { /* if ServiceTracker is not open */
- return -1;
- }
- {
- QMutexLocker lockT(t.data());
- return t->getTrackingCount();
- }
- }
- QObject* ctkServiceTracker::addingService(const ctkServiceReference& reference)
- {
- Q_D(ctkServiceTracker);
- return d->context->getService(reference);
- }
- void ctkServiceTracker::modifiedService(const ctkServiceReference& reference, QObject* service)
- {
- Q_UNUSED(reference)
- Q_UNUSED(service)
- /* do nothing */
- }
- void ctkServiceTracker::removedService(const ctkServiceReference& reference, QObject* service)
- {
- Q_UNUSED(service)
- Q_D(ctkServiceTracker);
- d->context->ungetService(reference);
- }
|