Skip to content

Commit

Permalink
Removed python loader: added unRegisterService() method
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarteau committed Jan 10, 2017
1 parent 4c8a5cb commit 705547e
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 181 deletions.
18 changes: 13 additions & 5 deletions python/server/qgsserviceregistry.sip
Expand Up @@ -62,15 +62,23 @@ class QgsServiceRegistry
*/
void registerService( QgsService* service /Transfer/ );

/**
* Unregister service from its name and version
*
* @param name the tame of the service
* @param version (optional) the specific version to unload
* @return the number of unregstered services
*
* If the version is not specified then all versions from the specified service
* are unloaded
*/
int unRegisterService( const QString& name, const QString& version = QString() );

/**
* Initialize registry, load modules and auto register services
* @param nativeModulepath the native module path
* @param pythonModulePath the python module path
*
* If pythonModulePath is not specified the environnement variables QGIS_PYTHON_SERVICE_PATH
* is examined.
*/
void init( const QString& nativeModulepath, const QString& pythonModulePath = QString() );
void init( const QString& nativeModulepath );

/**
* Clean up registered service and unregister modules
Expand Down
1 change: 0 additions & 1 deletion src/server/CMakeLists.txt
Expand Up @@ -55,7 +55,6 @@ SET ( qgis_mapserv_SRCS
qgsservicemodule.cpp
qgsserviceloader.cpp
qgsservicenativeloader.cpp
qgsservicepythonloader.cpp
qgsserviceregistry.cpp
qgsserverrequest.cpp
qgsserverresponse.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/server/qgsserverrequest.cpp
Expand Up @@ -40,7 +40,7 @@ QgsServerRequest::~QgsServerRequest()

}

QString QgsServerRequest::getHeader( const QString& name ) const
QString QgsServerRequest::getHeader( const QString& /*name*/ ) const
{
return "";
}
Expand Down
83 changes: 0 additions & 83 deletions src/server/qgsservicepythonloader.cpp

This file was deleted.

73 changes: 0 additions & 73 deletions src/server/qgsservicepythonloader.h

This file was deleted.

77 changes: 69 additions & 8 deletions src/server/qgsserviceregistry.cpp
Expand Up @@ -22,6 +22,9 @@
#include "qgslogger.h"
#include "qgsmessagelog.h"

#include <algorithm>
#include <functional>

namespace
{

Expand Down Expand Up @@ -145,23 +148,81 @@ void QgsServiceRegistry::registerService( QgsService* service )
}
}

void QgsServiceRegistry::init( const QString& nativeModulePath, const QString& pythonModulePath )
int QgsServiceRegistry::unRegisterService( const QString& name, const QString& version )
{
// Check that we have a service of that name
int removed = 0;
VersionTable::const_iterator v = mVersions.constFind( name );
if ( v != mVersions.constEnd() )
{
if ( version.isEmpty() )
{
// No version specified, remove all versions
ServiceTable::iterator it = mServices.begin();
while ( it != mServices.end() )
{
if (( *it )->name() == name )
{
QgsMessageLog::logMessage( QString( "Unregistering service %1 %2" ).arg( name, ( *it )->version() ) );
it = mServices.erase( it );
++removed;
}
else
{
++it;
}
}
// Remove from version table
mVersions.remove( name );
}
else
{
QString key = makeServiceKey( name, version );
ServiceTable::iterator found = mServices.find( key );
if ( found != mServices.end() )
{
QgsMessageLog::logMessage( QString( "Unregistering service %1 %2" ).arg( name, version ) );
mServices.erase( found );
removed = 1;

// Find if we have other services of that name
// but with different version
//
QString maxVer;
std::function < void ( const ServiceTable::mapped_type& ) >
findGreaterVersion = [name,&maxVer]( const ServiceTable::mapped_type & service )
{
if ( service->name() == name &&
( maxVer.isEmpty() || isVersionGreater( service->version(), maxVer ) ) )
maxVer = service->version();
};

mVersions.remove( name );

std::for_each( mServices.constBegin(), mServices.constEnd(), findGreaterVersion );
if ( !maxVer.isEmpty() )
{
// Set the new default service
QString key = makeServiceKey( name, maxVer );
mVersions.insert( name, VersionTable::mapped_type( version, key ) );
}
}
}
}
return removed;
}

void QgsServiceRegistry::init( const QString& nativeModulePath )
{
mNativeLoader.loadModules( nativeModulePath, *this );
#ifdef HAVE_SERVER_PYTHON_SERVICES
mPythonLoader.loadModules( pythonModulePath, *this );
#endif
}

void QgsServiceRegistry::cleanUp()
{
// Release all services
mVersions.clear();
mServices.clear();

mNativeLoader.unloadModules();
#ifdef HAVE_SERVER_PYTHON_SERVICES
mPythonLoader.unloadModules();
#endif
}


20 changes: 13 additions & 7 deletions src/server/qgsserviceregistry.h
Expand Up @@ -25,7 +25,6 @@
#include <QString>

#include "qgsservicenativeloader.h"
#include "qgsservicepythonloader.h"
#include <memory>

class QgsService;
Expand Down Expand Up @@ -75,15 +74,23 @@ class SERVER_EXPORT QgsServiceRegistry
*/
void registerService( QgsService* service );

/**
* Unregister service from its name and version
*
* @param name the tame of the service
* @param version (optional) the specific version to unload
* @return the number of services unregistered
*
* If the version is not specified then all versions from the specified service
* are unloaded
*/
int unRegisterService( const QString& name, const QString& version = QString() );

/**
* Initialize registry, load modules and auto register services
* @param nativeModulepath the native module path
* @param pythonModulePath the python module path
*
* If pythonModulePath is not specified the environnement variables QGIS_PYTHON_SERVICE_PATH
* is examined.
*/
void init( const QString& nativeModulepath, const QString& pythonModulePath = QString() );
void init( const QString& nativeModulepath );

/**
* Clean up registered service and unregister modules
Expand All @@ -95,7 +102,6 @@ class SERVER_EXPORT QgsServiceRegistry
typedef QHash<QString, QPair<QString, QString> > VersionTable;

QgsServiceNativeLoader mNativeLoader;
QgsServicePythonLoader mPythonLoader;

ServiceTable mServices;
VersionTable mVersions;
Expand Down
2 changes: 1 addition & 1 deletion src/server/services/DummyService/dummy.cpp
Expand Up @@ -31,7 +31,7 @@ class SampleService: public QgsService
return method == QgsServerRequest::GetMethod;
}

void executeRequest( const QgsServerRequest& request, QgsServerResponse& response )
void executeRequest( const QgsServerRequest& /*request*/, QgsServerResponse& response )
{
QgsDebugMsg( "SampleService::executeRequest called" );
response.write( QString( "Hello world from myService" ) );
Expand Down

0 comments on commit 705547e

Please sign in to comment.