dbus/glib/examples/statemachine/statemachine-server.c
John (J5) Palmieri 1a163e765c * configure.in: Add test/name-test/Makefile to the generated
Makefile list

	* dbus/dbus-shared.h (#define DBUS_NAME_FLAG_ALLOW_REPLACEMENT):
	New flag which replaces DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT
	(#define DBUS_NAME_FLAG_DO_NOT_QUEUE): New flag for specifying
	not to queue an ower if it can't be the primary owner

	* bus/bus.h: Add new internal BusOwner struct

	* bus/driver.c (bus_driver_handle_hello): Send flags (0 for default)
	to bus_registry_ensure and don't set the prohibit_replacement flag
	since they are now set per BusOwner and not per name.
	(bus_driver_handle_list_queued_owners): bus method (ListQueuedOwners)
	that returns the list of connections in a name's connection queue

	* bus/services.c (struct BusService): remove prohibit_replacement field
	(struct BusOwner): new struct for keeping track of queued connections
	and their associated flags for the queue
	(struct BusRegistry): add a BusOwner memory pool
	(bus_registry_new): initialize the BusOwner memory pool
	(bus_registry_unref): free the BusOwner memory pool
	(_bus_service_find_owner_link): new internal method for
	searching the queue for a specific connection
	(bus_owner_set_flags): new method for adding setting the flags on a
	bus owner
	(bus_owner_new): new method that creates a BusOwner object from the
	pool and sets its flags
	(bus_owner_ref, bus_owner_unref): ref counting for BusOwner objects
	(bus_registry_ensure): Add the flags parameter
	(bus_registry_acquire_service): Switch from using raw connections to
	using the BusOwner struct
	Add new state machine for dealing with the new set of flags
	(bus_registry_set_service_context_table, struct OwnershipCancelData,
	cancel_ownership, free_ownership_cancel_data,
	add_cancel_ownership_to_transaction, struct OwnershipRestoreData,
	restore_ownership, free_ownership_restore_data,
	add_restore_ownership_to_transaction): Switch to using BusOwner
	instead of raw connections
	(bus_service_add_owner): Add flags parameter
	Switch to using BusOwner instead of raw connections
	Add state machine for dealing with the new set of flags
	(bus_service_swap_owner): Swaps the first and second owners in the
	queue.  Used to make sure proper signals are sent when a service looses
	or gains primary ownership.  We never insert an owner at the top of the
	queue.  Instead we insert it in the second position and then swap.
	(bus_service_remove_owner): Remove the owner from the queue sending
	out the NameLost and NameOwnerChanged signals if the we were the
	primary owner
	(bus_service_get_primary_owners_connection): New method that extracts
	the connection from the primary owner
	(bus_service_get_primary_owner): Returns the BusOwner instead of the
	connection
	(bus_service_get_allow_replacement): Changed from the old
	bus_service_get_prohibit_replacement method.  Checks the flags of the
	primary owner and returns if it can be replaced or not
	(bus_service_set_prohibit_replacement): removed
	(bus_service_has_owner): returns TRUE if and owner with
	the specified connection exists in the queue

	* dbus/dbus-bus.c (dbus_bus_connection_get_unique_name): New helper
	method that only compiles if tests are enabled.  Allows us to get the
	unique name of a connection so we can check it against the queue when
	doing regression tests

	* bus/activation.c (bus_activation_send_pending_auto_activate),
	bus/dispatch.c (bus_dispatch),
	bus/driver.c (bus_driver_handle_get_service_owner,
	bus_driver_handle_get_connection_unix_user,
	bus_driver_handle_get_connection_unix_process_id,
	bus_driver_handle_get_connection_selinux_security_context),
	bus/signals.c (connection_is_primary_owner):
	use bus_service_get_primary_owners_connection instead of
	bus_service_get_primary_owner

	* dbus/dbus-sysdeps.c (_dbus_connect_unix_socket,
	_dbus_listen_unix_socket): Calculate the length of the socket
	path and use that instead of using a fixed length which was
	causing socket names to contain many trailing Nul bytes.

	* dbus/dbus-glib-lowlevel.h, glib/dbus-gobject.c
	(dbus_g_method_get_sender): New method for extracting the sender
	from a DBusGMethodInvocation
	(dbus_g_method_return_get_reply): changed name to
	dbus_g_method_get_reply
	(dbus_g_method_return_send_reply): changed name to
	dbus_g_method_send reply

	* doc/dbus-specification.xml: New docs that describe how the new
	queueing system works and talks about the changes to the how
	we specify socket names

	* glib/examples/example-service.c,
	glib/examples/example-signal-emitter.c,
	glib/examples/statemachine/statemachine-server.c:
	Changed the RequestName flags to the new system

	* test/name-test/ (test-names.c, run-test.sh, Makefile.am): New
	regression test suite for testing various states of the new
	queueing system
2005-11-22 20:37:00 +00:00

229 lines
5.7 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;
}
}
static void
machine_state_changed_cb (SMObject *obj, const char *state, gpointer data)
{
char *name;
g_object_get (obj, "name", &name, NULL);
g_print ("Machine %s switching to state %s\n", name, state);
g_free (name);
}
static void
machine_acquisition_changed_cb (SMObject *obj, gdouble progress, gpointer data)
{
char *name;
g_object_get (obj, "name", &name, NULL);
g_print ("Machine %s got progress %f\n", name, progress);
g_free (name);
}
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_connect_object (machine, "state-changed",
G_CALLBACK (machine_state_changed_cb),
NULL, 0);
g_signal_connect_object (machine, "acquisition-progress",
G_CALLBACK (machine_acquisition_changed_cb),
NULL, 0);
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, 0,
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);
}