Watching Proxies

Watching Proxies — Simple API for watching proxies

Synopsis

#include <gio/gio.h>

void                (*GBusProxyAppearedCallback)        (GDBusConnection *connection,
                                                         const gchar *name,
                                                         const gchar *name_owner,
                                                         GDBusProxy *proxy,
                                                         gpointer user_data);
void                (*GBusProxyVanishedCallback)        (GDBusConnection *connection,
                                                         const gchar *name,
                                                         gpointer user_data);
guint               g_bus_watch_proxy                   (GBusType bus_type,
                                                         const gchar *name,
                                                         GBusNameWatcherFlags flags,
                                                         const gchar *object_path,
                                                         const gchar *interface_name,
                                                         GType interface_type,
                                                         GDBusProxyFlags proxy_flags,
                                                         GBusProxyAppearedCallback proxy_appeared_handler,
                                                         GBusProxyVanishedCallback proxy_vanished_handler,
                                                         gpointer user_data,
                                                         GDestroyNotify user_data_free_func);
guint               g_bus_watch_proxy_on_connection     (GDBusConnection *connection,
                                                         const gchar *name,
                                                         GBusNameWatcherFlags flags,
                                                         const gchar *object_path,
                                                         const gchar *interface_name,
                                                         GType interface_type,
                                                         GDBusProxyFlags proxy_flags,
                                                         GBusProxyAppearedCallback proxy_appeared_handler,
                                                         GBusProxyVanishedCallback proxy_vanished_handler,
                                                         gpointer user_data,
                                                         GDestroyNotify user_data_free_func);
void                g_bus_unwatch_proxy                 (guint watcher_id);

Description

Convenience API for watching bus proxies.

Example 10. Simple application watching a proxy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <gio/gio.h>

static gchar *opt_name         = NULL;
static gchar *opt_object_path  = NULL;
static gchar *opt_interface    = NULL;
static gboolean opt_system_bus = FALSE;
static gboolean opt_auto_start = FALSE;
static gboolean opt_no_properties = FALSE;

static GOptionEntry opt_entries[] =
{
  { "name", 'n', 0, G_OPTION_ARG_STRING, &opt_name, "Name of the remote object to watch", NULL },
  { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_object_path, "Object path of the remote object", NULL },
  { "interface", 'i', 0, G_OPTION_ARG_STRING, &opt_interface, "D-Bus interface of remote object", NULL },
  { "system-bus", 's', 0, G_OPTION_ARG_NONE, &opt_system_bus, "Use the system-bus instead of the session-bus", NULL },
  { "auto-start", 'a', 0, G_OPTION_ARG_NONE, &opt_auto_start, "Instruct the bus to launch an owner for the name", NULL},
  { "no-properties", 'p', 0, G_OPTION_ARG_NONE, &opt_no_properties, "Do not load properties", NULL},
  { NULL}
};

static void
print_properties (GDBusProxy *proxy)
{
  gchar **property_names;
  guint n;

  g_print ("    properties:\n");

  property_names = g_dbus_proxy_get_cached_property_names (proxy);
  for (n = 0; property_names != NULL && property_names[n] != NULL; n++)
    {
      const gchar *key = property_names[n];
      GVariant *value;
      gchar *value_str;
      value = g_dbus_proxy_get_cached_property (proxy, key);
      value_str = g_variant_print (value, TRUE);
      g_print ("      %s -> %s\n", key, value_str);
      g_variant_unref (value);
      g_free (value_str);
    }
  g_strfreev (property_names);
}

static void
on_properties_changed (GDBusProxy          *proxy,
                       GVariant            *changed_properties,
                       const gchar* const  *invalidated_properties,
                       gpointer             user_data)
{
  /* Note that we are guaranteed that changed_properties and
   * invalidated_properties are never NULL
   */

  if (g_variant_n_children (changed_properties) > 0)
    {
      GVariantIter *iter;
      GVariant *item;

      g_print (" *** Properties Changed:\n");
      g_variant_get (changed_properties,
                     "a{sv}",
                     &iter);
      while ((item = g_variant_iter_next_value (iter)))
        {
          const gchar *key;
          GVariant *value;
          gchar *value_str;
          g_variant_get (item,
                         "{sv}",
                         &key,
                         &value);
          value_str = g_variant_print (value, TRUE);
          g_print ("      %s -> %s\n", key, value_str);
          g_free (value_str);
        }
    }

  if (g_strv_length ((GStrv) invalidated_properties) > 0)
    {
      guint n;
      g_print (" *** Properties Invalidated:\n");
      for (n = 0; invalidated_properties[n] != NULL; n++)
        {
          const gchar *key = invalidated_properties[n];
          g_print ("      %s\n", key);
        }
    }
}

static void
on_signal (GDBusProxy *proxy,
           gchar      *sender_name,
           gchar      *signal_name,
           GVariant   *parameters,
           gpointer    user_data)
{
  gchar *parameters_str;

  parameters_str = g_variant_print (parameters, TRUE);
  g_print (" *** Received Signal: %s: %s\n",
           signal_name,
           parameters_str);
  g_free (parameters_str);
}

static void
on_proxy_appeared (GDBusConnection *connection,
                   const gchar     *name,
                   const gchar     *name_owner,
                   GDBusProxy      *proxy,
                   gpointer         user_data)
{
  g_print ("+++ Acquired proxy object for remote object owned by %s\n"
           "    bus:          %s\n"
           "    name:         %s\n"
           "    object path:  %s\n"
           "    interface:    %s\n",
           name_owner,
           opt_system_bus ? "System Bus" : "Session Bus",
           opt_name,
           opt_object_path,
           opt_interface);

  print_properties (proxy);

  g_signal_connect (proxy,
                    "g-properties-changed",
                    G_CALLBACK (on_properties_changed),
                    NULL);

  g_signal_connect (proxy,
                    "g-signal",
                    G_CALLBACK (on_signal),
                    NULL);
}

static void
on_proxy_vanished (GDBusConnection *connection,
                   const gchar     *name,
                   gpointer         user_data)
{
  g_print ("--- Cannot create proxy object for\n"
           "    bus:          %s\n"
           "    name:         %s\n"
           "    object path:  %s\n"
           "    interface:    %s\n",
           opt_system_bus ? "System Bus" : "Session Bus",
           opt_name,
           opt_object_path,
           opt_interface);
}

int
main (int argc, char *argv[])
{
  guint watcher_id;
  GMainLoop *loop;
  GOptionContext *opt_context;
  GError *error;
  GBusNameWatcherFlags flags;
  GDBusProxyFlags proxy_flags;

  g_type_init ();

  opt_context = g_option_context_new ("g_bus_watch_proxy() example");
  g_option_context_set_summary (opt_context,
                                "Example: to watch the object of gdbus-example-server, use:\n"
                                "\n"
                                "  ./gdbus-example-watch-proxy -n org.gtk.GDBus.TestServer  \\\n"
                                "                              -o /org/gtk/GDBus/TestObject \\\n"
                                "                              -i org.gtk.GDBus.TestInterface");
  g_option_context_add_main_entries (opt_context, opt_entries, NULL);
  error = NULL;
  if (!g_option_context_parse (opt_context, &argc, &argv, &error))
    {
      g_printerr ("Error parsing options: %s", error->message);
      goto out;
    }
  if (opt_name == NULL || opt_object_path == NULL || opt_interface == NULL)
    {
      g_printerr ("Incorrect usage, try --help.\n");
      goto out;
    }

  flags = G_BUS_NAME_WATCHER_FLAGS_NONE;
  if (opt_auto_start)
    flags |= G_BUS_NAME_WATCHER_FLAGS_AUTO_START;

  proxy_flags = G_DBUS_PROXY_FLAGS_NONE;
  if (opt_no_properties)
    proxy_flags |= G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;

  watcher_id = g_bus_watch_proxy (opt_system_bus ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
                                  opt_name,
                                  flags,
                                  opt_object_path,
                                  opt_interface,
                                  G_TYPE_DBUS_PROXY,
                                  proxy_flags,
                                  on_proxy_appeared,
                                  on_proxy_vanished,
                                  NULL,
                                  NULL);

  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);

  g_bus_unwatch_proxy (watcher_id);

 out:
  g_option_context_free (opt_context);
  g_free (opt_name);
  g_free (opt_object_path);
  g_free (opt_interface);

  return 0;
}


Details

GBusProxyAppearedCallback ()

void                (*GBusProxyAppearedCallback)        (GDBusConnection *connection,
                                                         const gchar *name,
                                                         const gchar *name_owner,
                                                         GDBusProxy *proxy,
                                                         gpointer user_data);

Invoked when the proxy being watched is ready for use - the passed proxy object is valid until the GBusProxyVanishedCallback callback is invoked.

connection :

The GDBusConnection the proxy is being watched on.

name :

The name being watched.

name_owner :

Unique name of the owner of the name being watched.

proxy :

A GDBusProxy (or derived) instance with all properties loaded.

user_data :

User data passed to g_bus_watch_proxy().

Since 2.26


GBusProxyVanishedCallback ()

void                (*GBusProxyVanishedCallback)        (GDBusConnection *connection,
                                                         const gchar *name,
                                                         gpointer user_data);

Invoked when the proxy being watched has vanished. The GDBusProxy object passed in the GBusProxyAppearedCallback callback is no longer valid.

connection :

The GDBusConnection the proxy is being watched on.

name :

The name being watched.

user_data :

User data passed to g_bus_watch_proxy().

Since 2.26


g_bus_watch_proxy ()

guint               g_bus_watch_proxy                   (GBusType bus_type,
                                                         const gchar *name,
                                                         GBusNameWatcherFlags flags,
                                                         const gchar *object_path,
                                                         const gchar *interface_name,
                                                         GType interface_type,
                                                         GDBusProxyFlags proxy_flags,
                                                         GBusProxyAppearedCallback proxy_appeared_handler,
                                                         GBusProxyVanishedCallback proxy_vanished_handler,
                                                         gpointer user_data,
                                                         GDestroyNotify user_data_free_func);

Starts watching a remote object at object_path owned by name on the bus specified by bus_type. When the object is available, a GDBusProxy (or derived class cf. interface_type) instance is constructed for the interface_name D-Bus interface and then proxy_appeared_handler will be called when the proxy is ready and all properties have been loaded. When name vanishes, proxy_vanished_handler is called.

This function makes it very simple to write applications that wants to watch a well-known remote object on a well-known name, see Example 10, “Simple application watching a proxy”. Basically, the application simply starts using the proxy when proxy_appeared_handler is called and stops using it when proxy_vanished_handler is called. Callbacks will be invoked in the thread-default main loop of the thread you are calling this function from.

Applications typically use this function to watch the manager object of a well-known name. Upon acquiring a proxy for the manager object, applications typically construct additional proxies in response to the result of enumeration methods on the manager object.

Many of the comments that apply to g_bus_watch_name() also apply here. For example, you are guaranteed that one of the handlers will be invoked (on the main thread) after calling this function and also that the two handlers alternate. When you are done watching the proxy, just call g_bus_unwatch_proxy().

bus_type :

The type of bus to watch a name on.

name :

The name (well-known or unique) to watch.

flags :

Flags from the GBusNameWatcherFlags enumeration.

object_path :

The object path of the remote object to watch.

interface_name :

The D-Bus interface name for the proxy.

interface_type :

The GType for the kind of proxy to create. This must be a GDBusProxy derived type.

proxy_flags :

Flags from GDBusProxyFlags to use when constructing the proxy.

proxy_appeared_handler :

Handler to invoke when name is known to exist and the requested proxy is available.

proxy_vanished_handler :

Handler to invoke when name is known to not exist and the previously created proxy is no longer available.

user_data :

User data to pass to handlers.

user_data_free_func :

Function for freeing user_data or NULL.

Returns :

An identifier (never 0) that can be used with g_bus_unwatch_proxy() to stop watching the remote object.

Since 2.26


g_bus_watch_proxy_on_connection ()

guint               g_bus_watch_proxy_on_connection     (GDBusConnection *connection,
                                                         const gchar *name,
                                                         GBusNameWatcherFlags flags,
                                                         const gchar *object_path,
                                                         const gchar *interface_name,
                                                         GType interface_type,
                                                         GDBusProxyFlags proxy_flags,
                                                         GBusProxyAppearedCallback proxy_appeared_handler,
                                                         GBusProxyVanishedCallback proxy_vanished_handler,
                                                         gpointer user_data,
                                                         GDestroyNotify user_data_free_func);

Like g_bus_watch_proxy() but takes a GDBusConnection instead of a GBusType.

connection :

A GDBusConnection that is not closed.

name :

The name (well-known or unique) to watch.

flags :

Flags from the GBusNameWatcherFlags enumeration.

object_path :

The object path of the remote object to watch.

interface_name :

The D-Bus interface name for the proxy.

interface_type :

The GType for the kind of proxy to create. This must be a GDBusProxy derived type.

proxy_flags :

Flags from GDBusProxyFlags to use when constructing the proxy.

proxy_appeared_handler :

Handler to invoke when name is known to exist and the requested proxy is available.

proxy_vanished_handler :

Handler to invoke when name is known to not exist and the previously created proxy is no longer available.

user_data :

User data to pass to handlers.

user_data_free_func :

Function for freeing user_data or NULL.

Returns :

An identifier (never 0) that can be used with g_bus_unwatch_proxy() to stop watching the remote object.

Since 2.26


g_bus_unwatch_proxy ()

void                g_bus_unwatch_proxy                 (guint watcher_id);

Stops watching proxy.

watcher_id :

An identifier obtained from g_bus_watch_proxy()

Since 2.26