more updates
[handlervirt.git] / node / publish.c
blobd5d25bb1e447c33d5e4f498ee5112efb4019938f
1 #include <avahi-client/publish.h>
2 #include <avahi-client/lookup.h>
4 #include <avahi-common/alternative.h>
5 #include <avahi-common/simple-watch.h>
6 #include <avahi-common/malloc.h>
7 #include <avahi-common/error.h>
8 #include <avahi-common/timeval.h>
9 #include <avahi-common/strlst.h>
10 #include <avahi-common/thread-watch.h>
12 #include <libvirt/libvirt.h>
14 #include "node.h"
16 struct list_el {
17 unsigned int domainid;
18 AvahiEntryGroup *group;
19 unsigned short keep;
22 typedef struct list_el domu;
24 domu * domus = NULL; /* list of domains */
25 unsigned int domu_count = 0;
28 static AvahiStringList * domainProperties(virDomainPtr thisDomain) {
29 AvahiStringList *new = NULL;
30 // avahi_string_list_new_from_array (NULL, 0);
31 virDomainInfo info;
32 virDomainInterfaceStatsStruct stats;
33 virDomainGetInfo(thisDomain, &info);
34 new = avahi_string_list_add_printf(new, "maxMem=%lu", info.maxMem);
35 new = avahi_string_list_add_printf(new, "memory=%lu", info.memory);
36 new = avahi_string_list_add_printf(new, "cpuTime=%llu", info.cpuTime);
38 /* TODO: multiple network interfaces :( */
39 char vifname[16];
40 snprintf(vifname, 14, "vif%d.0", virDomainGetID(thisDomain));
41 vifname[15] = '\0';
42 unsigned int no = 0;
44 virDomainInterfaceStats(thisDomain, vifname, &stats, sizeof(stats));
46 new = avahi_string_list_add_printf(new, "interfaces=eth%d", no);
47 new = avahi_string_list_add_printf(new, "interface_eth%d_rxbytes=%lld", no, stats.rx_bytes);
48 new = avahi_string_list_add_printf(new, "interface_eth%d_rxpackets=%lld", no, stats.rx_packets);
49 // new = avahi_string_list_add_printf(new, "interface_eth%d_rxerrs=%lld", no, stats.rx_errs);
50 // new = avahi_string_list_add_printf(new, "interface_eth%d_rxdrop=%lld", no, stats.rx_drop);
51 new = avahi_string_list_add_printf(new, "interface_eth%d_txbytes=%lld", no, stats.tx_bytes);
52 new = avahi_string_list_add_printf(new, "interface_eth%d_txpackets=%lld", no, stats.tx_packets);
53 // new = avahi_string_list_add_printf(new, "interface_eth%d_txerrs=%lld", no, stats.tx_errs);
54 // new = avahi_string_list_add_printf(new, "interface_eth%d_txdrop=%lld", no, stats.tx_drop);
56 return new;
59 void remove_everything() {
60 if (domus) {
61 int i;
62 for (i = 0; i < domu_count; i++) {
63 avahi_entry_group_free(domus[i].group);
64 domus[i].group = NULL;
66 free(domus);
67 domus = NULL;
68 domu_count = 0;
72 void migrate_everything() {
73 if (domus) {
74 virConnectPtr conn;
75 conn = virConnectOpen(NULL);
77 if (conn) {
78 int i;
79 AvahiThreadedPoll *threaded_poll = NULL;
80 AvahiClient *client = NULL;
82 if ((threaded_poll = avahi_threaded_poll_new())) {
83 if ((client = avahi_client_new(avahi_threaded_poll_get(threaded_poll), 0, NULL, NULL, NULL)))
84 avahi_threaded_poll_start(threaded_poll);
87 for (i = 0; i < domu_count; i++) {
88 virDomainPtr dom;
89 dom = virDomainLookupByID (conn, domus[i].domainid);
91 if (dom) {
92 if (client == NULL || migrate_domain(client, threaded_poll, dom) != 0) {
93 virDomainShutdown(dom);
96 virDomainFree(dom);
101 if (threaded_poll) {
102 if (client) {
103 avahi_threaded_poll_stop(threaded_poll);
104 avahi_client_free(client);
107 avahi_threaded_poll_free(threaded_poll);
110 virConnectClose(conn);
116 void create_services(AvahiClient *c) {
117 virConnectPtr conn = virConnectOpenReadOnly(NULL);
118 if (conn == NULL)
119 return;
120 int maxid = virConnectNumOfDomains(conn);
121 if (maxid > 1) { /* ignore dom0 */
122 int *ids = (int *) malloc(sizeof(int) * maxid);
123 if ((maxid = virConnectListDomains(conn, &ids[0], maxid)) < 0) {
124 // error
125 } else {
126 int i;
127 unsigned int domu_count_new = (maxid - 1);
128 domu * domus_old = domus;
129 domus = (domu *) malloc(sizeof(domu) * domu_count_new);
130 const char *type = "_domu._tcp";
132 for (i = 0; i < domu_count_new; i++) {
133 int j;
134 domus[i].group = NULL;
135 domus[i].domainid = ids[i+1];
136 domus[i].keep = 0;
138 // fprintf(stderr, "Looking for %d\n", domus[i].domainid);
140 for (j = 0; j < domu_count; j++) {
141 // fprintf(stderr, "--> %d\n", domus_old[j].domainid);
142 if (ids[i+1] == domus_old[j].domainid) {
143 // fprintf(stderr, "got it\n");
144 virDomainPtr thisDomain = virDomainLookupByID(conn, domus[i].domainid);
145 if (thisDomain) {
146 const char *name;
147 name = virDomainGetName(thisDomain);
148 if (name) {
149 domus[i].group = domus_old[j].group;
150 AvahiStringList *domainProps;
151 domainProps = domainProperties(thisDomain);
152 domus_old[j].keep = 1;
153 avahi_entry_group_update_service_txt_strlst(domus[i].group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, type, NULL, domainProps);
154 avahi_string_list_free(domainProps);
156 virDomainFree(thisDomain);
160 if (i > domu_count || domus[i].group == NULL) {
161 const char *name;
162 AvahiStringList *domainProps;
163 virDomainPtr thisDomain = virDomainLookupByID(conn, ids[i+1]);
164 if (thisDomain) {
165 domus[i].group = avahi_entry_group_new(c, entry_group_callback, NULL);
166 domainProps = domainProperties(thisDomain);
167 name = virDomainGetName(thisDomain);
169 if (name) {
170 avahi_entry_group_add_service_strlst(domus[i].group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, type, NULL, NULL, 651, domainProps);
171 avahi_entry_group_commit(domus[i].group);
172 } else {
173 domus[i].keep = 0;
176 avahi_string_list_free(domainProps);
177 virDomainFree(thisDomain);
182 for (i = 0; i < domu_count; i++) {
183 // fprintf(stderr, "id: %d, keep: %d\n", domus_old[i].domainid, domus_old[i].keep);
184 if (domus_old[i].keep == 0 && domus_old[i].group != NULL) {
185 avahi_entry_group_free(domus_old[i].group);
186 domus_old[i].group = NULL;
190 free(domus_old);
191 domu_count = domu_count_new;
193 free(ids);
194 } else {
195 remove_everything();
197 virConnectClose(conn);
198 //fprintf(stderr, "exit: %s\n", __func__);