From 977a6d0035dc1add3f490fcb31376271450ebfac Mon Sep 17 00:00:00 2001 From: Stephen Gallagher Date: Mon, 17 Nov 2008 09:41:06 -0500 Subject: The default message handler will now pass both the method_ctx and the sbus_conn_ctx to all message handling functions. This will allow connection-specific data to be passed in by taking advantage of the sbus_conn_set_private_data() function on the sbus_conn_ctx struct. Presently this private data is global to all methods of the connection context. --- server/sbus/sssd_dbus.h | 18 ++++++++++------ server/sbus/sssd_dbus_connection.c | 44 ++++++++++++++++++++++++++------------ server/sbus/tests/test_client.c | 1 + 3 files changed, 42 insertions(+), 21 deletions(-) (limited to 'server/sbus') diff --git a/server/sbus/sssd_dbus.h b/server/sbus/sssd_dbus.h index 5510b6e4a..321bd603e 100644 --- a/server/sbus/sssd_dbus.h +++ b/server/sbus/sssd_dbus.h @@ -52,19 +52,17 @@ struct sbus_method { struct sbus_method_ctx { struct sbus_method_ctx *prev, *next; - /*struct event_context *ev;*/ char *interface; char *path; - - /* If a non-default message_handler is desired, set it in this - * object before calling sbus_conn_add_method_ctx() - * Otherwise it will default to message_handler() in - * sssd_dbus_connection.c - */ DBusObjectPathMessageFunction message_handler; struct sbus_method *methods; }; +struct sbus_message_handler_ctx { + struct sbus_conn_ctx *conn_ctx; + struct sbus_method_ctx *method_ctx; +}; + /* Server Functions */ int sbus_new_server(struct event_context *ev, struct sbus_method_ctx *ctx, const char *address, @@ -87,4 +85,10 @@ void sbus_conn_set_private_data(struct sbus_conn_ctx *conn_ctx, void *pvt_data); int sbus_conn_add_method_ctx(struct sbus_conn_ctx *conn_ctx, struct sbus_method_ctx *method_ctx); +/* Default message handler + * Should be usable for most cases */ +DBusHandlerResult sbus_message_handler(DBusConnection *conn, + DBusMessage *message, + void *user_data); + #endif /* _SSSD_DBUS_H_*/ diff --git a/server/sbus/sssd_dbus_connection.c b/server/sbus/sssd_dbus_connection.c index d84dd2184..15086f3bb 100644 --- a/server/sbus/sssd_dbus_connection.c +++ b/server/sbus/sssd_dbus_connection.c @@ -466,18 +466,21 @@ void sbus_disconnect (struct sbus_conn_ctx *dct_ctx) /* messsage_handler * Receive messages and process them */ -static DBusHandlerResult message_handler(DBusConnection *conn, +DBusHandlerResult sbus_message_handler(DBusConnection *conn, DBusMessage *message, void *user_data) { - struct sbus_method_ctx *ctx; + struct sbus_message_handler_ctx *ctx; const char *method; const char *path; const char *msg_interface; DBusMessage *reply = NULL; int i, ret; - ctx = talloc_get_type(user_data, struct sbus_method_ctx); + if (!user_data) { + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + ctx = talloc_get_type(user_data, struct sbus_message_handler_ctx); method = dbus_message_get_member(message); path = dbus_message_get_path(message); @@ -487,14 +490,14 @@ static DBusHandlerResult message_handler(DBusConnection *conn, return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; /* Validate the method interface */ - if (strcmp(msg_interface, ctx->interface) != 0) + if (strcmp(msg_interface, ctx->method_ctx->interface) != 0) return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; /* Validate the D-BUS path */ - if (strcmp(path, ctx->path) == 0) { - for (i = 0; ctx->methods[i].method != NULL; i++) { - if (strcmp(method, ctx->methods[i].method) == 0) { - ret = ctx->methods[i].fn(message, ctx, &reply); + if (strcmp(path, ctx->method_ctx->path) == 0) { + for (i = 0; ctx->method_ctx->methods[i].method != NULL; i++) { + if (strcmp(method, ctx->method_ctx->methods[i].method) == 0) { + ret = ctx->method_ctx->methods[i].fn(message, ctx, &reply); /* FIXME: check error */ break; } @@ -520,8 +523,10 @@ int sbus_conn_add_method_ctx(struct sbus_conn_ctx *dct_ctx, struct sbus_method_ctx *method_ctx) { DBusObjectPathVTable *connection_vtable; + struct sbus_message_handler_ctx *msg_handler_ctx; + dbus_bool_t dbret; - if (!method_ctx) { + if (!dct_ctx||!method_ctx) { return EINVAL; } @@ -529,18 +534,29 @@ int sbus_conn_add_method_ctx(struct sbus_conn_ctx *dct_ctx, return EINVAL; } + if (method_ctx->message_handler == NULL) { + return EINVAL; + } + DLIST_ADD(dct_ctx->method_ctx_list, method_ctx); /* Set up the vtable for the object path */ connection_vtable = talloc_zero(dct_ctx, DBusObjectPathVTable); - if (method_ctx->message_handler) { - connection_vtable->message_function = method_ctx->message_handler; - } else { - connection_vtable->message_function = message_handler; + if (!connection_vtable) { + return ENOMEM; + } + connection_vtable->message_function = method_ctx->message_handler; + + msg_handler_ctx = talloc_zero(dct_ctx, struct sbus_message_handler_ctx); + if (!msg_handler_ctx) { + talloc_free(connection_vtable); + return ENOMEM; } + msg_handler_ctx->conn_ctx = dct_ctx; + msg_handler_ctx->method_ctx = method_ctx; dbret = dbus_connection_register_object_path(dct_ctx->conn, method_ctx->path, - connection_vtable, method_ctx); + connection_vtable, msg_handler_ctx); if (!dbret) { return ENOMEM; } diff --git a/server/sbus/tests/test_client.c b/server/sbus/tests/test_client.c index 06cd11338..b2dce4260 100644 --- a/server/sbus/tests/test_client.c +++ b/server/sbus/tests/test_client.c @@ -182,6 +182,7 @@ int main (int argc, const char *argv[]) service_methods->interface = talloc_strdup(service_methods, SERVICE_INTERFACE); service_methods->path = talloc_strdup(service_methods, SERVICE_PATH); service_methods->methods = monitor_service_methods; + service_methods->message_handler = sbus_message_handler; sbus_conn_add_method_ctx(test_ctx->dct_ctx, service_methods); /* Enter the main loop (and hopefully never return) */ -- cgit