Skip to content
Permalink
Browse files

Remove Service Entry class and move name/version infos in QgsService

  • Loading branch information
dmarteau committed Jan 10, 2017
1 parent 5355558 commit 7600919517359e893100114e4d855a250dc4b6ef
@@ -42,6 +42,16 @@ class QgsService
//! Destructor
virtual ~QgsService();

/**
* @return the name of the service
*/
virtual QString name() const = 0;

/**
* @return the version of the service
*/
virtual QString version() const = 0;

/**
* Return true if the given method is supported for that
* service.
@@ -23,17 +23,17 @@
* Class defining the cegistry manager for QGIS server services
*
* This class provides methods for registering and retrieving
* services. Note that the regstiry does not hord ownership of
* registered service but vill call the 'release' method on cleanup)
* services.
*
* IMPORTANT: The registry hold ownership of registered services and
* will call 'delete' on cleanup
*
*/
class QgsServiceRegistry
{
%TypeHeaderCode
#include "qgsserviceregistry.h"
#include "qgsservice.h"
#include "qgsserverrequest.h"
#include "qgsserverresponse.h"
%End
public:

@@ -58,11 +58,9 @@ class QgsServiceRegistry
* services. A module may register multiple services.
* The registry gain ownership of services.
*
* @param name the name of the service
* @param service a QgsServerResponse to be registered
* @param version the service version
*/
void registerService( const QString& name, const QString& version, QgsService* service /Transfer/ );
void registerService( QgsService* service /Transfer/ );

/**
* Initialize registry, load modules and auto register services
@@ -44,6 +44,16 @@ class SERVER_EXPORT QgsService
//! Destructor
virtual ~QgsService();

/**
* @return the name of the service
*/
virtual QString name() const = 0;

/**
* @return the version of the service
*/
virtual QString version() const = 0;

/**
* Return true if the given method is supported for that
* service.
@@ -73,28 +73,6 @@ namespace {

} // namespace

class QgsServiceEntry
{
public:
~QgsServiceEntry()
{
// We have the ownership by design
// XXX Take care of /Transfer/ decorator for registerService in sip
QgsDebugMsg( QString("Deleting service %1 %2").arg(mName, mVersion) );
delete mService;
}

QgsServiceEntry( const QString& name, QgsService* service, const QString& version )
: mName(name)
, mService(service)
, mVersion(version)
{}

QString mName;
QgsService* mService;
QString mVersion;
};

QgsServiceRegistry::QgsServiceRegistry()
{
//TODO
@@ -115,10 +93,10 @@ QgsService* QgsServiceRegistry::getService( const QString& name, const QString&
if( v != mVersions.constEnd() )
{
key = version.isEmpty() ? v->second : makeServiceKey(name, version );
ServiceTable::iterator it = mServices.find(key);
if( it != mServices.end() )
ServiceTable::const_iterator it = mServices.constFind(key);
if( it != mServices.constEnd() )
{
service = (*it)->mService;
service = it->get();
}
else
{
@@ -132,8 +110,11 @@ QgsService* QgsServiceRegistry::getService( const QString& name, const QString&
return service;
}

void QgsServiceRegistry::registerService( const QString& name, const QString& version, QgsService* service )
void QgsServiceRegistry::registerService( QgsService* service )
{
QString name = service->name();
QString version = service->version();

// Test if service is already registered
QString key = makeServiceKey( name, version );
if( mServices.constFind(key) != mServices.constEnd() )
@@ -143,7 +124,7 @@ void QgsServiceRegistry::registerService( const QString& name, const QString& v
}

QgsMessageLog::logMessage( QString( "Adding service %1 %2").arg(name,version) );
mServices.insert( key, std::make_shared<QgsServiceEntry>( name, service, version ) );
mServices.insert( key, std::shared_ptr<QgsService>(service) );

// Check the default version
// and replace with te new one if it has a higher version
@@ -29,7 +29,6 @@
#include <memory>

class QgsService;
class QgsServiceEntry;

/**
* \ingroup server
@@ -65,18 +64,16 @@ class SERVER_EXPORT QgsServiceRegistry
QgsService* getService( const QString& name, const QString& version = QString() );

/**
* Register a service by its name
* Register a service by its name and version
*
* This method is intended to be called by modules for registering
* services. A module may register multiple services.
*
* The registry gain ownership of services and will call 'delete' on cleanup
*
* @param name the name of the service
* @param service a QgsServerResponse to be registered
* @param version the version string for the service (required)
*/
void registerService( const QString& name, const QString& version, QgsService* service );
void registerService( QgsService* service );

/**
* Initialize registry, load modules and auto register services
@@ -94,7 +91,7 @@ class SERVER_EXPORT QgsServiceRegistry
void cleanUp();

private:
typedef QHash<QString, std::shared_ptr<QgsServiceEntry> > ServiceTable;
typedef QHash<QString, std::shared_ptr<QgsService> > ServiceTable;
typedef QHash<QString, QPair<QString, QString> > VersionTable;

QgsServiceNativeLoader mNativeLoader;
@@ -31,9 +31,17 @@ def io(self):

class MyService(QgsService):

def __init__(self, response):
def __init__(self, name, version, response):
QgsService.__init__(self)
self._response = response
self._name = name
self._version = version

def name(self):
return self._name

def version(self):
return self._version

def executeRequest( self, request, response ):

@@ -51,9 +59,9 @@ def test_register(self):

reg = QgsServiceRegistry()

myserv = MyService("Hello world")
myserv = MyService("STUFF", "1.0", "Hello world")

reg.registerService("STUFF", "1.0", myserv )
reg.registerService( myserv )

# Retrieve service
request = QgsServerRequest("http://DoStufff", QgsServerRequest.GetMethod)
@@ -71,16 +79,15 @@ def test_register(self):
def test_version_registration(self):

reg = QgsServiceRegistry()
myserv1 = MyService("1.0")
myserv2 = MyService("1.1")
myserv1 = MyService("STUFF", "1.0", "Hello")
myserv2 = MyService("STUFF", "1.1", "Hello")

reg.registerService("STUFF", myserv1._response, myserv1 )
reg.registerService("STUFF", myserv2._response, myserv2)

reg.registerService( myserv1 )
reg.registerService( myserv2)

service = reg.getService("STUFF")
self.assertIsNotNone(service)
self.assertEquals(service._response, myserv2._response)
self.assertEquals(service.version(), "1.1")

service = reg.getService("STUFF", "2.0")
self.assertIsNone(service)

0 comments on commit 7600919

Please sign in to comment.
You can’t perform that action at this time.