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

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

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