[discover client] Make devices persistent
authorJeremy Kerr <jk@ozlabs.org>
Sun, 1 Feb 2009 04:45:34 +0000 (15:45 +1100)
committerJeremy Kerr <jk@ozlabs.org>
Sun, 1 Feb 2009 05:03:18 +0000 (16:03 +1100)
Change discover client to keep devices around after they've been
added, and freed after removal.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
ui/common/discover-client.c
ui/common/discover-client.h
ui/test/pb-test.c

index d30498cdf48b4e8007d4e24a9515c326d4e2d902..47c57cb11bc69d49e69467cbe0b259e8c07069c5 100644 (file)
@@ -18,6 +18,8 @@
 struct discover_client {
        int fd;
        struct discover_client_ops ops;
+       int n_devices;
+       struct device **devices;
 };
 
 static int discover_client_destructor(void *arg)
@@ -50,6 +52,9 @@ struct discover_client* discover_client_init(
 
        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);
 
@@ -75,6 +80,46 @@ void discover_client_destroy(struct discover_client *client)
        talloc_free(client);
 }
 
+static void add_device(struct discover_client *client, struct device *device)
+{
+       client->n_devices++;
+       client->devices = talloc_realloc(client, client->devices,
+                       struct device *, client->n_devices);
+
+       client->devices[client->n_devices - 1] = device;
+       talloc_steal(client, device);
+
+       client->ops.add_device(device, client->ops.cb_arg);
+}
+
+static void remove_device(struct discover_client *client, const char *id)
+{
+       struct device *device = NULL;
+       int i;
+
+       for (i = 0; i < client->n_devices; i++) {
+               if (!strcmp(client->devices[i]->id, id)) {
+                       device = client->devices[i];
+                       break;
+               }
+       }
+
+       if (!device)
+               return;
+
+       client->ops.remove_device(device, client->ops.cb_arg);
+
+       talloc_free(device);
+
+       /* remove the device from the client's device array */
+       client->n_devices--;
+       memmove(&client->devices[i], &client->devices[i+1],
+                       client->n_devices - i);
+       client->devices = talloc_realloc(client, client->devices,
+                       struct device *, client->n_devices);
+
+}
+
 int discover_client_process(struct discover_client *client)
 {
        struct pb_protocol_message *message;
@@ -93,8 +138,8 @@ int discover_client_process(struct discover_client *client)
                        pb_log("%s: no device?\n", __func__);
                        return 0;
                }
-               client->ops.add_device(dev, client->ops.cb_arg);
-               talloc_free(dev);
+
+               add_device(client, dev);
                break;
        case PB_PROTOCOL_ACTION_REMOVE:
                dev_id = pb_protocol_deserialise_string(client, message);
@@ -102,7 +147,7 @@ int discover_client_process(struct discover_client *client)
                        pb_log("%s: no device id?\n", __func__);
                        return 0;
                }
-               client->ops.remove_device(dev_id, client->ops.cb_arg);
+               remove_device(client, dev_id);
                break;
        default:
                pb_log("%s: unknown action %d\n", __func__, message->action);
index 132da02c30a3187fe65234a2a8fc485593a06e90..6fc964de8a375faab1f058817597a678ef3436cf 100644 (file)
@@ -14,7 +14,7 @@ struct discover_client;
 
 struct discover_client_ops {
        int (*add_device)(const struct device *device, void *arg);
-       void (*remove_device)(const char *dev_id, void *arg);
+       void (*remove_device)(const struct device *device, void *arg);
        void *cb_arg;
 };
 
index 551c9b070f8eaaa475eb6cd7b3be8bf23f07c0db..b32944f0cbceac747a727e51bee959be3672ad2c 100644 (file)
@@ -28,11 +28,12 @@ static int print_device_add(const struct device *device,
        return 0;
 }
 
-static void print_device_remove(const char *dev_id,
+static void print_device_remove(const struct device *device,
        void __attribute__((unused)) *arg)
 {
        printf("removed device:\n");
-       printf("\tid:   %s\n", dev_id);
+       printf("\tid:   %s\n", device->id);
+       printf("\tname: %s\n", device->name);
 }
 
 static struct discover_client_ops client_ops = {