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

1.15      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: 
1.19      ths        16: #define randint(N) ((int)(rand() / (((double)RAND_MAX + 1) / (N))))
1.15      ths        17: #define assert(p) if(!(p)) { printf("Assertion '%s' FAILED!\n", #p); exit(1); }
1.20    ! ths        18: #define REDUNDANCY (4*(100/percent))
        !            19: 
        !            20: int count_char(char*,char);
1.15      ths        21: 
                     22: typedef struct flow {
                     23:     int duration;
                     24:     int weight;
                     25:     char source[256];
                     26:     struct flow *next, *last;
                     27: } flow_t;
                     28: 
                     29: int num, peers, limit, maxload;
                     30: swaplinks_p swp;
1.18      ths        31: FILE *f = NULL, *s = NULL, *g = NULL;
1.15      ths        32: 
                     33: void *recvthread(void *arg) {
                     34:     swaplinks_p swp = (swaplinks_p)arg;
                     35:     struct sockaddr_ns peer;
                     36:     socklen_t len = sizeof(peer);
                     37:     flow_t *flows = NULL, *newflow = NULL, *ptr = NULL, *axe = NULL;
                     38:     char msg[64], tmp[128], buf[2048];
1.18      ths        39:     int time = 0, load = 0, exts, delta, expiration;
1.15      ths        40: 
                     41:     buf[0] = 0;
                     42: 
                     43:     while(time < limit) {
                     44:      if(swaplinks_recvfrom(swp, tmp, sizeof(tmp), 0, &peer, &len) > 0) {
                     45:     time++; delta = 0;
1.18      ths        46:     // parse new flow
1.15      ths        47:     newflow = (flow_t*)calloc(1,sizeof(flow_t));
                     48:     newflow->duration = atoi(strtok(tmp,";"));
                     49:     newflow->weight = atoi(strtok(NULL,";"));
                     50:     exts = atoi(strtok(NULL,";"));
1.18      ths        51:     strncpy(newflow->source,peer.user+4,sizeof(newflow->source));
1.19      ths        52:     if(s) fprintf(s,"      %d recvd request #%d from %s - w:%d d:%d\n",
                     53:          num,time,newflow->source,newflow->weight,newflow->duration);
1.18      ths        54:     snprintf(buf,2048,"->%d:%d:%s<->",newflow->duration,newflow->weight,newflow->source);
                     55:     // run through flows, decrementing duration, cleaning any expired flows
                     56:     for(ptr = flows; ptr;) {
                     57:    sprintf(buf,"%s%d:%d<->",buf,ptr->duration,ptr->weight);
                     58:    expiration = 0; axe = NULL;
                     59:    if(--ptr->duration == 0) {
                     60:   axe = ptr;
                     61:   load -= ptr->weight;
                     62:   delta -= ptr->weight;
                     63:   if(ptr->last && ptr->next) {
                     64:  assert(flows != ptr);
                     65:  ptr->last->next = ptr->next;
                     66:  ptr->next->last = ptr->last;
                     67:   }
                     68:   else if(ptr->next) {
                     69:  assert(flows == ptr);
                     70:  ptr->next->last = NULL;
                     71:  flows = ptr->next;
                     72:   }
                     73:   else if(ptr->last) {
                     74:  assert(flows != ptr);
                     75:  ptr->last->next = NULL;
                     76:   }
                     77:   else {
                     78:  assert(flows == ptr);
                     79:  flows = NULL;
                     80:   }
1.15      ths        81:      if(s) fprintf(s,"%d EXPIRED FLOW from %s:d%d/w%d\n",num,ptr->source,ptr->duration,ptr->weight);
1.18      ths        82:   expiration = 1;
1.15      ths        83:    }
1.18      ths        84:    ptr = ptr->next;
                     85:    if(expiration) { free(axe); axe = NULL; }
                     86:     }
                     87:     sprintf(buf,"%s||\n",buf);
1.15      ths        88: 
                     89:      //          if(s) fprintf(s,"%d ACCEPTED FLOW from %s:d%d/w%d\n",num,newflow->source,newflow->duration,newflow->weight);
1.18      ths        90:     // if this flow will not put us overbudget, accept
                     91:     if(load+newflow->weight <= maxload) {
1.15      ths        92:    load += newflow->weight;
                     93:    delta += newflow->weight;
                     94:    newflow->next = flows;
1.18      ths        95:    if(flows) flows->last = newflow;
1.15      ths        96:    flows = newflow;
1.18      ths        97:    if(s) fprintf(s,"        %d accepted flow: %d\n",num,load);
1.15      ths        98:     }
                     99:     // ADMISSION CONTROL
                    100:     // if we're overloaded, drop it if its been pushed too far,
1.18      ths       101:     // otherwise pass on to a neighbor and mark it
1.15      ths       102:     else if(exts < 5) {
1.18      ths       103:    fprintf(s,"        %d rejected flow, load too high: %d <= %d\n",num,maxload,load+newflow->weight);
1.15      ths       104:    swaplinks_update_walk_length(swp,1);
                    105:    snprintf(msg,64,"%d;%d;%d;",newflow->duration,newflow->weight,exts+1);
                    106:    swaplinks_sendtoany(swp,msg,strlen(msg)+1,0);
                    107:    swaplinks_update_walk_length(swp,peers);
                    108:     }
1.19      ths       109:     else
                    110:    fprintf(s,"        %d retired unserviceable request\n",num);
                    111: 
1.18      ths       112:     if(f) fprintf(f,"%d\n",load);
                    113:   if(s) fprintf(s,"%s",buf);
1.15      ths       114:      }
1.18      ths       115:       }
                    116:       if(s) fprintf(s,"%d DONE RECEIVING\n",num);
1.15      ths       117: 
1.18      ths       118:       // clean up
                    119:       if(f && f != stderr) { fclose(f); f = NULL; }
                    120:       if(s && s != stdout) { fclose(s); s = NULL; }
                    121:       for(ptr = flows;ptr;) {axe = ptr; ptr = ptr->next; free(axe);}
1.16      ths       122: 
1.18      ths       123:       pthread_exit(NULL);
1.15      ths       124: }
                    125: 
                    126: int main(int argc, char **argv) {
                    127:     struct sockaddr_ns reg;
1.18      ths       128:     char tmp[256], nbrs[4096];
1.20    ! ths       129:     int time = 0, duration, weight, percent, sendcount = 0;
1.15      ths       130:     pthread_t tid;
                    131: 
                    132:     if (argc > 6) {
                    133:         num = atoi(argv[1]);
                    134:         peers = atoi(argv[2]);
                    135:         limit = atoi(argv[3]);
1.20    ! ths       136:       percent = atoi(argv[4]);
        !           137:         duration = atoi(argv[5]);
        !           138:         weight = atoi(argv[6]);
        !           139:         maxload = atoi(argv[7]);
1.15      ths       140:     }
                    141:     else {
1.20    ! ths       142:         printf("Usage:\n  slinks_variable_load node_number peers_number time_limit percent_to_send flow_duration flow_weight max_load\n");
1.15      ths       143:         exit(1);
                    144:     }
                    145: 
                    146:     snprintf(tmp, sizeof(tmp), "test%d", num);
                    147:     nutss_config_set(NUTSS_CONFIG_USERNAME, tmp, strlen(tmp));
                    148:     nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, tmp, strlen(tmp));
                    149:     nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, tmp, strlen(tmp));
                    150:     strncpy(tmp, "nutss.net", sizeof(tmp));
                    151:     nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlen(tmp));
                    152:     strncpy(tmp, "sip.nutss.net:5060", sizeof(tmp));
                    153:     nutss_config_set(NUTSS_CONFIG_SIGPROXY, tmp, strlen(tmp));
                    154: 
                    155:     sprintf(tmp, "output/data_node%d.txt", num); f = fopen(tmp,"w");
                    156: //    sprintf(tmp, "output/output_node%d.txt", num); s = fopen(tmp,"w");
1.18      ths       157:     sprintf(tmp, "output/nbrs_node%d.txt", num); g = fopen(tmp,"w");
1.15      ths       158:     if (f == NULL) f = stderr;
                    159:     if (s == NULL) s = stdout;
                    160: 
                    161:     memset(&reg, 0, sizeof(reg));
                    162:     reg.family = AF_NUTSS;
                    163:     strncpy(reg.user, "ths1", sizeof(reg.user));
                    164:     strncpy(reg.domain, "nutss.net", sizeof(reg.domain));
                    165:     strncpy(reg.service, "swaplinksd", sizeof(reg.service));
                    166: 
                    167:     swaplinks_init();
1.19      ths       168: 
1.20    ! ths       169: //    sleep(randint(5));
1.19      ths       170: 
1.15      ths       171:     swp = swaplinks_new("cloud9", &reg, peers, peers);
                    172: 
                    173:     // let swaplinks get going.
                    174:     sleep(5);
                    175: 
                    176:     pthread_create(&tid, NULL, recvthread, swp);
                    177: 
1.20    ! ths       178: //    sleep(2);
1.15      ths       179: 
                    180:     snprintf(tmp, sizeof(tmp), "%d;%d;0;", duration, weight);
                    181: 
1.20    ! ths       182:     while (time++ < REDUNDANCY*limit) { // (i-- != 0) {
        !           183:         if(randint(100) < percent) swaplinks_sendtoany(swp, tmp, strlen(tmp)+1, 0);
        !           184: //      if(s) fprintf(s,"   %d sent request %d\n",num,time);
1.18      ths       185:       swaplinks_get_neighbors(swp, nbrs, 4096);
1.20    ! ths       186:       if(g) fprintf(g,"%d\n",count_char(nbrs,','));
        !           187:         nbrs[0] = 0;
        !           188:       sleep(1);
1.15      ths       189:     }
                    190: 
1.20    ! ths       191:     if(g) fclose(g);
        !           192:     if(s) fprintf(s,"%d DONE SENDING\n",num);
        !           193:     pthread_join(tid, NULL);
1.15      ths       194:     if(s) fprintf(s,"%d SHUTTING DOWN\n",num);
                    195: 
1.20    ! ths       196: //    if(num == 1) system("echo \" \" | mail -s\"node one finished sending!\" ths22@cs.cornell.edu");
1.15      ths       197: 
                    198:     return 0;
1.20    ! ths       199: }
        !           200: 
        !           201: int count_char(char *s, char h) {
        !           202:     int i, l = strlen(s), c = 0;
        !           203:     for(i = 0; i < l; i++)
        !           204:         if(s[i] == h) c++;
        !           205:     return c;
1.15      ths       206: }

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