+ ASSERT_IS_WUI_THREAD ();
+
+ /* Generate the vms URI from the base URI. */
+ len = strlen (uri) + 4 + 1;
+ vms_uri = g_alloca (len);
+ snprintf (vms_uri, len, "%s/vms", uri);
+
+ DEBUG ("vms URI %s", vms_uri);
+
+ /* Set the URI for libcurl. */
+ CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_URL, vms_uri));
+
+ /* We want to capture the output, so tell our write function
+ * to place the output into a buffer.
+ */
+ write_fn_start_capture ();
+
+ /* Try to fetch the URI. */
+ r = CURL_CHECK_ERROR (curl_easy_perform, (curl));
+ if (r != CURLE_OK) {
+ /* Signal an error back to the main thread. */
+ error_str = g_strdup (curl_easy_strerror (r));
+ g_idle_add (main_login_error, error_str);
+ return FALSE;
+ }
+
+ CURL_CHECK_ERROR (curl_easy_getinfo, (curl, CURLINFO_RESPONSE_CODE, &code));
+ DEBUG ("HTTP return code is %ld", code);
+ switch (code)
+ {
+ case 200: break;
+
+ /* Hmm - even though we previously logged in, the server is
+ * rejecting our attempts now with an authorization error.
+ * We move to the logged out state.
+ */
+ case 401:
+ set_logged_in (FALSE);
+ error_str = g_strdup ("server rejected the username or password");
+ g_idle_add (main_login_error, error_str);
+ return FALSE;
+
+ default:
+ /* XXX If only glib had g_asprintf. */
+ error_str = g_strdup ("unexpected HTTP return code from server");
+ g_idle_add (main_status_error, error_str);
+ return FALSE;
+ }
+
+ /* If we got here then we appear to have a correct
+ * XML document describing the list of VMs.
+ */
+ secs_between_refresh <<= 1;
+
+ xml = write_fn_finish_capture ();
+
+ parse_vmlist_from_xml (xml);
+ g_free (xml);
+
+ return TRUE;
+}
+
+/* Functions to deal with the list of VMs, parsing it from the XML, etc.
+ *
+ * A vmlist is a GSList (glib singly-linked list) of vm structures.
+ * The caller must call free_vmlist to free up this list correctly.
+ */
+
+static void
+free_vm (gpointer _vm, gpointer data)
+{
+ struct vm *vm = (struct vm *) _vm;
+
+ g_free (vm->description);
+ g_free (vm->state);
+ g_free (vm->uuid);
+ g_free (vm->mac_addr);
+ g_free (vm);
+}
+
+void
+free_vmlist (GSList *vmlist)
+{
+ g_slist_foreach (vmlist, free_vm, NULL);
+ g_slist_free (vmlist);
+}
+
+static struct vm *
+copy_vm (struct vm *vm)
+{
+ struct vm *vm2;
+
+ vm2 = g_memdup (vm, sizeof (*vm));
+ vm2->description = g_strdup (vm->description);
+ vm2->uuid = g_strdup (vm->uuid);
+ vm2->state = vm->state ? g_strdup (vm->state) : NULL;
+ vm2->mac_addr = vm->mac_addr ? g_strdup (vm->mac_addr) : NULL;
+ return vm2;
+}
+
+static int
+compare_vm (gconstpointer _vm1, gconstpointer _vm2)
+{
+ const struct vm *vm1 = (const struct vm *) _vm1;
+ const struct vm *vm2 = (const struct vm *) _vm2;
+
+ return strcmp (vm1->description, vm2->description);
+}
+
+static GSList *vmlist = NULL;
+static gboolean vmlist_valid = FALSE;
+static GStaticMutex vmlist_mutex;
+
+/* Called from the main thread to find out if we have a valid vmlist. */
+gboolean
+wui_thread_has_valid_vmlist (void)
+{
+ gboolean ret;
+
+ ASSERT_IS_MAIN_THREAD ();
+
+ g_static_mutex_lock (&vmlist_mutex);
+ ret = vmlist_valid;
+ g_static_mutex_unlock (&vmlist_mutex);
+ return ret;
+}
+
+/* Called from the main thread to find return the current vmlist. This
+ * actually returns a deep copy of it that the caller must free.
+ */
+static void
+duplicate_and_insert_vm (gpointer _vm, gpointer _ret)
+{
+ struct vm *vm = (struct vm *) _vm;
+ GSList **ret = (GSList **) _ret;
+
+ *ret = g_slist_prepend (*ret, copy_vm (vm));
+}
+
+gboolean
+wui_thread_get_vmlist (GSList **ret)
+{
+ gboolean r;
+
+ ASSERT_IS_MAIN_THREAD ();
+
+ r = FALSE;
+ *ret = NULL;
+
+ g_static_mutex_lock (&vmlist_mutex);
+ if (!vmlist_valid) goto done;
+
+ g_slist_foreach (vmlist, duplicate_and_insert_vm, ret);
+ *ret = g_slist_sort (*ret, compare_vm);
+
+ r = TRUE;
+ done:
+ g_static_mutex_unlock (&vmlist_mutex);
+ return r;
+}
+
+/* Called from the message loop in the WUI thread, with an XML document
+ * which we turn into a list of VM structures, and update the vmlist
+ * if we can.
+ */
+static void
+parse_vmlist_from_xml (const char *xml)
+{
+ xmlDocPtr doc = NULL;
+ xmlNodePtr root;
+ xmlNodePtr node;
+ char *error_str;
+ GSList *new_vmlist = NULL;
+ struct vm *vm;
+ int len;
+
+ /*DEBUG ("XML =\n%s", xml);*/
+ ASSERT_IS_WUI_THREAD ();
+
+ /* We don't really expect that we won't be able to parse the XML ... */
+ len = strlen (xml);
+ doc = xmlReadMemory (xml, len, NULL, NULL, 0);
+
+ if (!doc) {
+ DEBUG ("error parsing XML document, xml =\n%s", xml);
+ error_str = g_strdup ("error parsing XML document from remote server");
+ g_idle_add (main_status_error, error_str);
+ goto done;
+ }
+
+ root = xmlDocGetRootElement (doc);
+ if (!root) {
+ DEBUG ("XML document was empty");
+ error_str = g_strdup ("XML document was empty");
+ g_idle_add (main_status_error, error_str);
+ goto done;
+ }
+
+ /* We expect the root element will be either "nil-classes"
+ * or "vms", with the former indicating an empty list of VMs.
+ */
+ if (xmlStrcmp (root->name, (const xmlChar *) "nil-classes") == 0) {
+ g_static_mutex_lock (&vmlist_mutex);
+ vmlist_valid = TRUE;
+ free_vmlist (vmlist);
+ vmlist = NULL;
+ g_static_mutex_unlock (&vmlist_mutex);
+
+ /* Signal to the main UI thread that the list has been updated. */
+ g_idle_add (main_vmlist_updated, NULL);
+
+ goto done;
+ }
+
+ if (xmlStrcmp (root->name, (const xmlChar *) "vms") != 0) {
+ DEBUG ("unexpected root node in XML document, xml =\n%s", xml);
+ error_str = g_strdup ("unexpected root node in XML document");
+ g_idle_add (main_status_error, error_str);
+ goto done;
+ }
+
+ /* The document is <vms> with a list of <vm> elements which
+ * we process in turn.
+ */
+ for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
+ if (xmlStrcmp (node->name, (const xmlChar *) "vm") == 0) {
+ vm = parse_vm_from_xml (node);
+ if (!vm) {
+ error_str = g_strdup ("could not parse <vm> element");
+ g_idle_add (main_status_error, error_str);
+
+ free_vmlist (new_vmlist);
+ goto done;
+ }
+ new_vmlist = g_slist_prepend (new_vmlist, vm);
+ }
+ }
+
+ /* Successfully parsed all the <vm> nodes, so swap the old and new
+ * vmlists.
+ */
+ g_static_mutex_lock (&vmlist_mutex);
+ vmlist_valid = TRUE;
+ free_vmlist (vmlist);
+ vmlist = new_vmlist;
+ g_static_mutex_unlock (&vmlist_mutex);
+
+ /* Signal that the vmlist has been updated. */
+ g_idle_add (main_vmlist_updated, NULL);
+
+ done:
+ /* Free up XML resources used before returning. */
+ if (doc) xmlFreeDoc (doc);
+}
+
+static struct vm *
+parse_vm_from_xml (xmlNodePtr node)
+{
+ xmlNodePtr p;
+ struct vm vm, *ret;
+ xmlChar *str;
+
+ memset (&vm, 0, sizeof vm);
+ vm.hostid = -1;
+ vm.id = -1;
+ vm.vnc_port = -1;
+ vm.forward_vnc_port = -1;
+ vm.mem_allocated = -1;
+ vm.mem_used = -1;
+ vm.vcpus_allocated = -1;
+ vm.vcpus_used = -1;
+
+ for (p = node->xmlChildrenNode; p != NULL; p = p->next) {
+ if (xmlStrcmp (p->name, (const xmlChar *) "description") == 0) {
+ str = xmlNodeGetContent (p);
+ if (str != NULL) {
+ vm.description = g_strdup ((char *) str);
+ xmlFree (str);
+ }
+ }
+ else if (xmlStrcmp (p->name, (const xmlChar *) "host-id") == 0) {
+ str = xmlNodeGetContent (p);
+ if (str != NULL) {
+ vm.hostid = strtol ((char *) str, NULL, 10);
+ xmlFree (str);
+ }
+ }
+ else if (xmlStrcmp (p->name, (const xmlChar *) "id") == 0) {
+ str = xmlNodeGetContent (p);
+ if (str != NULL) {
+ vm.id = strtol ((char *) str, NULL, 10);
+ xmlFree (str);
+ }
+ }
+ else if (xmlStrcmp (p->name, (const xmlChar *) "memory-allocated") == 0) {
+ str = xmlNodeGetContent (p);
+ if (str != NULL) {
+ vm.mem_allocated = strtol ((char *) str, NULL, 10);
+ xmlFree (str);
+ }
+ }
+ else if (xmlStrcmp (p->name, (const xmlChar *) "memory-used") == 0) {
+ str = xmlNodeGetContent (p);
+ if (str != NULL) {
+ vm.mem_used = strtol ((char *) str, NULL, 10);
+ xmlFree (str);
+ }
+ }
+ else if (xmlStrcmp (p->name, (const xmlChar *) "num-vcpus-allocated") == 0) {
+ str = xmlNodeGetContent (p);
+ if (str != NULL) {
+ vm.vcpus_allocated = strtol ((char *) str, NULL, 10);
+ xmlFree (str);
+ }
+ }
+ else if (xmlStrcmp (p->name, (const xmlChar *) "num-vcpus-used") == 0) {
+ str = xmlNodeGetContent (p);
+ if (str != NULL) {
+ vm.vcpus_used = strtol ((char *) str, NULL, 10);
+ xmlFree (str);
+ }
+ }
+ else if (xmlStrcmp (p->name, (const xmlChar *) "state") == 0) {
+ str = xmlNodeGetContent (p);
+ if (str != NULL) {
+ vm.state = g_strdup ((char *) str);
+ xmlFree (str);
+ }
+ }
+ else if (xmlStrcmp (p->name, (const xmlChar *) "uuid") == 0) {
+ str = xmlNodeGetContent (p);
+ if (str != NULL) {
+ vm.uuid = g_strdup ((char *) str);
+ xmlFree (str);
+ }
+ }
+ else if (xmlStrcmp (p->name, (const xmlChar *) "vnc-port") == 0) {
+ str = xmlNodeGetContent (p);
+ if (str != NULL) {
+ vm.vnc_port = strtol ((char *) str, NULL, 10);
+ xmlFree (str);
+ }
+ }
+ else if (xmlStrcmp (p->name, (const xmlChar *) "forward-vnc-port") == 0) {
+ str = xmlNodeGetContent (p);
+ if (str != NULL) {
+ vm.forward_vnc_port = strtol ((char *) str, NULL, 10);
+ xmlFree (str);
+ }
+ }
+ else if (xmlStrcmp (p->name, (const xmlChar *) "vnic-mac-addr") == 0) {
+ str = xmlNodeGetContent (p);
+ if (str != NULL) {
+ vm.mac_addr = g_strdup ((char *) str);
+ xmlFree (str);
+ }
+ }
+ }
+
+ /* Make sure we've got the required fields. */
+ ret = NULL;
+ if (vm.description == NULL)
+ DEBUG ("required field \"description\" missing from <vm> structure");
+ else if (vm.hostid == -1)
+ DEBUG ("required field \"description\" missing from <vm> structure");
+ else if (vm.id == -1)
+ DEBUG ("required field \"description\" missing from <vm> structure");
+ else if (vm.vnc_port == -1)
+ DEBUG ("required field \"vnc-port\" missing from <vm> structure");
+ else if (vm.forward_vnc_port == -1)
+ DEBUG ("required field \"forward-vnc-port\" missing from <vm> structure");
+ else if (vm.uuid == NULL)
+ DEBUG ("required field \"uuid\" missing from <vm> structure");
+ else
+ ret = g_memdup (&vm, sizeof vm);
+
+ return ret;