Skip to content

Commit aeb5ff2

Browse files
committedSep 25, 2016
[Server 3.0] now needs a qApp
1 parent 7a685f1 commit aeb5ff2

File tree

5 files changed

+35
-63
lines changed

5 files changed

+35
-63
lines changed
 

‎python/server/qgsserver.sip

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,11 @@ class QgsServer
162162
%End
163163

164164
public:
165-
QgsServer();
166-
~QgsServer();
167-
/** Server initialization: intialise QGIS ang QT core application.
168-
* This method is automatically called by handleRequest if it wasn't
169-
* explicitly called before
170-
* @note Not available in Python bindings
165+
/** Creates the server instance
166+
* @param captureOutput set to false for stdout output (FCGI)
171167
*/
172-
//void init( int argc, char ** argv );
173-
//! The following is mainly for python bindings, that do not pass argc/argv
174-
void init();
168+
QgsServer( bool captureOutput = true );
169+
~QgsServer();
175170

176171
/** Set environment variable
177172
* @param var environment variable name

‎src/server/qgis_map_serv.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgsserver.h"
2222

2323
#include <fcgi_stdio.h>
24+
#include <stdlib.h>
2425

2526
int fcgi_accept()
2627
{
@@ -36,12 +37,14 @@ int fcgi_accept()
3637

3738
int main( int argc, char * argv[] )
3839
{
39-
QgsServer server( argc, argv );
40+
QgsApplication app( argc, argv, getenv( "DISPLAY" ), QString(), "server" );
41+
QgsServer server( false );
4042
// Starts FCGI loop
4143
while ( fcgi_accept() >= 0 )
4244
{
4345
server.handleRequest();
4446
}
47+
app.exitQgis();
4548
return 0;
4649
}
4750

‎src/server/qgsserver.cpp

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,25 @@ bool QgsServer::sInitPython = true;
6868
#endif
6969
// Initialization must run once for all servers
7070
bool QgsServer::sInitialised = false;
71-
char* QgsServer::sArgv[1];
72-
int QgsServer::sArgc;
73-
QgsApplication* QgsServer::sQgsApplication = nullptr;
74-
bool QgsServer::sCaptureOutput = false;
71+
bool QgsServer::sCaptureOutput = true;
7572

7673

7774

78-
QgsServer::QgsServer( int &argc, char **argv )
79-
{
80-
init( argc, argv );
81-
saveEnvVars();
82-
}
83-
84-
85-
QgsServer::QgsServer()
75+
QgsServer::QgsServer( bool captureOutput )
8676
{
77+
// Must be already instanciated
78+
if ( qApp == nullptr )
79+
{
80+
abort();
81+
}
82+
sCaptureOutput = captureOutput;
8783
init();
8884
saveEnvVars();
8985
}
9086

9187

9288
QgsServer::~QgsServer()
9389
{
94-
if ( sQgsApplication )
95-
sQgsApplication->exitQgis();
9690
}
9791

9892

@@ -310,34 +304,18 @@ QString QgsServer::configPath( const QString& defaultConfigPath, const QMap<QStr
310304

311305

312306
/**
313-
* This is used in python bindings only
307+
* Server initialization
314308
*/
315-
bool QgsServer::init()
309+
bool QgsServer::init( )
316310
{
317311
if ( sInitialised )
318312
{
319313
return false;
320314
}
321315

322-
sArgv[0] = serverName().toUtf8().data();
323-
sArgc = 1;
324-
sCaptureOutput = true;
325316
#ifdef HAVE_SERVER_PYTHON_PLUGINS
326317
sInitPython = false;
327318
#endif
328-
return init( sArgc , sArgv );
329-
}
330-
331-
332-
/**
333-
* Server initialization
334-
*/
335-
bool QgsServer::init( int & argc, char ** argv )
336-
{
337-
if ( sInitialised )
338-
{
339-
return false;
340-
}
341319

342320
QgsServerLogger::instance();
343321

@@ -353,8 +331,6 @@ bool QgsServer::init( int & argc, char ** argv )
353331
QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, optionsPath );
354332
}
355333

356-
sQgsApplication = new QgsApplication( argc, argv, getenv( "DISPLAY" ), QString(), "server" );
357-
358334
QCoreApplication::setOrganizationName( QgsApplication::QGIS_ORGANIZATION_NAME );
359335
QCoreApplication::setOrganizationDomain( QgsApplication::QGIS_ORGANIZATION_DOMAIN );
360336
QCoreApplication::setApplicationName( QgsApplication::QGIS_APPLICATION_NAME );
@@ -410,7 +386,6 @@ bool QgsServer::init( int & argc, char ** argv )
410386
// Store the config file path
411387
sConfigFilePath = new QString( defaultConfigFilePath );
412388

413-
414389
//create cache for capabilities XML
415390
sCapabilitiesCache = new QgsCapabilitiesCache();
416391
sMapRenderer = new QgsMapRenderer;
@@ -482,7 +457,8 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
482457
// performances and memory in the long run
483458
sMapRenderer->rendererContext()->setExpressionContext( QgsExpressionContext() );
484459

485-
sQgsApplication->processEvents();
460+
qApp->processEvents();
461+
486462
if ( logLevel < 1 )
487463
{
488464
time.start();

‎src/server/qgsserver.h

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,12 @@
4747
class SERVER_EXPORT QgsServer
4848
{
4949
public:
50-
/**
51-
* Standard ctor for CGI/FCGI
52-
* @note Not available in Python bindings
50+
/** Creates the server instance
51+
* @param captureOutput set to false for stdout output (FCGI)
5352
*/
54-
QgsServer( int & argc, char ** argv );
55-
//! The following is mainly for python bindings, that do not pass argc/argv
56-
QgsServer();
53+
QgsServer( bool captureOutput = true );
5754
~QgsServer();
5855

59-
/** Server initialization: intialise QGIS ang QT core application.
60-
* @note Not available in Python bindings
61-
*/
62-
static bool init( int & argc, char ** argv );
63-
//! The following is mainly for python bindings, that do not pass argc/argv
64-
static bool init();
65-
6656
/** Set environment variable
6757
* @param var environment variable name
6858
* @param val value
@@ -94,6 +84,9 @@ class SERVER_EXPORT QgsServer
9484

9585
private:
9686

87+
/** Server initialization */
88+
static bool init();
89+
9790
void saveEnvVars();
9891

9992
/** Saves environment variable into mEnvironmentVariables if defined*/
@@ -130,9 +123,6 @@ class SERVER_EXPORT QgsServer
130123
#endif
131124
//! Initialization must run once for all servers
132125
static bool sInitialised;
133-
static char* sArgv[1];
134-
static int sArgc;
135-
static QgsApplication* sQgsApplication;
136126
static bool sCaptureOutput;
137127

138128
/** Pass important environment variables to the fcgi processes*/

‎tests/src/python/test_qgsserver.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import email
2222
from io import StringIO
2323
from qgis.server import QgsServer
24-
from qgis.core import QgsMessageLog
24+
from qgis.core import QgsMessageLog, QgsApplication
2525
from qgis.testing import unittest
2626
from utilities import unitTestDataPath
2727
import osgeo.gdal
@@ -32,6 +32,14 @@
3232

3333
class TestQgsServer(unittest.TestCase):
3434

35+
@classmethod
36+
def setUpClass(cls):
37+
cls.app = QgsApplication([], False)
38+
39+
@classmethod
40+
def tearDownClass(cls):
41+
cls.app.exitQgis()
42+
3543
def setUp(self):
3644
"""Create the server instance"""
3745
self.testdata_path = unitTestDataPath('qgis_server') + '/'

0 commit comments

Comments
 (0)
Please sign in to comment.