Log In
New Account
  
 
Home My Page Project Tree Code Snippets Project Openings NUTSS
 
 
Summary Tracker Lists CVS Files
 

CVS | Administration

Diff for /libnutss/examples/slinks_variable_load.c between versions 1.14 and 1.15

version 1.14, 2007/06/30 21:57:25 version 1.15, 2007/06/30 22:37:20
Line 1 Line 1
#include <stdio.h>#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>#include <string.h>
#ifndef WIN32
#include <string.h>#include <poll.h>
#include <sys/socket.h>
#ifndef WIN32#endif
#include <pthread.h>
#include <poll.h>#include "../include/swaplinks.h"
#include <sys/socket.h>#ifdef WIN32
#define sleep(sec) Sleep((sec)*1000)
#endif#define snprintf   _snprintf
#endif
#include <pthread.h>
#define assert(p) if(!(p)) { printf("Assertion '%s' FAILED!\n", #p); exit(1); }
#include "../include/swaplinks.h"
typedef struct flow {
   int duration;
   int weight;
#ifdef WIN32    char source[256];
   struct flow *next, *last;
#define sleep(sec) Sleep((sec)*1000)} flow_t;
#define snprintf   _snprintfint num, peers, limit, maxload;
swaplinks_p swp;
#endifFILE *f = NULL, *s = NULL;
void *recvthread(void *arg) {
   swaplinks_p swp = (swaplinks_p)arg;
#define assert(p) if(!(p)) { printf("Assertion '%s' FAILED!\n", #p); exit(1); }    struct sockaddr_ns peer;
   socklen_t len = sizeof(peer);
   flow_t *flows = NULL, *newflow = NULL, *ptr = NULL, *axe = NULL;
   char msg[64], tmp[128], buf[2048];
typedef struct flow {    int time = 0, load = 0, busy = 0, exts, delta, expiration;
   int duration;    buf[0] = 0;
   int weight;    while(time < limit) {
      sleep(1);
   char source[256];       if(swaplinks_recvfrom(swp, tmp, sizeof(tmp), 0, &peer, &len) > 0) {
     time++; delta = 0;
   struct flow *next, *last;      if(s) fprintf(s,"      %d recvd request %d... load: %d\n",num,time, load);
     newflow = (flow_t*)calloc(1,sizeof(flow_t));
} flow_t;      newflow->duration = atoi(strtok(tmp,";"));
     newflow->weight = atoi(strtok(NULL,";"));
     exts = atoi(strtok(NULL,";"));
     if(load+newflow->weight <= maxload) {
int num, peers, limit, maxload;     strncpy(newflow->source,peer.user+4,sizeof(newflow->source));
    snprintf(buf,2048,"->%d:%d<->",newflow->duration,newflow->weight);
swaplinks_p swp;     for(ptr = flows; ptr;) {
   sprintf(buf,"%s%d:%d<->",buf,ptr->duration,ptr->weight);
FILE *f = NULL, *s = NULL;    expiration = 0; axe = NULL;
   if(--ptr->duration == 0) {
  axe = ptr;
  load -= ptr->weight;
void *recvthread(void *arg) {   delta -= ptr->weight;
   swaplinks_p swp = (swaplinks_p)arg;   if(ptr->last && ptr->next) {
 assert(flows != ptr);
   struct sockaddr_ns peer;  ptr->last->next = ptr->next;
 ptr->next->last = ptr->last;
   socklen_t len = sizeof(peer);   }
  else if(ptr->next) {
   flow_t *flows = NULL, *newflow = NULL, *ptr = NULL, *axe = NULL;  assert(flows == ptr);
 flows = ptr->next;
   char msg[64], tmp[128], buf[2048];   }
   int time = 0, load = 0, busy = 0, exts, delta, expiration;   if(flows) flows->last = NULL;
  else if(ptr->last) {
 assert(flows != ptr);
 ptr->last->next = NULL;
   buf[0] = 0;   }
  else {
 assert(flows == ptr);
 flows = NULL;
   while(time < limit) {   }
      sleep(1);       if(s) fprintf(s,"%d EXPIRED FLOW from %s:d%d/w%d\n",num,ptr->source,ptr->duration,ptr->weight);
  expiration = 1;
      if(swaplinks_recvfrom(swp, tmp, sizeof(tmp), 0, &peer, &len) > 0) {    }
   ptr = ptr->next;
     time++; delta = 0;    if(expiration) free(axe);
    }
     if(s) fprintf(s,"      %d recvd request %d\n",num,time);     sprintf(buf,"%s||\n",buf);
     newflow = (flow_t*)calloc(1,sizeof(flow_t));       //          if(s) fprintf(s,"%d ACCEPTED FLOW from %s:d%d/w%d\n",num,newflow->source,newflow->duration,newflow->weight);
       if(s) fprintf(s,"%s",buf);
     newflow->duration = atoi(strtok(tmp,";"));
    load += newflow->weight;
     newflow->weight = atoi(strtok(NULL,";"));     delta += newflow->weight;
    newflow->next = flows;
     exts = atoi(strtok(NULL,";"));     if(newflow->next) newflow->next->last = newflow;
    flows = newflow;
     if(load< maxload) {     if(f) fprintf(f,"%d\n",load);
     }
    strncpy(newflow->source,peer.user+4,sizeof(newflow->source));      // ADMISSION CONTROL
     // if we're overloaded, drop it if its been pushed too far,
    snprintf(buf,2048,"->%d:%d<->",newflow->duration,newflow->weight);      // or pass on to a neighbor and mark it as such if not
     else if(exts < 5) {
    for(ptr = flows; ptr;) {     swaplinks_update_walk_length(swp,1);
    snprintf(msg,64,"%d;%d;%d;",newflow->duration,newflow->weight,exts+1);
   sprintf(buf,"%s%d:%d<->",buf,ptr->duration,ptr->weight);     swaplinks_sendtoany(swp,msg,strlen(msg)+1,0);
    swaplinks_update_walk_length(swp,peers);
   expiration = 0; axe = NULL;      }
      }
   if(--ptr->duration == 0) {    }
   if(s) fprintf(s,"%d DONE RECEIVING\n",num);
  axe = ptr;
   if(f && f != stderr) { fclose(f); f = NULL; }
  load -= ptr->weight;    if(s && s != stdout) { fclose(s); s = NULL; }
  delta -= ptr->weight;    pthread_exit(NULL);
}
int main(int argc, char **argv) {
  if(ptr->last && ptr->next) {    struct sockaddr_ns reg;
   char tmp[256];
 assert(flows != ptr);    int time = 0, duration, weight, sendcount = 0;
   pthread_t tid;
 ptr->last->next = ptr->next;
   if (argc > 6) {
 ptr->next->last = ptr->last;        num = atoi(argv[1]);
       peers = atoi(argv[2]);
  }        limit = atoi(argv[3]);
       duration = atoi(argv[4]);
  else if(ptr->next) {        weight = atoi(argv[5]);
      maxload = atoi(argv[6]);
 assert(flows == ptr);    }
   else {
 flows = ptr->next;        printf("Usage:\n  slinks_variable_load node_number peers_number time_limit flow_duration flow_weight max_load\n");
       exit(1);
  }    }
   snprintf(tmp, sizeof(tmp), "test%d", num);
   nutss_config_set(NUTSS_CONFIG_USERNAME, tmp, strlen(tmp));
  if(flows) flows->last = NULL;    nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, tmp, strlen(tmp));
   nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, tmp, strlen(tmp));
  else if(ptr->last) {    strncpy(tmp, "nutss.net", sizeof(tmp));
   nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlen(tmp));
 assert(flows != ptr);    strncpy(tmp, "sip.nutss.net:5060", sizeof(tmp));
   nutss_config_set(NUTSS_CONFIG_SIGPROXY, tmp, strlen(tmp));
 ptr->last->next = NULL;
   sprintf(tmp, "output/data_node%d.txt", num); f = fopen(tmp,"w");
  }//    sprintf(tmp, "output/output_node%d.txt", num); s = fopen(tmp,"w");
   if (f == NULL) f = stderr;
  else {    if (s == NULL) s = stdout;
 assert(flows == ptr);    memset(&reg, 0, sizeof(reg));
   reg.family = AF_NUTSS;
 flows = NULL;    strncpy(reg.user, "ths1", sizeof(reg.user));
   strncpy(reg.domain, "nutss.net", sizeof(reg.domain));
  }    strncpy(reg.service, "swaplinksd", sizeof(reg.service));
   swaplinks_init();
   swp = swaplinks_new("cloud9", &reg, peers, peers);
      if(s) fprintf(s,"%d EXPIRED FLOW from %s:d%d/w%d\n",num,ptr->source,ptr->duration,ptr->weight);
   // let swaplinks get going.
  expiration = 1;    sleep(5);
   }    pthread_create(&tid, NULL, recvthread, swp);
   ptr = ptr->next;    sleep(1);
   if(expiration) free(axe);    snprintf(tmp, sizeof(tmp), "%d;%d;0;", duration, weight);
    }    while (time++ < 4*limit) { // (i-- != 0) {
       swaplinks_sendtoany(swp, tmp, strlen(tmp)+1, 0);
    sprintf(buf,"%s||\n",buf);        if(s) fprintf(s,"   %d sent request %d\n",num,time);
       sleep(2);
   }
      //          if(s) fprintf(s,"%d ACCEPTED FLOW from %s:d%d/w%d\n",num,newflow->source,newflow->duration,newflow->weight);        if(s) fprintf(s,"%d DONE SENDING\n",num);
       if(s) fprintf(s,"%s",buf);        pthread_join(tid, NULL);
   if(s) fprintf(s,"%d SHUTTING DOWN\n",num);
    load += newflow->weight;    if(num == 1) system("echo \" \" | mail -s\"node one finished sending!\" ths22@cs.cornell.edu");
    delta += newflow->weight;    return 0;
}
    newflow->next = flows; 
 
    if(newflow->next) newflow->next->last = newflow; 
 
    flows = newflow; 
 
    if(f) fprintf(f,"%d\n",load); 
 
     } 
 
     // ADMISSION CONTROL 
 
     // if we're overloaded, drop it if its been pushed too far, 
 
     // or pass on to a neighbor and mark it as such if not 
 
     else if(exts < 5) { 
 
    swaplinks_update_walk_length(swp,1); 
 
    snprintf(msg,64,"%d;%d;%d;",newflow->duration,newflow->weight,exts+1); 
 
    swaplinks_sendtoany(swp,msg,strlen(msg)+1,0); 
 
    swaplinks_update_walk_length(swp,peers); 
 
     } 
 
      } 
 
   } 
 
   if(s) fprintf(s,"%d DONE RECEIVING\n",num); 
 
 
 
   if(f && f != stderr) { fclose(f); f = NULL; } 
 
   if(s && s != stdout) { fclose(s); s = NULL; } 
 
 
 
   pthread_exit(NULL); 
 
} 
 
 
 
int main(int argc, char **argv) { 
 
   struct sockaddr_ns reg; 
 
   char tmp[256]; 
 
   int time = 0, duration, weight, sendcount = 0; 
 
   pthread_t tid; 
 
 
 
   if (argc > 6) { 
 
       num = atoi(argv[1]); 
 
       peers = atoi(argv[2]); 
 
       limit = atoi(argv[3]); 
 
       duration = atoi(argv[4]); 
 
       weight = atoi(argv[5]); 
 
      maxload = atoi(argv[6]); 
 
   } 
 
   else { 
 
       printf("Usage:\n  slinks_variable_load node_number peers_number time_limit flow_duration flow_weight max_load\n"); 
 
       exit(1); 
 
   } 
 
 
 
   snprintf(tmp, sizeof(tmp), "test%d", num); 
 
   nutss_config_set(NUTSS_CONFIG_USERNAME, tmp, strlen(tmp)); 
 
   nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, tmp, strlen(tmp)); 
 
   nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, tmp, strlen(tmp)); 
 
   strncpy(tmp, "nutss.net", sizeof(tmp)); 
 
   nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlen(tmp)); 
 
   strncpy(tmp, "sip.nutss.net:5060", sizeof(tmp)); 
 
   nutss_config_set(NUTSS_CONFIG_SIGPROXY, tmp, strlen(tmp)); 
 
 
 
   sprintf(tmp, "output/data_node%d.txt", num); f = fopen(tmp,"w"); 
 
//    sprintf(tmp, "output/output_node%d.txt", num); s = fopen(tmp,"w"); 
 
   if (f == NULL) f = stderr; 
 
   if (s == NULL) s = stdout; 
 
 
 
   memset(&reg, 0, sizeof(reg)); 
 
   reg.family = AF_NUTSS; 
 
   strncpy(reg.user, "ths1", sizeof(reg.user)); 
 
   strncpy(reg.domain, "nutss.net", sizeof(reg.domain)); 
 
   strncpy(reg.service, "swaplinksd", sizeof(reg.service)); 
 
 
 
   swaplinks_init(); 
 
   swp = swaplinks_new("cloud9", &reg, peers, peers); 
 
 
 
   // let swaplinks get going. 
 
   sleep(10); 
 
 
 
   pthread_create(&tid, NULL, recvthread, swp); 
 
 
 
   sleep(1); 
 
 
 
   snprintf(tmp, sizeof(tmp), "%d;%d;0;", duration, weight); 
 
 
 
   while (time++ < 4*limit) { // (i-- != 0) { 
 
       swaplinks_sendtoany(swp, tmp, strlen(tmp)+1, 0); 
 
       if(s) fprintf(s,"   %d sent request %d\n",num,time); 
 
       sleep(1); 
 
   } 
 
 
 
       if(s) fprintf(s,"%d DONE SENDING\n",num); 
 
 
 
       pthread_join(tid, NULL); 
 
 
 
   if(s) fprintf(s,"%d SHUTTING DOWN\n",num); 
 
 
 
   if(num == 1) system("echo \" \" | mail -s\"node one finished sending!\" ths22@cs.cornell.edu"); 
 
 
 
   return 0; 
 
} 
 

Removed from v.1.14  
changed lines
  Added in v.1.15


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