QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
server
qgsservicenativeloader.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgsservicerenativeloader.cpp
3
4
Define Loader for native service modules
5
-------------------
6
begin : 2016-12-05
7
copyright : (C) 2016 by David Marteau
8
email : david dot marteau at 3liz dot com
9
***************************************************************************/
10
11
/***************************************************************************
12
* *
13
* This program is free software; you can redistribute it and/or modify *
14
* it under the terms of the GNU General Public License as published by *
15
* the Free Software Foundation; either version 2 of the License, or *
16
* (at your option) any later version. *
17
* *
18
***************************************************************************/
19
20
#include "
qgsservicenativeloader.h
"
21
22
#include "
qgis.h
"
23
#include "
qgsmessagelog.h
"
24
#include "
qgsservicemodule.h
"
25
26
#include <QDebug>
27
#include <QDir>
28
#include <QLibrary>
29
#include <QString>
30
31
using namespace
Qt::StringLiterals;
32
33
typedef
void
unloadHook_t
(
QgsServiceModule
* );
34
41
class
QgsServiceNativeModuleEntry
42
{
43
public
:
48
QgsServiceNativeModuleEntry
(
const
QString &location )
49
:
mLocation
( location )
50
{}
51
52
QString
mLocation
;
53
QgsServiceModule
*
mModule
=
nullptr
;
54
unloadHook_t
*
mUnloadHook
=
nullptr
;
55
};
56
57
void
QgsServiceNativeLoader::loadModules
(
const
QString &modulePath,
QgsServiceRegistry
®istrar,
QgsServerInterface
*serverIface )
58
{
59
QDir moduleDir( modulePath );
60
moduleDir.setSorting( QDir::Name | QDir::IgnoreCase );
61
moduleDir.setFilter( QDir::Files );
62
63
#if defined( Q_OS_WIN ) || defined( __CYGWIN__ )
64
moduleDir.setNameFilters( QStringList(
"*.dll"
) );
65
#else
66
moduleDir.setNameFilters( QStringList(
"*.so"
) );
67
#endif
68
69
// qDebug() << QString( "Checking %1 for native services modules" ).arg( moduleDir.path() );
70
71
const
auto
constFiList( moduleDir.entryInfoList() );
72
for
(
const
QFileInfo &fi : constFiList )
73
{
74
QgsServiceModule
*module =
loadNativeModule
( fi.filePath() );
75
if
( module )
76
{
77
// Register services
78
module
->registerSelf( registrar, serverIface );
79
}
80
}
81
}
82
83
84
typedef
QgsServiceModule
*
serviceEntryPoint_t
();
85
86
QgsServiceModule
*
QgsServiceNativeLoader::loadNativeModule
(
const
QString &location )
87
{
88
QgsServiceNativeModuleEntry
*entry = findModuleEntry( location );
89
if
( entry )
90
{
91
return
entry->
mModule
;
92
}
93
94
QLibrary lib( location );
95
//QgsDebugMsgLevel( u"Loading native module %1"_s.arg( location ), 2 );
96
qDebug() << QString(
"Loading native module %1"
).arg( location );
97
if
( !lib.load() )
98
{
99
QgsMessageLog::logMessage
( QString(
"Failed to load library %1: %2"
).arg( lib.fileName(), lib.errorString() ) );
100
return
nullptr
;
101
}
102
// Load entry point
103
serviceEntryPoint_t
*entryPointFunc =
reinterpret_cast<
serviceEntryPoint_t
*
>
(
cast_to_fptr
( lib.resolve(
"QGS_ServiceModule_Init"
) ) );
104
105
if
( entryPointFunc )
106
{
107
QgsServiceModule
*module = entryPointFunc();
108
if
( module )
109
{
110
entry =
new
QgsServiceNativeModuleEntry
( location );
111
entry->
mModule
=
module
;
112
entry->
mUnloadHook
=
reinterpret_cast<
unloadHook_t
*
>
(
cast_to_fptr
( lib.resolve(
"QGS_ServiceModule_Exit"
) ) );
113
114
// Add entry
115
mModules.insert( location, ModuleTable::mapped_type( entry ) );
116
return
module
;
117
}
118
else
119
{
120
QgsMessageLog::logMessage
( QString(
"No entry point for module %1"
).arg( lib.fileName() ) );
121
}
122
}
123
else
124
{
125
QgsMessageLog::logMessage
( QString(
"Error: entry point returned null for %1"
).arg( lib.fileName() ) );
126
}
127
128
// No module found: release library
129
lib.unload();
130
return
nullptr
;
131
}
132
133
void
QgsServiceNativeLoader::unloadModules
()
134
{
135
ModuleTable::iterator it = mModules.begin();
136
const
ModuleTable::iterator end = mModules.end();
137
138
while
( it != end )
139
{
140
unloadModuleEntry( it->get() );
141
++it;
142
}
143
144
mModules.clear();
145
}
146
147
QgsServiceNativeModuleEntry
*QgsServiceNativeLoader::findModuleEntry(
const
QString &location )
148
{
149
QgsServiceNativeModuleEntry
*entry =
nullptr
;
150
const
ModuleTable::iterator item = mModules.find( location );
151
if
( item != mModules.end() )
152
{
153
entry = item->get();
154
}
155
return
entry;
156
}
157
158
void
QgsServiceNativeLoader::unloadModuleEntry(
QgsServiceNativeModuleEntry
*entry )
159
{
160
// Call cleanup function if it exists
161
if
( entry->
mUnloadHook
)
162
{
163
entry->
mUnloadHook
( entry->
mModule
);
164
}
165
166
QLibrary lib( entry->
mLocation
);
167
lib.unload();
168
}
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
Definition
qgsmessagelog.cpp:35
QgsServerInterface
Defines interfaces exposed by QGIS Server and made available to plugins.
Definition
qgsserverinterface.h:61
QgsServiceModule
Defines the service module interface for QGIS server services.
Definition
qgsservicemodule.h:41
QgsServiceNativeLoader::loadNativeModule
QgsServiceModule * loadNativeModule(const QString &location)
Load the native module from path.
Definition
qgsservicenativeloader.cpp:86
QgsServiceNativeLoader::unloadModules
void unloadModules()
Unload all modules.
Definition
qgsservicenativeloader.cpp:133
QgsServiceNativeLoader::loadModules
void loadModules(const QString &modulePath, QgsServiceRegistry ®istrar, QgsServerInterface *serverIface=nullptr)
Load all modules from path.
Definition
qgsservicenativeloader.cpp:57
QgsServiceNativeModuleEntry
Native module (location, the module itself and the unload function).
Definition
qgsservicenativeloader.cpp:42
QgsServiceNativeModuleEntry::mUnloadHook
unloadHook_t * mUnloadHook
Definition
qgsservicenativeloader.cpp:54
QgsServiceNativeModuleEntry::mLocation
QString mLocation
Definition
qgsservicenativeloader.cpp:52
QgsServiceNativeModuleEntry::QgsServiceNativeModuleEntry
QgsServiceNativeModuleEntry(const QString &location)
Constructor for QgsServiceNativeModuleEntry.
Definition
qgsservicenativeloader.cpp:48
QgsServiceNativeModuleEntry::mModule
QgsServiceModule * mModule
Definition
qgsservicenativeloader.cpp:53
QgsServiceRegistry
A registry manager for QGIS server services.
Definition
qgsserviceregistry.h:49
qgis.h
cast_to_fptr
#define cast_to_fptr(f)
Definition
qgis.h:7591
qgsmessagelog.h
qgsservicemodule.h
unloadHook_t
void unloadHook_t(QgsServiceModule *)
Definition
qgsservicenativeloader.cpp:33
serviceEntryPoint_t
QgsServiceModule * serviceEntryPoint_t()
Definition
qgsservicenativeloader.cpp:84
qgsservicenativeloader.h
Generated on
for QGIS API Documentation by
1.17.0