com.test.hello.service,安装到dbus系统目录下
[D-BUS Service]
Name=com.test.hello
Exec=/bin/false
SystemdService=hello.service
hello.service,安装到systemd下
[Unit]
Description=hello
[Service]
Type=dbus
BusName=com.test.hello
ExecStart=/usr/bin/hello
Restart=on-failure
SuccessExitStatus=0 38
[Install]
WantedBy=multi-user.target
client调用代码
bool request_to_server()
{
GError *gerror = NULL;
GDBusProxy *bproxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"com.test.hello",
"/com/test/hello",
"com.test.hello",
NULL,
&gerror);
if (NULL == bproxy) {
printf ("g_bus_get_sync Failed [%s]", gerror ? gerror->message : NULL);
return false;
}
GVariant *result = g_dbus_proxy_call_sync (bproxy,
"here is the message to send",
g_variant_new ("(i)", getpid()),
G_DBUS_CALL_FLAGS_NONE,
60000, /* msec, 60s*/
NULL,
&gerror);
if (!result) {
printf ("g_bus_get_sync Failed [%s]", gerror ? gerror->message : NULL);
return false;
}
return true;
}
server回应代码
static const gchar *dbus_interface_xml =
"<node>"
" <interface name='com.test.hello'>"
" <method name='here is the message to send'>"
" <arg type='i' name='caller_pid' direction='in' />"
" <arg type='i' name='ret' direction='out' />"
" </method>"
" </interface>"
"</node>";
static GDBusInterfaceInfo *dbus_interface_info;
static GDBusNodeInfo *introspection_data = NULL;
static guint owner_id;
static void
handle_method_call (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
if (!g_strcmp0 (method_name, "here is the message to send")) {
int caller_pid = 0;
g_variant_get (parameters, "(i)", &caller_pid);
g_dbus_method_invocation_return_value (invocation, g_variant_new ("(i)", 0));
printf ("cloud launched by pid [%d]", caller_pid);
}
}
static const GDBusInterfaceVTable interface_vtable = {
handle_method_call,
NULL,
NULL
};
static void
on_bus_acquired (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
guint registration_id;
GError *error = NULL;
registration_id = g_dbus_connection_register_object (connection,
"/com/test/hello",
introspection_data->interfaces[0],
&interface_vtable,
NULL, /* user_data */
NULL, /* user_data_free_func */
&error); /* GError** */
if (registration_id == 0) {
printf ("g_dbus_connection_register_object error[%s]", error->message);
g_error_free (error);
}
printf ("backup_on_bus_acquired end [%d]", registration_id);
//g_bus_unown_name(owner_id);
//g_dbus_node_info_unref(introspection_data);
}
static void
on_name_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
{
}
static void
on_name_lost (GDBusConnection *connection, const gchar *name, gpointer user_data)
{
}
static void __reply_launch_request_to_client(void)
{
introspection_data = g_dbus_node_info_new_for_xml (dbus_interface_xml, NULL);
if (NULL == introspection_data) {
printf ("g_dbus_node_info_new_for_xml Failed");
return;
}
dbus_interface_info = introspection_data->interfaces[0];
owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
"com.test.hello",
G_BUS_NAME_OWNER_FLAGS_NONE,
on_bus_acquired,
on_name_acquired,
on_name_lost,
NULL,
NULL);
}
来源:CSDN
作者:henry860916
链接:https://blog.csdn.net/henry860916/article/details/50443561