summaryrefslogtreecommitdiff
path: root/server/dbus/tests/test_client.c
diff options
context:
space:
mode:
authorSimo Sorce <ssorce@redhat.com>2008-10-24 19:09:44 +0200
committerSimo Sorce <idra@samba.org>2008-10-27 10:02:38 -0400
commit6fe2bfce9fb9bb1564be8257cccc52bcec589de4 (patch)
tree1b9e603f7b08f8697b443e94a0e358b4fb00f07c /server/dbus/tests/test_client.c
parent054a931b01f98cf94b42f1ff1f48fbcb4928c869 (diff)
downloadsssd-6fe2bfce9fb9bb1564be8257cccc52bcec589de4.tar.gz
sssd-6fe2bfce9fb9bb1564be8257cccc52bcec589de4.tar.bz2
sssd-6fe2bfce9fb9bb1564be8257cccc52bcec589de4.zip
D-BUS integration style changes.
Rework interfaces a bit to simplify and uniform function names so that they use a well defined namespace (sssd_*). Simplify headers file, split them into a private and a public one only. Make static all file private functions. Rename sssd_dbus_client.c to sssd_dbus_connection.c to reflect it's function, as it is is used by both a server and a client. Introduce a function table to know where to dipatch messages. Fix coding style issues, and start pointing out where clean-up fucntions are missing.
Diffstat (limited to 'server/dbus/tests/test_client.c')
-rw-r--r--server/dbus/tests/test_client.c126
1 files changed, 86 insertions, 40 deletions
diff --git a/server/dbus/tests/test_client.c b/server/dbus/tests/test_client.c
index 8a82284c..30401991 100644
--- a/server/dbus/tests/test_client.c
+++ b/server/dbus/tests/test_client.c
@@ -2,16 +2,27 @@
#include <stdio.h>
#include <sys/time.h>
#include "events.h"
-#include "dbus/dbus.h"
-#include "monitor.h"
-#include "dbus/sssd_dbus_common.h"
-#include "dbus/sssd_dbus_client.h"
#include "util/util.h"
+#include "dbus/dbus.h"
+#include "dbus/sssd_dbus.h"
-static void request_version_timed(struct event_context *ev,
- DBusConnection *conn);
+/* TODO: get this value from LDB */
+#define DBUS_ADDRESS "unix:path=/var/lib/sss/pipes/private/dbus"
-static void print_version (DBusPendingCall *pending, void *ptr) {
+/* Monitor Interface */
+#define MONITOR_DBUS_INTERFACE "org.freeipa.sssd.monitor"
+#define MONITOR_DBUS_PATH "/org/freeipa/sssd/monitor"
+#define MONITOR_METHOD_VERSION "getVersion"
+
+struct test_cli_ctx {
+ struct sssd_dbus_ctx *sd_ctx;
+ DBusConnection *conn;
+};
+
+static void request_version_timed(struct test_cli_ctx *ctx);
+
+static void print_version(DBusPendingCall *pending, void *data)
+{
DBusMessage *reply;
DBusError error;
const char *version_string;
@@ -25,25 +36,27 @@ static void print_version (DBusPendingCall *pending, void *ptr) {
* until reply is valid. If reply is NULL here, something is seriously
* wrong and we should bail out.
*/
- DEBUG(0,("Serious error. A reply callback was called but no reply was received"));
+ DEBUG(0, ("Serious error. A reply callback was called but no reply was received"));
exit(3);
}
type = dbus_message_get_type(reply);
- if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
- if(dbus_message_get_args(reply, &error, DBUS_TYPE_STRING, &version_string, DBUS_TYPE_INVALID)) {
+ switch (type) {
+ case DBUS_MESSAGE_TYPE_METHOD_RETURN:
+ if (dbus_message_get_args(reply, &error,
+ DBUS_TYPE_STRING,
+ &version_string,
+ DBUS_TYPE_INVALID)) {
fprintf(stdout, "Version: %s\n", version_string);
fflush(stdout);
- }
- else {
+ } else {
DEBUG(0, ("Error getting arguments in print_version"));
}
- }
- else if (type == DBUS_MESSAGE_TYPE_ERROR) {
-
- }
- else {
+ break;
+ case DBUS_MESSAGE_TYPE_ERROR:
+ break;
+ default:
DEBUG(0, ("Received unexpected message"));
exit(4);
}
@@ -51,22 +64,29 @@ static void print_version (DBusPendingCall *pending, void *ptr) {
static void test_timed_handler(struct event_context *ev,
struct timed_event *te,
- struct timeval t, void *ptr) {
- DBusConnection *conn;
+ struct timeval tv, void *data)
+{
+ struct test_cli_ctx *test_ctx;
+ struct sssd_dbus_ctx *ctx;
DBusPendingCall *pending_reply;
- DBusMessage *version_request;
+ DBusMessage *vmsg;
DBusError error;
- conn = (DBusConnection *) ptr;
+ dbus_bool_t dbret;
+
+ test_ctx = talloc_get_type(data, struct test_cli_ctx);
+ ctx = test_ctx->sd_ctx;
fprintf(stdout, ".");
fflush(stdout);
dbus_error_init(&error);
- version_request = dbus_message_new_method_call(
- NULL, MONITOR_DBUS_PATH, MONITOR_DBUS_INTERFACE,
- MONITOR_METHOD_VERSION);
+ vmsg = dbus_message_new_method_call(NULL,
+ ctx->path, ctx->name,
+ MONITOR_METHOD_VERSION);
- if(!dbus_connection_send_with_reply(conn, version_request, &pending_reply, -1)){
+ dbret = dbus_connection_send_with_reply(test_ctx->conn, vmsg,
+ &pending_reply, -1);
+ if (!dbret) {
/* Critical failure */
DEBUG(0,("Failed to send version_request"));
exit(2);
@@ -74,50 +94,76 @@ static void test_timed_handler(struct event_context *ev,
dbus_pending_call_set_notify(pending_reply, print_version, NULL, NULL);
- dbus_message_unref(version_request);
+ dbus_message_unref(vmsg);
- request_version_timed(ev,conn);
+ request_version_timed(test_ctx);
}
-static void request_version_timed(struct event_context *ev, DBusConnection *conn) {
+static void request_version_timed(struct test_cli_ctx *ctx)
+{
struct timed_event *te = NULL;
struct timeval tv;
gettimeofday(&tv, NULL);
tv.tv_sec += 5;
tv.tv_usec = 0;
- te = event_add_timed(ev, ev, tv, test_timed_handler, conn);
+ te = event_add_timed(ctx->sd_ctx->ev, ctx, tv, test_timed_handler, ctx);
if (te == NULL) {
DEBUG(0, ("failed to add event!\n"));
exit(1);
}
}
-int main (int argc, const char *argv[]) {
- DBusError dbus_error;
- DBusConnection *dbus_conn;
+int main (int argc, const char *argv[])
+{
struct event_context *event_ctx;
+ struct sssd_dbus_ctx *ctx;
+ struct test_cli_ctx *test_ctx;
+ DBusConnection *dbus_conn;
+ int ret;
event_ctx = event_context_init(talloc_autofree_context());
+ if (!event_ctx) {
+ printf("Out of memory!?\n");
+ exit(1);
+ }
+
+ ctx = talloc_zero(event_ctx, struct sssd_dbus_ctx);
+ if (!ctx) {
+ printf("Out of memory!?\n");
+ exit(1);
+ }
+ ctx->ev = event_ctx;
+ ctx->name = talloc_strdup(ctx, MONITOR_DBUS_INTERFACE);
+ ctx->path = talloc_strdup(ctx, MONITOR_DBUS_PATH);
+ if (!ctx->name || !ctx->path) {
+ printf("Out of memory!?\n");
+ exit(1);
+ }
+
+ ret = sssd_new_dbus_connection(ctx, DBUS_ADDRESS, &dbus_conn);
+ if (ret != EOK) {
+ exit(1);
+ }
- dbus_error_init(&dbus_error);
- dbus_conn = dbus_connection_open(DBUS_ADDRESS, &dbus_error);
- if (dbus_conn == NULL) {
- printf ("Error: name=%s, message=%s\n", dbus_error.name, dbus_error.message);
+ test_ctx = talloc(ctx, struct test_cli_ctx);
+ if (!test_ctx) {
+ printf("Out of memory!?\n");
exit(1);
}
+ test_ctx->sd_ctx = ctx;
+ test_ctx->conn = dbus_conn;
- integrate_connection_with_event_loop(event_ctx,dbus_conn);
- dbus_connection_set_exit_on_disconnect (dbus_conn, TRUE);
+ dbus_connection_set_exit_on_disconnect(dbus_conn, TRUE);
/* Set up a timed event to request the server version every
* five seconds and print it to the screen.
*/
- request_version_timed(event_ctx,dbus_conn);
+ request_version_timed(test_ctx);
/* Enter the main loop (and hopefully never return) */
event_loop_wait(event_ctx);
talloc_free(event_ctx);
return EXIT_SUCCESS;
-} \ No newline at end of file
+}