Skip to content

Commit d266582

Browse files
committed
Renamed static members and moved init into ctor
1 parent 1c177d0 commit d266582

File tree

5 files changed

+89
-76
lines changed

5 files changed

+89
-76
lines changed

src/server/qgis_map_serv.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ int fcgi_accept()
3636

3737
int main( int argc, char * argv[] )
3838
{
39-
QgsServer server;
40-
server.init( argc, argv );
39+
QgsServer server( argc, argv );
4140
// Starts FCGI loop
4241
while ( fcgi_accept() >= 0 )
4342
{

src/server/qgsserver.cpp

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,36 @@
5454
#include <stdlib.h>
5555

5656

57-
// Static initialisers, default values for fcgi server
58-
QgsApplication* QgsServer::mQgsApplication = nullptr;
59-
bool QgsServer::mInitialised = false;
60-
bool QgsServer::mCaptureOutput = false;
61-
char* QgsServer::mArgv[1];
62-
int QgsServer::mArgc = 1;
63-
QString QgsServer::mConfigFilePath;
64-
QgsMapRenderer* QgsServer::mMapRenderer = nullptr;
65-
QgsCapabilitiesCache* QgsServer::mCapabilitiesCache;
6657

58+
// Server status static initialisers.
59+
// Default values are for C++, SIP bindings will override their
60+
// options in in init()
61+
62+
QString QgsServer::sConfigFilePath = QString();
63+
QgsCapabilitiesCache* QgsServer::sCapabilitiesCache = nullptr;
64+
QgsMapRenderer* QgsServer::sMapRenderer = nullptr;
6765
#ifdef HAVE_SERVER_PYTHON_PLUGINS
68-
bool QgsServer::mInitPython = true;
69-
QgsServerInterfaceImpl* QgsServer::mServerInterface = nullptr;
66+
QgsServerInterfaceImpl*QgsServer::sServerInterface = nullptr;
67+
bool QgsServer::sInitPython = true;
7068
#endif
69+
// Initialization must run once for all servers
70+
bool QgsServer::sInitialised = false;
71+
char* QgsServer::sArgv[1];
72+
int QgsServer::sArgc;
73+
QgsApplication* QgsServer::sQgsApplication = nullptr;
74+
bool QgsServer::sCaptureOutput = false;
75+
76+
77+
78+
QgsServer::QgsServer( int &argc, char **argv )
79+
{
80+
init( argc, argv );
81+
}
7182

7283

7384
QgsServer::QgsServer()
7485
{
86+
init();
7587
}
7688

7789

@@ -298,17 +310,18 @@ QString QgsServer::configPath( const QString& defaultConfigPath, const QMap<QStr
298310
*/
299311
bool QgsServer::init()
300312
{
301-
if ( mInitialised )
313+
if ( sInitialised )
302314
{
303315
return false;
304316
}
305-
mArgv[0] = serverName().toUtf8().data();
306-
mArgc = 1;
307-
mCaptureOutput = true;
317+
318+
sArgv[0] = serverName().toUtf8().data();
319+
sArgc = 1;
320+
sCaptureOutput = true;
308321
#ifdef HAVE_SERVER_PYTHON_PLUGINS
309-
mInitPython = false;
322+
sInitPython = false;
310323
#endif
311-
return init( mArgc , mArgv );
324+
return init( sArgc , sArgv );
312325
}
313326

314327

@@ -317,7 +330,7 @@ bool QgsServer::init()
317330
*/
318331
bool QgsServer::init( int & argc, char ** argv )
319332
{
320-
if ( mInitialised )
333+
if ( sInitialised )
321334
{
322335
return false;
323336
}
@@ -336,7 +349,7 @@ bool QgsServer::init( int & argc, char ** argv )
336349
QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, optionsPath );
337350
}
338351

339-
mQgsApplication = new QgsApplication( argc, argv, getenv( "DISPLAY" ), QString(), "server" );
352+
sQgsApplication = new QgsApplication( argc, argv, getenv( "DISPLAY" ), QString(), "server" );
340353

341354
QCoreApplication::setOrganizationName( QgsApplication::QGIS_ORGANIZATION_NAME );
342355
QCoreApplication::setOrganizationDomain( QgsApplication::QGIS_ORGANIZATION_DOMAIN );
@@ -392,24 +405,24 @@ bool QgsServer::init( int & argc, char ** argv )
392405
}
393406
if ( !defaultConfigFilePath.isEmpty() )
394407
{
395-
mConfigFilePath = defaultConfigFilePath;
408+
sConfigFilePath = defaultConfigFilePath;
396409
}
397410

398411
//create cache for capabilities XML
399-
mCapabilitiesCache = new QgsCapabilitiesCache();
400-
mMapRenderer = new QgsMapRenderer;
401-
mMapRenderer->setLabelingEngine( new QgsPalLabeling() );
412+
sCapabilitiesCache = new QgsCapabilitiesCache();
413+
sMapRenderer = new QgsMapRenderer;
414+
sMapRenderer->setLabelingEngine( new QgsPalLabeling() );
402415

403416
#ifdef ENABLE_MS_TESTS
404417
QgsFontUtils::loadStandardTestFonts( QStringList() << "Roman" << "Bold" );
405418
#endif
406419

407420
#ifdef HAVE_SERVER_PYTHON_PLUGINS
408-
mServerInterface = new QgsServerInterfaceImpl( mCapabilitiesCache );
409-
if ( mInitPython )
421+
sServerInterface = new QgsServerInterfaceImpl( sCapabilitiesCache );
422+
if ( sInitPython )
410423
{
411424
// Init plugins
412-
if ( ! QgsServerPlugins::initPlugins( mServerInterface ) )
425+
if ( ! QgsServerPlugins::initPlugins( sServerInterface ) )
413426
{
414427
QgsMessageLog::logMessage( "No server python plugins are available", "Server", QgsMessageLog::INFO );
415428
}
@@ -421,7 +434,7 @@ bool QgsServer::init( int & argc, char ** argv )
421434
#endif
422435

423436
QgsEditorWidgetRegistry::initEditors();
424-
mInitialised = true;
437+
sInitialised = true;
425438
QgsMessageLog::logMessage( "Server initialized", "Server", QgsMessageLog::INFO );
426439
return true;
427440
}
@@ -442,12 +455,6 @@ void QgsServer::putenv( const QString &var, const QString &val )
442455
*/
443456
QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryString )
444457
{
445-
// Run init if handleRequest was called without previously initialising
446-
// the server
447-
if ( ! mInitialised )
448-
{
449-
init();
450-
}
451458

452459
/*
453460
* This is mainly for python bindings, passing QUERY_STRING
@@ -459,15 +466,15 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
459466
int logLevel = QgsServerLogger::instance()->logLevel();
460467
QTime time; //used for measuring request time if loglevel < 1
461468
QgsMapLayerRegistry::instance()->removeAllMapLayers();
462-
mQgsApplication->processEvents();
469+
sQgsApplication->processEvents();
463470
if ( logLevel < 1 )
464471
{
465472
time.start();
466473
printRequestInfos();
467474
}
468475

469476
//Request handler
470-
QScopedPointer<QgsRequestHandler> theRequestHandler( createRequestHandler( mCaptureOutput ) );
477+
QScopedPointer<QgsRequestHandler> theRequestHandler( createRequestHandler( sCaptureOutput ) );
471478

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

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

500507
// Copy the parameters map
501508
QMap<QString, QString> parameterMap( theRequestHandler->parameterMap() );
502509
#ifdef HAVE_SERVER_PYTHON_PLUGINS
503510
const QgsAccessControl* accessControl = nullptr;
504-
accessControl = mServerInterface->accessControls();
511+
accessControl = sServerInterface->accessControls();
505512
#endif
506513

507514
printRequestParameters( parameterMap, logLevel );
508515
QMap<QString, QString>::const_iterator paramIt;
509516
//Config file path
510-
QString configFilePath = configPath( mConfigFilePath, parameterMap );
517+
QString configFilePath = configPath( sConfigFilePath, parameterMap );
511518
#ifdef HAVE_SERVER_PYTHON_PLUGINS
512-
mServerInterface->setConfigFilePath( configFilePath );
519+
sServerInterface->setConfigFilePath( configFilePath );
513520
#endif
514521
//Service parameter
515522
QString serviceString = theRequestHandler->parameter( "SERVICE" );
@@ -606,8 +613,8 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
606613
, parameterMap
607614
, p
608615
, theRequestHandler.data()
609-
, mMapRenderer
610-
, mCapabilitiesCache
616+
, sMapRenderer
617+
, sCapabilitiesCache
611618
#ifdef HAVE_SERVER_PYTHON_PLUGINS
612619
, accessControl
613620
#endif
@@ -623,14 +630,14 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
623630

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

636643
theRequestHandler->sendResponse();

src/server/qgsserver.h

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@
4747
class SERVER_EXPORT QgsServer
4848
{
4949
public:
50+
/**
51+
* Standard ctor for CGI/FCGI
52+
* @note Not available in Python bindings
53+
*/
54+
QgsServer( int & argc, char ** argv );
55+
//! The following is mainly for python bindings, that do not pass argc/argv
5056
QgsServer();
5157
~QgsServer();
58+
5259
/** Server initialization: intialise QGIS ang QT core application.
53-
* This method is automatically called by handleRequest if it wasn't
54-
* explicitly called before
5560
* @note Not available in Python bindings
5661
*/
5762
static bool init( int & argc, char ** argv );
@@ -84,7 +89,7 @@ class SERVER_EXPORT QgsServer
8489

8590
/** Returns a pointer to the server interface */
8691
#ifdef HAVE_SERVER_PYTHON_PLUGINS
87-
QgsServerInterfaceImpl* serverInterface() { return mServerInterface; }
92+
QgsServerInterfaceImpl* serverInterface() { return sServerInterface; }
8893
#endif
8994

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

109-
// Server status
110-
static QString mConfigFilePath;
111-
static QgsCapabilitiesCache* mCapabilitiesCache;
112-
static QgsMapRenderer* mMapRenderer;
113-
#ifdef HAVE_SERVER_PYTHON_PLUGINS
114-
static QgsServerInterfaceImpl* mServerInterface;
115-
static bool mInitPython;
116-
#endif
117-
static bool mInitialised;
118-
static char* mArgv[1];
119-
static int mArgc;
120-
static QgsApplication* mQgsApplication;
121-
static bool mCaptureOutput;
122114
// Return the server name
123115
static QString &serverName();
116+
117+
// Status
118+
static QString sConfigFilePath;
119+
static QgsCapabilitiesCache* sCapabilitiesCache;
120+
static QgsMapRenderer* sMapRenderer;
121+
#ifdef HAVE_SERVER_PYTHON_PLUGINS
122+
static QgsServerInterfaceImpl* sServerInterface;
123+
static bool sInitPython;
124+
#endif
125+
//! Initialization must run once for all servers
126+
static bool sInitialised;
127+
static char* sArgv[1];
128+
static int sArgc;
129+
static QgsApplication* sQgsApplication;
130+
static bool sCaptureOutput;
124131
};
125132
#endif // QGSSERVER_H
126133

src/server/qgsserverplugins.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@
2828

2929

3030
// Initialize static members
31-
QgsPythonUtils* QgsServerPlugins::mPythonUtils;
31+
QgsPythonUtils* QgsServerPlugins::sPythonUtils;
3232

3333

3434
QgsServerPlugins::QgsServerPlugins()
3535
{
3636
}
3737

38-
39-
// Initialize static members
38+
// Construct on first use
4039
QStringList &QgsServerPlugins::serverPlugins()
4140
{
4241
static QStringList* pluginList = new QStringList();
4342
return *pluginList;
4443
}
4544

45+
4646
// This code is mainly borrowed from QGIS desktop Python plugin initialization
4747
bool QgsServerPlugins::initPlugins( QgsServerInterface *interface )
4848
{
@@ -81,10 +81,10 @@ bool QgsServerPlugins::initPlugins( QgsServerInterface *interface )
8181
}
8282

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

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

9797
//Init plugins: loads a list of installed plugins and filter them
9898
//for "server" metadata
99-
QListIterator<QString> plugins( mPythonUtils->pluginList() );
99+
QListIterator<QString> plugins( sPythonUtils->pluginList() );
100100
bool atLeastOneEnabled = false;
101101
while ( plugins.hasNext() )
102102
{
103103
QString pluginName = plugins.next();
104-
QString pluginService = mPythonUtils->getPluginMetadata( pluginName, "server" );
104+
QString pluginService = sPythonUtils->getPluginMetadata( pluginName, "server" );
105105
if ( pluginService == "True" )
106106
{
107-
if ( mPythonUtils->loadPlugin( pluginName ) )
107+
if ( sPythonUtils->loadPlugin( pluginName ) )
108108
{
109-
if ( mPythonUtils->startServerPlugin( pluginName ) )
109+
if ( sPythonUtils->startServerPlugin( pluginName ) )
110110
{
111111
atLeastOneEnabled = true;
112112
serverPlugins().append( pluginName );
@@ -123,7 +123,7 @@ bool QgsServerPlugins::initPlugins( QgsServerInterface *interface )
123123
}
124124
}
125125
}
126-
return mPythonUtils && mPythonUtils->isEnabled() && atLeastOneEnabled;
126+
return sPythonUtils && sPythonUtils->isEnabled() && atLeastOneEnabled;
127127
}
128128

129129

src/server/qgsserverplugins.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class SERVER_EXPORT QgsServerPlugins
3737
* @return bool true on success
3838
*/
3939
static bool initPlugins( QgsServerInterface* interface );
40-
//! Pointer to QgsPythonUtils
41-
static QgsPythonUtils* mPythonUtils;
4240
//! List of available server plugin names
4341
static QStringList& serverPlugins();
42+
//! Pointer to QgsPythonUtils
43+
static QgsPythonUtils* sPythonUtils;
4444
};
4545

4646
#endif // QGSSERVERPLUGINS_H

0 commit comments

Comments
 (0)