Skip to content

Commit

Permalink
Renamed static members and moved init into ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Feb 23, 2016
1 parent 1c177d0 commit d266582
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 76 deletions.
3 changes: 1 addition & 2 deletions src/server/qgis_map_serv.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ int fcgi_accept()


int main( int argc, char * argv[] ) int main( int argc, char * argv[] )
{ {
QgsServer server; QgsServer server( argc, argv );
server.init( argc, argv );
// Starts FCGI loop // Starts FCGI loop
while ( fcgi_accept() >= 0 ) while ( fcgi_accept() >= 0 )
{ {
Expand Down
97 changes: 52 additions & 45 deletions src/server/qgsserver.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -54,24 +54,36 @@
#include <stdlib.h> #include <stdlib.h>




// Static initialisers, default values for fcgi server
QgsApplication* QgsServer::mQgsApplication = nullptr;
bool QgsServer::mInitialised = false;
bool QgsServer::mCaptureOutput = false;
char* QgsServer::mArgv[1];
int QgsServer::mArgc = 1;
QString QgsServer::mConfigFilePath;
QgsMapRenderer* QgsServer::mMapRenderer = nullptr;
QgsCapabilitiesCache* QgsServer::mCapabilitiesCache;


// Server status static initialisers.
// Default values are for C++, SIP bindings will override their
// options in in init()

QString QgsServer::sConfigFilePath = QString();
QgsCapabilitiesCache* QgsServer::sCapabilitiesCache = nullptr;
QgsMapRenderer* QgsServer::sMapRenderer = nullptr;
#ifdef HAVE_SERVER_PYTHON_PLUGINS #ifdef HAVE_SERVER_PYTHON_PLUGINS
bool QgsServer::mInitPython = true; QgsServerInterfaceImpl*QgsServer::sServerInterface = nullptr;
QgsServerInterfaceImpl* QgsServer::mServerInterface = nullptr; bool QgsServer::sInitPython = true;
#endif #endif
// Initialization must run once for all servers
bool QgsServer::sInitialised = false;
char* QgsServer::sArgv[1];
int QgsServer::sArgc;
QgsApplication* QgsServer::sQgsApplication = nullptr;
bool QgsServer::sCaptureOutput = false;



QgsServer::QgsServer( int &argc, char **argv )
{
init( argc, argv );
}




QgsServer::QgsServer() QgsServer::QgsServer()
{ {
init();
} }




Expand Down Expand Up @@ -298,17 +310,18 @@ QString QgsServer::configPath( const QString& defaultConfigPath, const QMap<QStr
*/ */
bool QgsServer::init() bool QgsServer::init()
{ {
if ( mInitialised ) if ( sInitialised )
{ {
return false; return false;
} }
mArgv[0] = serverName().toUtf8().data();
mArgc = 1; sArgv[0] = serverName().toUtf8().data();
mCaptureOutput = true; sArgc = 1;
sCaptureOutput = true;
#ifdef HAVE_SERVER_PYTHON_PLUGINS #ifdef HAVE_SERVER_PYTHON_PLUGINS
mInitPython = false; sInitPython = false;
#endif #endif
return init( mArgc , mArgv ); return init( sArgc , sArgv );
} }




Expand All @@ -317,7 +330,7 @@ bool QgsServer::init()
*/ */
bool QgsServer::init( int & argc, char ** argv ) bool QgsServer::init( int & argc, char ** argv )
{ {
if ( mInitialised ) if ( sInitialised )
{ {
return false; return false;
} }
Expand All @@ -336,7 +349,7 @@ bool QgsServer::init( int & argc, char ** argv )
QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, optionsPath ); QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, optionsPath );
} }


mQgsApplication = new QgsApplication( argc, argv, getenv( "DISPLAY" ), QString(), "server" ); sQgsApplication = new QgsApplication( argc, argv, getenv( "DISPLAY" ), QString(), "server" );


QCoreApplication::setOrganizationName( QgsApplication::QGIS_ORGANIZATION_NAME ); QCoreApplication::setOrganizationName( QgsApplication::QGIS_ORGANIZATION_NAME );
QCoreApplication::setOrganizationDomain( QgsApplication::QGIS_ORGANIZATION_DOMAIN ); QCoreApplication::setOrganizationDomain( QgsApplication::QGIS_ORGANIZATION_DOMAIN );
Expand Down Expand Up @@ -392,24 +405,24 @@ bool QgsServer::init( int & argc, char ** argv )
} }
if ( !defaultConfigFilePath.isEmpty() ) if ( !defaultConfigFilePath.isEmpty() )
{ {
mConfigFilePath = defaultConfigFilePath; sConfigFilePath = defaultConfigFilePath;
} }


//create cache for capabilities XML //create cache for capabilities XML
mCapabilitiesCache = new QgsCapabilitiesCache(); sCapabilitiesCache = new QgsCapabilitiesCache();
mMapRenderer = new QgsMapRenderer; sMapRenderer = new QgsMapRenderer;
mMapRenderer->setLabelingEngine( new QgsPalLabeling() ); sMapRenderer->setLabelingEngine( new QgsPalLabeling() );


#ifdef ENABLE_MS_TESTS #ifdef ENABLE_MS_TESTS
QgsFontUtils::loadStandardTestFonts( QStringList() << "Roman" << "Bold" ); QgsFontUtils::loadStandardTestFonts( QStringList() << "Roman" << "Bold" );
#endif #endif


#ifdef HAVE_SERVER_PYTHON_PLUGINS #ifdef HAVE_SERVER_PYTHON_PLUGINS
mServerInterface = new QgsServerInterfaceImpl( mCapabilitiesCache ); sServerInterface = new QgsServerInterfaceImpl( sCapabilitiesCache );
if ( mInitPython ) if ( sInitPython )
{ {
// Init plugins // Init plugins
if ( ! QgsServerPlugins::initPlugins( mServerInterface ) ) if ( ! QgsServerPlugins::initPlugins( sServerInterface ) )
{ {
QgsMessageLog::logMessage( "No server python plugins are available", "Server", QgsMessageLog::INFO ); QgsMessageLog::logMessage( "No server python plugins are available", "Server", QgsMessageLog::INFO );
} }
Expand All @@ -421,7 +434,7 @@ bool QgsServer::init( int & argc, char ** argv )
#endif #endif


QgsEditorWidgetRegistry::initEditors(); QgsEditorWidgetRegistry::initEditors();
mInitialised = true; sInitialised = true;
QgsMessageLog::logMessage( "Server initialized", "Server", QgsMessageLog::INFO ); QgsMessageLog::logMessage( "Server initialized", "Server", QgsMessageLog::INFO );
return true; return true;
} }
Expand All @@ -442,12 +455,6 @@ void QgsServer::putenv( const QString &var, const QString &val )
*/ */
QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryString ) QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryString )
{ {
// Run init if handleRequest was called without previously initialising
// the server
if ( ! mInitialised )
{
init();
}


/* /*
* This is mainly for python bindings, passing QUERY_STRING * This is mainly for python bindings, passing QUERY_STRING
Expand All @@ -459,15 +466,15 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
int logLevel = QgsServerLogger::instance()->logLevel(); int logLevel = QgsServerLogger::instance()->logLevel();
QTime time; //used for measuring request time if loglevel < 1 QTime time; //used for measuring request time if loglevel < 1
QgsMapLayerRegistry::instance()->removeAllMapLayers(); QgsMapLayerRegistry::instance()->removeAllMapLayers();
mQgsApplication->processEvents(); sQgsApplication->processEvents();
if ( logLevel < 1 ) if ( logLevel < 1 )
{ {
time.start(); time.start();
printRequestInfos(); printRequestInfos();
} }


//Request handler //Request handler
QScopedPointer<QgsRequestHandler> theRequestHandler( createRequestHandler( mCaptureOutput ) ); QScopedPointer<QgsRequestHandler> theRequestHandler( createRequestHandler( sCaptureOutput ) );


try try
{ {
Expand All @@ -482,10 +489,10 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri


#ifdef HAVE_SERVER_PYTHON_PLUGINS #ifdef HAVE_SERVER_PYTHON_PLUGINS
// Set the request handler into the interface for plugins to manipulate it // Set the request handler into the interface for plugins to manipulate it
mServerInterface->setRequestHandler( theRequestHandler.data() ); sServerInterface->setRequestHandler( theRequestHandler.data() );
// Iterate filters and call their requestReady() method // Iterate filters and call their requestReady() method
QgsServerFiltersMap::const_iterator filtersIterator; QgsServerFiltersMap::const_iterator filtersIterator;
QgsServerFiltersMap filters = mServerInterface->filters(); QgsServerFiltersMap filters = sServerInterface->filters();
for ( filtersIterator = filters.constBegin(); filtersIterator != filters.constEnd(); ++filtersIterator ) for ( filtersIterator = filters.constBegin(); filtersIterator != filters.constEnd(); ++filtersIterator )
{ {
filtersIterator.value()->requestReady(); filtersIterator.value()->requestReady();
Expand All @@ -494,22 +501,22 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
//Pass the filters to the requestHandler, this is needed for the following reasons: //Pass the filters to the requestHandler, this is needed for the following reasons:
// 1. allow core services to access plugin filters and implement thir own plugin hooks // 1. allow core services to access plugin filters and implement thir own plugin hooks
// 2. allow requestHandler to call sendResponse plugin hook // 2. allow requestHandler to call sendResponse plugin hook
theRequestHandler->setPluginFilters( mServerInterface->filters() ); theRequestHandler->setPluginFilters( sServerInterface->filters() );
#endif #endif


// Copy the parameters map // Copy the parameters map
QMap<QString, QString> parameterMap( theRequestHandler->parameterMap() ); QMap<QString, QString> parameterMap( theRequestHandler->parameterMap() );
#ifdef HAVE_SERVER_PYTHON_PLUGINS #ifdef HAVE_SERVER_PYTHON_PLUGINS
const QgsAccessControl* accessControl = nullptr; const QgsAccessControl* accessControl = nullptr;
accessControl = mServerInterface->accessControls(); accessControl = sServerInterface->accessControls();
#endif #endif


printRequestParameters( parameterMap, logLevel ); printRequestParameters( parameterMap, logLevel );
QMap<QString, QString>::const_iterator paramIt; QMap<QString, QString>::const_iterator paramIt;
//Config file path //Config file path
QString configFilePath = configPath( mConfigFilePath, parameterMap ); QString configFilePath = configPath( sConfigFilePath, parameterMap );
#ifdef HAVE_SERVER_PYTHON_PLUGINS #ifdef HAVE_SERVER_PYTHON_PLUGINS
mServerInterface->setConfigFilePath( configFilePath ); sServerInterface->setConfigFilePath( configFilePath );
#endif #endif
//Service parameter //Service parameter
QString serviceString = theRequestHandler->parameter( "SERVICE" ); QString serviceString = theRequestHandler->parameter( "SERVICE" );
Expand Down Expand Up @@ -606,8 +613,8 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
, parameterMap , parameterMap
, p , p
, theRequestHandler.data() , theRequestHandler.data()
, mMapRenderer , sMapRenderer
, mCapabilitiesCache , sCapabilitiesCache
#ifdef HAVE_SERVER_PYTHON_PLUGINS #ifdef HAVE_SERVER_PYTHON_PLUGINS
, accessControl , accessControl
#endif #endif
Expand All @@ -623,14 +630,14 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri


#ifdef HAVE_SERVER_PYTHON_PLUGINS #ifdef HAVE_SERVER_PYTHON_PLUGINS
// Iterate filters and call their responseComplete() method // Iterate filters and call their responseComplete() method
filters = mServerInterface->filters(); filters = sServerInterface->filters();
for ( filtersIterator = filters.constBegin(); filtersIterator != filters.constEnd(); ++filtersIterator ) for ( filtersIterator = filters.constBegin(); filtersIterator != filters.constEnd(); ++filtersIterator )
{ {
filtersIterator.value()->responseComplete(); filtersIterator.value()->responseComplete();
} }
// We are done using theRequestHandler in plugins, make sure we don't access // We are done using theRequestHandler in plugins, make sure we don't access
// to a deleted request handler from Python bindings // to a deleted request handler from Python bindings
mServerInterface->clearRequestHandler(); sServerInterface->clearRequestHandler();
#endif #endif


theRequestHandler->sendResponse(); theRequestHandler->sendResponse();
Expand Down
39 changes: 23 additions & 16 deletions src/server/qgsserver.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@
class SERVER_EXPORT QgsServer class SERVER_EXPORT QgsServer
{ {
public: public:
/**
* Standard ctor for CGI/FCGI
* @note Not available in Python bindings
*/
QgsServer( int & argc, char ** argv );
//! The following is mainly for python bindings, that do not pass argc/argv
QgsServer(); QgsServer();
~QgsServer(); ~QgsServer();

/** Server initialization: intialise QGIS ang QT core application. /** Server initialization: intialise QGIS ang QT core application.
* This method is automatically called by handleRequest if it wasn't
* explicitly called before
* @note Not available in Python bindings * @note Not available in Python bindings
*/ */
static bool init( int & argc, char ** argv ); static bool init( int & argc, char ** argv );
Expand Down Expand Up @@ -84,7 +89,7 @@ class SERVER_EXPORT QgsServer


/** Returns a pointer to the server interface */ /** Returns a pointer to the server interface */
#ifdef HAVE_SERVER_PYTHON_PLUGINS #ifdef HAVE_SERVER_PYTHON_PLUGINS
QgsServerInterfaceImpl* serverInterface() { return mServerInterface; } QgsServerInterfaceImpl* serverInterface() { return sServerInterface; }
#endif #endif


private: private:
Expand All @@ -106,21 +111,23 @@ class SERVER_EXPORT QgsServer
//! Create and return a request handler instance //! Create and return a request handler instance
static QgsRequestHandler* createRequestHandler( const bool captureOutput = false ); static QgsRequestHandler* createRequestHandler( const bool captureOutput = false );


// Server status
static QString mConfigFilePath;
static QgsCapabilitiesCache* mCapabilitiesCache;
static QgsMapRenderer* mMapRenderer;
#ifdef HAVE_SERVER_PYTHON_PLUGINS
static QgsServerInterfaceImpl* mServerInterface;
static bool mInitPython;
#endif
static bool mInitialised;
static char* mArgv[1];
static int mArgc;
static QgsApplication* mQgsApplication;
static bool mCaptureOutput;
// Return the server name // Return the server name
static QString &serverName(); static QString &serverName();

// Status
static QString sConfigFilePath;
static QgsCapabilitiesCache* sCapabilitiesCache;
static QgsMapRenderer* sMapRenderer;
#ifdef HAVE_SERVER_PYTHON_PLUGINS
static QgsServerInterfaceImpl* sServerInterface;
static bool sInitPython;
#endif
//! Initialization must run once for all servers
static bool sInitialised;
static char* sArgv[1];
static int sArgc;
static QgsApplication* sQgsApplication;
static bool sCaptureOutput;
}; };
#endif // QGSSERVER_H #endif // QGSSERVER_H


22 changes: 11 additions & 11 deletions src/server/qgsserverplugins.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@




// Initialize static members // Initialize static members
QgsPythonUtils* QgsServerPlugins::mPythonUtils; QgsPythonUtils* QgsServerPlugins::sPythonUtils;




QgsServerPlugins::QgsServerPlugins() QgsServerPlugins::QgsServerPlugins()
{ {
} }



// Construct on first use
// Initialize static members
QStringList &QgsServerPlugins::serverPlugins() QStringList &QgsServerPlugins::serverPlugins()
{ {
static QStringList* pluginList = new QStringList(); static QStringList* pluginList = new QStringList();
return *pluginList; return *pluginList;
} }



// This code is mainly borrowed from QGIS desktop Python plugin initialization // This code is mainly borrowed from QGIS desktop Python plugin initialization
bool QgsServerPlugins::initPlugins( QgsServerInterface *interface ) bool QgsServerPlugins::initPlugins( QgsServerInterface *interface )
{ {
Expand Down Expand Up @@ -81,10 +81,10 @@ bool QgsServerPlugins::initPlugins( QgsServerInterface *interface )
} }


QgsDebugMsg( "Python support library's instance() symbol resolved." ); QgsDebugMsg( "Python support library's instance() symbol resolved." );
mPythonUtils = pythonlib_inst(); sPythonUtils = pythonlib_inst();
mPythonUtils->initServerPython( interface ); sPythonUtils->initServerPython( interface );


if ( mPythonUtils && mPythonUtils->isEnabled() ) if ( sPythonUtils && sPythonUtils->isEnabled() )
{ {
QgsDebugMsg( "Python support ENABLED :-)" ); QgsDebugMsg( "Python support ENABLED :-)" );
} }
Expand All @@ -96,17 +96,17 @@ bool QgsServerPlugins::initPlugins( QgsServerInterface *interface )


//Init plugins: loads a list of installed plugins and filter them //Init plugins: loads a list of installed plugins and filter them
//for "server" metadata //for "server" metadata
QListIterator<QString> plugins( mPythonUtils->pluginList() ); QListIterator<QString> plugins( sPythonUtils->pluginList() );
bool atLeastOneEnabled = false; bool atLeastOneEnabled = false;
while ( plugins.hasNext() ) while ( plugins.hasNext() )
{ {
QString pluginName = plugins.next(); QString pluginName = plugins.next();
QString pluginService = mPythonUtils->getPluginMetadata( pluginName, "server" ); QString pluginService = sPythonUtils->getPluginMetadata( pluginName, "server" );
if ( pluginService == "True" ) if ( pluginService == "True" )
{ {
if ( mPythonUtils->loadPlugin( pluginName ) ) if ( sPythonUtils->loadPlugin( pluginName ) )
{ {
if ( mPythonUtils->startServerPlugin( pluginName ) ) if ( sPythonUtils->startServerPlugin( pluginName ) )
{ {
atLeastOneEnabled = true; atLeastOneEnabled = true;
serverPlugins().append( pluginName ); serverPlugins().append( pluginName );
Expand All @@ -123,7 +123,7 @@ bool QgsServerPlugins::initPlugins( QgsServerInterface *interface )
} }
} }
} }
return mPythonUtils && mPythonUtils->isEnabled() && atLeastOneEnabled; return sPythonUtils && sPythonUtils->isEnabled() && atLeastOneEnabled;
} }




4 changes: 2 additions & 2 deletions src/server/qgsserverplugins.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class SERVER_EXPORT QgsServerPlugins
* @return bool true on success * @return bool true on success
*/ */
static bool initPlugins( QgsServerInterface* interface ); static bool initPlugins( QgsServerInterface* interface );
//! Pointer to QgsPythonUtils
static QgsPythonUtils* mPythonUtils;
//! List of available server plugin names //! List of available server plugin names
static QStringList& serverPlugins(); static QStringList& serverPlugins();
//! Pointer to QgsPythonUtils
static QgsPythonUtils* sPythonUtils;
}; };


#endif // QGSSERVERPLUGINS_H #endif // QGSSERVERPLUGINS_H

0 comments on commit d266582

Please sign in to comment.