Skip to content

Commit

Permalink
Merge pull request #3528 from elpaso/server-needs-qgsapp
Browse files Browse the repository at this point in the history
[Server 3.0] now needs a qApp and tests are back
  • Loading branch information
elpaso authored Sep 27, 2016
2 parents 7d8fba8 + e795393 commit 6b7d7b0
Show file tree
Hide file tree
Showing 13 changed files with 641 additions and 619 deletions.
1 change: 0 additions & 1 deletion ci/travis/linux/qt5/blacklist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ PyQgsComposerPicture
PyQgsJSONUtils
PyQgsLocalServer
PyQgsPalLabelingServer
PyQgsServer
qgis_composermapgridtest
qgis_composerutils
ProcessingGrass7AlgorithmsImageryTest
Expand Down
13 changes: 4 additions & 9 deletions python/server/qgsserver.sip
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}

49 changes: 13 additions & 36 deletions src/server/qgsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,26 @@ 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 )
{
// QgsApplication must exist
if ( qobject_cast<QgsApplication*>( qApp ) == nullptr )
{
qFatal( "A QgsApplication must exist before a QgsServer instance can be created." );
abort();
}
sCaptureOutput = captureOutput;
init();
saveEnvVars();
}


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


Expand Down Expand Up @@ -310,34 +305,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 +332,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 +387,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 +458,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
Original file line number Diff line number Diff line change
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
3 changes: 3 additions & 0 deletions tests/src/python/qgis_wrapped_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import os
import urllib.parse
from http.server import BaseHTTPRequestHandler, HTTPServer
from qgis.core import QgsApplication
from qgis.server import QgsServer

try:
QGIS_SERVER_DEFAULT_PORT = int(os.environ['QGIS_SERVER_DEFAULT_PORT'])
except KeyError:
QGIS_SERVER_DEFAULT_PORT = 8081

qgs_app = QgsApplication([], False)
qgs_server = QgsServer()


Expand Down Expand Up @@ -70,3 +72,4 @@ def do_POST(self):
print('Starting server on localhost:%s, use <Ctrl-C> to stop' %
QGIS_SERVER_DEFAULT_PORT)
server.serve_forever()
qgs_app.exitQgis()
Loading

0 comments on commit 6b7d7b0

Please sign in to comment.