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

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

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