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

CVS | Administration

Annotation of libnutss/examples/slinks_variable_load.c, revision 1.5

1.1       ths         1: #include <stdio.h>
                      2: #include <stdlib.h>
                      3: #include <string.h>
                      4: #ifndef WIN32
                      5: #include <poll.h>
                      6: #include <sys/socket.h>
                      7: #endif
                      8: #include <pthread.h>
                      9: #include "../include/swaplinks.h"
                     10:
                     11: #ifdef WIN32
                     12: #define sleep(sec) Sleep((sec)*1000)
                     13: #define snprintf   _snprintf
                     14: #endif
                     15:
                     16: typedef struct flow {
                     17:     int duration;
                     18:     int weight;
1.4       ths        19:     char source[256];
1.1       ths        20:     struct flow *next, *last;
                     21: } flow_t;
                     22:
                     23: int num, limit;
                     24: FILE *f = NULL, *s = NULL;
                     25:
                     26:
                     27: void *recvthread(void *arg) {
                     28:     swaplinks_p swp = (swaplinks_p)arg;
                     29:     struct sockaddr_ns peer;
                     30:     socklen_t len = sizeof(peer);
                     31:     flow_t *flows, *newflow, *ptr;
                     32:     char tmp[128];
1.4       ths        33:     int time = 0, load = 0, delta;
1.1       ths        34:
                     35:     while(time < limit) {
                     36:         if(swaplinks_recvfrom(swp, tmp, sizeof(tmp), 0, &peer, &len) > 0) {
1.4       ths        37:             time++; delta = 0;
1.2       ths        38:             newflow = (flow_t*)calloc(1,sizeof(flow_t));
1.1       ths        39:             newflow->duration = atoi(strtok(tmp,";"));
                     40:             newflow->weight = atoi(strtok(NULL,";"));
1.5     ! ths        41:           strncpy(newflow->source,peer.user+4,sizeof(newflow->source));
        !            42:             if(s) fprintf(s,"%d ACCEPTED FLOW from %s: d%d/w%d\n",num,newflow->source,newflow->duration,newflow->weight);
1.1       ths        43:             for(ptr = flows; ptr; ptr = ptr->next) {
                     44:                 if(--ptr->duration == 0) {
                     45:                     load -= ptr->weight;
1.4       ths        46:                     delta -= ptr->weight;
                     47:          if(ptr->last) ptr->last->next = ptr->next;
1.1       ths        48:                     else          flows = ptr->next;
1.5     ! ths        49:          if(s) fprintf(s,"%d EXPIRED FLOW from %s:d%d/w%d\n",num,ptr->source,ptr->duration,ptr->weight);
1.1       ths        50:          free(ptr);
                     51:                 }
                     52:             }
1.2       ths        53:             load += newflow->weight;
1.4       ths        54:           delta += newflow->weight;
1.1       ths        55:           newflow->next = flows;
                     56:           flows = newflow;
1.4       ths        57:             if(f) fprintf(f,"%d\t%d\t%d\n",time,load,delta);
1.1       ths        58:         }
                     59:     }
1.5     ! ths        60:     if(s) fprintf(s,"%d DONE RECEIVING\n",num);
1.1       ths        61: }
                     62:
                     63: int main(int argc, char **argv) {
                     64:     struct sockaddr_ns reg;
                     65:     swaplinks_p swp;
                     66:     char tmp[256];
                     67:     int time = 0, peers, duration, weight, sendcount = 0;
                     68:     pthread_t tid;
                     69:
                     70:     if (argc > 5) {
                     71:         num = atoi(argv[1]);
                     72:         peers = atoi(argv[2]);
                     73:         limit = atoi(argv[3]);
                     74:         duration = atoi(argv[4]);
                     75:         weight = atoi(argv[5]);
                     76:     }
                     77:     else {
                     78:         printf("Usage:\n  slinks_variable_load node_number peers_number time_limit flow_duration flow_weight\n");
                     79:         exit(1);
                     80:     }
                     81:
                     82:     snprintf(tmp, sizeof(tmp), "test%d", num);
                     83:     nutss_config_set(NUTSS_CONFIG_USERNAME, tmp, strlen(tmp));
                     84:     nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, tmp, strlen(tmp));
                     85:     nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, tmp, strlen(tmp));
                     86:     strncpy(tmp, "nutss.net", sizeof(tmp));
                     87:     nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlen(tmp));
                     88:     strncpy(tmp, "sip.nutss.net:5060", sizeof(tmp));
                     89:     nutss_config_set(NUTSS_CONFIG_SIGPROXY, tmp, strlen(tmp));
                     90:
                     91:     sprintf(tmp, "output/data_node%d.txt", num); f = fopen(tmp,"w");
                     92:     sprintf(tmp, "output/output_node%d.txt", num); s = fopen(tmp,"w");
                     93:     if (f == NULL) f = stderr;
                     94:     if (s == NULL) s = stdout;
                     95:
                     96:     memset(&reg, 0, sizeof(reg));
                     97:     reg.family = AF_NUTSS;
                     98:     strncpy(reg.user, "ths1", sizeof(reg.user));
                     99:     strncpy(reg.domain, "nutss.net", sizeof(reg.domain));
                    100:     strncpy(reg.service, "swaplinksd", sizeof(reg.service));
                    101:
                    102:     swaplinks_init();
                    103:     swp = swaplinks_new("cloud9", &reg, peers, peers);
                    104:
                    105:     // let swaplinks get going.
                    106:     sleep(10);
                    107:
                    108:     pthread_create(&tid, NULL, recvthread, swp);
                    109:
                    110:     sleep(1);
                    111:
1.5     ! ths       112:     snprintf(tmp, sizeof(tmp), "%d;%d;", duration, weight);
        !           113:
1.1       ths       114:     while (time++ < limit*10) { // (i-- != 0) {
1.5     ! ths       115:         swaplinks_sendtoany(swp, tmp, strlen(tmp)+1, 0);
1.1       ths       116:         sleep(1);
                    117:     }
1.5     ! ths       118:
        !           119:     if(s) fprintf(s,"%d DONE SENDING\n",num);
1.1       ths       120:
                    121:     pthread_join(tid,NULL);
                    122:
                    123:     if(f) fclose(f);
                    124:     if(s) fclose(s);
                    125:
                    126:     return 0;
                    127: }

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