【发布时间】:2013-01-21 11:29:33
【问题描述】:
在没有启动并运行实现 D-Bus 方法的应用程序的情况下,我可以调用 dbus_g_proxy_new_for_name 吗? 我不确定这样做是否是好的做法,或者是否是通常的做法。
【问题讨论】:
在没有启动并运行实现 D-Bus 方法的应用程序的情况下,我可以调用 dbus_g_proxy_new_for_name 吗? 我不确定这样做是否是好的做法,或者是否是通常的做法。
【问题讨论】:
在回答之前,我想指出 DBus-GLib is deprecated。但是,答案也适用于g_dbus_proxy_new(基本上是dbus_g_proxy_new_for_name 的替代品)。
是的。 dbus_g_proxy_new_for_name 谈到了所有者如何随着时间而改变,尽管它没有明确提到调用时没有所有者的情况(原文强调):
名称所有者可能会随着时间而改变,例如在两个不同的方法调用之间,除非名称是唯一的名称。如果需要固定所有者,则需要请求当前所有者并将代理绑定到其唯一名称而不是通用名称;参见 dbus_g_proxy_new_for_name_owner()。
将它与 D-Bus 激活一起使用实际上很常见。查看Raphaël Slinckx' DBus Activation Tutorial 的“客户端实现”部分。它包括这个sn-p(注意cmets):
/* This won't trigger activation! */
proxy = dbus_g_proxy_new_for_name (connection,
"org.gnome.ServiceName",
"/org/gnome/ServiceName",
"org.gnome.ServiceName");
/* The method call will trigger activation, more on that later */
if (!org_gnome_ServiceName_echo_string (proxy, "The string we want echo-ed", &result, &error))
{
/* Method failed, the GError is set, let's warn everyone */
g_warning ("Woops remote method failed: %s", error->message);
g_error_free (error);
return;
}
D-Bus 激活甚至在调用方法之后才会被触发,因此显然该名称在此之前不一定存在。
【讨论】: