Skip to content
Permalink
Browse files

[Server 3.0] now needs a qApp

  • Loading branch information
elpaso committed Sep 25, 2016
1 parent 7a685f1 commit aeb5ff25b65b00c1d03672033f11973abd9270fb
@@ -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
@@ -21,6 +21,7 @@
#include "qgsserver.h"

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

int fcgi_accept()
{
@@ -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;
}

@@ -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();
}


@@ -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();

@@ -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 );
@@ -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;
@@ -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();
@@ -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
@@ -94,6 +84,9 @@ class SERVER_EXPORT QgsServer

private:

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

void saveEnvVars();

/** Saves environment variable into mEnvironmentVariables if defined*/
@@ -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*/
@@ -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
@@ -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') + '/'

0 comments on commit aeb5ff2

Please sign in to comment.