D-Bus学习(七):利用XML定义D-Bus之Client的例子

本文介绍了如何使用D-Bus的XML接口定义及自动生成的客户端代码实现同步和异步消息传递。通过具体的代码示例展示了如何简化D-Bus的使用。

  继续学习D-Bus。之前学习了使用底层的API来发送,监听消息的方式。在 D- Bus学习(四):基础小例子(同步和异步) 之中,我们给出了利用proxy进行发送method_call,并等待method_reply或者error的client情况。在D-Bus中,可以将D-Bus接口定义用XML格式表述处理,并利用工具,自动生成头文件,给出工整的调用方式。下面是一个XML的例子。

<?xml version="1.0" encoding="UTF-8" ?>
<node name="/com/wei/MyObject">
  <interface name="com.wei.MyObject.Sample">
    <method name="Test">
      <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="wei_response" />
      <arg type="u" name="x" direction="in" />
      <arg type="d" name="d_ret" direction="out" />
    </method >
  </interface >
</node >

  其中annotation是作为server用,在下一次学习中使用。我们给这个xml文件一个名字,wei.xml。结合我们上两次学习中的图,node实际上就是相当于对象,用path来进行描述。使用dbus-bingding-tool来生成头文件:

dbus-binding-tool --mode=glib-client --prefix=com_wei wei.xml > wei_client.h    

  头文件中给出了同步调用和异步调用的内容,我们给出一个前缀com_wei,将在头文件中出现,头文件内容如下:

/* Generated by dbus-binding-tool; do not edit! */

#include <glib.h>
#include <dbus/dbus-glib.h>

G_BEGIN_DECLS

#ifndef _DBUS_GLIB_ASYNC_DATA_FREE
#define _DBUS_GLIB_ASYNC_DATA_FREE
static
#ifdef G_HAVE_INLINE
inline
#endif
void
_dbus_glib_async_data_free (gpointer stuff)
{
        g_slice_free (DBusGAsyncData, stuff);
}
#endif

#ifndef DBUS_GLIB_CLIENT_WRAPPERS_com_wei_MyObject_Sample
#define DBUS_GLIB_CLIENT_WRAPPERS_com_wei_MyObject_Sample

static
#ifdef G_HAVE_INLINE
inline
#endif

//这是同步调用内容
gboolean
com_wei _MyObject_Sample_test (DBusGProxy *proxy, const guint IN_x, gdouble* OUT_d_ret, GError **error)
{
  return dbus_g_proxy_call (proxy, "Test", error, G_TYPE_UINT, IN_x, G_TYPE_INVALID, G_TYPE_DOUBLE, OUT_d_ret, G_TYPE_INVALID);
}

//下面是异步调用内容
typedef void (*com_wei _MyObject_Sample_test_reply) (DBusGProxy *proxy, gdouble OUT_d_ret, GError *error, gpointer userdata);

static void
com_wei _ MyObject_Sample_test_async_callback (DBusGProxy *proxy, DBusGProxyCall *call, void *user_data)
{
  DBusGAsyncData *data = (DBusGAsyncData*) user_data;
  GError *error = NULL;
  gdouble OUT_d_ret;
  dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_DOUBLE, &OUT_d_ret, G_TYPE_INVALID);
  (*(com_wei_MyObject_Sample_test_reply)data->cb) (proxy, OUT_d_ret, error, data->userdata);
  return;
}

static
#ifdef G_HAVE_INLINE
inline
#endif
DBusGProxyCall*
com_ wei _MyObject_Sample_test_async (DBusGProxy *proxy, const guint IN_x, com_wei_MyObject_Sample_test_reply callback, gpointer userdata)

{
  DBusGAsyncData *stuff;
  stuff = g_slice_new (DBusGAsyncData);
  stuff->cb = G_CALLBACK (callback);
  stuff->userdata = userdata;
  return dbus_g_proxy_begin_call (proxy, "Test", com_wei_MyObject_Sample_test_async_callback, stuff, _dbus_glib_async_data_free, G_TYPE_UINT, IN_x, G_TYPE_INVALID);
}
#endif /* defined DBUS_GLIB_CLIENT_WRAPPERS_com_wei_MyObject_Sample */


G_END_DECLS

  在这个头文件中,给出了比较工整的函数定义prefix_interface_method,并且已经包含了一些底层API的代码。利用这个头文件,我们在写程序中,可以更为简洁和直观。下面是利用同步方式写的小例子:

void wei_request(DBusGProxy * proxy)
{
  gdouble PUT_d_ret;
  GError * error = NULL;

  if(!com_wei_MyObject_Sample_test ( proxy,1000,&OUT_d_ret,&error )){
    g_printerr("Error: %s/n",error->message);
    return;
  }

  g_print("Proxy get return d_ret = %f",d_ret);

}

  proxy的获取见 D- Bus学习(四):基础小例子(同步和异步) ,显示建立D-Bus连接,为连接捆绑一个可读名字(well-known name),建立proxy,不再重复。异步小例子如下:

void wei_wait_reply (DBusGProxy * proxy, gdouble OUT_d_ret, GError * error,gpointer userdata)
{
  if(error != NULL){
    g_printerr("Asyn Error : %s/n",error->message);
    return;
  }

  g_print("Proxy get return d_ret = %f",OUT_d_ret);
}

void wei_request_async(DBusGProxy * proxy)
{

  com_wei_MyObject_Sample_test_async (proxy,2000,wei_wait_reply, NULL);
}

  在异步的例子中,wei_request_async表示发起一个消息请求,当D-Bus返回应答时候,触发wei_wait_reply。

相关链接: 我 的Linux相关文章

server: port: 8802 # tomcat: # uriEncoding: "UTF-8" # remoteip: # protocol-header-https-value: "https" # remote-ip-header: "RemoteIpValve" # protocol-header: "X-Forwarded-Proto" servlet: context-path: /gateway cors: allowedOrigin: "*" management: endpoints: web: exposure: include: "*" spring: data: redis: client-type: lettuce # 使用响应式客户端 config: import: - "nacos:uap-gateway-test.properties?group=latest&server-addr=uap.qctc.com:8800" - "nacos:common-config.properties?group=latest&server-addr=uap.qctc.com:8800" application: name: uap-gateway main: web-application-type: reactive allow-bean-definition-overriding: true profiles: active: test # group: # latest: [ test ] # 使用数组形式定义包含的profile # 可添加其他组: # production: [prod, metrics] resources: add-mappings: true cloud: config: profile: dev discovery: enabled: true service-id: zhny-config bus: enabled: false trace: enabled: false nacos: # 增加全局日志禁用参数(关键修复) bootstrap: log: enabled: false # 关闭Nacos启动时的日志系统初始化 logging: config: false # 禁用Nacos内部日志配置 naming: false remote: false default: config: enabled: false discovery: server-addr: uap.qctc.com:8801 #uap.qctc.com:8900 11.14.30.16:8848 group: latest config: server-addr: uap.qctc.com:8801 file-extension: properties namespace: public group: latest name: uap-gateway # Data ID prefix: uap-gateway # shared-configs: # - data-id: common-config.properties # group: latest # refresh: true import-check: enabled: false # 禁用导入检查(可选) gateway: # 全局连接池与超时配置 httpclient: retry: false # 全局关闭重试 # 允许特殊字符 relaxed-cookies: true pool: max-connections: 2000 # 最大总连接数 [2,5](@ref) max-idle-time: 50000ms # 连接空闲超时 connect-timeout: 50000 # 连接超时(毫秒)[2](@ref) response-timeout: 50000 # 响应超时(毫秒)[2](@ref) protool: h2c keepalive: true # 全局跨域配置 globalcors: add-to-simple-url-handler-mapping: true # 确保处理OPTIONS请求 cors-configurations: '[/**]': allowedOriginPatterns: "*" # 使用通配符模式 allowedMethods: "*" allowedHeaders: "*" exposedHeaders: "X-trace-id" # 按需暴露头 allowCredentials: true # 允许凭证 maxAge: 3600 # 全局默认过滤器:移除 /api 前缀 default-filters: - RewritePath=/api/(?<segment>.*), /$\{segment} discovery: locator: enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名进行路由 routes: - id: gateway_self_api uri: no://op # 特殊标记,表示不转发 predicates: - Path=/gateway/api/isLicenseOverdue filters: - SetPath=/ # 内置过滤器(仅作占位符) # - id: gateway_prefix_route # uri: lb://uap-base # predicates: # - Path=/gateway/api/uap/** # filters: # - StripPrefix=2 # 移除 /gateway/api,保留 /uap/... # 1. uap-base 服务 - id: base uri: lb://uap-base predicates: - Path=/gateway/api/uap/** filters: - StripPrefix=2 - id: customer uri: lb://uap-base predicates: - Path=/gateway/api/customer/** filters: - StripPrefix=2 # 2. zhny-publish-test 服务 - id: market uri: lb://zhny-publish-test predicates: - Path=/gateway/api/market/** filters: - StripPrefix=2 - id: cmsservice uri: lb://zhny-publish-test predicates: - Path=/gateway/api/cmsservice/** filters: - StripPrefix=2 - id: sns uri: lb://zhny-publish-test predicates: - Path=/gateway/api/sns/** filters: - StripPrefix=2 - id: trade uri: lb://zhny-publish-test predicates: - Path=/gateway/api/trade/** filters: - StripPrefix=2 - id: settle uri: lb://zhny-publish-test predicates: - Path=/gateway/api/settle/** filters: - StripPrefix=2 - id: vpp uri: lb://zhny-publish-test predicates: - Path=/gateway/api/vpp/** filters: - StripPrefix=2 - id: nbxy uri: lb://zhny-publish-test predicates: - Path=/gateway/api/nbxy/** filters: - StripPrefix=2 - id: pla uri: lb://zhny-publish-test predicates: - Path=/gateway/api/pla/** filters: - StripPrefix=2 # 3. 其他独立服务 - id: analysis # zhny-esf uri: lb://zhny-publish-test predicates: - Path=/gateway/api/analysis/** filters: - StripPrefix=2 - id: dta # zhny-dta uri: lb://zhny-dta predicates: - Path=/gateway/api/dta/** filters: - StripPrefix=2 - id: crd # zhny-crd uri: lb://zhny-crd predicates: - Path=/gateway/api/crd/** filters: - StripPrefix=2 - id: hnsd # zhny-hnsd uri: lb://zhny-hnsd predicates: - Path=/gateway/api/hnsd/** filters: - StripPrefix=2 - id: grc # zhny-grc uri: lb://zhny-grc predicates: - Path=/gateway/api/grc/** filters: - StripPrefix=2 - id: ecarbon # zhny-ecarbon uri: lb://zhny-ecarbon predicates: - Path=/gateway/api/ecarbon/** filters: - StripPrefix=2 - id: rpt # zhny-rpt uri: lb://zhny-rpt predicates: - Path=/gateway/api/rpt/** filters: - StripPrefix=2 - id: gdt # zhny-gdt uri: lb://zhny-gdt predicates: - Path=/gateway/api/gdt/** filters: - StripPrefix=2 - id: ems # zhny-ems uri: lb://zhny-ems predicates: - Path=/gateway/api/ems/** filters: - StripPrefix=2 - id: ecm # zhny-ecm uri: lb://zhny-ecm predicates: - Path=/gateway/api/ecm/** filters: - StripPrefix=2 - id: hbvpp # zhny-hbvpp uri: lb://zhny-hbvpp predicates: - Path=/gateway/api/hbvpp/** filters: - StripPrefix=2 - id: gcd # zhny-gcd uri: lb://zhny-publish-test predicates: - Path=/gateway/api/gcd/** filters: - StripPrefix=2 - id: hbdkclient # zhny-hbdkclient uri: lb://zhny-hbdkclient predicates: - Path=/gateway/api/hbdkclient/** filters: - StripPrefix=2 - id: fhjhjy # zhny-fhjhjy uri: lb://zhny-fhjhjy predicates: - Path=/gateway/api/fhjhjy/** filters: - StripPrefix=2 - id: sdpxvpp # sd-sdpxvpp uri: lb://sd-sdpxvpp predicates: - Path=/gateway/api/sdpxvpp/** filters: - StripPrefix=2 - id: dts # zhny-dts uri: lb://zhny-dts predicates: - Path=/gateway/api/dts/** filters: - StripPrefix=2 - id: rec # zhny-rec uri: lb://zhny-rec predicates: - Path=/gateway/api/rec/** filters: - StripPrefix=2 - id: ptc # zhny-ptc uri: lb://zhny-ptc predicates: - Path=/gateway/api/ptc/** filters: - StripPrefix=2 - id: lmp # zhny-lmp uri: lb://zhny-publish-test predicates: - Path=/gateway/api/lmp/** filters: - StripPrefix=2 eureka: client: fetch-registry: true # 必须显式启用 register-with-eureka: true # 必须显式启用 healthcheck: enabled: false serviceUrl: defaultZone: http://qctczhny:QCTCzhny6608!@uap.qctc.com:8800/eureka instance: metadata-map: management: context-path: ${server.servlet.context-path}/actuator prefer-ip-address: true instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}} lease-renewal-interval-in-seconds: 2 #续约间隔 lease-expiration-duration-in-seconds: 4 #租约寿命 #status-page-url: http://${spring.cloud.client.ip-address}:${server.port}/api/doc.html health-check-url-path: ${server.servlet.context-path}/actuator/health #swagger: # enabled: true # name: QCTC统一开发平台 #api名称 # description: QCTC统一开发平台接口文档说明 #api描述 # termsOfServiceUrl: http://uap.qctc.com:8802${server.servlet.context-path} # developer: QCTC #api开发 # version: 3.0.0 #api开发 # basic: # enable: true # username: qctc # password: qctcadmin6608 feign: # 启用 Resilience4j circuitbreaker: enabled: true group: enabled: true httpclient: enabled: false okhttp: enabled: true compression: request: enabled: true mime-types: text/html,text/xml,text/plain,application/xml,application/json response: enabled: true # 定义客户端超时 client: config: default: # 全局默认配置 connectTimeout: 5000 # 连接超时(ms) readTimeout: 10000 # 读取超时(ms) # 绑定熔断器和超时器 resilience4jCircuitBreakerName: default resilience4jTimelimiterName: default ####超时配置#### resilience4j: circuitbreaker: instances: default: # 默认熔断器配置(作用于所有 Feign 调用) failureRateThreshold: 50 # 失败率阈值% minimumNumberOfCalls: 10 # 最小调用次数 slidingWindowType: COUNT_BASED slidingWindowSize: 10 # 统计窗口大小 waitDurationInOpenState: 10s # 熔断持续时间 timelimiter: instances: default: timeoutDuration: 5s # 单次调用超时时间 # 日志 logging: file: name: /usr/qctcapps/gateway/data/${spring.application.name}/debug.log #/usr/qctcapps/publish_test pattern: file: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx" level: root: info com.qctc.uap: info # com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder: DEBUG # org.springframework.core.env.PropertySourcesPropertyResolver: DEBUG org.springframework.cloud.gateway: TRACE org.springframework.web.cors: DEBUG reactor.netty.http.client: DEBUG 这样行吗
最新发布
09-02
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值