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

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

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