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

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

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