Skip to content

Commit

Permalink
[Server 3.0] now needs a qApp
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Sep 25, 2016
1 parent 7a685f1 commit aeb5ff2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 63 deletions.
13 changes: 4 additions & 9 deletions python/server/qgsserver.sip
Expand Up @@ -162,16 +162,11 @@ class QgsServer
%End

public:
QgsServer();
~QgsServer();
/** 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
/** Creates the server instance
* @param captureOutput set to false for stdout output (FCGI)
*/
//void init( int argc, char ** argv );
//! The following is mainly for python bindings, that do not pass argc/argv
void init();
QgsServer( bool captureOutput = true );
~QgsServer();

/** Set environment variable
* @param var environment variable name
Expand Down
5 changes: 4 additions & 1 deletion src/server/qgis_map_serv.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgsserver.h"

#include <fcgi_stdio.h>
#include <stdlib.h>

int fcgi_accept()
{
Expand All @@ -36,12 +37,14 @@ int fcgi_accept()

int main( int argc, char * argv[] )
{
QgsServer server( argc, argv );
QgsApplication app( argc, argv, getenv( "DISPLAY" ), QString(), "server" );
QgsServer server( false );
// Starts FCGI loop
while ( fcgi_accept() >= 0 )
{
server.handleRequest();
}
app.exitQgis();
return 0;
}

48 changes: 12 additions & 36 deletions src/server/qgsserver.cpp
Expand Up @@ -68,31 +68,25 @@ bool QgsServer::sInitPython = true;
#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;
bool QgsServer::sCaptureOutput = true;



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


QgsServer::QgsServer()
QgsServer::QgsServer( bool captureOutput )
{
// Must be already instanciated
if ( qApp == nullptr )
{
abort();
}
sCaptureOutput = captureOutput;
init();
saveEnvVars();
}


QgsServer::~QgsServer()
{
if ( sQgsApplication )
sQgsApplication->exitQgis();
}


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


/**
* This is used in python bindings only
* Server initialization
*/
bool QgsServer::init()
bool QgsServer::init( )
{
if ( sInitialised )
{
return false;
}

sArgv[0] = serverName().toUtf8().data();
sArgc = 1;
sCaptureOutput = true;
#ifdef HAVE_SERVER_PYTHON_PLUGINS
sInitPython = false;
#endif
return init( sArgc , sArgv );
}


/**
* Server initialization
*/
bool QgsServer::init( int & argc, char ** argv )
{
if ( sInitialised )
{
return false;
}

QgsServerLogger::instance();

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

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

QCoreApplication::setOrganizationName( QgsApplication::QGIS_ORGANIZATION_NAME );
QCoreApplication::setOrganizationDomain( QgsApplication::QGIS_ORGANIZATION_DOMAIN );
QCoreApplication::setApplicationName( QgsApplication::QGIS_APPLICATION_NAME );
Expand Down Expand Up @@ -410,7 +386,6 @@ bool QgsServer::init( int & argc, char ** argv )
// Store the config file path
sConfigFilePath = new QString( defaultConfigFilePath );


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

sQgsApplication->processEvents();
qApp->processEvents();

if ( logLevel < 1 )
{
time.start();
Expand Down
22 changes: 6 additions & 16 deletions src/server/qgsserver.h
Expand Up @@ -47,22 +47,12 @@
class SERVER_EXPORT QgsServer
{
public:
/**
* Standard ctor for CGI/FCGI
* @note Not available in Python bindings
/** Creates the server instance
* @param captureOutput set to false for stdout output (FCGI)
*/
QgsServer( int & argc, char ** argv );
//! The following is mainly for python bindings, that do not pass argc/argv
QgsServer();
QgsServer( bool captureOutput = true );
~QgsServer();

/** Server initialization: intialise QGIS ang QT core application.
* @note Not available in Python bindings
*/
static bool init( int & argc, char ** argv );
//! The following is mainly for python bindings, that do not pass argc/argv
static bool init();

/** Set environment variable
* @param var environment variable name
* @param val value
Expand Down Expand Up @@ -94,6 +84,9 @@ class SERVER_EXPORT QgsServer

private:

/** Server initialization */
static bool init();

void saveEnvVars();

/** Saves environment variable into mEnvironmentVariables if defined*/
Expand Down Expand Up @@ -130,9 +123,6 @@ class SERVER_EXPORT QgsServer
#endif
//! Initialization must run once for all servers
static bool sInitialised;
static char* sArgv[1];
static int sArgc;
static QgsApplication* sQgsApplication;
static bool sCaptureOutput;

/** Pass important environment variables to the fcgi processes*/
Expand Down
10 changes: 9 additions & 1 deletion tests/src/python/test_qgsserver.py
Expand Up @@ -21,7 +21,7 @@
import email
from io import StringIO
from qgis.server import QgsServer
from qgis.core import QgsMessageLog
from qgis.core import QgsMessageLog, QgsApplication
from qgis.testing import unittest
from utilities import unitTestDataPath
import osgeo.gdal
Expand All @@ -32,6 +32,14 @@

class TestQgsServer(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.app = QgsApplication([], False)

@classmethod
def tearDownClass(cls):
cls.app.exitQgis()

def setUp(self):
"""Create the server instance"""
self.testdata_path = unitTestDataPath('qgis_server') + '/'
Expand Down

0 comments on commit aeb5ff2

Please sign in to comment.