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

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

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