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

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

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