| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 | 
							- /*=============================================================================
 
-   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_p.h"
 
- #include "ctkTrackedService_p.h"
 
- #include "ctkServiceException.h"
 
- #include "ctkPluginConstants.h"
 
- #include "ctkPluginContext.h"
 
- #include <QVarLengthArray>
 
- #include <QDebug>
 
- #include <stdexcept>
 
- #include <limits>
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- ctkServiceTracker<S,T>::~ctkServiceTracker()
 
- {
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- ctkServiceTracker<S,T>::ctkServiceTracker(ctkPluginContext* context,
 
-                                           const ctkServiceReference& reference,
 
-                                           ServiceTrackerCustomizer* customizer)
 
-   : d_ptr(new ServiceTrackerPrivate(this, context, reference, customizer))
 
- {
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- ctkServiceTracker<S,T>::ctkServiceTracker(ctkPluginContext* context, const QString& clazz,
 
-                                           ServiceTrackerCustomizer* customizer)
 
-   : d_ptr(new ServiceTrackerPrivate(this, context, clazz, customizer))
 
- {
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- ctkServiceTracker<S,T>::ctkServiceTracker(ctkPluginContext* context, const ctkLDAPSearchFilter& filter,
 
-                                           ServiceTrackerCustomizer* customizer)
 
-   : d_ptr(new ServiceTrackerPrivate(this, context, filter, customizer))
 
- {
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- ctkServiceTracker<S,T>::ctkServiceTracker(ctkPluginContext *context, ctkServiceTrackerCustomizer<T> *customizer)
 
-   : d_ptr(new ServiceTrackerPrivate(this, context, qobject_interface_iid<S>(), customizer))
 
- {
 
-   const char* clazz = qobject_interface_iid<S>();
 
-   if (clazz == 0) throw ctkServiceException("The service interface class has no Q_DECLARE_INTERFACE macro");
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- void ctkServiceTracker<S,T>::open()
 
- {
 
-   Q_D(ServiceTracker);
 
-   QSharedPointer<TrackedService> t;
 
-   {
 
-     QMutexLocker lock(&d->mutex);
 
-     if (d->trackedService)
 
-     {
 
-       return;
 
-     }
 
-     if (d->DEBUG_FLAG)
 
-     {
 
-       qDebug() << "ctkServiceTracker<S,T>::open: " << d->filter;
 
-     }
 
-     t = QSharedPointer<TrackedService>(
 
-           new TrackedService(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.getPlugin().isNull())
 
-           {
 
-             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 ctkInvalidArgumentException& e)
 
-       {
 
-         throw ctkRuntimeException(QString("unexpected ctkInvalidArgumentException exception: %1").arg(e.what()));
 
-       }
 
-     }
 
-     d->trackedService = t;
 
-   }
 
-   /* Call tracked outside of synchronized region */
 
-   t->trackInitial(); /* process the initial references */
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- void ctkServiceTracker<S,T>::close()
 
- {
 
-   Q_D(ServiceTracker);
 
-   QSharedPointer<TrackedService> outgoing;
 
-   QList<ctkServiceReference> references;
 
-   {
 
-     QMutexLocker lock(&d->mutex);
 
-     outgoing = d->trackedService;
 
-     if (outgoing.isNull())
 
-     {
 
-       return;
 
-     }
 
-     if (d->DEBUG_FLAG)
 
-     {
 
-       qDebug() << "ctkServiceTracker<S,T>::close:" << d->filter;
 
-     }
 
-     outgoing->close();
 
-     references = getServiceReferences();
 
-     d->trackedService.clear();;
 
-     try
 
-     {
 
-       d->context->disconnectServiceListener(outgoing.data(), "serviceChanged");
 
-     }
 
-     catch (const ctkIllegalStateException& /*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_FLAG)
 
-   {
 
-     QMutexLocker lock(&d->mutex);
 
-     if ((d->cachedReference.getPlugin().isNull()) && (d->cachedService == 0))
 
-     {
 
-       qDebug() << "ctkServiceTracker<S,T>::close[cached cleared]:"
 
-           << d->filter;
 
-     }
 
-   }
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- T ctkServiceTracker<S,T>::waitForService(unsigned long timeout)
 
- {
 
-   Q_D(ServiceTracker);
 
-   T object = getService();
 
-   while (object == 0)
 
-   {
 
-     QSharedPointer<TrackedService> 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;
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- QList<ctkServiceReference> ctkServiceTracker<S,T>::getServiceReferences() const
 
- {
 
-   Q_D(const ServiceTracker);
 
-   QSharedPointer<TrackedService> t = d->tracked();
 
-   if (t.isNull())
 
-   { /* if ServiceTracker is not open */
 
-     return QList<ctkServiceReference>();
 
-   }
 
-   {
 
-     QMutexLocker lockT(t.data());
 
-     return d->getServiceReferences_unlocked(t.data());
 
-   }
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- ctkServiceReference ctkServiceTracker<S,T>::getServiceReference() const
 
- {
 
-   Q_D(const ServiceTracker);
 
-   ctkServiceReference reference(0);
 
-   {
 
-     QMutexLocker lock(&d->mutex);
 
-     reference = d->cachedReference;
 
-   }
 
-   if (!reference.getPlugin().isNull())
 
-   {
 
-     if (d->DEBUG_FLAG)
 
-     {
 
-       qDebug() << "ctkServiceTracker<S,T>::getServiceReference[cached]:"
 
-                    << d->filter;
 
-     }
 
-     return reference;
 
-   }
 
-   if (d->DEBUG_FLAG)
 
-   {
 
-     qDebug() << "ctkServiceTracker<S,T>::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;
 
-   }
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- T ctkServiceTracker<S,T>::getService(const ctkServiceReference& reference) const
 
- {
 
-   Q_D(const ServiceTracker);
 
-   QSharedPointer<TrackedService> t = d->tracked();
 
-   if (t.isNull())
 
-   { /* if ServiceTracker is not open */
 
-     return 0;
 
-   }
 
-   {
 
-     QMutexLocker lockT(t.data());
 
-     return t->getCustomizedObject(reference);
 
-   }
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- QList<T> ctkServiceTracker<S,T>::getServices() const
 
- {
 
-   Q_D(const ServiceTracker);
 
-   QSharedPointer<TrackedService> t = d->tracked();
 
-   if (t.isNull())
 
-   { /* if ServiceTracker is not open */
 
-     return QList<T>();
 
-   }
 
-   {
 
-     QMutexLocker lockT(t.data());
 
-     QList<ctkServiceReference> references = d->getServiceReferences_unlocked(t.data());
 
-     QList<T> objects;
 
-     foreach (ctkServiceReference ref, references)
 
-     {
 
-       //objects << getService(ref);
 
-       objects << t->getCustomizedObject(ref);
 
-     }
 
-     return objects;
 
-   }
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- T ctkServiceTracker<S,T>::getService() const
 
- {
 
-   Q_D(const ServiceTracker);
 
-   T service = d->cachedService;
 
-   if (service != 0)
 
-   {
 
-     if (d->DEBUG_FLAG)
 
-     {
 
-       qDebug() << "ctkServiceTracker<S,T>::getService[cached]:"
 
-                << d->filter;
 
-     }
 
-     return service;
 
-   }
 
-   if (d->DEBUG_FLAG)
 
-   {
 
-     qDebug() << "ctkServiceTracker<S,T>::getService:" << d->filter;
 
-   }
 
-   try
 
-   {
 
-     ctkServiceReference reference = getServiceReference();
 
-     if (reference.getPlugin().isNull())
 
-     {
 
-       return 0;
 
-     }
 
-     return d->cachedService = getService(reference);
 
-   }
 
-   catch (const ctkServiceException&)
 
-   {
 
-     return 0;
 
-   }
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- void ctkServiceTracker<S,T>::remove(const ctkServiceReference& reference)
 
- {
 
-   Q_D(ServiceTracker);
 
-   QSharedPointer<TrackedService> t = d->tracked();
 
-   if (t.isNull())
 
-   { /* if ServiceTracker is not open */
 
-     return;
 
-   }
 
-   t->untrack(reference, ctkServiceEvent());
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- int ctkServiceTracker<S,T>::size() const
 
- {
 
-   Q_D(const ServiceTracker);
 
-   QSharedPointer<TrackedService> t = d->tracked();
 
-   if (t.isNull())
 
-   { /* if ServiceTracker is not open */
 
-     return 0;
 
-   }
 
-   {
 
-     QMutexLocker lockT(t.data());
 
-     return t->size();
 
-   }
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- int ctkServiceTracker<S,T>::getTrackingCount() const
 
- {
 
-   Q_D(const ServiceTracker);
 
-   QSharedPointer<TrackedService> t = d->tracked();
 
-   if (t.isNull())
 
-   { /* if ServiceTracker is not open */
 
-     return -1;
 
-   }
 
-   {
 
-     QMutexLocker lockT(t.data());
 
-     return t->getTrackingCount();
 
-   }
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- QMap<ctkServiceReference, T> ctkServiceTracker<S,T>::getTracked() const
 
- {
 
-   QMap<ctkServiceReference, T> map;
 
-   Q_D(const ServiceTracker);
 
-   QSharedPointer<TrackedService> t = d->tracked();
 
-   if (t.isNull())
 
-   { /* if ServiceTracker is not open */
 
-     return map;
 
-   }
 
-   {
 
-     QMutexLocker lockT(t.data());
 
-     return t->copyEntries(map);
 
-   }
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- bool ctkServiceTracker<S,T>::isEmpty() const
 
- {
 
-   Q_D(const ServiceTracker);
 
-   QSharedPointer<TrackedService> t = d->tracked();
 
-   if (t.isNull())
 
-   { /* if ServiceTracker is not open */
 
-     return true;
 
-   }
 
-   {
 
-     QMutexLocker lockT(t.data());
 
-     return t->isEmpty();
 
-   }
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- T ctkServiceTracker<S,T>::addingService(const ctkServiceReference& reference)
 
- {
 
-   Q_D(ServiceTracker);
 
-   return qobject_cast<T>(d->context->getService(reference));
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- void ctkServiceTracker<S,T>::modifiedService(const ctkServiceReference& reference, T service)
 
- {
 
-   Q_UNUSED(reference)
 
-   Q_UNUSED(service)
 
-   /* do nothing */
 
- }
 
- //----------------------------------------------------------------------------
 
- template<class S, class T>
 
- void ctkServiceTracker<S,T>::removedService(const ctkServiceReference& reference, T service)
 
- {
 
-   Q_UNUSED(service)
 
-   Q_D(ServiceTracker);
 
-   d->context->ungetService(reference);
 
- }
 
 
  |