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.8

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);
1.7       ths        31:     flow_t *flows = NULL, *newflow = NULL, *ptr = NULL;
                     32:     char tmp[128], buf[2048];
1.4       ths        33:     int time = 0, load = 0, delta;
1.1       ths        34:
1.7       ths        35:     buf[0] = 0;
                     36:
1.1       ths        37:     while(time < limit) {
                     38:         if(swaplinks_recvfrom(swp, tmp, sizeof(tmp), 0, &peer, &len) > 0) {
1.4       ths        39:             time++; delta = 0;
1.8     ! ths        40:           if(s) fprintf(s,"\t\tTIME at node %d: %d\n",num,time);
1.2       ths        41:             newflow = (flow_t*)calloc(1,sizeof(flow_t));
1.1       ths        42:             newflow->duration = atoi(strtok(tmp,";"));
                     43:             newflow->weight = atoi(strtok(NULL,";"));
1.5       ths        44:           strncpy(newflow->source,peer.user+4,sizeof(newflow->source));
1.8     ! ths        45:             snprintf(buf,2048,"->%d:%d<->",newflow->duration,newflow->weight);
1.7       ths        46:           for(ptr = flows; ptr; ptr = ptr->next) {
                     47:      sprintf(buf,"%s%d:%d<->",buf,ptr->duration,ptr->weight);
1.1       ths        48:                 if(--ptr->duration == 0) {
                     49:                     load -= ptr->weight;
1.4       ths        50:                     delta -= ptr->weight;
1.8     ! ths        51:          if(ptr->last)
1.7       ths        52:     ptr->last->next = ptr->next;
                     53:                     else {
                     54:     flows = ptr->next;
                     55:     flows->last = NULL;
                     56:          }
1.8     ! ths        57:          if(ptr->next)
        !            58:     ptr->next->last = ptr->last;
        !            59:
1.5       ths        60:          if(s) fprintf(s,"%d EXPIRED FLOW from %s:d%d/w%d\n",num,ptr->source,ptr->duration,ptr->weight);
1.1       ths        61:          free(ptr);
                     62:                 }
                     63:             }
1.7       ths        64:           sprintf(buf,"%s||\n",buf);
                     65:
                     66:             if(s) fprintf(s,"%d ACCEPTED FLOW from %s: d%d/w%d\n",num,newflow->source,newflow->duration,newflow->weight);
                     67:           if(s) fprintf(s,"%s",buf);
                     68:
1.2       ths        69:             load += newflow->weight;
1.4       ths        70:           delta += newflow->weight;
1.1       ths        71:           newflow->next = flows;
1.7       ths        72:           if(newflow->next) newflow->next->last = newflow;
1.1       ths        73:           flows = newflow;
1.4       ths        74:             if(f) fprintf(f,"%d\t%d\t%d\n",time,load,delta);
1.1       ths        75:         }
                     76:     }
1.5       ths        77:     if(s) fprintf(s,"%d DONE RECEIVING\n",num);
1.8     ! ths        78:
        !            79:     pthread_exit(NULL);
1.1       ths        80: }
                     81:
                     82: int main(int argc, char **argv) {
                     83:     struct sockaddr_ns reg;
                     84:     swaplinks_p swp;
                     85:     char tmp[256];
                     86:     int time = 0, peers, duration, weight, sendcount = 0;
                     87:     pthread_t tid;
                     88:
                     89:     if (argc > 5) {
                     90:         num = atoi(argv[1]);
                     91:         peers = atoi(argv[2]);
                     92:         limit = atoi(argv[3]);
                     93:         duration = atoi(argv[4]);
                     94:         weight = atoi(argv[5]);
                     95:     }
                     96:     else {
                     97:         printf("Usage:\n  slinks_variable_load node_number peers_number time_limit flow_duration flow_weight\n");
                     98:         exit(1);
                     99:     }
                    100:
                    101:     snprintf(tmp, sizeof(tmp), "test%d", num);
                    102:     nutss_config_set(NUTSS_CONFIG_USERNAME, tmp, strlen(tmp));
                    103:     nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, tmp, strlen(tmp));
                    104:     nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, tmp, strlen(tmp));
                    105:     strncpy(tmp, "nutss.net", sizeof(tmp));
                    106:     nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlen(tmp));
                    107:     strncpy(tmp, "sip.nutss.net:5060", sizeof(tmp));
                    108:     nutss_config_set(NUTSS_CONFIG_SIGPROXY, tmp, strlen(tmp));
                    109:
                    110:     sprintf(tmp, "output/data_node%d.txt", num); f = fopen(tmp,"w");
1.7       ths       111: //    sprintf(tmp, "output/output_node%d.txt", num); s = fopen(tmp,"w");
1.1       ths       112:     if (f == NULL) f = stderr;
                    113:     if (s == NULL) s = stdout;
                    114:
                    115:     memset(&reg, 0, sizeof(reg));
                    116:     reg.family = AF_NUTSS;
                    117:     strncpy(reg.user, "ths1", sizeof(reg.user));
                    118:     strncpy(reg.domain, "nutss.net", sizeof(reg.domain));
                    119:     strncpy(reg.service, "swaplinksd", sizeof(reg.service));
                    120:
                    121:     swaplinks_init();
                    122:     swp = swaplinks_new("cloud9", &reg, peers, peers);
                    123:
                    124:     // let swaplinks get going.
                    125:     sleep(10);
                    126:
                    127:     pthread_create(&tid, NULL, recvthread, swp);
                    128:
                    129:     sleep(1);
                    130:
1.5       ths       131:     snprintf(tmp, sizeof(tmp), "%d;%d;", duration, weight);
                    132:
1.8     ! ths       133:     while (time++ < limit) { // (i-- != 0) {
1.5       ths       134:         swaplinks_sendtoany(swp, tmp, strlen(tmp)+1, 0);
1.1       ths       135:         sleep(1);
                    136:     }
1.5       ths       137:
                    138:     if(s) fprintf(s,"%d DONE SENDING\n",num);
1.1       ths       139:
                    140:     pthread_join(tid,NULL);
                    141:
                    142:     if(f) fclose(f);
                    143:     if(s) fclose(s);
1.6       ths       144:
1.8     ! ths       145: //    if(num == 1) system("echo \" \" | mail -s\"experiment done!\" ths22@cs.cornell.edu");
1.6       ths       146:
1.1       ths       147:     return 0;
                    148: }

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