discover-client: interact directly with waitset
authorJeremy Kerr <jk@ozlabs.org>
Wed, 27 Feb 2013 08:04:23 +0000 (16:04 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 15 Apr 2013 07:42:26 +0000 (15:42 +0800)
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 <jk@ozlabs.org>
ui/common/discover-client.c
ui/common/discover-client.h
ui/ncurses/nc-cui.c
ui/test/discover-test.c
ui/twin/pbt-client.c

index 9374dc538506fd788a4c246a547f6d0b0629d38f..f75cfb71822949d9f5533a2bd303519592a8b301 100644 (file)
@@ -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)
 {
index 6e768d9f9c50b463a296d6fab9a1d235f0a3faf6..6d5d1c483ac33b6c3bace0b3b457d4c6721cb6a6 100644 (file)
@@ -2,6 +2,7 @@
 #define _DISCOVER_CLIENT_H
 
 #include <types/types.h>
+#include <waiter/waiter.h>
 
 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
index ae3d7c6d9741da102655850700b7b534b894fd94..6ef43ee89afa62150004c8ce96a64af1bfbad077 100644 (file)
@@ -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);
 
index f3ef746de2382488fc40c469dcbe0e39c37383da..ae43b1e524b30421e694ed4fee4d04dcb309a8b3 100644 (file)
@@ -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;
        }
index ad418a32e2e95941c9d7186471890f070da3f815..dfbe9552c32bca48a8c4f33700249896dd755c5e 100644 (file)
@@ -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: