/* this program execute the load-balancing algorithm I change: 1 The Bandwidth from LBA to router is 5 Mb ( I hard code it) 2 The Bandwidth from client to router is 5 Mb 3 Distribution time interval see procedure time_interval() for not concern the 145*8 LBA-I in inputflie use 203 code as 2nd param, in the first line LBA-I-1 in inputflie use 205 code as 2nd param, in the first line LBA-I-2 in inputflie use 204 code as 2nd param, in the first line random 201 roundrobin 202 for those algorithm, the algorithm just refer the static information ,e.g., distance, bandwidth, process power, and every LBA has a ssigning load table, that record the how many request the LBA has ready assign to a web, I transform all units to bit, for example, the process power is 5 MB I transform it to 5*10^6*8. for avoiding overflow, I divide all data by 1000, so for a web server that has 5 MB process power, in sider my the program, process power=5*10^6*8/1000, router power: 1000Mb basic power 10MB ethinet bandwidth 5Mb/s each Subnet has 20 client */ #include #include #include #include #include #include #include #include /* following parameters are about the network */ #define LIGHTSPEED 299796.0 /* km/sec */ #define REFRACINDEX 1.4; #define NODELEN 3 #define MAXPNL 30 #define MAX_NODE_NUM 100 #define MAX_AL_LEN 10 #define MAX_TABLE_LEN 100 #define MAX_PATH_NUM 100 #define MAX_WEB_NUM 50 #define MAX_LBA_NUM 20 /* max LBA number */ #define MAX_CLIENT_NUM 50 /* maximum client number */ #define MAX_EVENT_NUM 100000 /* maximum event number */ #define MAX_FILE_TYPE 7 #define PROPOWER 10*1000*8 /* mean 10MB */ #define Req_Msg_Size 145 /* bit here is fixed part for example: GET /Stuff/Funny/stilly.html HTTP/1.0 User-agent: NCSA Mosaic for the X Window System/2.5 Accept: text/plain Accept: text/html Accept: application/postscript Accept: image/gif here except of the URL Stuffy/still.html we account rest part as 50 bits because execpt this part, UML and other optinal message, so the Request-Msg-Size = 145 + ( 1~50) randomly number */ #define Resp_Msg 175 /* 175 just for the imformation 175 + actually document size */ /* we account the 175 from example: HTTP/1.0 Status 200 Document follows Server: NCSA/1.4 Date: tue, 4 Jul, 1776 19:17:05 GMT Content-type: text-html Content-length: 5280 Last-modified: Web, 1 Jan 1776 01:00:02 GMT because we can not exactly account the msg, 175+Docoment size + (1~10 random number ) */ #define TCPheader 20; #define UDPheader 8; /* notice uniform all unit bandwitch kps process power kps header bit msg bit document kB distance km */ /* table contains all directly link information */ typedef struct Table { char dest[NODELEN]; /* directly destination node */ float tran_speed; /* bandwitch */ float distance; /* distance */ } table; /* table contains path information */ typedef struct Path { char name[20]; /* path name, put all node name that path passes together */ float totallen; /* total distance */ float avebw; /* total bandwitch */ float totaldelay; /* total time delay */ char dest[5]; int hopnum; } path; /* client information */ typedef struct ClientInfor { char id[NODELEN]; }clientinfor; typedef struct WebInfor { char id[NODELEN]; float prospeed; int loadnum; float accumload; }webinfor; typedef struct Node { char id[NODELEN]; char type[1]; float Prospeed; float web_Prospeed; float time; float web_time; int linkcount; table tableinfor[MAX_TABLE_LEN]; path pathtable[MAX_PATH_NUM]; path lbapath[20]; int pathnum; webinfor webtable[MAX_WEB_NUM]; int webnum; path webpath[MAX_LBA_NUM]; int clientnum; int subnetnum; clientinfor clienttable[MAX_CLIENT_NUM]; path clientpath[MAX_CLIENT_NUM]; float clientlist[MAX_CLIENT_NUM][10]; int clientid; int TurNum; float accumload; int loadnum; int timeinter; float sload; /* this do not change */ int sloadnum; int lbanumm; }node; typedef struct Pack{ float size; char dest[4]; char send[4]; float headerbit; int msgbit; float docubit; } pack; typedef struct erec { float time; char src[4]; pack packinfor; char type[1]; char pathname[10]; float betime; int msg; int clientid; char nextnode[4]; char dest_node[4]; }erec; typedef struct enode { float idx; erec e; int num; struct enode *next; } enode; typedef struct file_struct { int id; char type[10]; } filetype; /* here all variable is global variable */ node NodeList[MAX_NODE_NUM]; int al_type = 0;/*here mean the type of algorithm MAX_AL_LEN mean the algorithm length*/ int netsize,totalnode; time_t ltime; struct tm *local; enode eventlist[MAX_EVENT_NUM]; int Eventnum; float TotalResponseTime; filetype filelist[10]; int timeinter = 2; float wpdelay; float pdelay; float rpdelay; float wqdelay; float rqdelay; float qdelay; float trdelay; float prodelay; float lbaqudelay; float totaldelay; int eventcount; float adjacentm[100][100]; int lowTxSpeed; int highTxSpeed; int version; char *netFileName="nj3m.net"; int totalEventCount=1000; FILE *fp; FILE *fpsum; int debug = 0; /* *********** function ******************* */ void get_path() { int totalpath,i,j,k,count,ind1; int temp[100]; int pred[100][100]; FILE *pt; pt=fopen("path.out","w"); for(i=0;i (adjacentm[i][k]+adjacentm[k][j]) ) && ( i!=j) ) { adjacentm[i][j]=adjacentm[i][k]+ adjacentm[k][j]; pred[i][j]=pred[k][j]; } } } fprintf(pt,"*** Path From LBA to Web ***\n"); for(i=0;i=1;ind1--) { if(temp[ind1]<10) fprintf(pt,"N0%d",temp[ind1]); else fprintf(pt,"N%d",temp[ind1]); } if(j<10) fprintf(pt,"N0%d",j); else fprintf(pt,"N%d",j); if(j<10) fprintf(pt," N0%d",j); else fprintf(pt," N%d",j); fprintf(pt,"\n"); } } } } fprintf(pt,"*** Path From Web to LBA ***\n"); for(i=0;i=1;ind1--) { if(temp[ind1]<10) fprintf(pt,"N0%d",temp[ind1]); else fprintf(pt,"N%d",temp[ind1]); } if(j<10) fprintf(pt,"N0%d",j); else fprintf(pt,"N%d",j); if(j<10) fprintf(pt," N0%d",j); else fprintf(pt," N%d",j); fprintf(pt,"\n"); } } } } fclose(pt); } float getfile(int num) { switch(num) { case 1:{ return 1*1.024*8; break; } case 2:{ return 2*1.024*8; break; } case 3:{ return 3*1.024*8; break; } case 4:{ return 4*1.024*8; break; } case 5:{ return 5*1.024*8; break; } case 6:{ return 1*1.024*8; break; } case 7:{ return 2*1.024*8; break; } case 8:{ return 6*1.024*8; break; } case 9:{ return 7*1.024*8; break; } case 10:{ return 10*1.024*8; break; } } } /* get CUP time */ void gettime(time_t *pointer) { time_t long_time; time( &long_time ); /* Get time as long integer. */ *pointer = long_time; } /* insert event according to time */ void inser_event(enode *oneevent) { int index,ind1; enode temp[10000]; if(Eventnum==0) { Eventnum++; eventlist[Eventnum]=*oneevent; } else { Eventnum++; for(ind1=1;ind1<=Eventnum-1;ind1++) temp[ind1]=eventlist[ind1]; for(index=1;index<=Eventnum-1;index++) { if(eventlist[index].idx > oneevent->idx) break; } eventlist[index]=*oneevent; for(ind1=index;ind1<=Eventnum-1;ind1++) eventlist[ind1+1]=temp[ind1]; } } int getwebnum() { int num,index; num=0; for(index=1;index<=totalnode;index++) { if(NodeList[index].type[0]=='W') num++; } return num; } int getsubnetnum() { int num,index; num=0; for(index=1;index<=totalnode;index++) { if(NodeList[index].type[0]=='S') num++; } return num; } /* this function is initialed the event node */ void init_eventnode(enode *oneevent) { int ind1,dummy; oneevent->idx=0.0; oneevent->e.time=0.0; oneevent->e.type[0]=' '; dummy=strlen(oneevent->e.src); for(ind1=0;ind1e.src[ind1]=' '; dummy=strlen(oneevent->e.pathname); for(ind1=0;ind1e.pathname[ind1]=' '; } /* this procedure get all parameters */ void getpathpara( int index, int ind1,float *distance, int *hopnum, float *avebw) { int ind2,dummy2,flag,ind3,ind4; int count; char name[4],dummy[40]; int number,flag1,hop; float dis, avbw,small_bw; dummy2=strlen(NodeList[index].pathtable[ind1].name); /* get the hop */ count=0; hop=0; dis=0; small_bw=10000.0; for(ind2=0;ind2=dummy2) break; for(ind4=flag1;ind4e.clientid][2]< oneevent->idx) NodeList[index].clientlist[oneevent->e.clientid][2]=oneevent->idx; else oneevent->idx=NodeList[index].clientlist[oneevent->e.clientid][2]; oneevent->e.type[0]='R'; oneevent->e.betime=oneevent->idx; dummy3=oneevent->idx; /* what kind file type */ oneevent->e.msg=rand()%10+1; oneevent->e.time=0.0; len=strlen(NodeList[index].id); for(ind2=0;ind2e.src[ind2]=NodeList[index].id[ind2]; /* msg sender */ len=strlen(NodeList[index].id); for(ind2=0;ind2e.packinfor.send[ind2]=NodeList[index].id[ind2]; /* msg header and msg size */ oneevent->e.packinfor.headerbit=TCPheader; oneevent->e.packinfor.msgbit=145*8+( rand()%50+1 ); /* total Msg size header+msg */ oneevent->e.packinfor.size=(oneevent->e.packinfor.headerbit)*8/((float)1000)+ oneevent->e.packinfor.msgbit/((float)1000); /* time msg reach the router */ temptime5=oneevent->e.packinfor.size/NodeList[index].clientlist[oneevent->e.clientid][3]; pdelay=pdelay+temptime5; oneevent->idx = temptime5+oneevent->idx; oneevent->e.time = oneevent->e.time+temptime5; temptime= NodeList[index].clientlist[oneevent->e.clientid][1]/LIGHTSPEED/REFRACINDEX; prodelay=prodelay+temptime; temptime2 = oneevent->e.packinfor.size/NodeList[index].clientlist[oneevent->e.clientid][0]; trdelay=trdelay+temptime2; if (debug) { fprintf(fp, "src=%d, dst=%d, txspeed=%f, pktsize=%f, trdelay=%f\n", index, oneevent->e.clientid, NodeList[index].clientlist[oneevent->e.clientid][0], oneevent->e.packinfor.size, temptime2); } oneevent->idx = temptime + temptime2 + oneevent->idx; oneevent->e.time=oneevent->e.time + temptime + temptime2; inser_event( oneevent); if(eventcount<=totalEventCount) { interval=rand()%10; eventstart =time_interval(interval); nextevent.e.type[0]='G'; nextevent.e.clientid=oneevent->e.clientid; for(ind2=0;ind2<3;ind2++) nextevent.e.src[ind2]=NodeList[index].id[ind2]; for(ind2=0;ind2<3;ind2++) nextevent.e.pathname[ind2]=oneevent->e.pathname[ind2]; nextevent.idx= dummy3 + eventstart; inser_event( &nextevent); eventcount++; } } void init_event() { int ind1,ind2; int index; int Lbanum; int interval; float sttime; enode oneevent; Lbanum=netsize; for( index=1;index<=netsize;index++) { if(NodeList[index].type[0]=='S') { Lbanum++; for( ind1=1;ind1<=NodeList[index].clientnum;ind1++) { eventcount++; oneevent.num=eventcount; interval=rand()%10; sttime =time_interval(interval); oneevent.e.type[0]='G'; oneevent.e.clientid=ind1; for(ind2=0;ind2<3;ind2++) oneevent.e.src[ind2]=NodeList[index].id[ind2]; for(ind2=0;ind2<3;ind2++) oneevent.e.pathname[ind2]=NodeList[Lbanum].id[ind2]; oneevent.idx=sttime; oneevent.e.betime=sttime; oneevent.e.time=sttime; inser_event( &oneevent); init_eventnode(&oneevent); } } } } /**************************************************** this function get a event /****************************************************/ enode get_event() { int index; enode oneevent; if(Eventnum!=0) { oneevent=eventlist[1]; Eventnum=Eventnum-1; for(index=1;index<=Eventnum;index++) eventlist[index]=eventlist[index+1]; return oneevent; } else { printf("Program finish!\n"); exit(0); } } void TNNC_2_algorithm( enode *oneevent, int index) { int ind1,webnumm,dummy; static float distf,avebwf,prospeedf; static int hopf,webloadf; float choice1,choice2, loadt; char name1[4]; int flag,ind2; float dummy2; dummy2=NodeList[index].webtable[1].loadnum/NodeList[index].webtable[1].prospeed; choice1=dummy2; webnumm=1; for(ind1=2;ind1<=NodeList[index].webnum;ind1++) { dummy2=NodeList[index].webtable[ind1].loadnum/NodeList[index].webtable[ind1].prospeed; choice2=dummy2; if(choice1 > choice2) { choice1=choice2; webnumm=ind1; } } /* copy the path from the LBA to the Web */ dummy=strlen(NodeList[index].pathtable[webnumm].name); for(ind1=0;ind1e.pathname[ind1]=NodeList[index].pathtable[webnumm].name[ind1]; for(ind1=0;ind1<3;ind1++) name1[ind1]=NodeList[index].pathtable[webnumm].dest[ind1]; for(ind1=0;ind1<3;ind1++) oneevent->e.packinfor.dest[ind1]=name1[ind1]; flag=0; for(ind1=1;ind1<=totalnode;ind1++) { for(ind2=0;ind2<3;ind2++) if(NodeList[ind1].id[ind2]==name1[ind2]) flag++; if(flag==3) break; else flag=0; } loadt=getfile(oneevent->e.msg); NodeList[ind1].accumload=NodeList[ind1].accumload+loadt; NodeList[ind1].loadnum=NodeList[ind1].loadnum++; NodeList[ind1].sload=NodeList[ind1].sload+loadt; NodeList[ind1].sloadnum=NodeList[ind1].sloadnum++; NodeList[index].webtable[webnumm].accumload=NodeList[index].webtable[webnumm].accumload+loadt; NodeList[index].webtable[webnumm].loadnum=NodeList[index].webtable[webnumm].loadnum+1; } void TNNC_1_algorithm( enode *oneevent, int index) { int ind1,webnumm,dummy; static float distf,avebwf,prospeedf; static int hopf,webloadf; float choice1,choice2, loadt; char name[4]; int flag,ind2; float dummy2,dummy3,dummy4; dummy2=NodeList[index].webtable[1].loadnum/NodeList[index].webtable[1].prospeed; dummy4=NodeList[index].pathtable[1].totallen/NodeList[index].pathtable[1].avebw; choice1=dummy2+dummy4+NodeList[index].pathtable[1].hopnum; webnumm=1; for(ind1=2;ind1<=NodeList[index].webnum;ind1++) { dummy2=NodeList[index].webtable[ind1].loadnum/NodeList[index].webtable[ind1].prospeed; dummy3=NodeList[index].pathtable[ind1].totallen/NodeList[index].pathtable[ind1].avebw; choice2=dummy2+dummy3+NodeList[index].pathtable[ind1].hopnum; if(choice1 > choice2) { choice1=choice2; webnumm=ind1; } } /* copy the path from the LBA to the Web */ dummy=strlen(NodeList[index].pathtable[webnumm].name); for(ind1=0;ind1e.pathname[ind1]=NodeList[index].pathtable[webnumm].name[ind1]; for(ind1=0;ind1<3;ind1++) name[ind1]=NodeList[index].pathtable[webnumm].dest[ind1]; for(ind1=0;ind1<3;ind1++) oneevent->e.packinfor.dest[ind1]=name[ind1]; flag=0; for(ind1=1;ind1<=totalnode;ind1++) { for(ind2=0;ind2<3;ind2++) if(NodeList[ind1].id[ind2]==name[ind2]) flag++; if(flag==3) break; else flag=0; } loadt=getfile(oneevent->e.msg); NodeList[ind1].accumload=NodeList[ind1].accumload+loadt; NodeList[ind1].loadnum=NodeList[ind1].loadnum++; NodeList[ind1].sload=NodeList[ind1].sload+loadt; NodeList[ind1].sloadnum=NodeList[ind1].sloadnum++; NodeList[index].webtable[webnumm].accumload=NodeList[index].webtable[webnumm].accumload+loadt; NodeList[index].webtable[webnumm].loadnum=NodeList[index].webtable[webnumm].loadnum+1; } /************************************************************ this procedure calculate that look for the table, but un connected with Web and LBA are not connected /************************************************************/ void TNNC_algorithm( enode *oneevent, int index) { int ind1,webnumm,dummy; static float distf,avebwf,prospeedf; static int hopf,webloadf; float choice1,choice2, loadt; char name[4]; int flag,ind2; float dummy2,dummy3,dummy4; dummy2=NodeList[index].webtable[1].loadnum/NodeList[index].webtable[1].prospeed; dummy4=NodeList[index].pathtable[1].totallen/NodeList[index].pathtable[1].avebw; choice1=dummy2+dummy4; webnumm=1; for(ind1=2;ind1<=NodeList[index].webnum;ind1++) { dummy2=NodeList[index].webtable[ind1].loadnum/NodeList[index].webtable[ind1].prospeed; dummy3=NodeList[index].pathtable[ind1].totallen/NodeList[index].pathtable[ind1].avebw; choice2=dummy2+dummy3; if(choice1 > choice2) { choice1=choice2; webnumm=ind1; } } /* copy the path from the LBA to the Web */ dummy=strlen(NodeList[index].pathtable[webnumm].name); for(ind1=0;ind1e.pathname[ind1]=NodeList[index].pathtable[webnumm].name[ind1]; for(ind1=0;ind1<3;ind1++) name[ind1]=NodeList[index].pathtable[webnumm].dest[ind1]; for(ind1=0;ind1<3;ind1++) oneevent->e.packinfor.dest[ind1]=name[ind1]; flag=0; for(ind1=1;ind1<=totalnode;ind1++) { for(ind2=0;ind2<3;ind2++) if(NodeList[ind1].id[ind2]==name[ind2]) flag++; if(flag==3) break; else flag=0; } loadt=getfile(oneevent->e.msg); NodeList[ind1].accumload=NodeList[ind1].accumload+loadt; NodeList[ind1].loadnum=NodeList[ind1].loadnum++; NodeList[ind1].sload=NodeList[ind1].sload+loadt; NodeList[ind1].sloadnum=NodeList[ind1].sloadnum++; NodeList[index].webtable[webnumm].accumload=NodeList[index].webtable[webnumm].accumload+loadt; NodeList[index].webtable[webnumm].loadnum=NodeList[index].webtable[webnumm].loadnum+1; } void R_algorithm(enode *oneevent,int index) { int temp,ind1,ind2,ind3,dummy; int flag=0; char name[4]; float loadt; int webnnum; /* NodeList[].TurNum means the RR number which web sit is assigned */ webnnum=getwebnum(); temp=rand() % webnnum+1; /* copy the path from the LBA to the Web */ dummy=strlen(NodeList[index].pathtable[temp].name); for(ind1=0;ind1e.pathname[ind1]=NodeList[index].pathtable[temp].name[ind1]; for(ind1=0;ind1<3;ind1++) name[ind1]=NodeList[index].pathtable[temp].dest[ind1]; for(ind1=0;ind1<3;ind1++) oneevent->e.packinfor.dest[ind1]=name[ind1]; flag=0; for(ind1=1;ind1<=totalnode;ind1++) { for(ind2=0;ind2<3;ind2++) if(NodeList[ind1].id[ind2]==name[ind2]) flag++; if(flag==3) break; else flag=0; } loadt=getfile(oneevent->e.msg); NodeList[ind1].accumload=NodeList[ind1].accumload+loadt; NodeList[ind1].loadnum=NodeList[ind1].loadnum++; NodeList[ind1].sload=NodeList[ind1].sload+loadt; NodeList[ind1].sloadnum=NodeList[ind1].sloadnum++; } /* this load balance algorithm */ void RR_algorithm(enode *oneevent,int index) { int temp,ind1,ind2,ind3,dummy; int flag=0; char name[4]; float loadt; static int webid; int webnnum; /* NodeList[].TurNum means the RR number which web sit is assigned */ webnnum=getwebnum(); webid++; if(webid>webnnum) webid=1; temp=webid; /* copy the path from the LBA to the Web */ dummy=strlen(NodeList[index].pathtable[temp].name); for(ind1=0;ind1e.pathname[ind1]=NodeList[index].pathtable[temp].name[ind1]; for(ind1=0;ind1<3;ind1++) name[ind1]=NodeList[index].pathtable[temp].dest[ind1]; for(ind1=0;ind1<3;ind1++) oneevent->e.packinfor.dest[ind1]=name[ind1]; flag=0; for(ind1=1;ind1<=totalnode;ind1++) { for(ind2=0;ind2<3;ind2++) if(NodeList[ind1].id[ind2]==name[ind2]) flag++; if(flag==3) break; else flag=0; } loadt=getfile(oneevent->e.msg); NodeList[ind1].accumload=NodeList[ind1].accumload+loadt; NodeList[ind1].loadnum=NodeList[ind1].loadnum++; NodeList[ind1].sload=NodeList[ind1].sload+loadt; NodeList[ind1].sloadnum=NodeList[ind1].sloadnum++; } /* compute CPU time */ float cal_time() { float temtime; gettime( <ime ); /* Get time as long integer. */ local = localtime( <ime ); /* Convert to local time. */ temtime=local->tm_hour+local->tm_min; return temtime; } /* this function address a web address */ void LBA_process(int index,enode *oneevent, FILE *fp ) { float times,fintime; float temtime,temtime1,temtime2; int ind1,dummy; int ind2,flag; char dummy5[3]; float qudelay; /* call CPU time for allocate address start */ times=cal_time(); /* call function to compute the address */ if( al_type==202 ) RR_algorithm(oneevent,index); else if(al_type==203) TNNC_algorithm(oneevent,index); else if( al_type==201 ) R_algorithm(oneevent,index); else if(al_type==204) TNNC_2_algorithm(oneevent, index); else if(al_type==205) TNNC_1_algorithm(oneevent, index); /* compute finish time, so we know how long the allocate to cost */ fintime=cal_time(); temtime1=(fintime-times)/NodeList[index].Prospeed; pdelay=pdelay+temtime1; /* remeber the the initial event time */ temtime=oneevent->idx; qudelay=NodeList[index].time-oneevent->idx; /* update the event time */ if(NodeList[index].time > oneevent->idx) { qdelay=qdelay+qudelay; lbaqudelay=lbaqudelay+qudelay; oneevent->idx=oneevent->idx + qudelay +temtime1; oneevent->e.time=oneevent->e.time+qudelay+temtime1; } else if(NodeList[index].time<=oneevent->idx) { oneevent->idx=oneevent->idx+temtime1; oneevent->e.time=oneevent->e.time+temtime1; } /* get the next node that will process this event along the path */ for(ind1=0;ind1<3;ind1++) dummy5[ind1]=oneevent->e.pathname[ind1]; /* delete the node from the path */ dummy=strlen(oneevent->e.pathname); for(ind1=0;ind1e.pathname[ind1]=oneevent->e.pathname[ind1+3]; /* find the link and send this event to next node */ flag=0; for(ind1=1;ind1<=NodeList[index].linkcount;ind1++) { for(ind2=0;ind2<3;ind2++) if(NodeList[index].tableinfor[ind1].dest[ind2]==dummy5[ind2]) flag++; if(flag==3) break; else flag=0; } /* update the event time */ temtime1=NodeList[index].tableinfor[ind1].distance/LIGHTSPEED/REFRACINDEX; prodelay=prodelay+temtime1; temtime2=oneevent->e.packinfor.size/NodeList[index].tableinfor[ind1].tran_speed; oneevent->idx=oneevent->idx+temtime1+temtime2; oneevent->e.time=oneevent->e.time+temtime1+temtime2; trdelay=trdelay+temtime2; if (debug) { fprintf(fp, "src=%d, dst=%d, txspeed=%f, pktsize=%f, trdelay=%f\n", index, ind1, NodeList[index].tableinfor[ind1].tran_speed, oneevent->e.packinfor.size, temtime2); } /* update the event receive */ for(ind1=0;ind1<3;ind1++) oneevent->e.src[ind1]=dummy5[ind1]; /* insert event queue */ inser_event(oneevent); /* update the local time */ if(NodeList[index].time==0) NodeList[index].time=temtime + (fintime-times)/NodeList[index].Prospeed; else if(NodeList[index].timetemtime) NodeList[index].time=NodeList[index].time + (fintime-times)/NodeList[index].Prospeed; } float getload( int index) { int ind1,flag,ind2,ind3ind4; float load,load2; load=0; flag=0; for(ind1=1;ind1<=Eventnum;ind1++) { flag=0; for(ind2=0;ind2<3;ind2++) { if(eventlist[ind1].e.src[ind2]== NodeList[index].id[ind2]) flag++; if(flag==3) { load2=getfile(eventlist[ind1].e.msg); load=load+load2; flag=0; } else flag=0; } } return load; } /* web process this event */ void web_process(int index,enode *oneevent, FILE *fp) { int ind1,ind2,dummy,flag; float timedelay,timedelay2,temptime,starttime,fintime; char dummy5[3]; float load; float filesize; float qudelay; float temp; /* process the document delay */ timedelay=oneevent->e.packinfor.size/NodeList[index].web_Prospeed; // timedelay=oneevent->e.packinfor.size/10*1000*8; filesize = getfile(oneevent->e.msg); NodeList[index].accumload=NodeList[index].accumload-filesize; oneevent->e.packinfor.docubit=filesize; timedelay2=oneevent->e.packinfor.docubit/NodeList[index].web_Prospeed; pdelay=pdelay+timedelay+timedelay2; wpdelay=wpdelay+timedelay+timedelay2; temp=TCPheader; oneevent->e.packinfor.headerbit=175*8+(rand()%50+1) + temp*8; oneevent->e.packinfor.size = oneevent->e.packinfor.headerbit/(float)1000+ oneevent->e.packinfor.docubit; /* remember the old local time */ temptime=NodeList[index].web_time; /* update the local time */ if(NodeList[index].web_time==0) NodeList[index].web_time = oneevent->idx + timedelay + timedelay2; else if(NodeList[index].web_time < oneevent->idx) NodeList[index].web_time = oneevent->idx + timedelay + timedelay2; else if(NodeList[index].web_time == oneevent->idx) NodeList[index].web_time=NodeList[index].web_time + timedelay+timedelay2; else if(NodeList[index].web_time > oneevent->idx) NodeList[index].web_time=NodeList[index].web_time + timedelay+timedelay2; qudelay= temptime - oneevent->idx; /* update the event time */ if(temptime>oneevent->idx) { qdelay=qdelay+qudelay; wqdelay=wqdelay+qudelay; oneevent->idx=oneevent->idx + qudelay +timedelay+timedelay2; oneevent->e.time=oneevent->e.time + qudelay +timedelay+timedelay2; } else { oneevent->idx=oneevent->idx+timedelay+timedelay2; oneevent->e.time=oneevent->e.time+timedelay+timedelay2; } /* find the return path */ flag=0; for(ind1=1;ind1<=NodeList[index].subnetnum;ind1++) { for(ind2=0;ind2<3;ind2++) if(NodeList[index].pathtable[ind1].dest[ind2]==oneevent->e.packinfor.send[ind2]) flag++; if(flag==3) break; else flag=0; } dummy=strlen(NodeList[index].pathtable[ind1].name); for(ind2=0;ind2e.pathname[ind2]=NodeList[index].pathtable[ind1].name[ind2]; for(ind2=0;ind2<3;ind2++) oneevent->e.packinfor.dest[ind2]=NodeList[index].pathtable[ind1].dest[ind2]; /* find next node that will process this event */ dummy=strlen(oneevent->e.pathname); for(ind1=0;ind1<3;ind1++) dummy5[ind1]=oneevent->e.pathname[ind1]; for(ind1=0;ind1e.pathname[ind1]=oneevent->e.pathname[ind1+3]; /* compute link cost */ flag=0; for(ind1=1;ind1<=NodeList[index].linkcount;ind1++) { for(ind2=0;ind2<3;ind2++) if(NodeList[index].tableinfor[ind1].dest[ind2]==dummy5[ind2]) flag++; if(flag==3) break; else flag=0; } /* update the event time */ timedelay2=NodeList[index].tableinfor[ind1].distance/LIGHTSPEED/REFRACINDEX; prodelay=prodelay+timedelay2; oneevent->idx=oneevent->idx+timedelay2; oneevent->e.time=oneevent->e.time+timedelay2; timedelay2=oneevent->e.packinfor.size/NodeList[index].tableinfor[ind1].tran_speed; oneevent->idx=oneevent->idx+timedelay2; oneevent->e.time=oneevent->e.time+timedelay2; if (debug) { fprintf(fp, "src=%d, dst=%d, txspeed=%f, pktsize=%f, trdelay=%f\n", index, ind1, NodeList[index].tableinfor[ind1].tran_speed, oneevent->e.packinfor.size, timedelay2); } trdelay=trdelay+timedelay2; /* send this event to next node */ for(ind1=0;ind1<3;ind1++) oneevent->e.src[ind1]=dummy5[ind1]; /* change event size and type */ oneevent->e.type[0]='D'; /* insert event into queue */ inser_event(oneevent); } /* this function is switch process */ void swit_process(int index,enode *oneevent, FILE *fp) { float trandelay,delaytime2; int dummy,flag,ind1,ind2; char dummy5[4]; float qudelay; /* compute the transmittion cost */ trandelay=oneevent->e.packinfor.size/NodeList[index].Prospeed; pdelay=pdelay+trandelay; rpdelay=rpdelay+trandelay; /* assign local old time */ delaytime2=NodeList[index].time; /* renew the local time */ if(NodeList[index].time==0) NodeList[index].time=oneevent->idx+trandelay; else if(NodeList[index].time < oneevent->idx) NodeList[index].time=oneevent->idx+trandelay; else if(NodeList[index].time > oneevent->idx) NodeList[index].time=NodeList[index].time+trandelay; else if(NodeList[index].time == oneevent->idx) NodeList[index].time=NodeList[index].time+trandelay; qudelay = delaytime2 - oneevent->idx; /* renew the event time */ if(delaytime2>oneevent->idx) { qdelay=qdelay+qudelay; rqdelay=rqdelay+qudelay; oneevent->idx = oneevent->idx + qudelay + trandelay; oneevent->e.time = oneevent->e.time + qudelay+ trandelay; } else { oneevent->idx=oneevent->idx+trandelay; oneevent->e.time=oneevent->e.time+trandelay; } /* get the next node along the path */ for(ind1=0;ind1<3;ind1++) dummy5[ind1]=oneevent->e.pathname[ind1]; /* delete the node that the event already pass */ dummy=strlen(oneevent->e.pathname); for(ind1=0;ind1e.pathname[ind1]=oneevent->e.pathname[ind1+3]; /* find the link information */ flag=0; for(ind1=1;ind1<=NodeList[index].linkcount;ind1++) { for(ind2=0;ind2<3;ind2++) if(NodeList[index].tableinfor[ind1].dest[ind2] == dummy5[ind2]) flag++; if(flag==3) break; else flag=0; } /* compute the link cost and renew the event time, send the event to next node*/ delaytime2=NodeList[index].tableinfor[ind1].distance/LIGHTSPEED/REFRACINDEX; oneevent->idx=oneevent->idx+delaytime2; oneevent->e.time=oneevent->e.time+delaytime2; prodelay=prodelay+delaytime2; delaytime2=oneevent->e.packinfor.size/NodeList[index].tableinfor[ind1].tran_speed; trdelay=trdelay+delaytime2; if (debug) { fprintf(fp, "src=%d, dst=%d, txspeed=%f, pktsize=%f, trdelay=%f\n", index, ind1, NodeList[index].tableinfor[ind1].tran_speed, oneevent->e.packinfor.size, delaytime2); } oneevent->idx=oneevent->idx+delaytime2; oneevent->e.time=oneevent->e.time+delaytime2; /* change the event receivers */ for(ind1=0;ind1<3;ind1++) oneevent->e.src[ind1]=dummy5[ind1]; /* inser_event */ inser_event(oneevent); } /* this function is switch process */ void router_process(int index,enode *oneevent, FILE *fp ) { float trandelay,delaytime2; int dummy,flag,ind1,ind2; char dummy5[4]; float qudelay; /* compute the transmittion cost */ trandelay=oneevent->e.packinfor.size/NodeList[index].Prospeed; pdelay=pdelay+trandelay; rpdelay=rpdelay+trandelay; /* assign local old time */ delaytime2=NodeList[index].time; /* renew the local time */ if(NodeList[index].time==0) NodeList[index].time=oneevent->idx+trandelay; else if(NodeList[index].time< oneevent->idx) NodeList[index].time=oneevent->idx+trandelay; else if(NodeList[index].time>oneevent->idx) NodeList[index].time=NodeList[index].time+trandelay; else if(NodeList[index].time == oneevent->idx) NodeList[index].time=NodeList[index].time+trandelay; qudelay= delaytime2-oneevent->idx; /* renew the event time */ if(delaytime2>oneevent->idx) { qdelay=qdelay+qudelay; rqdelay=rqdelay+qudelay; oneevent->idx=oneevent->idx+qudelay+ trandelay; oneevent->e.time=oneevent->e.time+qudelay+ trandelay; } else { oneevent->idx=oneevent->idx+trandelay; oneevent->e.time=oneevent->e.time+trandelay; } /* get the next node along the path */ for(ind1=0;ind1<3;ind1++) dummy5[ind1]=oneevent->e.pathname[ind1]; /* delete the node that the event already pass */ dummy=strlen(oneevent->e.pathname); for(ind1=0;ind1e.pathname[ind1]=oneevent->e.pathname[ind1+3]; /* find the link information */ flag=0; for(ind1=1;ind1<=NodeList[index].linkcount;ind1++) { for(ind2=0;ind2<3;ind2++) if(NodeList[index].tableinfor[ind1].dest[ind2] == dummy5[ind2]) flag++; if(flag==3) break; else flag=0; } /* compute the link cost and renew the event time, send the event to next node*/ delaytime2=NodeList[index].tableinfor[ind1].distance/LIGHTSPEED/REFRACINDEX; prodelay=prodelay+delaytime2; oneevent->idx=oneevent->idx+delaytime2; oneevent->e.time=oneevent->e.time+delaytime2; delaytime2=oneevent->e.packinfor.size/NodeList[index].tableinfor[ind1].tran_speed; trdelay=trdelay+delaytime2; oneevent->idx=oneevent->idx+delaytime2; oneevent->e.time=oneevent->e.time+delaytime2; if (debug) { fprintf(fp, "src=%d, dst=%d, txspeed=%f, pktsize=%f, trdelay=%f\n", index, ind1, NodeList[index].tableinfor[ind1].tran_speed, oneevent->e.packinfor.size, delaytime2); } /* change the event receivers */ for(ind1=0;ind1<3;ind1++) oneevent->e.src[ind1]=dummy5[ind1]; /* inser_event */ inser_event(oneevent); } void LBA_process_special1(int index, enode *oneevent, FILE *fp) { } void LBA_process_special2(int index, enode *oneevent, FILE *fp) { } void Web_Process_special1(int index, enode *oneevent, FILE *fp) { } void compute_response_time(int index,enode *oneevent) { static int number; FILE *pf; float dummy,dummy1; float timedelay; float temptime; float qudelay; temptime=NodeList[index].clientlist[oneevent->e.clientid][2]; timedelay=oneevent->e.packinfor.size/NodeList[index].clientlist[oneevent->e.clientid][3]; pdelay=pdelay+timedelay; if(NodeList[index].clientlist[oneevent->e.clientid][2]>= oneevent->idx) NodeList[index].clientlist[oneevent->e.clientid][2]= NodeList[index].clientlist[oneevent->e.clientid][2]+timedelay; else if(NodeList[index].clientlist[oneevent->e.clientid][2]< oneevent->idx) NodeList[index].clientlist[oneevent->e.clientid][2]= oneevent->idx+timedelay; qudelay=NodeList[index].clientlist[oneevent->e.clientid][2]-oneevent->idx; if(temptime>oneevent->idx) { qdelay=qdelay+qudelay; rqdelay=rqdelay+qudelay; oneevent->idx= oneevent->idx+ qudelay+timedelay; oneevent->e.time= oneevent->e.time+ qudelay+timedelay; } else { oneevent->idx=oneevent->idx+timedelay; oneevent->e.time=oneevent->e.time+timedelay; } number++; TotalResponseTime=TotalResponseTime + oneevent->e.time; } void computing_time( int index, enode *oneevent, FILE *fp) { float trandelay,delaytime2; int ind1,ind2; float qudelay; /* compute the transmittion cost */ trandelay=oneevent->e.packinfor.size/NodeList[index].Prospeed; pdelay=pdelay+trandelay; rpdelay=rpdelay+trandelay; /* assign local old time */ delaytime2=NodeList[index].time; /* renew the local time */ if(NodeList[index].time==0) NodeList[index].time=oneevent->idx+trandelay; else if(NodeList[index].time< oneevent->idx) NodeList[index].time=oneevent->idx+trandelay; else if(NodeList[index].time>oneevent->idx) NodeList[index].time=NodeList[index].time+trandelay; else if(NodeList[index].time==oneevent->idx) NodeList[index].time=NodeList[index].time+trandelay; qudelay=delaytime2 - oneevent->idx; /* renew the event time */ if(delaytime2>oneevent->idx) { qdelay=qdelay + qudelay; rqdelay=rqdelay+ qudelay; oneevent->idx=oneevent->idx+ qudelay + trandelay; oneevent->e.time=oneevent->e.time+ qudelay + trandelay; } else { oneevent->idx=oneevent->idx+trandelay; oneevent->e.time=oneevent->e.time+trandelay; } /* compute the link cost and renew the event time, send the event to next node*/ delaytime2=NodeList[index].clientlist[ oneevent->e.clientid ][1]/LIGHTSPEED/REFRACINDEX; prodelay=prodelay+delaytime2; oneevent->idx=oneevent->idx+delaytime2; oneevent->e.time=oneevent->e.time+delaytime2; delaytime2=oneevent->e.packinfor.size/NodeList[index].clientlist[oneevent->e.clientid][0]; trdelay=trdelay+delaytime2; oneevent->idx=oneevent->idx+delaytime2; oneevent->e.time=oneevent->e.time+delaytime2; if (debug) { fprintf(fp, "src=%d, dst=%d, (use[0])txspeed=%f, pktsize=%f, trdelay=%f\n", index, ind1, NodeList[index].clientlist[oneevent->e.clientid][0], oneevent->e.packinfor.size, delaytime2); } oneevent->e.type[0]='C'; inser_event(oneevent); } /* this function process all event according to type of the event and node */ void process_event(enode *oneevent, FILE *fp) { int index,dummy,ind1,flag,flag1,ind2; /* get node that will process this event */ flag=0; for(ind1=1;ind1<=totalnode;ind1++) { for(ind2=0;ind2<3;ind2++) if(oneevent->e.src[ind2]==NodeList[ind1].id[ind2]) flag++; if(flag==3) { index=ind1; break; } else flag=0; } /* according to event type and node event to process event */ if(NodeList[index].type[0]=='S' && oneevent->e.type[0]=='R') swit_process(index,oneevent,fp); else if (NodeList[index].type[0]== 'R') router_process(index, oneevent,fp); else if(NodeList[index].type[0]=='W') { flag=0; for(ind2=0;ind2<3;ind2++) if(oneevent->e.packinfor.dest[ind2]==NodeList[index].id[ind2]) flag++; if(flag==3) web_process(index, oneevent,fp); else swit_process(index,oneevent,fp); } else if(NodeList[index].type[0]=='L') LBA_process(index, oneevent,fp); else if( NodeList[index].type[0]=='S' && oneevent->e.type[0]=='D') { flag=0; for(ind2=0;ind2<3;ind2++) if(oneevent->e.packinfor.dest[ind2]==NodeList[index].id[ind2]) flag++; if(flag==3) computing_time(index, oneevent,fp); else swit_process(index,oneevent,fp); } else if( NodeList[index].type[0]=='S' && oneevent->e.type[0]=='C') compute_response_time(index,oneevent); else if(oneevent->e.type[0]=='G' && NodeList[index].type[0]=='S') generate_event( index, oneevent); } /************************************************************* This procedure generate: 1. Client number of every subnet 2. distance between the each client and Router. 3. Bandwidth from each client to the Router. /*************************************************************/ void init_client() { int index, dummy; for(index=1;index<=netsize;index++) { /* if the node is a sun net, set the distance between Router and the client, banwidth, and processing speed for client we let the client has the same index with the Router associated */ if(NodeList[index].type[0]=='S') { /* generate the client number */ NodeList[index].clientnum=20; for(dummy=1;dummy<=NodeList[index].clientnum;dummy++) { /* generate the distance between the Router and Client 0:bandwidth KB maximum 10 Mbs 1:distance km 2:the local time every client 3:the node's process speed KB */ // Chow: change to command line range /* ying: I change bandwidth as 5 Mb, it is from client to subnet router */ NodeList[index].clientlist[dummy][0]= 1*5000; // NodeList[index].clientlist[dummy][0]= // lowTxSpeed+rand()%(highTxSpeed-lowTxSpeed); NodeList[index].clientlist[dummy][1]= 0.5 + (rand()%5)*0.1; NodeList[index].clientlist[dummy][2]=0.0; NodeList[index].clientlist[dummy][3]=10*1000*8; } } } } /********************************************************** this procedure initialize all the file type /**********************************************************/ void init_filetype() { filelist[1].id=1; strcpy(filelist[1].type,"HTML"); filelist[2].id=2; strcpy(filelist[2].type,"Images"); filelist[3].id=3; strcpy(filelist[3].type,"Video"); filelist[4].id=4; strcpy(filelist[4].type,"Sound"); filelist[5].id=5; strcpy(filelist[5].type,"dynamic"); filelist[6].id=6; strcpy(filelist[6].type,"formatted"); filelist[7].id=7; strcpy(filelist[7].type,"other"); } help_msg() { printf("format: lbai -[aelhvd] netFile outputFile\n"); printf(" option: a set algorithm type\n"); printf(" option: e set end event count\n"); printf(" option: l set the low end of tx speed range\n"); printf(" option: h set the high end of tx speed range\n"); printf(" option: v set the version of algorithm\n"); printf(" option: d set debug mode\n"); } /********************************************************** MAIN PROGRAM CALL THE EVENT PROCESS TILL ALL EVENT IS PROCESSED /**********************************************************/ void main(int argc, char *argv[]) { enode oneevent; int ind1, ind2,count; int num=1; float minimload,maximload,imload,temp; extern char *optarg; extern int optind; int option_exist = 0; int c; char *outFileName="fresult.out"; char *sumFileName="summary.txt"; /* process command line args */ while ((c=getopt(argc,argv,"a:e:l:h:v:d")) != EOF) { option_exist = 1; switch(c) { case 'a': if ((al_type = atoi(optarg)) < 1) al_type = 0; printf("The al_type is %d\n", al_type); break; case 'e': if ((totalEventCount = atoi(optarg)) < 1) totalEventCount = 1000; printf("The totalEventCount is %d\n", totalEventCount); break; case 'l': if ((lowTxSpeed = atoi(optarg)) < 1) lowTxSpeed = 1; printf("The low end of transmission speed range is %d\n", lowTxSpeed); break; case 'h': if ((highTxSpeed = atoi(optarg)) < 1) highTxSpeed = 10; printf("The high end of transmission speed range is %d\n", highTxSpeed); break; case 'v': if ((version = atoi(optarg)) < 1) version = 0; printf("The version is %d\n", version); break; case 'd': debug = 1; printf("set debug mode on\n"); break; case '?': help_msg(); exit (1); default: fprintf (stderr, "unrecognized arg >%s<\n", optarg); help_msg(); exit (1); } } netFileName = argv[optind++]; outFileName = argv[optind++]; if ((fpsum = fopen(sumFileName, "a")) == NULL) { fprintf(stderr, "Cannot open output file: %s\n", sumFileName); exit(1); } if ((fp = fopen(outFileName, "w")) == NULL) { fprintf(stderr, "Cannot open output file: %s\n", outFileName); exit(1); } /* in this procedure, all node are initialed and event queue is initialed */ init_nodes(NodeList); /* initialize the file type */ // init_filetype(); /* in this procedure, randomly generate the client number, distance from the client to the Router, bandwidth, distance fron the Router to LBA */ init_client(); init_event(); do { oneevent=get_event(); process_event(&oneevent,fp); }while(Eventnum>0); if( al_type==201) printf("algorithm: random\n"); else if( al_type==202 ) printf("algorithm: simple RR\n"); else if( al_type==203 ) printf("algorithm: LBAI \n"); else if( al_type==204) printf("algorithm: LBAI-2 \n"); else if( al_type==205) printf("algorithm: LBAI-1 \n"); printf("Total Ave Queuing Delay %f\n",qdelay/eventcount); printf("Ave web Queuing Delay %f\n",wqdelay/eventcount); printf("Ave router Queuing Delay %f\n",rqdelay/eventcount); printf("Total Trans Delay %f\n",trdelay/eventcount); printf("Total Prog Delay %f\n",prodelay/eventcount); printf("Total process Delay %f\n",pdelay/eventcount); // printf("web process Delay %f\n",wpdelay); // printf("router process Delay %f\n",rpdelay); printf("average response time %f\n",TotalResponseTime/eventcount); printf("Total response time %f\n",TotalResponseTime); printf("EXITING , END of SIMULATION\n"); minimload=100000000.0; maximload=0.0; for(ind1=1;ind1<=netsize; ind1++) { if(NodeList[ind1].type[0]=='W') { temp = NodeList[ind1].sload / NodeList[ind1].Prospeed; if( minimload > temp ) minimload=temp; if( maximload < temp ) maximload=temp; } } imload=maximload-minimload; printf("Imbalancing Rate %f\n\n\n",imload); fprintf(fp,"\n\n"); if( al_type==201) fprintf(fp,"algorithm: random\n"); else if( al_type==202 ) fprintf(fp,"algorithm: simple RR\n"); else if( al_type==203 ) fprintf(fp,"algorithm: LBAI \n"); else if( al_type==204) fprintf(fp,"algorithm: LBAI-2 \n"); else if( al_type==205) fprintf(fp,"algorithm: LBAI-1 \n"); fprintf(fp,"TotalEventCount: %d\n", totalEventCount); fprintf(fp,"Total Queuing Delay %f\n",qdelay/eventcount); fprintf(fp,"web Queuing Delay %f\n",wqdelay/eventcount); fprintf(fp,"router Queuing Delay %f\n",rqdelay/eventcount); // fprintf(fp,"lba Queuing Delay %f\n",lbaqudelay/eventcount); fprintf(fp,"Trans Delay %f\n",trdelay/eventcount); fprintf(fp,"Progation Delay %f\n",prodelay/eventcount); fprintf(fp,"Total process Delay %f\n",pdelay/eventcount); fprintf(fp,"web process Delay %f\n",wpdelay/eventcount); fprintf(fp,"router process Delay %f\n",rpdelay/eventcount); fprintf(fp,"average response time %f\n",TotalResponseTime/eventcount); fprintf(fpsum,"lowTxSpeed=%d, avgRespTime= %f\n", lowTxSpeed, TotalResponseTime/eventcount); fprintf(fp,"Total response time %f\n",TotalResponseTime); fprintf(fp,"EXITING , END of SIMULATION\n"); fprintf(fp,"Imbalancing Rate %f\n\n\n",imload); for(ind1=1;ind1<=netsize; ind1++) { if(NodeList[ind1].type[0]=='W') { for(ind2=0;ind2<3;ind2++) fprintf(fp,"%c",NodeList[ind1].id[ind2]); fprintf(fp," load : %f %d\n",NodeList[ind1].sload/10.0, NodeList[ind1].sloadnum); } } fclose(fp); }/* End. */