mirror of
https://gitlab.freedesktop.org/dbus/dbus.git
synced 2026-05-23 21:18:11 +02:00
* tools/Makefile.am: Kill of print-introspect in favor of using dbus-send --print-reply=literal. * tools/print-introspect.c: Deleted. * test/glib/test-service-glib.xml: * test/glib/test-service-glib.c (my_object_get_objs): New test for "ao". * test/glib/test-dbus-glib.c (echo_received_cb): Free echo data. (main): Test GetObjs. * glib/examples/statemachine/Makefile.am: * glib/examples/statemachine/sm-marshal.list: * glib/examples/statemachine/statemachine-client.c: * glib/examples/statemachine/statemachine-server.c: * glib/examples/statemachine/statemachine-server.xml: * glib/examples/statemachine/statemachine.c: * glib/examples/statemachine/statemachine.h: * glib/examples/statemachine/statemachine.xml: New example. * glib/examples/example-service.c (main): Move invocation of dbus_g_object_type_install_info earlier, to emphasize it should only be done once. * glib/examples/example-signal-emitter.c (main): Ditto. * glib/examples/Makefile.am (SUBDIRS): Include statemachine. * glib/dbus-gvalue.h (dbus_gtype_to_signature) (dbus_gvalue_marshal): Update prototypes. * glib/dbus-gvalue.c: Update all marshalling functions to take const GValue instead of GValue. (signature_iter_to_g_type_array): Return a GPtrArray for nonfixed types. (dbus_gvalue_to_signature): Update for dbus_gtype_to_signature change. (dbus_gtype_to_signature): Handle generic collecitons and maps. Return a newly-allocated string. (demarshal_proxy, demarshal_object_path, demarshal_object) (demarshal_strv, demarshal_ghashtable): Set error, don't assert if we get the wrong types from message. (get_type_demarshaller): New function, extracted from dbus_gvalue_demarshal. (demarshal_collection): New function, demarshals generic collection. (dbus_gvalue_demarshal): Just invoke result of get_type_demarshaller. Throw error if we don't have one. (marshal_garray_basic): Abort on OOM. (get_type_marshaller): New function, extracted from dbus_gvalue_marshal. (collection_marshal_iterator, marshal_collection): New functions; implements generic marshalling for an iteratable specialized collection. (dbus_gvalue_marshal): Just invoke result of get_type_marshaller. * glib/dbus-gvalue-utils.c (gvalue_from_ptrarray_value): Handle G_TYPE_STRING. (ptrarray_value_from_gvalue): Ditto. (ptrarray_append, ptrarray_free): New functions. (slist_constructor, slist_iterator, slist_copy_elt, slist_copy) (slist_append, slist_end_append, slist_free): New functions. (dbus_g_type_specialized_builtins_init): Add append fuctions for GPtrArray and GSList. Register GSList. (test_specialized_hash, _dbus_gvalue_utils_test): New functions. * glib/dbus-gtype-specialized.h (DBusGTypeSpecializedAppendContext): New. (dbus_g_type_specialized_collection_init_append) (dbus_g_type_specialized_collection_append) (dbus_g_type_specialized_collection_end_append): Prototype. (DBusGTypeSpecializedCollectionVtable): Add append_func and end_append_func. * glib/dbus-gtype-specialized.c (dbus_g_type_specialized_collection_init_append) (dbus_g_type_specialized_collection_append) (dbus_g_type_specialized_collection_end_append): New functions. (dbus_g_type_map_value_iterate): Take const GValue. (dbus_g_type_collection_value_iterate): Ditto. * glib/dbus-gtest.c (dbus_glib_internal_do_not_use_run_tests): Run _dbus_gvalue_utils_test. * glib/dbus-gtest.h: Prototype it. * glib/dbus-gproxy.c (dbus_g_proxy_manager_filter): Avoid using uninitialized owner_list. (dbus_g_proxy_begin_call_internal): Move return_if_fail to public API. (dbus_g_proxy_end_call_internal): Update to use error set from dbus_gvalue_demarshal instead of setting it here. (dbus_g_proxy_begin_call): Move return_if_fail here. * glib/dbus-gobject.c (write_interface): Update for dbus_gtype_to_signature returning new string. * configure.in: Add glib/examples/statemachine.
202 lines
5 KiB
C
202 lines
5 KiB
C
#include <dbus/dbus-glib.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "statemachine.h"
|
|
#include "sm-marshal.h"
|
|
#include "statemachine-server.h"
|
|
|
|
enum
|
|
{
|
|
PROP_O,
|
|
PROP_BUS
|
|
};
|
|
|
|
enum
|
|
{
|
|
MACHINE_CREATED,
|
|
LAST_SIGNAL
|
|
};
|
|
|
|
static guint sm_server_signals[LAST_SIGNAL] = { 0 };
|
|
|
|
static void sm_server_set_property (GObject *object,
|
|
guint prop_id,
|
|
const GValue *value,
|
|
GParamSpec *pspec);
|
|
static void sm_server_get_property (GObject *object,
|
|
guint prop_id,
|
|
GValue *value,
|
|
GParamSpec *pspec);
|
|
|
|
G_DEFINE_TYPE(SMServer, sm_server, G_TYPE_OBJECT)
|
|
|
|
#include "statemachine-server-glue.h"
|
|
#include "statemachine-glue.h"
|
|
|
|
static void
|
|
sm_server_init (SMServer *obj)
|
|
{
|
|
obj->machines = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
|
|
}
|
|
|
|
static void
|
|
sm_server_class_init (SMServerClass *klass)
|
|
{
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
object_class->set_property = sm_server_set_property;
|
|
object_class->get_property = sm_server_get_property;
|
|
|
|
g_object_class_install_property (object_class,
|
|
PROP_BUS,
|
|
g_param_spec_boxed ("bus",
|
|
"bus",
|
|
"bus",
|
|
DBUS_TYPE_G_CONNECTION,
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
|
|
|
sm_server_signals[MACHINE_CREATED] =
|
|
g_signal_new ("machine-created",
|
|
G_OBJECT_CLASS_TYPE (klass),
|
|
G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
|
|
0,
|
|
NULL, NULL,
|
|
sm_marshal_VOID__STRING_BOXED,
|
|
G_TYPE_NONE, 2, G_TYPE_STRING, DBUS_TYPE_G_OBJECT_PATH);
|
|
}
|
|
|
|
static void
|
|
sm_server_set_property (GObject *object,
|
|
guint prop_id,
|
|
const GValue *value,
|
|
GParamSpec *pspec)
|
|
{
|
|
SMServer *server = SM_SERVER (object);
|
|
|
|
switch (prop_id)
|
|
{
|
|
case PROP_BUS:
|
|
server->bus = g_value_get_boxed (value);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void
|
|
sm_server_get_property (GObject *object,
|
|
guint prop_id,
|
|
GValue *value,
|
|
GParamSpec *pspec)
|
|
{
|
|
SMServer *server = SM_SERVER (object);
|
|
|
|
switch (prop_id)
|
|
{
|
|
case PROP_BUS:
|
|
g_value_set_boxed (value, server->bus);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
break;
|
|
}
|
|
}
|
|
|
|
gboolean
|
|
sm_server_create_machine (SMServer *server, const char *name, GError **error)
|
|
{
|
|
SMObject *machine;
|
|
char *path;
|
|
|
|
machine = g_hash_table_lookup (server->machines, name);
|
|
if (machine != NULL)
|
|
{
|
|
g_set_error (error,
|
|
SM_ERROR,
|
|
SM_ERROR_NAME_IN_USE,
|
|
"Statemachine name \"%s\" is already in use",
|
|
name);
|
|
return FALSE;
|
|
}
|
|
|
|
machine = g_object_new (SM_TYPE_OBJECT, "name", name, NULL);
|
|
|
|
path = g_strdup_printf ("/com/example/StateMachines/%s", name);
|
|
dbus_g_connection_register_g_object (server->bus, path, G_OBJECT (machine));
|
|
|
|
g_hash_table_insert (server->machines, g_strdup (name), machine);
|
|
|
|
g_print ("Created state machine with name %s at %s\n", name, path);
|
|
|
|
g_signal_emit (server, sm_server_signals[MACHINE_CREATED], 0, name, path);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static void
|
|
add_machine_to_ptr_array (gpointer key, gpointer val, gpointer data)
|
|
{
|
|
const char *name = key;
|
|
/* SMObject *sm = val; */
|
|
GPtrArray *ptrarray = data;
|
|
|
|
g_ptr_array_add (ptrarray, g_strdup_printf ("/com/example/StateMachines/%s",
|
|
name));
|
|
}
|
|
|
|
gboolean
|
|
sm_server_get_machines (SMServer *server, GPtrArray **machines, GError **error)
|
|
{
|
|
*machines = g_ptr_array_new ();
|
|
|
|
g_hash_table_foreach (server->machines, add_machine_to_ptr_array, *machines);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
int
|
|
main (int argc, char **argv)
|
|
{
|
|
DBusGConnection *bus;
|
|
DBusGProxy *bus_proxy;
|
|
GError *error = NULL;
|
|
SMServer *server;
|
|
GMainLoop *mainloop;
|
|
guint request_name_result;
|
|
|
|
g_type_init ();
|
|
|
|
dbus_g_object_type_install_info (SM_TYPE_SERVER, &dbus_glib_sm_server_object_info);
|
|
dbus_g_object_type_install_info (SM_TYPE_OBJECT, &dbus_glib_sm_object_object_info);
|
|
dbus_g_error_domain_register (SM_ERROR, NULL, SM_TYPE_ERROR);
|
|
|
|
mainloop = g_main_loop_new (NULL, FALSE);
|
|
|
|
bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
|
|
if (!bus)
|
|
g_critical ("Couldn't connect to session bus: %s\n", error->message);
|
|
|
|
bus_proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.DBus",
|
|
"/org/freedesktop/DBus",
|
|
"org.freedesktop.DBus");
|
|
|
|
if (!dbus_g_proxy_call (bus_proxy, "RequestName", &error,
|
|
G_TYPE_STRING, "com.example.StateServer",
|
|
G_TYPE_UINT, DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT,
|
|
G_TYPE_INVALID,
|
|
G_TYPE_UINT, &request_name_result,
|
|
G_TYPE_INVALID))
|
|
g_critical ("Couldn't acquire com.example.StateServer: %s\n", error->message);
|
|
|
|
server = g_object_new (SM_TYPE_SERVER, "bus", bus, NULL);
|
|
|
|
dbus_g_connection_register_g_object (bus, "/com/example/StateServer", G_OBJECT (server));
|
|
|
|
g_print ("StateMachine server initialized\n");
|
|
|
|
g_main_loop_run (mainloop);
|
|
|
|
exit (0);
|
|
}
|