📄 router_5.cpp
字号:
Router::Router(const Router & a)
//Copy constructor
{
voluem=a.voluem;
speed=a.speed;
link=a.link;
Queue_frame::Queue_len=(a.router_p)->len_q();//change the value of default constructor in class Queue_frmae(needed to bulid array of queue)
router_p = new Queue_frame[link];
time_links=new int [link];
size_links= new int [link];
for (int i=0;i<link;++i)
{
//cpoy the contents of 3 array (queue,remain size, service time )
router_p[i]=(a.router_p[i]);
time_links[i]=a.time_links[i];
size_links[i]=a.size_links[i];
}
}
Router::~Router (void)
//Destructor
{
delete [] router_p;
delete size_links;
delete time_links;
}
int Router::push_msg( frame & msg)
//Store message in the Queue(in the desirable link)
{
if ( (msg.size>*(size_links+msg.path))||msg.path>link-1||msg.path<0) return 0;//there is not enough size remain in the link
//or the wanted link dont exist return 0
if ( ((router_p+msg.path)->size_q())==0) *(time_links+msg.path)=msg.size/speed;//in case it is the 1st message,
//store service time in service time array
if ( ((router_p+msg.path)->enqueue(msg))==0) return 0;//in case enqueue didnt success return 0
*(size_links+msg.path)=*(size_links+msg.path)-msg.size;//update the remain size of the link
return 1;
}
int Router::pop_msg (frame & msg,int link_i)
//Pull the 1st message from the Queue(in the desirable link)
{
if ((((router_p+link_i)->dequeue(msg))==0)||(link_i>link-1)||(link_i<0)) return 0;// in case dequeue didnt success
//or the wanted link dont exist return 0
*(size_links+link_i)=*(size_links+link_i)+msg.size;//update the remain size in the link
if (((router_p+link_i)->size_q())==0) *(time_links+link_i)=-1;//in case it was the only message store in the queue
//,set service time areay to -1(initial value)
else *(time_links+link_i)=(((router_p+link_i)->first()).size)/speed;//update the service time array with the
//new service time of 1st message in the queue
return 1;
}
int Router::min_time (int & link_i)
//Find the minimum service time from all of the link
{
int time=-2;//initiail value
link_i=-1;//initiail value
for (int i=0;i<link;i++)
{
if (time==-2&&*(time_links+i)!=-1) {time=*(time_links+i);link_i=i;}//store the first service time in (time)
//store number of the link in (link_i)
//,-1 is ignored it means that queue is empty
if (*(time_links+i)!=-1&&(time!=-2)&&*(time_links+i)<time) {time=*(time_links+i);link_i=i;}
//if there is a smaller service time store it in (time),
//(-1 is ignored),also store the link number in (link_i)
}
return time;
}
void Router::time_update (int t)
//Update (subtract) the service time array
{
for (int i=0;i<link;i++)
if (*(time_links+i)!=-1) *(time_links+i)=*(time_links+i)-t;
//Subtract all the cells in array by value of t (-1 is ignored)
}
//-------------------------------------------------------------------------------------------------------------------
void main (void)
{
char key_in;
int t_messages=0,f_messages=0,dumped_messages=0,total_delay=0;//messages forward,messages dumped,total delay of messages
float total_delay_normal=0.0;//total delay normal to size of messages
int link_speed,link_voluem,number_of_links,len_of_queue;//Parameters for the Router
int msg_avg_size,msg_avg_income,t=0,t_limit,link_i;//Parameters for income messages
int evant_msg_in,evant_msg_pop;//store the current time to evant (message in come/message pop)
frame msg;//a struct that contain diffrent data of the message(size,path,start time)
cout<<"--------------Router Simulation-----------------------------"<<endl;
while(1)
{
//insert data parameters for router simulation ,
//read data paremeters from file Router_data.dat or insert data directely
cout<<endl<<"Read data simulation from file Router_data.dat Or "<<endl<<"insert data simulation directly ?"<<endl<<endl;
cout<<"y-Read from file n-Insert data simulation directly x-exit :";
cin>>key_in;
if (key_in!='y' && key_in!='n' && key_in!='x' ){ cout<<"Error insert only [y,n,x] press Enter to contiue..."<<endl; getch(); }
//in case the answer to the Qusetion wasnt the opintion (y,n,x)
else {
if (key_in=='x') exit(1);//in case of x exit the program
if (key_in=='y')
//in case of 'y' read data parmeters for the simulation for file Router_data.dat
{
char *buffer=new char[100];//neede later for skip a line in the file
fstream dump("Router_data.dat",ios::in);//open and read form file
if(dump==NULL){ printf("\nFile problem!"); _getch(); exit(2); }//eror in open file
dump>>number_of_links;//read first parmeter form the file
dump.getline(buffer, 100);//skip line (in the file after each data parmeter there is a comment that describe the parameter)
dump>>link_speed;//read second parameter
dump.getline(buffer, 100);//skip line
dump>>len_of_queue;
dump.getline(buffer, 100);
dump>>link_voluem;
dump.getline(buffer, 100);
dump>>msg_avg_income;
dump.getline(buffer, 100);
dump>>msg_avg_size;
dump.getline(buffer, 100);
dump>>t_limit;
delete buffer;
dump.close();//close file
}
if (key_in=='n')
//in case of 'n' insert data parameters for simulation directly
{
cout<<endl<<"\nNumber of Outgoing Links:";
cin>>number_of_links;
cout<<endl<<"\nSpeed of link:";
cin>>link_speed;
cout<<endl<<"\nLength of Queue in the Link:";
cin>>len_of_queue;
cout<<endl<<"\nSize of Link (bit):";
cin>>link_voluem;
cout<<endl<<"\nAverage Arrival Of Messages :";
cin>>msg_avg_income;
cout<<endl<<"\nAverage Size Of Messages(bit):";
cin>>msg_avg_size;
cout<<endl<<"\nTime of simulation:";
cin>>t_limit;
}
if((number_of_links<1||link_speed<1||len_of_queue<1||link_voluem<1||msg_avg_income<1||msg_avg_size<1||t_limit<1))//chek all input
cout<<endl<<"Error in input paramter,all parameter must be >0"<<endl ; //parameter >0
//check if any of the data parmeters for the simulation is < 0
else break;//the data parameters is in range contiue to next stage
}
}
cout<<"----------------Simulation Starting-------------------------";
Router rtr(link_voluem,link_speed,number_of_links,len_of_queue);//build a new Router object
evant_msg_in=rand()%(msg_avg_income*2)+1;//calculate the time of first message arrival
while (!_kbhit()&&t<t_limit)
//evant process
{
evant_msg_pop=rtr.min_time(link_i);
if (evant_msg_pop<0||evant_msg_pop>evant_msg_in)
//in case evant message arrive <evant meassage pop ,or the Router is empty of messages(all the queues is empty)
//(if Router is empty rtr.min_time(link_i) will return number<0 )
{
t+=evant_msg_in;//add evant time to golbal clock
msg.in(rand()%(number_of_links),rand()%(msg_avg_size*2)+link_speed,t);//build new message(minimum message size=link speed)
Console::ForegroundColor =ConsoleColor::Yellow;//change color print yellow
cout<<endl<<"New Message arrive t = "<<t<<" Service time = "<<msg.size/link_speed<<" Link = "<<msg.path;
rtr.time_update(evant_msg_in);//update time to service in the Router by evant message arrival(if necessary)
if (rtr.push_msg(msg)==0) {Console::ForegroundColor =ConsoleColor::Red;cout<<endl<<"Message Dumped !!!! t= "<<t<<" Link = "<<msg.path;dumped_messages++; }
//in case push message didnt success print dumped message !!!
t_messages++;//total incomeing messages
evant_msg_in=rand()%(msg_avg_income*2)+1;//calculate the time of next message arrival
}
else
//in case evant message arrive >=evant meassage pop and the Router is not empty of messages
{
t+=evant_msg_pop;//add evant time to golbal clock
evant_msg_in-=evant_msg_pop;//update evant message arrival by evant message pop
rtr.time_update(evant_msg_pop);//update the service time array by evant message pop
if ( rtr.pop_msg(msg,link_i)==0){cout<<endl<<"Error Pop message t= "<<t<<" Link = "<<msg.path;break;}//pop message failed
f_messages++;//total forward messages
msg.time=t-msg.time;//store delay time in the message(end clock-start clock)
total_delay+=msg.time;//total delay of all messages forward
total_delay_normal+=((float)msg.time/msg.size);//total (delay normal to size) of all messages forward
Console::ForegroundColor =ConsoleColor::Cyan;
cout<<endl<<"Message Finished Service t ="<<t<<" Delay time = "<<msg.time<<" Link = "<<msg.path;
}
}
Console::ForegroundColor =ConsoleColor::White;
cout<<endl<<"-------------------------Simulation End---------------------";
cout<<endl<<"-------------------------Simulation Resultes----------------";
cout<<endl<<"Total Meassages = "<<t_messages;
cout<<endl<<"ForWard Meassages = "<<f_messages;
cout<<endl<<"Dumped Messages = "<<dumped_messages;
cout<<endl<<"Average Delay time per Meassage = "<<total_delay/(f_messages>0?f_messages:1);//(messages>0?messages:1, in case messages=0)
cout<<endl<<"Average Delay/Size per Meassage = "<<total_delay_normal/(f_messages>0?f_messages:1);
cout<<endl<<"--------------Data Simulation Parameters---------------------"<<endl;
cout<<endl<<"Number of Outgoing Links = "<<number_of_links;
cout<<endl<<"Speed of link = "<<link_speed;
cout<<endl<<"Length of Queue in the Link = "<<len_of_queue;
cout<<endl<<"Size of Link (bit) = "<<link_voluem;
cout<<endl<<"Average Arrival Of Messages Per Unit Time = "<<msg_avg_income;
cout<<endl<<"Average Size Of Messages (bit) = "<<msg_avg_size;
cout<<endl<<"Time limit of simulation = "<<t_limit<<endl;
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -