What am I doing wrong?

Tako Schotanus quintesse at palacio-cristal.com
Wed May 19 08:30:03 PDT 2004


I've got some code here (basically Amaury Jacquot's code with name 
changes) that compiles and starts but just doesn't seem te receive any 
messages. After starting it in one console window I enter:

dbus-send --system --dest='org.codejive.dbus.test' 
/org/codejive/dbus/test/Test org.codejive.dbus.test.Test.Boo

in another which I assumed should print out at least something in the 
other window but without any luck so far.

On the other hand it might be that I just don't understand things 
correctly yet. Maybe I _need_ a service for example? And maybe therefore 
dbus-send can only be used as a "generic client"?

Any ideas?

Thanks,
 -Tako

-------------- next part --------------
/*
 * Just a D-Bus test
 */

#ifndef __TEST_DBUS_H__
#define __TEST_DBUS_H__

#include <dbus/dbus-glib.h>

#define CODEJIVE_DBUS_TEST_SERVICE "org.codejive.dbus.test"

/* the player interface */
#define CODEJIVE_DBUS_TEST_TEST_INTERFACE "org.codejive.dbus.test.Test"
#define CODEJIVE_DBUS_TEST_TEST_OBJECT "/org/codejive/dbus/test/Test"

#define CODEJIVE_DBUS_TEST_CMD_FOO "Foo"
#define CODEJIVE_DBUS_TEST_CMD_BAR "Bar"

#define CODEJIVE_DBUS_TEST_SIGNAL_BOO "Boo"

#endif /* __TEST_DBUS_H__ */
-------------- next part --------------
/*
 * Just a D-Bus client test
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>

#include "test.h"

static const char* client_path[] = { "org", "codejive", "dbus", NULL };

void client_unregistered_func(DBusConnection* connection, void* user_data) {
	g_print("Client object unregistered\n");
}

static DBusHandlerResult client_message_func(
		DBusConnection* connection,
		DBusMessage* message,
		gpointer data) {
	const gchar* type;
	DBusMessageIter	iter;	
	guint64 tick;

	g_print("%s\n", dbus_message_get_interface(message));
	switch (dbus_message_get_type(message)) {
		case DBUS_MESSAGE_TYPE_METHOD_CALL: type="METHOD_CALL"; break;
		case DBUS_MESSAGE_TYPE_METHOD_RETURN: type="METHOD_RETURN"; break;
		case DBUS_MESSAGE_TYPE_ERROR: type="ERROR"; break;
		case DBUS_MESSAGE_TYPE_SIGNAL: type="SIGNAL"; break;
		default: type="UNKNOWN";
	}
	g_print("type message : %s\n",type);

	if (dbus_message_is_signal(message, CODEJIVE_DBUS_TEST_TEST_INTERFACE, CODEJIVE_DBUS_TEST_SIGNAL_BOO)) {
		dbus_message_iter_init(message, &iter);
		tick = dbus_message_iter_get_uint64(&iter);
		g_print("tick: %llu\n",tick);
		return DBUS_HANDLER_RESULT_HANDLED;
	}
	
	g_print("WTF !!!\n");
	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

DBusObjectPathVTable client_vtable = {
	client_unregistered_func,
	client_message_func,
	NULL,
};

int main (int argc, char* argv[]) {
	DBusError error;
	DBusConnection* bus_conn;

	GMainLoop* loop = g_main_loop_new(NULL, FALSE);

	dbus_error_init(&error);
	bus_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
	if (bus_conn == NULL) {
		g_warning("Failed to connect to bus");
		g_warning(error.message);
		dbus_error_free(&error);
		return -1;
	}
	g_print("Connected to the bus\n");

	if (!dbus_connection_register_fallback(bus_conn, client_path, &client_vtable, NULL)) {
		g_warning("Failed to register server object with the D-BUS bus daemon");
		return -1;
	}
	g_print("fallback handler registered\n");

	dbus_bus_add_match(bus_conn, "type='signal',member='Boo',sender='org.codejive.dbus.test',interface='org.codejive.dbus.test.Test'", &error);
	if (dbus_error_is_set(&error)) {
		g_warning("Failed to add match to bus connection");
		dbus_error_free(&error);
		return -1;
	}
	g_print("match added\n");

	dbus_connection_setup_with_g_main(bus_conn, NULL);

	g_main_loop_run(loop);

	return 0;	
}


More information about the dbus mailing list