From 9939af2652ce479645eaa78e891ee06f33845a99 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Wed, 27 Feb 2013 16:04:23 +0800 Subject: discover-client: interact directly with waitset Currently, clients need to mess with the discover client fd directly, and manually register the waiter. Instead, this change adds a waitset parameter to discover_client_register, so that the discover client can register itself, and call discover_client_process directly. This means no proxy handlers, and no casts to waiter callbacks. We can also get rid of discover_client_get_fd. Signed-off-by: Jeremy Kerr --- ui/common/discover-client.c | 89 ++++++++++++++++++++++----------------------- ui/common/discover-client.h | 14 +------ 2 files changed, 46 insertions(+), 57 deletions(-) (limited to 'ui/common') diff --git a/ui/common/discover-client.c b/ui/common/discover-client.c index 9374dc5..f75cfb7 100644 --- a/ui/common/discover-client.c +++ b/ui/common/discover-client.c @@ -32,50 +32,6 @@ static int discover_client_destructor(void *arg) return 0; } -struct discover_client* discover_client_init( - const struct discover_client_ops *ops, void *cb_arg) -{ - struct discover_client *client; - struct sockaddr_un addr; - - client = talloc(NULL, struct discover_client); - if (!client) - return NULL; - - memcpy(&client->ops, ops, sizeof(client->ops)); - client->ops.cb_arg = cb_arg; - - client->fd = socket(AF_UNIX, SOCK_STREAM, 0); - if (client->fd < 0) { - pb_log("%s: socket: %s\n", __func__, strerror(errno)); - goto out_err; - } - - talloc_set_destructor(client, discover_client_destructor); - - client->n_devices = 0; - client->devices = NULL; - - addr.sun_family = AF_UNIX; - strcpy(addr.sun_path, PB_SOCKET_PATH); - - if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) { - pb_log("%s: connect: %s\n", __func__, strerror(errno)); - goto out_err; - } - - return client; - -out_err: - talloc_free(client); - return NULL; -} - -int discover_client_get_fd(const struct discover_client *client) -{ - return client->fd; -} - void discover_client_destroy(struct discover_client *client) { talloc_free(client); @@ -121,8 +77,9 @@ static void device_remove(struct discover_client *client, const char *id) talloc_free(device); } -int discover_client_process(struct discover_client *client) +static int discover_client_process(void *arg) { + struct discover_client *client = arg; struct pb_protocol_message *message; struct device *dev; char *dev_id; @@ -158,6 +115,48 @@ int discover_client_process(struct discover_client *client) return 0; } +struct discover_client* discover_client_init(struct waitset *waitset, + const struct discover_client_ops *ops, void *cb_arg) +{ + struct discover_client *client; + struct sockaddr_un addr; + + client = talloc(NULL, struct discover_client); + if (!client) + return NULL; + + memcpy(&client->ops, ops, sizeof(client->ops)); + client->ops.cb_arg = cb_arg; + + client->fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (client->fd < 0) { + pb_log("%s: socket: %s\n", __func__, strerror(errno)); + goto out_err; + } + + talloc_set_destructor(client, discover_client_destructor); + + client->n_devices = 0; + client->devices = NULL; + + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, PB_SOCKET_PATH); + + if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) { + pb_log("%s: connect: %s\n", __func__, strerror(errno)); + goto out_err; + } + + waiter_register(waitset, client->fd, WAIT_IN, discover_client_process, + client); + + return client; + +out_err: + talloc_free(client); + return NULL; +} + /* accessors for discovered devices */ int discover_client_device_count(struct discover_client *client) { diff --git a/ui/common/discover-client.h b/ui/common/discover-client.h index 6e768d9..6d5d1c4 100644 --- a/ui/common/discover-client.h +++ b/ui/common/discover-client.h @@ -2,6 +2,7 @@ #define _DISCOVER_CLIENT_H #include +#include struct discover_client; @@ -24,22 +25,11 @@ struct discover_client_ops { void *cb_arg; }; -struct discover_client *discover_client_init( +struct discover_client *discover_client_init(struct waitset *waitset, const struct discover_client_ops *ops, void *cb_arg); -int discover_client_get_fd(const struct discover_client *client); - void discover_client_destroy(struct discover_client *client); -/** - * Process data from the server. - * - * Will read from the client socket, and call device_add on any discovered - * devices. - * - */ -int discover_client_process(struct discover_client *client); - /** * Get the number of devices that the discover client has stored. This * is the set of devices that have been added and not removed -- cgit v1.2.1