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

1.14    ! ths         1: #include <stdio.h>
        !             2:
        !             3: #include <stdlib.h>
        !             4:
        !             5: #include <string.h>
        !             6:
        !             7: #ifndef WIN32
        !             8:
        !             9: #include <poll.h>
        !            10:
        !            11: #include <sys/socket.h>
        !            12:
        !            13: #endif
        !            14:
        !            15: #include <pthread.h>
        !            16:
        !            17: #include "../include/swaplinks.h"
        !            18:
        !            19:
        !            20:
        !            21: #ifdef WIN32
        !            22:
        !            23: #define sleep(sec) Sleep((sec)*1000)
        !            24:
        !            25: #define snprintf   _snprintf
        !            26:
        !            27: #endif
        !            28:
        !            29:
        !            30:
        !            31: #define assert(p) if(!(p)) { printf("Assertion '%s' FAILED!\n", #p); exit(1); }
        !            32:
        !            33:
        !            34:
        !            35: typedef struct flow {
        !            36:
        !            37:     int duration;
        !            38:
        !            39:     int weight;
        !            40:
        !            41:     char source[256];
        !            42:
        !            43:     struct flow *next, *last;
        !            44:
        !            45: } flow_t;
        !            46:
        !            47:
        !            48:
        !            49: int num, peers, limit, maxload;
        !            50:
        !            51: swaplinks_p swp;
        !            52:
        !            53: FILE *f = NULL, *s = NULL;
        !            54:
        !            55:
        !            56:
        !            57: void *recvthread(void *arg) {
        !            58:
        !            59:     swaplinks_p swp = (swaplinks_p)arg;
        !            60:
        !            61:     struct sockaddr_ns peer;
        !            62:
        !            63:     socklen_t len = sizeof(peer);
        !            64:
        !            65:     flow_t *flows = NULL, *newflow = NULL, *ptr = NULL, *axe = NULL;
        !            66:
        !            67:     char msg[64], tmp[128], buf[2048];
        !            68:
        !            69:     int time = 0, load = 0, busy = 0, exts, delta, expiration;
        !            70:
        !            71:
        !            72:
        !            73:     buf[0] = 0;
        !            74:
        !            75:
        !            76:
        !            77:     while(time < limit) {
        !            78:
        !            79:      sleep(1);
        !            80:
        !            81:      if(swaplinks_recvfrom(swp, tmp, sizeof(tmp), 0, &peer, &len) > 0) {
        !            82:
        !            83:     time++; delta = 0;
        !            84:
        !            85:     if(s) fprintf(s,"      %d recvd request %d\n",num,time);
        !            86:
        !            87:     newflow = (flow_t*)calloc(1,sizeof(flow_t));
        !            88:
        !            89:     newflow->duration = atoi(strtok(tmp,";"));
        !            90:
        !            91:     newflow->weight = atoi(strtok(NULL,";"));
        !            92:
        !            93:     exts = atoi(strtok(NULL,";"));
        !            94:
        !            95:     if(load < maxload) {
        !            96:
        !            97:    strncpy(newflow->source,peer.user+4,sizeof(newflow->source));
        !            98:
        !            99:    snprintf(buf,2048,"->%d:%d<->",newflow->duration,newflow->weight);
        !           100:
        !           101:    for(ptr = flows; ptr;) {
        !           102:
        !           103:   sprintf(buf,"%s%d:%d<->",buf,ptr->duration,ptr->weight);
        !           104:
        !           105:   expiration = 0; axe = NULL;
        !           106:
        !           107:   if(--ptr->duration == 0) {
        !           108:
        !           109:  axe = ptr;
        !           110:
        !           111:  load -= ptr->weight;
        !           112:
        !           113:  delta -= ptr->weight;
        !           114:
        !           115:
        !           116:
        !           117:  if(ptr->last && ptr->next) {
        !           118:
        !           119: assert(flows != ptr);
        !           120:
        !           121: ptr->last->next = ptr->next;
        !           122:
        !           123: ptr->next->last = ptr->last;
        !           124:
        !           125:  }
        !           126:
        !           127:  else if(ptr->next) {
        !           128:
        !           129: assert(flows == ptr);
        !           130:
        !           131: flows = ptr->next;
        !           132:
        !           133:  }
        !           134:
        !           135:
        !           136:
        !           137:  if(flows) flows->last = NULL;
        !           138:
        !           139:  else if(ptr->last) {
        !           140:
        !           141: assert(flows != ptr);
        !           142:
        !           143: ptr->last->next = NULL;
        !           144:
        !           145:  }
        !           146:
        !           147:  else {
        !           148:
        !           149: assert(flows == ptr);
        !           150:
        !           151: flows = NULL;
        !           152:
        !           153:  }
        !           154:
        !           155:
        !           156:
        !           157:      if(s) fprintf(s,"%d EXPIRED FLOW from %s:d%d/w%d\n",num,ptr->source,ptr->duration,ptr->weight);
        !           158:
        !           159:  expiration = 1;
        !           160:
        !           161:   }
        !           162:
        !           163:   ptr = ptr->next;
        !           164:
        !           165:   if(expiration) free(axe);
        !           166:
        !           167:    }
        !           168:
        !           169:    sprintf(buf,"%s||\n",buf);
        !           170:
        !           171:
        !           172:
        !           173:      //          if(s) fprintf(s,"%d ACCEPTED FLOW from %s:d%d/w%d\n",num,newflow->source,newflow->duration,newflow->weight);
        !           174:
        !           175:       if(s) fprintf(s,"%s",buf);
        !           176:
        !           177:
        !           178:
        !           179:    load += newflow->weight;
        !           180:
        !           181:    delta += newflow->weight;
        !           182:
        !           183:    newflow->next = flows;
        !           184:
        !           185:    if(newflow->next) newflow->next->last = newflow;
        !           186:
        !           187:    flows = newflow;
        !           188:
        !           189:    if(f) fprintf(f,"%d\n",load);
        !           190:
        !           191:     }
        !           192:
        !           193:     // ADMISSION CONTROL
        !           194:
        !           195:     // if we're overloaded, drop it if its been pushed too far,
        !           196:
        !           197:     // or pass on to a neighbor and mark it as such if not
        !           198:
        !           199:     else if(exts < 5) {
        !           200:
        !           201:    swaplinks_update_walk_length(swp,1);
        !           202:
        !           203:    snprintf(msg,64,"%d;%d;%d;",newflow->duration,newflow->weight,exts+1);
        !           204:
        !           205:    swaplinks_sendtoany(swp,msg,strlen(msg)+1,0);
        !           206:
        !           207:    swaplinks_update_walk_length(swp,peers);
        !           208:
        !           209:     }
        !           210:
        !           211:      }
        !           212:
        !           213:     }
        !           214:
        !           215:     if(s) fprintf(s,"%d DONE RECEIVING\n",num);
        !           216:
        !           217:
        !           218:
        !           219:     if(f && f != stderr) { fclose(f); f = NULL; }
        !           220:
        !           221:     if(s && s != stdout) { fclose(s); s = NULL; }
        !           222:
        !           223:
        !           224:
        !           225:     pthread_exit(NULL);
        !           226:
        !           227: }
        !           228:
        !           229:
        !           230:
        !           231: int main(int argc, char **argv) {
        !           232:
        !           233:     struct sockaddr_ns reg;
        !           234:
        !           235:     char tmp[256];
        !           236:
        !           237:     int time = 0, duration, weight, sendcount = 0;
        !           238:
        !           239:     pthread_t tid;
        !           240:
        !           241:
        !           242:
        !           243:     if (argc > 6) {
        !           244:
        !           245:         num = atoi(argv[1]);
        !           246:
        !           247:         peers = atoi(argv[2]);
        !           248:
        !           249:         limit = atoi(argv[3]);
        !           250:
        !           251:         duration = atoi(argv[4]);
        !           252:
        !           253:         weight = atoi(argv[5]);
        !           254:
        !           255:      maxload = atoi(argv[6]);
        !           256:
        !           257:     }
        !           258:
        !           259:     else {
        !           260:
        !           261:         printf("Usage:\n  slinks_variable_load node_number peers_number time_limit flow_duration flow_weight max_load\n");
        !           262:
        !           263:         exit(1);
        !           264:
        !           265:     }
        !           266:
        !           267:
        !           268:
        !           269:     snprintf(tmp, sizeof(tmp), "test%d", num);
        !           270:
        !           271:     nutss_config_set(NUTSS_CONFIG_USERNAME, tmp, strlen(tmp));
        !           272:
        !           273:     nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, tmp, strlen(tmp));
        !           274:
        !           275:     nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, tmp, strlen(tmp));
        !           276:
        !           277:     strncpy(tmp, "nutss.net", sizeof(tmp));
        !           278:
        !           279:     nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlen(tmp));
        !           280:
        !           281:     strncpy(tmp, "sip.nutss.net:5060", sizeof(tmp));
        !           282:
        !           283:     nutss_config_set(NUTSS_CONFIG_SIGPROXY, tmp, strlen(tmp));
        !           284:
        !           285:
        !           286:
        !           287:     sprintf(tmp, "output/data_node%d.txt", num); f = fopen(tmp,"w");
        !           288:
        !           289: //    sprintf(tmp, "output/output_node%d.txt", num); s = fopen(tmp,"w");
        !           290:
        !           291:     if (f == NULL) f = stderr;
        !           292:
        !           293:     if (s == NULL) s = stdout;
        !           294:
        !           295:
        !           296:
        !           297:     memset(&reg, 0, sizeof(reg));
        !           298:
        !           299:     reg.family = AF_NUTSS;
        !           300:
        !           301:     strncpy(reg.user, "ths1", sizeof(reg.user));
        !           302:
        !           303:     strncpy(reg.domain, "nutss.net", sizeof(reg.domain));
        !           304:
        !           305:     strncpy(reg.service, "swaplinksd", sizeof(reg.service));
        !           306:
        !           307:
        !           308:
        !           309:     swaplinks_init();
        !           310:
        !           311:     swp = swaplinks_new("cloud9", &reg, peers, peers);
        !           312:
        !           313:
        !           314:
        !           315:     // let swaplinks get going.
        !           316:
        !           317:     sleep(10);
        !           318:
        !           319:
        !           320:
        !           321:     pthread_create(&tid, NULL, recvthread, swp);
        !           322:
        !           323:
        !           324:
        !           325:     sleep(1);
        !           326:
        !           327:
        !           328:
        !           329:     snprintf(tmp, sizeof(tmp), "%d;%d;0;", duration, weight);
        !           330:
        !           331:
        !           332:
        !           333:     while (time++ < 4*limit) { // (i-- != 0) {
        !           334:
        !           335:         swaplinks_sendtoany(swp, tmp, strlen(tmp)+1, 0);
        !           336:
        !           337:         if(s) fprintf(s,"   %d sent request %d\n",num,time);
        !           338:
        !           339:         sleep(1);
        !           340:
        !           341:     }
        !           342:
        !           343:
        !           344:
        !           345:       if(s) fprintf(s,"%d DONE SENDING\n",num);
        !           346:
        !           347:
        !           348:
        !           349:       pthread_join(tid, NULL);
        !           350:
        !           351:
        !           352:
        !           353:     if(s) fprintf(s,"%d SHUTTING DOWN\n",num);
        !           354:
        !           355:
        !           356:
        !           357:     if(num == 1) system("echo \" \" | mail -s\"node one finished sending!\" ths22@cs.cornell.edu");
        !           358:
        !           359:
        !           360:
        !           361:     return 0;
        !           362:
        !           363: }
        !           364:

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