1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>Writing a UPnP Client</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
7 <link rel="home" href="index.html" title="GUPnP Reference Manual">
8 <link rel="up" href="tutorial.html" title="Part I. Tutorial">
9 <link rel="prev" href="overview.html" title="Overview">
10 <link rel="next" href="server-tutorial.html" title="Writing a UPnP Service">
11 <meta name="generator" content="GTK-Doc V1.15.1 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
16 <td><a accesskey="p" href="overview.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
17 <td><a accesskey="u" href="tutorial.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
18 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
19 <th width="100%" align="center">GUPnP Reference Manual</th>
20 <td><a accesskey="n" href="server-tutorial.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
23 <div class="titlepage"><div><div><h2 class="title">
24 <a name="client-tutorial"></a>Writing a UPnP Client</h2></div></div></div>
25 <div class="simplesect">
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="id2923787"></a>Introduction</h2></div></div></div>
29 This chapter explains how to write an application which fetches the
30 external IP address from an UPnP-compliant modem. To do this a
31 <em class="glossterm">Control Point</em> is created, which searches for
33 <code class="literal">urn:schemas-upnp-org:service:WANIPConnection:1</code> (part of
34 the <a class="ulink" href="http://upnp.org/standardizeddcps/igd.asp" target="_top">Internet Gateway
35 Device</a> specification). As services are discovered
36 <em class="firstterm">Service Proxy</em> objects are created by GUPnP to allow
37 interaction with the service, on which we can invoke the action
38 <code class="function">GetExternalIPAddress</code> to fetch the external IP
42 <div class="simplesect">
43 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
44 <a name="id2901109"></a>Finding Services</h2></div></div></div>
46 First, we initialize GUPnP and create a control point targeting the
47 service type. Then we connect a signal handler so that we are notified
48 when services we are interested in are found.
50 <pre class="programlisting">#include <libgupnp/gupnp-control-point.h>
52 static GMainLoop *main_loop;
55 service_proxy_available_cb (GUPnPControlPoint *cp,
56 GUPnPServiceProxy *proxy,
63 main (int argc, char **argv)
65 GUPnPContext *context;
66 GUPnPControlPoint *cp;
68 /* Required initialisation */
72 /* Create a new GUPnP Context. By here we are using the default GLib main
73 context, and connecting to the current machine's default IP on an
74 automatically generated port. */
75 context = gupnp_context_new (NULL, NULL, 0, NULL);
77 /* Create a Control Point targeting WAN IP Connection services */
78 cp = gupnp_control_point_new
79 (context, "urn:schemas-upnp-org:service:WANIPConnection:1");
81 /* The service-proxy-available signal is emitted when any services which match
82 our target are found, so connect to it */
84 "service-proxy-available",
85 G_CALLBACK (service_proxy_available_cb),
88 /* Tell the Control Point to start searching */
89 gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
91 /* Enter the main loop. This will start the search and result in callbacks to
92 service_proxy_available_cb. */
93 main_loop = g_main_loop_new (NULL, FALSE);
94 g_main_loop_run (main_loop);
97 g_main_loop_unref (main_loop);
99 g_object_unref (context);
104 <div class="simplesect">
105 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
106 <a name="id2851466"></a>Invoking Actions</h2></div></div></div>
108 Now we have an application which searches for the service we specified and
109 calls <code class="function">service_proxy_available_cb</code> for each one it
110 found. To get the external IP address we need to invoke the
111 <code class="literal">GetExternalIPAddress</code> action. This action takes no in
112 arguments, and has a single out argument called "NewExternalIPAddress".
113 GUPnP has a set of methods to invoke actions (which will be very familiar
114 to anyone who has used <code class="literal">dbus-glib</code>) where you pass a
115 <code class="constant">NULL</code>-terminated varargs list of (name, GType, value)
116 tuples for the in arguments, then a <code class="constant">NULL</code>-terminated
117 varargs list of (name, GType, return location) tuples for the out
120 <pre class="programlisting">static void
121 service_proxy_available_cb (GUPnPControlPoint *cp,
122 GUPnPServiceProxy *proxy,
125 GError *error = NULL;
128 gupnp_service_proxy_send_action (proxy,
129 /* Action name and error location */
130 "GetExternalIPAddress", &error,
134 "NewExternalIPAddress",
135 G_TYPE_STRING, &ip,
139 g_print ("External IP address is %s\n", ip);
142 g_printerr ("Error: %s\n", error->message);
143 g_error_free (error);
145 g_main_loop_quit (main_loop);
148 Note that gupnp_service_proxy_send_action() blocks until the service has
149 replied. If you need to make non-blocking calls then use
150 gupnp_service_proxy_begin_action(), which takes a callback that will be
151 called from the mainloop when the reply is received.
154 <div class="simplesect">
155 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
156 <a name="id2894035"></a>Subscribing to state variable change notifications</h2></div></div></div>
158 It is possible to get change notifications for the service state variables
159 that have attribute <code class="literal">sendEvents="yes"</code>. We'll demonstrate
160 this by modifying <code class="function">service_proxy_available_cb()</code> and using
161 gupnp_service_proxy_add_notify() to setup a notification callback:
163 <pre class="programlisting">static void
164 external_ip_address_changed (GUPnPServiceProxy *proxy,
165 const char *variable,
169 g_print ("External IP address changed: %s\n", g_value_get_string (value));
173 service_proxy_available_cb (GUPnPControlPoint *cp,
174 GUPnPServiceProxy *proxy,
177 g_print ("Found a WAN IP Connection service\n");
179 gupnp_service_proxy_set_subscribed (proxy, TRUE);
180 if (!gupnp_service_proxy_add_notify (proxy,
183 external_ip_address_changed,
185 g_printerr ("Failed to add notify");
189 <div class="simplesect">
190 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
191 <a name="id2877235"></a>Generating Wrappers</h2></div></div></div>
193 Using gupnp_service_proxy_send_action() and gupnp_service_proxy_add_notify ()
194 can become tedious, because of the requirement to specify the types and deal
196 alternative is to use <a class="xref" href="gupnp-binding-tool.html" title="gupnp-binding-tool"><span class="refentrytitle">gupnp-binding-tool</span>(1)</a>, which
197 generates wrappers that hide the boilerplate code from you. Using a
198 wrapper generated with prefix 'ipconn' would replace
199 gupnp_service_proxy_send_action() with this code:
201 <pre class="programlisting">ipconn_get_external_ip_address (proxy, &ip, &error);</pre>
203 State variable change notifications are friendlier with wrappers as well:
205 <pre class="programlisting">static void
206 external_ip_address_changed (GUPnPServiceProxy *proxy,
207 const gchar *external_ip_address,
210 g_print ("External IP address changed: '%s'\n", external_ip_address);
214 service_proxy_available_cb (GUPnPControlPoint *cp,
215 GUPnPServiceProxy *proxy
218 g_print ("Found a WAN IP Connection service\n");
220 gupnp_service_proxy_set_subscribed (proxy, TRUE);
221 if (!ipconn_external_ip_address_add_notify (proxy,
222 external_ip_address_changed,
224 g_printerr ("Failed to add notify");
231 Generated by GTK-Doc V1.15.1</div>