/**
* client-x
* A really simple D-Bus Client which speaks to server-x
*
* Copyright (c) 2013 Samuel A. Rebelsky. All rights reserved.
*
* This file is part of the Glimmer Guide to writing D-Bus Clients (GGDBC).
*
* GGDBC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GGDBC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GGDBC. If not, see .
*/
// +---------+---------------------------------------------------------
// | Headers |
// +---------+
#include // We use fprintf for error messages during
// development.
#include // For the GDBus functions.
// +---------+---------------------------------------------------------
// | Globals |
// +---------+
/**
* The name of the service.
*/
static const gchar service[] = "edu.grinnell.glimmer.guide.SampleServerY";
/**
* The name of the object.
*/
static const gchar object[] = "/edu/grinnell/glimmer/guide/SampleObjectX";
/**
* And the name of the interface.
*/
static const gchar interface[] = "edu.grinnell.glimmer.guide.SampleInterfaceX";
// +------+------------------------------------------------------------
// | Main |
// +------+
int
main (int argc, char *argv[])
{
GError *error = NULL; // Some functions return errors through params
GDBusProxy *proxy; // The proxy that we work with
GVariant *params; // The parameters to a function call
GVariant *result; // The value result from a call
// Prepare to use GLib
g_type_init ();
// Create the proxy
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
service,
object,
interface,
NULL,
&error);
// Check for success
if ((proxy == NULL) || (error != NULL))
{
fprintf (stderr, "Could not connect to %s:%s on %s.\n",
service, object, interface);
if (error != NULL)
{
fprintf (stderr, " %s\n", error->message);
}
return 1;
}
// Send a message
params = g_variant_new ("()");
result = g_dbus_proxy_call_sync (proxy,
"HelloWorld",
params,
0,
-1,
NULL,
&error);
// Check whether an error occurred.
if (result == NULL)
{
if (error != NULL)
{
fprintf (stderr, "Call failed because %s.\n", error->message);
} // if we got an error
else
{
fprintf (stderr, "Call failed for an unknown reason.\n");
}
return 1; // Give up!
} // if no value was result
// Okay, we got a value back. Deal with it.
else
{
gchar *str;
g_variant_get (result, "(&s)", &str);
printf ("The function result is %s\n", str);
} // succcessfully got a result
// Send a message
params = g_variant_new ("(d)", 3.0);
result = g_dbus_proxy_call_sync (proxy,
"circle",
params,
0,
-1,
NULL,
&error);
// Check whether an error occurred.
if (result == NULL)
{
if (error != NULL)
{
fprintf (stderr, "Call failed because %s.\n", error->message);
} // if we got an error
else
{
fprintf (stderr, "Call failed for an unknown reason.\n");
}
return 1; // Give up!
} // if no value was result
// Okay, we got a value back. Deal with it.
else
{
double area, circumference;
g_variant_get (result, "(dd)", &area, &circumference);
printf ("Area: %lf, Circumference: %lf\n", area, circumference);
} // succcessfully got a result
// Clean up
g_object_unref (proxy);
g_variant_unref (params);
g_variant_unref (result);
// And we're done
return 0;
} // main