Log In
New Account
  
 
Home My Page Project Tree Code Snippets Project Openings NUTSS
 
 
Summary Tracker Lists CVS Files
 

CVS | Administration


File:  [GForge:cvsroot/nutss] / libnutss / examples / slinks_variable_load.c
Revision 1.8: download - view: text, annotated - select for diffs
Thu May 31 22:42:53 2007 UTC (10 years, 3 months ago) by ths
Branches: MAIN
CVS tags: HEAD
more tweaks to new slinks test

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <poll.h>
#include <sys/socket.h>
#endif
#include <pthread.h>
#include "../include/swaplinks.h"

#ifdef WIN32
#define sleep(sec) Sleep((sec)*1000)
#define snprintf   _snprintf
#endif

typedef struct flow {
    int duration;
    int weight;
    char source[256];
    struct flow *next, *last;
} flow_t;

int num, limit;
FILE *f = NULL, *s = NULL;


void *recvthread(void *arg) {
    swaplinks_p swp = (swaplinks_p)arg;
    struct sockaddr_ns peer;
    socklen_t len = sizeof(peer);
    flow_t *flows = NULL, *newflow = NULL, *ptr = NULL;
    char tmp[128], buf[2048];
    int time = 0, load = 0, delta;

    buf[0] = 0;

    while(time < limit) {
        if(swaplinks_recvfrom(swp, tmp, sizeof(tmp), 0, &peer, &len) > 0) {
            time++; delta = 0;
	    if(s) fprintf(s,"\t\tTIME at node %d: %d\n",num,time); 
            newflow = (flow_t*)calloc(1,sizeof(flow_t));
            newflow->duration = atoi(strtok(tmp,";"));
            newflow->weight = atoi(strtok(NULL,";"));
	    strncpy(newflow->source,peer.user+4,sizeof(newflow->source));
            snprintf(buf,2048,"->%d:%d<->",newflow->duration,newflow->weight);
	    for(ptr = flows; ptr; ptr = ptr->next) {
		sprintf(buf,"%s%d:%d<->",buf,ptr->duration,ptr->weight);
                if(--ptr->duration == 0) {
                    load -= ptr->weight;
                    delta -= ptr->weight;
		    if(ptr->last)
			ptr->last->next = ptr->next;
                    else {
			flows = ptr->next;
			flows->last = NULL;
		    }
		    if(ptr->next)
			ptr->next->last = ptr->last;

		    if(s) fprintf(s,"%d EXPIRED FLOW from %s:d%d/w%d\n",num,ptr->source,ptr->duration,ptr->weight);
		    free(ptr);
                }
            }
	    sprintf(buf,"%s||\n",buf);

            if(s) fprintf(s,"%d ACCEPTED FLOW from %s: d%d/w%d\n",num,newflow->source,newflow->duration,newflow->weight);
	    if(s) fprintf(s,"%s",buf);	

            load += newflow->weight;
	    delta += newflow->weight;
	    newflow->next = flows;
	    if(newflow->next) newflow->next->last = newflow;
	    flows = newflow;
            if(f) fprintf(f,"%d\t%d\t%d\n",time,load,delta);
        }
    }
    if(s) fprintf(s,"%d DONE RECEIVING\n",num);

    pthread_exit(NULL);
}

int main(int argc, char **argv) {
    struct sockaddr_ns reg;
    swaplinks_p swp;
    char tmp[256];
    int time = 0, peers, duration, weight, sendcount = 0;
    pthread_t tid;

    if (argc > 5) {
        num = atoi(argv[1]);
        peers = atoi(argv[2]);
        limit = atoi(argv[3]);
        duration = atoi(argv[4]);
        weight = atoi(argv[5]);
    }
    else {
        printf("Usage:\n  slinks_variable_load node_number peers_number time_limit flow_duration flow_weight\n");
        exit(1);
    }

    snprintf(tmp, sizeof(tmp), "test%d", num);
    nutss_config_set(NUTSS_CONFIG_USERNAME, tmp, strlen(tmp));
    nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, tmp, strlen(tmp));
    nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, tmp, strlen(tmp));
    strncpy(tmp, "nutss.net", sizeof(tmp));
    nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlen(tmp));
    strncpy(tmp, "sip.nutss.net:5060", sizeof(tmp));
    nutss_config_set(NUTSS_CONFIG_SIGPROXY, tmp, strlen(tmp));

    sprintf(tmp, "output/data_node%d.txt", num); f = fopen(tmp,"w");
//    sprintf(tmp, "output/output_node%d.txt", num); s = fopen(tmp,"w");
    if (f == NULL) f = stderr;
    if (s == NULL) s = stdout;

    memset(&reg, 0, sizeof(reg));
    reg.family = AF_NUTSS;
    strncpy(reg.user, "ths1", sizeof(reg.user));
    strncpy(reg.domain, "nutss.net", sizeof(reg.domain));
    strncpy(reg.service, "swaplinksd", sizeof(reg.service));

    swaplinks_init();
    swp = swaplinks_new("cloud9", &reg, peers, peers);
    
    // let swaplinks get going.
    sleep(10);
    
    pthread_create(&tid, NULL, recvthread, swp);

    sleep(1);

    snprintf(tmp, sizeof(tmp), "%d;%d;", duration, weight);        

    while (time++ < limit) { // (i-- != 0) {
        swaplinks_sendtoany(swp, tmp, strlen(tmp)+1, 0);
        sleep(1);
    }

    if(s) fprintf(s,"%d DONE SENDING\n",num);

    pthread_join(tid,NULL);

    if(f) fclose(f);
    if(s) fclose(s);

//    if(num == 1) system("echo \" \" | mail -s\"experiment done!\" ths22@cs.cornell.edu");

    return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>
 

GForge.cis.cornell.edu is brought to you by

Cornell Computing and Information Science


Powered By GForge Collaborative Development Environment