From 9939af2652ce479645eaa78e891ee06f33845a99 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Wed, 27 Feb 2013 16:04:23 +0800 Subject: [PATCH] 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 +----- ui/ncurses/nc-cui.c | 17 +------ ui/test/discover-test.c | 7 ++- ui/twin/pbt-client.c | 8 +--- 5 files changed, 55 insertions(+), 80 deletions(-) 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 diff --git a/ui/ncurses/nc-cui.c b/ui/ncurses/nc-cui.c index ae3d7c6..6ef43ee 100644 --- a/ui/ncurses/nc-cui.c +++ b/ui/ncurses/nc-cui.c @@ -264,17 +264,6 @@ static int cui_process_js(void *arg) return 0; } -/** - * cui_client_process_socket - Process a socket event from the discover server. - */ - -static int cui_client_process_socket(void *arg) -{ - struct discover_client *client = arg; - - discover_client_process(client); - return 0; -} /** * cui_handle_timeout - Handle the timeout. @@ -559,7 +548,8 @@ struct cui *cui_init(void* platform_info, retry_start: for (i = start_deamon ? 2 : 10; i; i--) { - client = discover_client_init(&cui_client_ops, cui); + client = discover_client_init(cui->waitset, + &cui_client_ops, cui); if (client || !i) break; pb_log("%s: waiting for server %d\n", __func__, i); @@ -596,9 +586,6 @@ retry_start: atexit(nc_atexit); nc_start(); - waiter_register(cui->waitset, discover_client_get_fd(client), WAIT_IN, - cui_client_process_socket, client); - waiter_register(cui->waitset, STDIN_FILENO, WAIT_IN, cui_process_key, cui); diff --git a/ui/test/discover-test.c b/ui/test/discover-test.c index f3ef746..ae43b1e 100644 --- a/ui/test/discover-test.c +++ b/ui/test/discover-test.c @@ -44,15 +44,18 @@ static struct discover_client_ops client_ops = { int main(void) { struct discover_client *client; + struct waitset *waitset; - client = discover_client_init(&client_ops, NULL); + waitset = waitset_create(NULL); + + client = discover_client_init(waitset, &client_ops, NULL); if (!client) return -1; for (;;) { int rc; - rc = discover_client_process(client); + rc = waiter_poll(waitset); if (rc) break; } diff --git a/ui/twin/pbt-client.c b/ui/twin/pbt-client.c index ad418a3..dfbe955 100644 --- a/ui/twin/pbt-client.c +++ b/ui/twin/pbt-client.c @@ -298,7 +298,8 @@ struct pbt_client *pbt_client_init(enum pbt_twin_backend backend, retry_start: for (i = start_deamon ? 2 : 10; i; i--) { pbt_client->discover_client - = discover_client_init(&pbt_client_ops, pbt_client); + = discover_client_init(pbt_client->waitset, + &pbt_client_ops, pbt_client); if (pbt_client->discover_client || !i) break; pb_log("%s: waiting for server %d\n", __func__, i); @@ -332,11 +333,6 @@ retry_start: goto fail_client_init; } - waiter_register(pbt_client->waitset, - discover_client_get_fd(pbt_client->discover_client), - WAIT_IN, (waiter_cb)discover_client_process, - pbt_client->discover_client); - return pbt_client; fail_client_init: -- 2.39.2