| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2017 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 |
** Contact: http://www.qt-project.org/legal |
| 5 |
** |
| 6 |
** This file is part of the documentation of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:FDL$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU Free Documentation License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU Free |
| 19 |
** Documentation License version 1.3 as published by the Free Software |
| 20 |
** Foundation and appearing in the file included in the packaging of |
| 21 |
** this file. Please review the following information to ensure |
| 22 |
** the GNU Free Documentation License version 1.3 requirements |
| 23 |
** will be met: https://www.gnu.org/licenses/fdl-1.3.html. |
| 24 |
** $QT_END_LICENSE$ |
| 25 |
** |
| 26 |
****************************************************************************/ |
| 27 |
|
| 28 |
/*! |
| 29 |
\group servicefw |
| 30 |
\title Service Frameworks Classes |
| 31 |
|
| 32 |
A set of APIs to that allows clients to discover and instantiate |
| 33 |
arbitrary services. |
| 34 |
|
| 35 |
*/ |
| 36 |
|
| 37 |
|
| 38 |
/*! |
| 39 |
\page service-frameworks.html |
| 40 |
|
| 41 |
\title Qt Service Framework |
| 42 |
|
| 43 |
\brief A set of Qt APIs to that allows clients to discover and instantiate |
| 44 |
arbitrary services. |
| 45 |
|
| 46 |
\ingroup technology-apis |
| 47 |
|
| 48 |
The Qt Service Framework allows clients to discover and instantiate |
| 49 |
arbitrary services. |
| 50 |
|
| 51 |
\tableofcontents |
| 52 |
|
| 53 |
\section1 Introduction |
| 54 |
|
| 55 |
The Qt Service Framework defines a unified way of finding, implementing and |
| 56 |
accessing services across multiple platforms. |
| 57 |
|
| 58 |
Due to the service frameworks knowledge of service interfaces, their |
| 59 |
versions, and QObject-based introspection, it may even be used to unify and |
| 60 |
access multiple platform specific service implementations via the same |
| 61 |
Qt-based client application. |
| 62 |
|
| 63 |
\section1 Getting Started |
| 64 |
|
| 65 |
To load the Qt Service Framework module into your .qml files, add the following |
| 66 |
|
| 67 |
\qml |
| 68 |
import QtServiceFramework 5.0 |
| 69 |
\endqml |
| 70 |
|
| 71 |
For C++ files you will need to add the directive |
| 72 |
|
| 73 |
\code |
| 74 |
#include <QtServiceFramework> |
| 75 |
\endcode |
| 76 |
|
| 77 |
And for linking against the module add this to your qmake .pro file |
| 78 |
|
| 79 |
\code |
| 80 |
QT += serviceframework |
| 81 |
\endcode |
| 82 |
|
| 83 |
\section1 Overview |
| 84 |
|
| 85 |
A service is an independent component that allows a client to perform a |
| 86 |
well-defined operation. Clients can find services based on their name and |
| 87 |
version as well as the interface that is implemented by the service object. |
| 88 |
Once the service has been identified the framework starts the service and |
| 89 |
returns a pointer to it. QServiceManager is the main interface through |
| 90 |
which clients can access the mentioned framework functionality. In |
| 91 |
addition services themselves may also act as clients to the service |
| 92 |
framework by requesting other services installed on the system. |
| 93 |
|
| 94 |
Service provider are implemented via plug-ins. QServicePluginInterface |
| 95 |
defines the main interface for each plug-in. |
| 96 |
In order to avoid that clients have to link against service specific |
| 97 |
libraries each service object must be derived from QObject. Therefore the |
| 98 |
QMetaObject system can be used to dynamically discover and invoke the |
| 99 |
service's capabilities. |
| 100 |
To achieve the best level of access via the Qt meta object system services |
| 101 |
should be implemented in such a way that their entire functionality is |
| 102 |
accessible via signals, slots, properties or invokable functions |
| 103 |
(see Q_INVOKABLE macro for more details). |
| 104 |
|
| 105 |
Each service plug-in implements one service only but can provide multiple |
| 106 |
implementations for multiple interfaces. |
| 107 |
Therefore a service (plug-in) can retain a certain level of backwards |
| 108 |
compatibility even if the main service interface breaks in such a way that |
| 109 |
a new interface name has to be used. Existing clients can still use the |
| 110 |
previous version of service interface whereas new clients can utilize the |
| 111 |
new interface. |
| 112 |
|
| 113 |
Services can also include remote processes. By registering with the |
| 114 |
service manager processes can communicate via signals, slots, |
| 115 |
invokable functions and properties as if they were local objects. |
| 116 |
Services can choose to be either shared between all clients, or |
| 117 |
unique to that client. |
| 118 |
|
| 119 |
|
| 120 |
\section1 Using the Framework |
| 121 |
|
| 122 |
This section assumes that the user wants to access the \e FileStorage |
| 123 |
service which offers an implementation for the \e com.nokia.qt.examples.FileStorage |
| 124 |
interface The service framework enables multiple ways of accessing those |
| 125 |
implementations. |
| 126 |
|
| 127 |
QServiceManager is the main class to lookup and instantiate services. |
| 128 |
Services can be found by constraining the search via service meta data or |
| 129 |
by using the default lookup mechanism. |
| 130 |
|
| 131 |
\section2 Verbose Lookup |
| 132 |
|
| 133 |
The client code has precise knowledge of the service and its interfaces. |
| 134 |
|
| 135 |
\code |
| 136 |
QServiceManager manager; |
| 137 |
QServiceFilter filter("com.nokia.qt.examples.FileStorage"); |
| 138 |
filter.setServiceName("FileStorage"); |
| 139 |
|
| 140 |
// find services complying with filter |
| 141 |
QList<QServiceInterfaceDescriptor> foundServices; |
| 142 |
foundServices = manager.findInterfaces(filter); |
| 143 |
Q_ASSERT(foundServices.count()); |
| 144 |
|
| 145 |
// instantiate the FileStorage object |
| 146 |
QObject *fileStorage; |
| 147 |
fileStorage = manager.loadInterface(foundServices.at(0)); |
| 148 |
\endcode |
| 149 |
|
| 150 |
\section2 Default Lookup |
| 151 |
|
| 152 |
It is assumed that the client has knowledge of the interface but does not really care |
| 153 |
about the specific type or version of the service implementing the interface. In such cases the default |
| 154 |
service lookup can be utilized to create a service object instance. |
| 155 |
|
| 156 |
\code |
| 157 |
QServiceManager manager; |
| 158 |
manager.setInterfaceDefault("FileStorageService", "com.nokia.qt.examples.FileStorage"); |
| 159 |
\endcode |
| 160 |
|
| 161 |
The above call to \l QServiceManager::setInterfaceDefault() registers the \e FileStorageService as default implementation. |
| 162 |
Whenever a client asks for an implementation of \e com.nokia.qt.examples.FileStorage the \e FileStorageService |
| 163 |
service will be loaded. If (at the time of this call) the \e FileStorageService provides multiple registered implementations/versions for the |
| 164 |
same interface the latest version becomes the default. Therefore subsequent versions of the same interface |
| 165 |
must always be binary compatible to previous versions. |
| 166 |
|
| 167 |
The current scope of the service manager object determines whether the default assignment is valid for all users |
| 168 |
or whether it is valid for the current user only. The system default is used if the user |
| 169 |
scope has not been defined. This enables users to customize their personal preferences. |
| 170 |
By default the first service installing a so far unknown interface becomes the system wide default selection. |
| 171 |
|
| 172 |
\section3 \b{QObject based services} |
| 173 |
|
| 174 |
This is the most common way of interacting with services. |
| 175 |
|
| 176 |
\code |
| 177 |
storage = manager.loadInterface("com.nokia.qt.examples.FileStorage"); |
| 178 |
if (storage) |
| 179 |
QMetaObject::invokeMethod(storage, "deleteFile", Q_ARG(QString, "/tmp/readme.txt")); |
| 180 |
\endcode |
| 181 |
|
| 182 |
The above invocation of the file storage object's deleteFile() function is done via the service's |
| 183 |
QMetaObject. The client does not require any knowledge of the objects actual type and therefore |
| 184 |
does not have to link against a service specific library. |
| 185 |
|
| 186 |
\section3 \b{Typed services} |
| 187 |
|
| 188 |
So far all lookup mechanism returned a QObject pointer. This pointer can be |
| 189 |
utilized by introspecting the object, using Qt's meta object system. |
| 190 |
However in some use cases it may be more convenient to |
| 191 |
directly interact with the service object by including the service header |
| 192 |
and/or linking against the service provider. The main advantage is compile time checking. |
| 193 |
Its disadvantage is that client and service must share the implementation of the service object via a library |
| 194 |
they link against or via a common header file. Note that such sharing breaks the fundamental ServiceFramework |
| 195 |
principle of separating clients from service as changes of the service type may require changes to both, services and clients. |
| 196 |
|
| 197 |
The subsequent code snippet demonstrates how this may look like: |
| 198 |
|
| 199 |
\code |
| 200 |
#include <filestorage.h> |
| 201 |
... |
| 202 |
QServiceManager manager; |
| 203 |
FileStorage *storage = 0; |
| 204 |
... |
| 205 |
storage = manager.loadLocalTypedInterface<FileStorage>("com.nokia.qt.examples.FileStorage"); |
| 206 |
if (storage) |
| 207 |
storage->deleteFile("/tmp/readme.txt"); |
| 208 |
\endcode |
| 209 |
|
| 210 |
|
| 211 |
\section1 Service Scope |
| 212 |
|
| 213 |
The QServiceManager operates in either \l{QService::UserScope}{User scope} |
| 214 |
or \l{QService::SystemScope}{System scope}. By default, it operates |
| 215 |
in user scope. The choice of scope affects whether registered services are |
| 216 |
available system-wide or only to the current user, and whether service and |
| 217 |
interface lookups are limited to system-wide service or whether the current |
| 218 |
user's together with system service configurations are considered. |
| 219 |
|
| 220 |
\section2 User Scope |
| 221 |
|
| 222 |
In user scope, services are registered in a storage location specific to |
| 223 |
the current user. When a default service is set for an interface using |
| 224 |
\l{QServiceManager::setInterfaceDefault()}{setInterfaceDefault()}, the |
| 225 |
referenced service can be either a user-specific or system-wide service. |
| 226 |
|
| 227 |
For service and interface lookups, a manager will first search the |
| 228 |
user-specific services; if the requested component is not found, the |
| 229 |
manager then searches the system-wide services, if the user has |
| 230 |
sufficient permissions to do so. |
| 231 |
|
| 232 |
The \l{QServiceManager::serviceAdded()} and \l{QServiceManager::serviceRemoved()} |
| 233 |
notifications are emitted when services are added or removed from either |
| 234 |
the user-specific or system-wide services. These signals have a \c scope |
| 235 |
parameter to indicate the scope in which the service was added or removed. |
| 236 |
(Note the system-wide service notifications are only available if the user |
| 237 |
has sufficient permissions to access the system-wide service storage.) |
| 238 |
|
| 239 |
\section2 System Scope |
| 240 |
|
| 241 |
In system scope, services are registered in a system-wide storage location. |
| 242 |
The manager does not access user-specific services for any operations. |
| 243 |
Service and interface lookups fail if the requested services are not found |
| 244 |
in the system-wide service storage. Service registration is performed in |
| 245 |
the system-wide storage. |
| 246 |
|
| 247 |
If \l{QServiceManager::setInterfaceDefault()}{setInterfaceDefault()} is |
| 248 |
called for a user-specific service, the operation fails. |
| 249 |
|
| 250 |
Also, the \l{QServiceManager::serviceAdded()} and \l{QServiceManager::serviceRemoved()} |
| 251 |
notifications are only emitted for system-wide services. |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
\section1 Adding and Removing of Services |
| 256 |
|
| 257 |
New services can be installed and removed at runtime. An XML file is used |
| 258 |
to describe the service meta data and links the service code to its meta |
| 259 |
description. |
| 260 |
|
| 261 |
\section2 XML Format |
| 262 |
|
| 263 |
Services are installed via an XML file which describes the meta data and |
| 264 |
location of the service. The XML file can be described via the following |
| 265 |
DTD: |
| 266 |
|
| 267 |
\code |
| 268 |
<!ELEMENT SFW ( service ) > |
| 269 |
<!ATTLIST SFW version (1.0|1.1) #REQUIRED > |
| 270 |
<!ELEMENT service ( name, filepath | ipcaddress, description?, interface+ ) > |
| 271 |
<!ELEMENT description ( #CDATA ) > |
| 272 |
<!ELEMENT filepath ( #PCDATA ) > |
| 273 |
<!ELEMENT ipcaddress ( #PCDATA ) > |
| 274 |
<!ELEMENT interface ( name, version, description?, capabilities?, customproperty* ) > |
| 275 |
<!ELEMENT capabilities ( #PCDATA ) > |
| 276 |
<!ELEMENT name ( #PCDATA ) > |
| 277 |
<!ELEMENT version ( #PCDATA ) > |
| 278 |
<!ELEMENT customproperty ( #CDATA ) > |
| 279 |
<!ATTLIST customproperty key NMTOKEN #REQUIRED > |
| 280 |
|
| 281 |
\endcode |
| 282 |
|
| 283 |
The elements and attributes have the following meanings: |
| 284 |
|
| 285 |
\table |
| 286 |
\header |
| 287 |
\li Element \li SubElement \li Description |
| 288 |
\row |
| 289 |
\li SFW |
| 290 |
\li |
| 291 |
\li The service framework XML version tag that encapsulates the service XML. Must provide the |
| 292 |
version attribute that follows the \e major.minor notation and must equate to supported |
| 293 |
Qt Service Framework versions. This means the minimum version is 1.0 however users are |
| 294 |
recommended to always specify the highest available version. |
| 295 |
\row |
| 296 |
\li service |
| 297 |
\li |
| 298 |
\li The \e service tag can contain an arbitrary number of \e interface tags and one description tag. |
| 299 |
\row |
| 300 |
\li " |
| 301 |
\li description |
| 302 |
\li A user readable description of the purpose of the service. |
| 303 |
\row |
| 304 |
\li " |
| 305 |
\li filepath |
| 306 |
\li The absolute path and name of the plug-in to be loaded when this |
| 307 |
service is requested. Alternatively if the plug-in name only is |
| 308 |
provided the standard library paths (see QCoreApplication::libraryPaths()) |
| 309 |
are used to find the plug-in. |
| 310 |
Note that if the plugin name is given only, platform specific |
| 311 |
parts such as the suffix ".dll" and ".so" or plugin prefix "lib" |
| 312 |
should be removed to enable cross platform resolution. QLibrary |
| 313 |
is used to determine the platform specific parts of the plugin. |
| 314 |
\row |
| 315 |
\li " |
| 316 |
\li ipcaddress |
| 317 |
\li The socket name or path that the inter-process service will be published |
| 318 |
on that will provide clients with remote access. Should be the same name |
| 319 |
as the executable for the service without any suffix such as ".exe" etc. |
| 320 |
\row |
| 321 |
\li " |
| 322 |
\li name |
| 323 |
\li The name of the service. |
| 324 |
\row |
| 325 |
\li interface |
| 326 |
\li |
| 327 |
\li The \e interface describes the properties of the interface. |
| 328 |
\row |
| 329 |
\li " |
| 330 |
\li capabilities |
| 331 |
\li This property is a list of arbitrary strings which are |
| 332 |
interpreted as permissions/capabilities. The list elements are |
| 333 |
comma-separated and spaces after commas are not permitted. |
| 334 |
This list can be empty. |
| 335 |
\row |
| 336 |
\li " |
| 337 |
\li name |
| 338 |
\li The name of the interface using the Java class name notation. (e.g. com.nokia.qt.TestService) |
| 339 |
\row |
| 340 |
\li " |
| 341 |
\li version |
| 342 |
\li This property contains the interface and implementation version. |
| 343 |
The version tag follows the \e major.minor notation. The major |
| 344 |
version indicates the interface version the minor version the |
| 345 |
implementation version. |
| 346 |
|
| 347 |
The version number \b must be greater than 1.0. The version |
| 348 |
cannot be less than 1.0 because the Service Framework is |
| 349 |
dependent on the fact that services must be binary compatible |
| 350 |
between major versions, and services with versions less than 1.0 |
| 351 |
are unlikely to be binary compatible with later versions. |
| 352 |
\row |
| 353 |
\li " |
| 354 |
\li description |
| 355 |
\li A user readable description of the purpose of the interface. |
| 356 |
\row |
| 357 |
\li " |
| 358 |
\li customproperty |
| 359 |
\li An implementation specific key value pair which can be used for |
| 360 |
filtering or as description. |
| 361 |
|
| 362 |
\endtable |
| 363 |
|
| 364 |
|
| 365 |
An example for a valid XML service description could be the following \e TestService: |
| 366 |
|
| 367 |
\code |
| 368 |
<?xml version="1.0" encoding="utf-8" ?> |
| 369 |
<SFW version="1.1"> |
| 370 |
<service> |
| 371 |
<name>TestService</name> |
| 372 |
<filepath>testserviceplugin</filepath> |
| 373 |
<description>Test service description</description> |
| 374 |
<interface> |
| 375 |
<name>com.nokia.qt.ILocation</name> |
| 376 |
<version>1.4</version> |
| 377 |
<capabilities></capabilities> |
| 378 |
<description>Interface that provides location support</description> |
| 379 |
</interface> |
| 380 |
<interface> |
| 381 |
<name>com.nokia.qt.ILocation</name> |
| 382 |
<version>1.5</version> |
| 383 |
<capabilities></capabilities> |
| 384 |
<description>Interface that provides location support</description> |
| 385 |
</interface> |
| 386 |
<interface> |
| 387 |
<name>com.nokia.qt.ISysInfo</name> |
| 388 |
<capabilities>ReadUserData</capabilities> |
| 389 |
<version>2.3</version> |
| 390 |
<description>Interface that provides system information support</description> |
| 391 |
<customproperty key="key1">value1</customproperty> |
| 392 |
<customproperty key="key2">value2</customproperty> |
| 393 |
</interface> |
| 394 |
</service> |
| 395 |
</SFW> |
| 396 |
\endcode |
| 397 |
|
| 398 |
\section1 Tools for XML Generation |
| 399 |
|
| 400 |
The framework includes a GUI tool under \tt tools/servicexmlgen for |
| 401 |
generating and inspecting service XML files. This makes it easy to enter |
| 402 |
service metadata and interface details and generate the appropriate XML to |
| 403 |
describe the plugin or IPC based service. It can also be used to load and |
| 404 |
inspect existing service XML files. |
| 405 |
|
| 406 |
Note that the tool will default to the highest known existing Qt Service Framework |
| 407 |
version as the XML version attribute and subsequent saves on loaded files will |
| 408 |
enforce this default version. |
| 409 |
|
| 410 |
Here is a screenshot of the application, loaded with the \e TestService XML |
| 411 |
description provided above. |
| 412 |
|
| 413 |
\image servicexmlgen.png |
| 414 |
|
| 415 |
|
| 416 |
\section1 Installing the Service at Runtime |
| 417 |
|
| 418 |
New services can be added and removed at any time via QServiceManager::addService() |
| 419 |
and QServiceManager::removeService(). |
| 420 |
|
| 421 |
\section1 Identifying Services |
| 422 |
|
| 423 |
Each implementation is identified by a service name, an interface name and |
| 424 |
its version. This information is encapsulated by |
| 425 |
QServiceInterfaceDescriptor which can be used to request references to |
| 426 |
service objects. |
| 427 |
|
| 428 |
QServiceFilter simplifies the lookup of already installed services. |
| 429 |
Developers specifies the criteria used during the meta data lookup. |
| 430 |
The subsequent example demonstrates the interaction between |
| 431 |
QServiceInterfaceDescriptor and QServiceFilter by creating references to |
| 432 |
all services that implement the interface \e com.nokia.qt.ILocation: |
| 433 |
|
| 434 |
\code |
| 435 |
QServiceManager mgr; |
| 436 |
QServiceFilter filter; |
| 437 |
filter.setInterface("com.nokia.qt.ILocation"); |
| 438 |
QList<QServiceInterfaceDescriptor> list = mgr.findInterfaces(filter); |
| 439 |
for(int i = 0; i < list.size(); i++) { |
| 440 |
QObject *serviceObject; |
| 441 |
serviceObject = mgr.loadInterface(list[i]); |
| 442 |
|
| 443 |
// returned object owned by client |
| 444 |
if (serviceObject) |
| 445 |
serviceObject->setParent(this); |
| 446 |
} |
| 447 |
\endcode |
| 448 |
|
| 449 |
\section2 Upgrading Services |
| 450 |
|
| 451 |
There are two ways in which to upgrade a service. The first is an |
| 452 |
incremental approach whereby QServiceManager::addService() is used to |
| 453 |
register an XML descriptor whose service name already exists but defines |
| 454 |
new interface implementations. For example an already existing service, |
| 455 |
"ovi" may define version 1.0 of interface,"IDownload". |
| 456 |
QServiceManager::addService() is then called with an XML descriptor that |
| 457 |
declares itself as belonging the "ovi" service, but implements version 1.1 |
| 458 |
of the interface "IDownload". Both implementation versions will be |
| 459 |
available for use. |
| 460 |
|
| 461 |
The second method is a replacement approach whereby an existing service is |
| 462 |
completely removed and replaced by a new service. |
| 463 |
|
| 464 |
As an example the already existing "ovi" service may implement interface |
| 465 |
"IDownload" version 1.0, the new "ovi" service may implement "IDownload" |
| 466 |
version 1.0 and version 1.1. In this case the old service implementation |
| 467 |
must be first be removed using QServiceManager::removeService() to make |
| 468 |
way for the new service implementation. |
| 469 |
|
| 470 |
\section1 Out of Process Services |
| 471 |
|
| 472 |
Qt Service Framework provides a mechanism for out of process services |
| 473 |
so that clients can remotely load interfaces. These inter-process |
| 474 |
services are deployed similarly to local services, using the IPC address |
| 475 |
tag from the XML description and calling QRemoteServiceRegister::publishEntries() |
| 476 |
to add the service to the remote services manager. Clients access services |
| 477 |
via the metaobject similarly to in-process services and each remote service |
| 478 |
is represented as a QRemoteServiceRegister::Entry by using |
| 479 |
QRemoteServiceRegister::createEntry(). The remote dialer service example |
| 480 |
demonstrates using a out of process service. |
| 481 |
|
| 482 |
Inter-process services are deployed using a variety of different IPC mechanisms |
| 483 |
available on the current platform and as such may inherit several limitations. For |
| 484 |
some linux environments services can published using QtDBus otherwise it |
| 485 |
falls back on QLocalSocket for inter-process communication. |
| 486 |
|
| 487 |
\section2 D-Bus Services |
| 488 |
|
| 489 |
The QtDBus module is used to provide IPC in correspondence to the Qt Service |
| 490 |
Framework architecture and provides access to signals, slots, invokable methods |
| 491 |
and properties. |
| 492 |
|
| 493 |
The D-Bus protocol requires concept identifiers that represent D-Bus objects: |
| 494 |
service names, object paths, and interfaces. D-Bus objects are registered using |
| 495 |
a combination of the service description supplied in the service XML file. An |
| 496 |
example of this, using the previous sample XML description, is provided below. |
| 497 |
|
| 498 |
\table 90% |
| 499 |
\header \li D-Bus Concept \li Derived Name |
| 500 |
\row \li Service Name \li "com.qtmobility.sfw.TestService" |
| 501 |
\row \li Object Path \li "/com/nokia/qt/ILocation/1/4/" |
| 502 |
\row \li Interface \li "" |
| 503 |
\endtable |
| 504 |
|
| 505 |
\section3 Limitations |
| 506 |
|
| 507 |
When using integer types for arguments to signals and slots, |
| 508 |
bass types as defined by http://doc.qt.nokia.com/4.7/qdbustypesystem.html. |
| 509 |
For example, quint64 is generally equivalent to qulonglong, but |
| 510 |
all unsigned 64 bit values must be defined as qulonglong or they will |
| 511 |
not work with the dbus implementation. For more details and the current |
| 512 |
state of the limitation please refer to bug |
| 513 |
\l {http://bugreports.qt.io/browse/QTMOBILITY-1694}{QTMOBILITY-1694}. |
| 514 |
|
| 515 |
A current known limitation for Qt Service Framework using QtDBus for IPC is |
| 516 |
that enumerators cannot be read due to lack of support on D-Bus. This will be |
| 517 |
added in future versions once the QtDBus module enables enumerator support. A |
| 518 |
current known workaround is to manually define and use integers as demonstrated |
| 519 |
in the \l{Service Framework using QML Example}{Declarative Serviceframework |
| 520 |
Dialer} example. Furthermore, due to the nature of IPC, pointers to objects |
| 521 |
cannot be passed between services and clients. Unfortunately, this also means |
| 522 |
that anything wrapped in a typedef macro cannot be communicated effectively |
| 523 |
between processes. |
| 524 |
|
| 525 |
\section3 Service Autostart |
| 526 |
D-Bus services inherit automatic start support by providing .service files to the |
| 527 |
D-Bus daemon search paths. Aside from producing these files manually, the |
| 528 |
servicefw tool provides an option to generate the corresponding files inside |
| 529 |
the local user D-Bus folder for convenience. This will allow all client |
| 530 |
requests to automatically startup the service if it isn't already running. |
| 531 |
Consequently, once every client has closed its service instance the service will |
| 532 |
be automatically terminated after invoking any slots connected to notification signals. |
| 533 |
|
| 534 |
Running the following command will enable rendezvous for D-Bus IPC. |
| 535 |
|
| 536 |
\code |
| 537 |
./servicefw dbusservice <service-xml-file> <service-file> |
| 538 |
\endcode |
| 539 |
|
| 540 |
The .service files will typically be generated in the $home/.local/share/dbus-1/services |
| 541 |
directory unless the $$XDG_DATA_HOME environment variable is set. If the --system |
| 542 |
option is used the tool will generate the file inside /usr/share/dbus-1/services if |
| 543 |
the user permissions are met. |
| 544 |
|
| 545 |
Note that in order for the autostart feature to function correctly, the service |
| 546 |
needs to be either pre-registered or registered at run-time so that clients can use |
| 547 |
a QServiceManager to discover the IPC interfaces that invoke the D-Bus service files. |
| 548 |
This is usually done on deployment by adding services through the servicefw tool. |
| 549 |
|
| 550 |
|
| 551 |
\section2 Local Socket |
| 552 |
|
| 553 |
On platforms that do not provide D-Bus or do not use it, the |
| 554 |
underlying IPC mechanism utilized is a local socket based protocol. |
| 555 |
|
| 556 |
\section3 Limitations |
| 557 |
Similar to the other IPC mechanisms, passing object pointers or typedefs will not |
| 558 |
work on local socket. Local socket also requires the executable for the service to |
| 559 |
be installed somewhere in your PATH for autostart to work. Not all platforms support |
| 560 |
all fields on the QRemoteServiceRegisterCredentials and will return -1 in unsupported |
| 561 |
fields. |
| 562 |
|
| 563 |
\section3 Service Autostart |
| 564 |
This mechanism provides automatic service startup. The <ipcaddress> tag |
| 565 |
must be set to the executable filename. For example |
| 566 |
<ipcaddress>xyzabc</ipcaddress> must be used if the executable is xyzabc, |
| 567 |
or xyzabc.exe on windows systems. The PATH is searched for the executable. |
| 568 |
If the executable is not found, or the service does not start, then |
| 569 |
the error is reported to the client. |
| 570 |
|
| 571 |
\section3 Service Registration |
| 572 |
All services must be registered. The 'servicefw' tool registers xml |
| 573 |
files. It must be run before a service can be used. For example: |
| 574 |
|
| 575 |
\code |
| 576 |
servicefw add <service xml file> |
| 577 |
\endcode |
| 578 |
|
| 579 |
\section3 Service Security |
| 580 |
|
| 581 |
Service objects can filter the clients that are allowed to connect to them. |
| 582 |
This is done by creating an invokable constructor that has a |
| 583 |
QServiceClientCredentials as its first and only paramter. For example: |
| 584 |
|
| 585 |
\code |
| 586 |
class ExampleService : public QObject |
| 587 |
{ |
| 588 |
Q_OBJECT |
| 589 |
public: |
| 590 |
ExampleService(QObject* parent = 0) |
| 591 |
: QObject(parent) |
| 592 |
{ |
| 593 |
// this constructor will not be called |
| 594 |
} |
| 595 |
|
| 596 |
Q_INVOKABLE ExampleService(QServiceClientCredentials *creds) |
| 597 |
: QObject(0) |
| 598 |
{ |
| 599 |
if (creds->getUserIdentifier() == 0) |
| 600 |
creds->setClientAccepted(true); |
| 601 |
else |
| 602 |
creds->setClientAccepted(false); |
| 603 |
} |
| 604 |
}; |
| 605 |
\endcode |
| 606 |
|
| 607 |
The new invokable constructor will be called instead of the default |
| 608 |
constructor. Client credentials are passed in via the QServiceClientCredetials |
| 609 |
class. If the client wishes to authorize the connection it must call either |
| 610 |
setClientAccepted(true) or call setClientAccepted(false) to reject the client. |
| 611 |
If the client is rejected the constructor should return immediatly, and object |
| 612 |
will be destroyed. |
| 613 |
|
| 614 |
If the object is a global object multiple clients can connect to the same |
| 615 |
object. On construction the secure constructor will be called. For subsequent |
| 616 |
client connections the invokable method "void |
| 617 |
verifyNewServiceClientCredentials(QServiceClientCredentials *creds)" |
| 618 |
will be called to authenticate new client connections. For example a global |
| 619 |
class maybe defined as: |
| 620 |
|
| 621 |
\code |
| 622 |
class GlobalService : public QObject |
| 623 |
{ |
| 624 |
Q_OBJECT |
| 625 |
public: |
| 626 |
GlobalService(QObject* parent = 0) |
| 627 |
: QObject(parent) |
| 628 |
{ |
| 629 |
// this constructor will not be called |
| 630 |
} |
| 631 |
|
| 632 |
Q_INVOKABLE GlobalService(QServiceClientCredentials *creds) |
| 633 |
: QObject(0) |
| 634 |
{ |
| 635 |
if (creds->getUserIdentifier() == 0) |
| 636 |
creds->setClientAccepted(true); |
| 637 |
else |
| 638 |
creds->setClientAccepted(false); |
| 639 |
} |
| 640 |
|
| 641 |
Q_INVOKABLE void verifyNewServiceClientCredentials(QServiceClientCredentials *creds) |
| 642 |
{ |
| 643 |
if (creds->getUserIdentifier() == 0) |
| 644 |
creds->setClientAccepted(true); |
| 645 |
else |
| 646 |
creds->setClientAccepted(false); |
| 647 |
} |
| 648 |
}; |
| 649 |
|
| 650 |
\endcode |
| 651 |
|
| 652 |
For global objects, when the first client connects the secure |
| 653 |
constructor is called. Subsequent connections will result in |
| 654 |
the verifyNewServiceClientCredentials being called. When the |
| 655 |
last client disconnected the object will be destroyed, and |
| 656 |
the constructor will be called when the next client connects. |
| 657 |
|
| 658 |
\section3 Service Security Filters |
| 659 |
The service can install a security filter to reject connections from |
| 660 |
some clients on initial connection. Service security described above allows |
| 661 |
this to be controlled per object. This is implemented on D-Bus and |
| 662 |
local socket implementations of IPC. |
| 663 |
|
| 664 |
\code |
| 665 |
void securityFilter(QServiceClientCredentials *creds) |
| 666 |
{ |
| 667 |
if (creds->getUserIdentifier() == 0) { |
| 668 |
creds->setClientAccepted(true); // accept the connection |
| 669 |
} else { |
| 670 |
creds->setClientAccepted(false); // reject the connection |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
\endcode |
| 675 |
|
| 676 |
By calling QServiceClientCredentials::setClientAccepted the connection |
| 677 |
can be terminated or accepted. Certain platforms do not provide all |
| 678 |
the results, and undefined values are set to -1. Registration of the filter is done by |
| 679 |
calling QRemoteServiceRegister::setSecurityFilter(). Only 1 filter maybe installed |
| 680 |
at one time. |
| 681 |
|
| 682 |
\section2 Error handling |
| 683 |
Out of process services behave similarly to local services, with one |
| 684 |
exception. There can be IPC errors, or the remote service can |
| 685 |
terminate unexpectedly. QObjects returned by the service manager for |
| 686 |
out of process services have an additional signal created, |
| 687 |
errorUnrecoverableIPCFault(QService::UnrecoverableIPCError) which the |
| 688 |
client must connect to. If an error occurs the object must be |
| 689 |
destroyed; no further calls can be made. The error recovery is left up to |
| 690 |
the client application. |
| 691 |
|
| 692 |
|
| 693 |
\section1 Main Classes |
| 694 |
\annotatedlist servicefw |
| 695 |
|
| 696 |
|
| 697 |
\section1 QML Elements |
| 698 |
|
| 699 |
The Qt Service Framework offers discovery and selection of services through declarative |
| 700 |
UI by providing service and service list elements. Detailed information can be found |
| 701 |
at \l{QML Service Framework Plugin}. |
| 702 |
|
| 703 |
\annotatedlist qml-serviceframework |
| 704 |
|
| 705 |
|
| 706 |
\section1 Examples |
| 707 |
\list |
| 708 |
\li \l{dialer}{Declarative Serviceframework Dialer} |
| 709 |
\endlist |
| 710 |
|
| 711 |
*/ |