×
Namespaces

Variants
Actions
(Difference between revisions)

Qt Service Framework 文档

From Nokia Developer Wiki
Jump to: navigation, search
flycarl (Talk | contribs)
flycarl (Talk | contribs)
Line 32: Line 32:
 
=='''如何使用框架'''==
 
=='''如何使用框架'''==
  
这一章假设用户想访问文件存储服务(FileStorage service)
+
这一章假设用户想访问文件存储服务(FileStorage service),这个服务提供了''com.nokia.qt.examples.FileStorage''接口的一个实现。service framework提供了访问这些实现的多种方法。
Using the Framework
+
  
 +
[http://doc.qt.nokia.com/qtmobility-1.1.0/qservicemanager.html QServiceManager] service管理器是查找和实例化service的主要类。可以通过指定service的meta data 来查找service,也可以通过默认的查找机制来查找service。
  
This section assumes that the user wants to access the FileStorage service which offers an implementation for the com.nokia.qt.examples.FileStorage interface The service framework enables multiple ways of accessing those implementations.
+
==='''冗余查找(Verbose)'''===
  
QServiceManager is the main class to lookup and instantiate services. Services can be found by constraining the search via service meta data or by using the default lookup mechanism.
+
客户端代码知道service及其接口的详细信息时,可用如下代码找到service
 +
 
 +
<code cpp>
 +
QServiceManager manager;
 +
QServiceFilter filter("com.nokia.qt.examples.FileStorage");
 +
filter.setServiceName("FileStorage");
 +
 
 +
// find services complying with filter
 +
QList<QServiceInterfaceDescriptor> foundServices;
 +
foundServices = manager.findInterfaces(filter);
 +
Q_ASSERT(foundServices.count());
 +
 
 +
// instantiate the FileStorage object
 +
QObject *fileStorage;
 +
fileStorage = manager.loadInterface(foundServices.at(0));
 +
</code>
 +
 
 +
==='''默认查找'''===
 +
假设客户端知道用哪个接口,但是并不关心实现接口的服务是哪种类型,哪种版本。 这时可使用默认查找机制来创建service对象的实例。
 +
 
 +
<code cpp>
 +
QServiceManager manager;
 +
manager.setInterfaceDefault("FileStorageService", "com.nokia.qt.examples.FileStorage");
 +
</code>
 +
上面的代码 [http://doc.qt.nokia.com/qtmobility-1.1.0/qservicemanager.html#setInterfaceDefault QServiceManager::setInterfaceDefault()] 把FileStorageService 注册成默认实现。 当客户端调用''com.nokia.qt.examples.FileStorage''的实现时,FileStorageService 服务会被加载。
 +
如果FileStorageService对同一个接口 注册了多个实现或版本,这个接口的最新版本将作为默认实现被加载。 因此接口的不同版本间需要保持二进制兼容性。
 +
 
 +
service管理器对象的当前作用域决定了 接口默认的指配是对所有用户有效还是只对当前用户有效。 用户作用域没有定义时讲使用系统默认。这使得用户可以自定义个人参数。当一个新的接口第一次安装服务时,默认设置为 system wide default selection。
 +
 
 +
====基于QObject的service====
 +
这是最常见的与servicer交互的方法。
 +
 
 +
<code cpp>
 +
storage = manager.loadInterface("com.nokia.qt.examples.FileStorage");
 +
if (storage)
 +
    QMetaObject::invokeMethod(storage, "deleteFile", Q_ARG(QString, "/tmp/readme.txt"));
 +
</code>
 +
The above invocation of the file storage object's deleteFile() function is done via the service's QMetaObject. The client does not require any knowledge of the objects actual type and therefore does not have to link against a service specific library.
 +
Typed services
 +
 
 +
So far all lookup mechanism returned a QObject pointer. This pointer can be utilized by introspecting the object, using Qt's meta object system. However in some use cases it may be more convenient to directly interact with the service object by including the service header and/or linking against the service provider. The main advantage is compile time checking. Its disadvantage is that client and service must share the implementation of the service object via a library they link against or via a common header file. Note that such sharing breaks the fundamental ServiceFramework principle of separating clients from service as changes of the service type may require changes to both, services and clients.
 +
 
 +
The subsequent code snippet demonstrates how this may look like:
 +
 
 +
#include <filestorage.h>
 +
...
 +
QServiceManager manager;
 +
FileStorage *storage = 0;
 +
...
 +
storage = manager.loadLocalTypedInterface<FileStorage>("com.nokia.qt.examples.FileStorage");
 +
if (storage)
 +
    storage->deleteFile("/tmp/readme.txt");
 
==<u>相关链接</u>==
 
==<u>相关链接</u>==
 
*[[Qt Mobility 开发]]
 
*[[Qt Mobility 开发]]

Revision as of 11:20, 9 January 2011

Contents

Qt Service Framework

Qt服务框架(Qt Service Framework )使客户端可以发现和实例化任意服务(service)。



命名空间

QtMobility API 放在 QtMobility 的命名空间. 这是为以后Mobility APIs 集成到 Qt做准备. 参考Quickstart guide中的例子体会这个命名空间如何影响使用QtMobility的开发development.


简介

Qt Service Framework 定义了统一的方法 在不同的平台间查找,实现和访问各种service。

利用 服务接口(service interface),版本号(version),基于QObject的内省(introspection)这3个信息,可以兼容和访问多个平台的某个service, 使得通过Qt的客户端可以不依赖平台的调用服务,实现跨进程的通信(IPC)。


概览

一个service是一个独立的组件提供给客户端(client)定义好的操作。客户端可以通过service的名称,版本号和service对象提供的接口来查找服务。 查找到service后,框架启动service并返回一个指针。QServiceManager 是做这些事情的主要接口,客户端通过QserviceManager来访问service。service也可以像一个客户端一样向service framework查询安装在系统上的其他service。

服务提供者通过插件(plug-ins)来实现。QServicePluginInterface是plug-in的主要接口。为了避免客户端依赖某个具体的库,服务必须继承自QObject。这样QMetaObject 系统可以用来动态的发现和唤醒服务的能力。要使QmetaObject机制充分的工作,service必须满足,其所有的方法都是通过 signal,slot,property 或invokable function来实现(参阅Q_INVOKABLE宏的定义)

每个service plug-in 只实现一个service,但是可以为多个接口提供多个实现。这样 service (plug-in)具有一定的后向兼容性,即使 当你需要使用一个新的接口名而破坏了主要的接口服务的兼容性,已有的客户端可以继续使用之前的服务接口,新的客户端可以利用新的接口。

service可以接受远程进程。在service管理器上注册后 进程通过signal,slot,invokable function和property来通信,就像本地对象一样。service可以设定为在客户端间共享,或针对一个客户端。


如何使用框架

这一章假设用户想访问文件存储服务(FileStorage service),这个服务提供了com.nokia.qt.examples.FileStorage接口的一个实现。service framework提供了访问这些实现的多种方法。

QServiceManager service管理器是查找和实例化service的主要类。可以通过指定service的meta data 来查找service,也可以通过默认的查找机制来查找service。

冗余查找(Verbose)

客户端代码知道service及其接口的详细信息时,可用如下代码找到service

 QServiceManager manager;
QServiceFilter filter("com.nokia.qt.examples.FileStorage");
filter.setServiceName("FileStorage");
 
// find services complying with filter
QList<QServiceInterfaceDescriptor> foundServices;
foundServices = manager.findInterfaces(filter);
Q_ASSERT(foundServices.count());
 
// instantiate the FileStorage object
QObject *fileStorage;
fileStorage = manager.loadInterface(foundServices.at(0));

默认查找

假设客户端知道用哪个接口,但是并不关心实现接口的服务是哪种类型,哪种版本。 这时可使用默认查找机制来创建service对象的实例。

 QServiceManager manager;
manager.setInterfaceDefault("FileStorageService", "com.nokia.qt.examples.FileStorage");

上面的代码 QServiceManager::setInterfaceDefault() 把FileStorageService 注册成默认实现。 当客户端调用com.nokia.qt.examples.FileStorage的实现时,FileStorageService 服务会被加载。 如果FileStorageService对同一个接口 注册了多个实现或版本,这个接口的最新版本将作为默认实现被加载。 因此接口的不同版本间需要保持二进制兼容性。

service管理器对象的当前作用域决定了 接口默认的指配是对所有用户有效还是只对当前用户有效。 用户作用域没有定义时讲使用系统默认。这使得用户可以自定义个人参数。当一个新的接口第一次安装服务时,默认设置为 system wide default selection。

基于QObject的service

这是最常见的与servicer交互的方法。

 storage = manager.loadInterface("com.nokia.qt.examples.FileStorage");
if (storage)
QMetaObject::invokeMethod(storage, "deleteFile", Q_ARG(QString, "/tmp/readme.txt"));

The above invocation of the file storage object's deleteFile() function is done via the service's QMetaObject. The client does not require any knowledge of the objects actual type and therefore does not have to link against a service specific library. Typed services

So far all lookup mechanism returned a QObject pointer. This pointer can be utilized by introspecting the object, using Qt's meta object system. However in some use cases it may be more convenient to directly interact with the service object by including the service header and/or linking against the service provider. The main advantage is compile time checking. Its disadvantage is that client and service must share the implementation of the service object via a library they link against or via a common header file. Note that such sharing breaks the fundamental ServiceFramework principle of separating clients from service as changes of the service type may require changes to both, services and clients.

The subsequent code snippet demonstrates how this may look like:

#include <filestorage.h>
...
QServiceManager manager;
FileStorage *storage = 0;
...
storage = manager.loadLocalTypedInterface<FileStorage>("com.nokia.qt.examples.FileStorage");
if (storage)
    storage->deleteFile("/tmp/readme.txt");

相关链接

697 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×