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

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

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