Changed the node code so it also works on systems that have multiple
authorroot <root@xenapi.xen>
Tue, 30 Sep 2008 00:49:33 +0000 (30 02:49 +0200)
committerroot <root@xenapi.xen>
Tue, 30 Sep 2008 00:49:33 +0000 (30 02:49 +0200)
interfaces that avahi works on. Basically it check if there is already
an offer outgoing, if so, it ignores the additional tender.

handler_virt.c
node/avahi.c
node/node.h
node/publish.c
node/tender.c

index 9bdbbb8..5a02560 100644 (file)
@@ -618,11 +618,13 @@ static ret_t save_xml(cherokee_handler_virt_t *hdl, virDomainPtr result, cheroke
         perror(__func__);
     } else {
         char *output = virDomainGetXMLDesc(result, VIR_DOMAIN_XML_INACTIVE);
-        fwrite(output, strlen(output), sizeof(char), fd);
+        if (output) {
+            fwrite(output, strlen(output), sizeof(char), fd);
+            if (buf)
+                cherokee_buffer_add(buf, output, strlen(output));
+            free(output);
+        }
         fclose(fd);
-        if (buf)
-            cherokee_buffer_add(buf, output, strlen(output));
-        free(output);
         ret = ret_ok;
     }
     cherokee_buffer_mrproper(&path);
@@ -956,17 +958,22 @@ virt_virt_function(cherokee_handler_virt_t *hdl, virDomainPtr dom, virConnectPtr
                                         TRACE("virt", "Can't undefine %s, some idiot run it already?\n", best_tender.name);
                                         cherokee_buffer_add_long10(buf, -1);
                                     } else {
+                                        virConnectPtr virConnNew;
                                         cherokee_buffer_t uri = CHEROKEE_BUF_INIT;
-                                        virConnectClose(virConn);
+                                        // virConnectClose(virConn);
                                         cherokee_buffer_add_va (&uri, "xen://%s/", best_tender.dom);
-                                        virConn = virConnectOpen (uri.buf);
-                                        if (virConn == NULL) {
+                                        virConnNew = virConnectOpen (uri.buf);
+                                        if (virConnNew == NULL) {
                                             TRACE("virt", "Can't connect to %s\n", uri.buf);
                                             cherokee_buffer_add_long10(buf, -1);
                                         } else {
                                             cherokee_buffer_add_long10(buf,
-                                                                       (virDomainCreateLinux(virConn, xmlDesc.buf, 0) != NULL ?
+                                                                       (virDomainCreateLinux(virConnNew, xmlDesc.buf, 0) != NULL ?
                                                                        0 : -1 ));
+                                            virConnectClose(virConn);
+
+                                            virConn = virConnNew;
+                                            /* TODO: Here I actually want virConnNew to be virConn */
                                         }
                                         cherokee_buffer_mrproper(&uri);
                                     }
@@ -1509,7 +1516,11 @@ virt_build_page (cherokee_handler_virt_t *hdl)
                 char **calcpr  = NULL;
                 int xsize, ysize;
                 double ymin, ymax;
-                char *filename = mktemp(strdup("handler_virt_XXXXXX"));
+                /* TODO: betere random hier */
+                char filenametemp[L_tmpnam+1];
+                char *filename = tmpnam(filenametemp);
+
+//                char *filename = mktemp(strdup("handler_virt_XXXXXX"));
                 char *r_graph[] = { "rrdgraph", filename,
                                     "-a", "PNG",
                                     "-w", width,
@@ -1531,7 +1542,6 @@ virt_build_page (cherokee_handler_virt_t *hdl)
                 }
 
                 unlink(filename);
-                free(filename);
 
                 cherokee_buffer_mrproper(&def1);
                 cherokee_buffer_mrproper(&def2);
index 3b40ebc..9cbd2fa 100644 (file)
@@ -51,15 +51,14 @@ static void modify_callback(AvahiTimeout *e, void *userdata) {
 
     /* If the server is currently running, we need to remove our
      * service and create it anew */
-    if (avahi_client_get_state(client) == AVAHI_CLIENT_S_RUNNING) {
+
+    if (avahi_client_get_state(client) != AVAHI_CLIENT_FAILURE) {
         /* And create them again with the new name */
         struct timeval tv;
 
         create_services(client);
 
         avahi_simple_poll_get(simple_poll)->timeout_update(e, avahi_elapse_time(&tv, 1000*POLL, 0));
-
-//        fprintf(stderr, "modify\n");
     }
 }
 
@@ -80,34 +79,35 @@ static void browse_callback(
 
     switch (event) {
         case AVAHI_BROWSER_FAILURE:
+            pull_tender(name);
             fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
             avahi_service_browser_free(b);
             return;
 
         case AVAHI_BROWSER_NEW: {
-            AvahiClient *c = avahi_service_browser_get_client(b);
-            fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
+                                    AvahiClient *c = avahi_service_browser_get_client(b);
+                                    fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
 
-            /* We ignore the returned resolver object. In the callback
-               function we free it. If the server is terminated before
-               the callback function is called the server will free
-               the resolver for us. */
+                                    /* We ignore the returned resolver object. In the callback
+                                       function we free it. If the server is terminated before
+                                       the callback function is called the server will free
+                                       the resolver for us. */
 
-            if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, NULL)))
-                fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
+                                    if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, NULL)))
+                                        fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
 
-            break;
-        }
+                                    break;
+                                }
 
         case AVAHI_BROWSER_REMOVE:
-            pull_tender(name);
-            fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
-            break;
+                                pull_tender(name);
+                                fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
+                                break;
 
         case AVAHI_BROWSER_ALL_FOR_NOW:
         case AVAHI_BROWSER_CACHE_EXHAUSTED:
-            fprintf(stderr, "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
-            break;
+                                fprintf(stderr, "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
+                                break;
     }
 }
 
@@ -117,23 +117,23 @@ void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED vo
 
     switch (state) {
         case AVAHI_CLIENT_S_RUNNING: {
-            /* The server has startup successfully and registered its host
-             * name on the network, so it's time to create our services */
-            struct timeval tv;
+                                         /* The server has startup successfully and registered its host
+                                          * name on the network, so it's time to create our services */
+                                         struct timeval tv;
 
-            if (!avahi_service_browser_new(c, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_offer._tcp", NULL, 0, browse_callback, NULL)) {
-                fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(c)));
-            }
+                                         if (!avahi_service_browser_new(c, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_offer._tcp", NULL, 0, browse_callback, NULL)) {
+                                             fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(c)));
+                                         }
 
-            /* After 10s do some weird modification to the service */
-            avahi_simple_poll_get(simple_poll)->timeout_new(
-                avahi_simple_poll_get(simple_poll),
-                avahi_elapse_time(&tv, 1000*POLL, 0),
-                modify_callback,
-                c);
+                                         /* After 10s do some weird modification to the service */
+                                         avahi_simple_poll_get(simple_poll)->timeout_new(
+                                                 avahi_simple_poll_get(simple_poll),
+                                                 avahi_elapse_time(&tv, 1000*POLL, 0),
+                                                 modify_callback,
+                                                 c);
 
-            break;
-        }
+                                         break;
+                                     }
 
         case AVAHI_CLIENT_FAILURE: {
             int error;
@@ -143,6 +143,8 @@ void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED vo
                 fprintf(stderr, "Client failure: %s\n", avahi_strerror(avahi_client_errno(c)));
                 avahi_client_free(c);
             }
+            remove_everything();
+            pull_all();
 
             /* Allocate a new client */
             client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, NULL, &error);
index 5c10f1b..ea04bc4 100644 (file)
@@ -21,3 +21,6 @@ void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_
 void parse_tender(AvahiServiceResolver *r, const char *name, AvahiStringList *txt);
 void create_services(AvahiClient *c);
 int pull_tender(const char *name);
+void remove_everything();
+void pull_all();
+int has_tender(const char *name);
index 9a03961..38b5dca 100644 (file)
@@ -55,6 +55,12 @@ static AvahiStringList * domainProperties(virDomainPtr thisDomain) {
     return new;
 }
 
+void remove_everything() {
+    free(domus);
+    domus = NULL;
+    domu_count = 0;
+}
+
 
 void create_services(AvahiClient *c) {
     virConnectPtr conn = virConnectOpenReadOnly(NULL);
index 15a67a8..3385f28 100644 (file)
@@ -105,6 +105,38 @@ static int offer_tender(const char *name, unsigned long memory, AvahiClient *c)
 }
 
 
+
+void pull_all() {
+    OFFER *offer_prev = NULL;
+    OFFER *offer_find = offers_head;
+
+    while (offer_find) {
+        if (offer_find->reservate == 1) reservate =- offer_find->memory;
+        free(offer_find->name);
+        avahi_entry_group_free(offer_find->group);
+        if (!offer_prev)
+            offers_head = offer_find->next;
+        else
+            offer_prev->next = offer_find->next;
+        free(offer_find);
+    }
+}
+
+int has_tender(const char *name) {
+    OFFER *offer_prev = NULL;
+    OFFER *offer_find = offers_head;
+
+    while (offer_find) {
+        if (strcmp(offer_find->name, name) == 0) {
+            return 0;
+        } else {
+            offer_prev = offer_find;
+            offer_find = offer_find->next;
+        }
+    }
+    return -1;
+}
+
 int pull_tender(const char *name) {
     OFFER *offer_prev = NULL;
     OFFER *offer_find = offers_head;
@@ -148,7 +180,8 @@ void parse_tender(AvahiServiceResolver *r, const char *name, AvahiStringList *tx
 
                 avahi_free(winner);
             } else {
-                offer_tender(name, amount, avahi_service_resolver_get_client(r));
+                if (has_tender(name) == -1)
+                    offer_tender(name, amount, avahi_service_resolver_get_client(r));
             }
         }