Migrating code from deprecated dbus-glib to GDBus

101 Views Asked by At

I have been working to update some old code that uses the deprecated dbus-glib DBUS binding for Glib. The idea is to use GDBus instead. Here is the original code: `

static gpointer afpstats_thread(gpointer _data) { 
  DBusGConnection *bus;
  DBusGProxy *bus_proxy;
  GError *error = NULL;
  GMainContext *ctxt;
  GMainLoop *thread_loop;
  guint request_name_result; 
  sigset_t sigs;

/* Block all signals in this thread */
sigfillset(&sigs);
pthread_sigmask(SIG_BLOCK, &sigs, NULL);

ctxt = g_main_context_new();
thread_loop = g_main_loop_new(ctxt, FALSE);

dbus_g_object_type_install_info(AFPSTATS_TYPE_OBJECT, &dbus_glib_afpstats_obj_object_info);

if (!(bus = dbus_g_bus_get_private(DBUS_BUS_SYSTEM, ctxt, &error))) {
    LOG(log_error, logtype_afpd,"Couldn't connect to system bus: %s", error->message);
    return NULL;
}

if (!(bus_proxy = dbus_g_proxy_new_for_name(bus, "org.freedesktop.DBus",
                                            "/org/freedesktop/DBus",
                                            "org.freedesktop.DBus"))) {
    LOG(log_error, logtype_afpd,"Couldn't create bus proxy");
    return NULL;
}

if (!dbus_g_proxy_call(bus_proxy, "RequestName", &error,
                       G_TYPE_STRING, "org.netatalk.AFPStats",
                       G_TYPE_UINT, 0,
                       G_TYPE_INVALID,
                       G_TYPE_UINT, &request_name_result,
                       G_TYPE_INVALID)) {
    LOG(log_error, logtype_afpd, "Failed to acquire DBUS name: %s", error->message);
    return NULL;
}

AFPStatsObj *obj = g_object_new(AFPSTATS_TYPE_OBJECT, NULL);
dbus_g_connection_register_g_object(bus, "/org/netatalk/AFPStats", G_OBJECT(obj));

g_main_loop_run(thread_loop);
return thread_loop;
}

I have tried to replace the deprecated functions with their GDBus equivalents as per the guidelines here

This is what I have achieved so far:

static gpointer afpstats_thread(gpointer _data) { 
  GDBusGonnection *bus;
  GDBusProxy *bus_proxy;
  GError *error = NULL;
  GMainContext *ctxt;
  GMainLoop *thread_loop;
  guint request_name_result; 
  sigset_t sigs;

/* Block all signals in this thread */
sigfillset(&sigs);
pthread_sigmask(SIG_BLOCK, &sigs, NULL);

ctxt = g_main_context_new();
thread_loop = g_main_loop_new(ctxt, FALSE);

if (!(bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
                           NULL,
                           &error))) {
    LOG(log_error, logtype_afpd,"Couldn't connect to system bus: %s", error->message);
    return NULL;
}

if (!(bus_proxy = g_dbus_proxy_new_sync(bus,
                                                G_DBUS_PROXY_FLAGS_NONE,
                                                NULL, /* GDBusInterfaceInfo */
                                                "org.freedesktop.DBus",
                                                "/org/freedesktop/DBus",
                                                "org.freedesktop.DBus",
                                                NULL, /* GCancellable */
                                                &error))) {
    LOG(log_error, logtype_afpd,"Couldn't create bus proxy");
    return NULL;
}

if (!g_dbus_proxy_call_sync(bus_proxy,
                            "RequestName",
                            g_variant_new("(su)", "org.netatalk.AFPStats", 0),
                            G_DBUS_CALL_FLAGS_NONE,
                            -1, /* timeout */
                            NULL,
                            &error)) {
    LOG(log_error, logtype_afpd, "Failed to acquire DBUS name: %s", error->message);
    return NULL;
}

static const gchar introspection_xml[] =
    "<node>"
    "  <interface name='org.netatalk.AFPStats'>"
    "    <method name='GetUsers'>"
    "      <arg name='ret' type='as' direction='out'/>"
    "    </method>"
    "  </interface>"
    "</node>";

  introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
  g_assert (introspection_data != NULL);

AFPStatsObj *obj = g_object_new(AFPSTATS_TYPE_OBJECT, NULL);

g_dbus_connection_register_object(bus,
                                  "/org/netatalk/AFPStats",
                                  introspection_data->interfaces[0],
                                  NULL,
                                  G_OBJECT(obj),
                                  NULL,
                                  &error);

g_main_loop_run(thread_loop);
return thread_loop;

}

When testing the code I do not get errors saying that the bus proxy hasn't been created or that the DBUS name has not been acquired. However, when running the compiled code I get an error saying that:

dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: Object does not exist at path “/org/netatalk/AFPStats”

This indicates that the AFPStats object has not been exported as expected.

I do not know where to go from here. Any ideas would be very helpful.

0

There are 0 best solutions below