Exemple #1
0
int flow()
{
    int i,v,lh;
    list *ptr;
    for(i=0;i<=t;i++)h[i]=n[i]=e[i]=0;
    for(i=0;i<=t;i++)
    {
        if(c[0][i]==0)
            continue;
        e[i]=c[0][i];
        c[0][i]-=e[i];
        c[i][0]+=e[i];
    }
    h[0]=t+1;
    makelist();
    for(ptr=head;ptr!=NULL;ptr=ptr->next)
    {
        v=ptr->v;
        lh=h[v];
        discharge(v);
        if(h[v]>lh && head!=ptr)
            tofront(ptr);
    }
    return e[t];
    cleanlist();
}
Exemple #2
0
void cleanlist(list *&ptr=head)
{
    if(ptr)
    {
        cleanlist(ptr->next);
        delete ptr;
        ptr=NULL;
    }
}
Exemple #3
0
void run(char *myIP, virConnectPtr localhost) {

	printf("*** New run\n");
	virDomainPtr *domains, *domainsptr;
	virConnectListAllDomains(localhost, &domains, VIR_CONNECT_LIST_DOMAINS_ACTIVE|VIR_CONNECT_LIST_DOMAINS_INACTIVE|VIR_CONNECT_LIST_DOMAINS_RUNNING|VIR_CONNECT_LIST_DOMAINS_SHUTOFF);
	domainsptr = domains;
	char **domainuuids = malloc(sizeof(char*)*1024);
	char **domainuuidsptr = domainuuids;
	*domainuuids = NULL;
	printf("Domains\n");
	while(*domainsptr != NULL) {
		char buf[VIR_UUID_STRING_BUFLEN];
		virDomainGetUUIDString(*domainsptr, buf);
		const char *name = virDomainGetName(*domainsptr);
		if (!strncmp("ignore",name,strlen("ignore"))) {
			domainsptr++;
			continue;
		}
		printf("%s\n", buf);
		*(domainuuidsptr++) = strdup(buf);
		*domainuuidsptr = NULL;
		domainsptr++;
	}
	domainuuidsptr = domainuuids;
	size_t domainuuids_count=0;
	while(*domainuuidsptr != NULL) domainuuidsptr++, domainuuids_count++;
	domainuuidsptr = domainuuids;
	domainsptr = domains;
	char **hosts = gethosts();
	char **hostsptr=hosts;
	size_t hosts_count = 0;
	while (*hostsptr != NULL) hosts_count++, hostsptr++;
	hostsptr = hosts;
	struct hostwithvmhash **cs = malloc(sizeof(struct hostwithvmhash)*1024);
	*cs = NULL;
	struct hostwithvmhash **csptr = cs;
	int sign=0;
	while(*hostsptr != NULL) {
		if (!strcmp(*hostsptr, myIP)) printf("ME! ");
		sign = !sign;
		size_t k=1;
		while(*domainuuidsptr != NULL) {
/*			char *num = "$6$hest";
			char *hash = crypt(*domainuuidsptr, num);
			char *hash2 = crypt(*hostsptr, num);*/
			struct hostwithvmhash *c=malloc(sizeof(struct hostwithvmhash));
			c->host = *hostsptr;
//			c->host_hash = strdup(hash+10);
			c->vm = *domainuuidsptr;
//			c->vm_hash = strdup(hash2+10);
			c->combined = malloc(10000);
			strcpy(c->combined, "");
			strcat(c->combined, c->host);
			strcat(c->combined, c->vm);
			//c->combined = strdup(crypt(c->combined, num));


			*(csptr++) = c;
			*csptr = NULL;

			domainuuidsptr++;
		}
		domainuuidsptr = domainuuids;
		printf("%s\n", *hostsptr);
	/*	char URL[512];
		sprintf(URL, "qemu+ssh://%s/system", *hosts);
		virConnectPtr dest = virConnectOpen(URL);
		*(destsptr++) = dest;
		*destsptr = NULL;*/
		hostsptr++;
	}
	csptr = cs;
	
	printf("\n");

	size_t count=0;
	while(*csptr != NULL) count++, csptr++;
	csptr = cs;

	qsort(csptr, count, sizeof(struct hostwithvmhash*), (__compar_fn_t)cscompare);

	struct hostwithvmhash **csptrptr = csptr;
	size_t i=0,k=0,j=0,q=1,m=0;
	char *last_host=NULL,*last_vm=NULL;
	struct hostwithvmhash **seen=malloc(sizeof(struct hostwithvmhash*)*1024);
	*seen = NULL;
	struct hostwithvmhash **seenptr=seen;
	struct hostwithvmhash **seenaddptr=seen;
	while(*csptrptr != NULL) {
		if (last_host == NULL || strcmp(last_host, (*csptrptr)->host))
			last_host = (*csptrptr)->host,k++;
		
		seenptr=seen;
		int is_seen=0;
		while(*seenptr != NULL) {
			if (!strcmp((*seenptr)->vm, (*csptrptr)->vm))
				is_seen=1;
			seenptr++;
		}
		if (!is_seen) {
			m++,*(seenaddptr++) = *csptrptr, *seenaddptr = NULL;
			(*csptrptr)->prio = 100*m+(k+m)%hosts_count;
			if (m == domainuuids_count)
				m = 0, seenaddptr=seenptr=seen,*seen=NULL;
		} else
			(*csptrptr)->prio = k;
		csptrptr++;
	}
	qsort(csptr, count, sizeof(struct hostwithvmhash*), (__compar_fn_t)cscompareprio);

	struct hostwithvmhash **cleaned = cleanlist(csptr);
	struct hostwithvmhash **cleanedstart = cleaned;

	while(*cleaned != NULL) {
		virDomainPtr d = virDomainLookupByUUIDString(localhost, (*cleaned)->vm);
		const char *name = virDomainGetName(d);
		int state, reason;
		virDomainGetState(d, &state, &reason, 0);
		if (!strcmp((*cleaned)->host, myIP)) {
			printf("I should be running %s (%s)\n", name, (*cleaned)->vm);
			if (state == VIR_DOMAIN_RUNNING)
				printf("...and it's running.\n");
			else
				virDomainCreate(d);
		} else {
			printf("I should NOT be running %s (%s)\n", name, (*cleaned)->vm);
			if (state == VIR_DOMAIN_RUNNING) {
				printf("Destroying.\n");
				virDomainDestroy(d);
			} else
				printf("...and it's not running.\n");
		}
		if (d) {
			virDomainFree(d);
		}
		//printf("prio:%d host:%s, vm:%s combined:%s\n", (*cleaned)->prio, (*cleaned)->host, (*cleaned)->vm, (*cleaned)->combined);
		cleaned++;
	}
	domainuuidsptr = domainuuids;
	while(*domainuuidsptr != NULL) {
		free(*domainuuidsptr);
		domainuuidsptr++;
	}
	free(domainuuids);
	free(cleanedstart);
	csptr = cs;
	while(*csptr != NULL) {
		free((*csptr)->combined);
		free(*csptr);
		csptr++;
	}
	free(cs);
	free(seen);
	hostsptr = hosts;
	while(*hostsptr != NULL) {
		free(*hostsptr);
		hostsptr++;
	}
	free(hosts);
	domainsptr = domains;
	while(*domainsptr != NULL) {
		virDomainFree(*domainsptr);
		domainsptr++;
	}
	free(domains);
/*
	virDomainPtr *domains;
	virConnectListAllDomains(src, &domains, VIR_CONNECT_LIST_DOMAINS_ACTIVE);
	while(*domains != NULL) {
		char *config = virDomainGetXMLDesc(*domains, 0);
		char uuid[VIR_UUID_BUFLEN];
		char uuidstr[VIR_UUID_STRING_BUFLEN];
		virDomainGetUUIDString(*domains, uuidstr);
		virDomainGetUUID(*domains, uuid);
		destsptr = dests;
		while (*destsptr != NULL) {
			virConnectPtr dest = *destsptr;
			virDomainPtr d = virDomainLookupByUUID(dest, uuid);
			if (d)
				printf("%s already in dest\n", uuidstr);
			if (!d) {
				printf("%s\n", config);
				printf("Injecting domain on dest\n");
				virDomainPtr new = virDomainDefineXML(dest, config);
			}
			fflush(stdout);
			destsptr++;
		}
		domains++;
	}*/
}