04-12-2012, 05:52 PM
Below is the code for Go Back N ARQ. I need to implement the same code using selective repeat with different timer(real time) for each frame and a case where negative acknowledgement is sent by reciever.Please go through the code and post appropriate addition for Selective repeat ARQ
Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class sender
{
public :
int size,sf,sn,t,a[500],q[500],w,ff,sent ;
sender(){}//default constructor
sender(int x,int aa)//parameterised constructor
{
ff=aa;//ff and aa = total no of frames
size=pow(2,x)-1;//size of sliding window
sn=0;//next frame to sent
sf=0;//first outstanding frame
w=0;//count of frames
if(aa<=5)
t=2;//timer (decremented by 1 after a frame is sent)
else
t=5;//timer (decremented by 1 after a frame is sent)
sent=0;
for(int i=0;i<aa;i=i+size+1)//to assign sequence no to frames
{
for(int j=0;j<=size;j++)
{
if(i+j < aa)
a[i+j]=j;
}
}
}
void reqstsend()//to send frames
{
if(w<ff)//check whether the no. of frame sent is less than total no of frames
{
if(t<=0 )//check for time out
{
cout<<endl<<"Caution : Timer out"<<endl;
timeout();
}
else if((sn-sf)>=size || sf==sn+1 )
{
cout<<endl<<"Caution : Window is full"<<endl;
t--;
}
/*else if(sn == size)//special case when sn==size sn should be =0
{
cout<<endl<<"frame F"<<sn<<" sent..."<<endl;
sn=0;
t--;
w++;
sent++;
show();
cout<<endl<<"frames sent :- ";
for(int i=0;i<w;i++)
{
cout<<" "<<a[i]<<" ";
}
}*/
else//normal sending
{
cout<<endl<<"Frame F"<<sn<<" sent..."<<endl;
sn++;
t--;
w++;
sent++;
show();
cout<<endl<<"Frames sent :- ";
for(int i=0;i<w;i++)
{
cout<<" "<<a[i]<<" ";
}
}
}
else
cout<<endl<<" ***All Frames Sent***"<<endl;
}
void arivack(int y)//recieving acknowledgement
{
sf=y;
t=sn-sf;
}
void timeout()
{
sn=sf;
if(ff<5)
{
t=2;
w=w-2;
sent=sent-2;
}
Else
{
t=5;
w=w-5;
sent=sent-5;
}
}
void sdisplay()//displaying frames initially
{
cout<<endl<<"Frames :- ";
for(int i=0;i<ff;i++)
cout<<" "<<a[i]<<" ";
cout<<endl<<endl<<"sn = "<<sn<<endl<<"sf = "<<sf<<endl;
}
void show()
{
cout<<endl<<"No of frames sent ="<<sent<<endl;
}
};//class sender ends
class reciever : public sender
{
public :
int rn,b[500],c,reci;
sender *s;//sender class pointer
reciever(sender *y,int q)//contructor to initialise variables and element of array
{
reci =0;
rn=size;//next expected frame
c=0;//counter
for(int i=0;i<q;i++)
b[i]=0;
s=y;
}
void arivnot(int x)
{
if(rn == x)//recieve in order
{
if(rn != s->size)//recieving frames
{
b[c]=rn;
c++;
rn++;
}
else//special case when rn =size, then rn should be =0
{
b[c]=rn;
c++;
rn=0;
}
}
}
void sendack(int z)
{
cout<<endl<<"Acknowledgement A"<<z<<" sent"<<endl;
s->arivack(z);
}
void sendlostack(int z)
{
cout<<endl<<"Acknowledgement A"<<z<<" sent"<<endl;
}
void rdisplay()
{
cout<<endl<<"No of frames recieved ="<<c<<endl;
cout<<endl<<"Frames recieved :- ";
for(int i=0;i<c;i++)
cout<<" "<<b[i]<<" ";
cout<<endl<<endl<<"rn = "<<rn<<endl<<endl;
}
};//class reciever ends
int main()
{
int m,x=3,h,y,counter=0;
cout<<endl<<"Enter the value of sequence no. (m no of bits) => ";
cin>>m;
cout<<"Sliding window size ="<<pow(2,m-1)<<endl;
cout<<"Recieving window size ="<<pow(2,m-1)<<endl;
cout<<"Enter the total no of frames you want to send "<<endl;
cin>> h;
sender pqr(m,h),*g=&pqr;
reciever rec(g,h),*r=&rec;
while(1)
{
if(counter==0)
{
cout<<endl<<"Press 1 for going to sender side"<<endl
<<"Press 0 to exit"<<endl <<"enter code => ";
cin>>x;
if(x==2)
x=8;
counter++;
if(x!=1)
counter--;
}
else
{
cout<<endl<<"Press 1 for going to sender side"<<endl <<"Press 2 for going to receiver side"<<endl <<"Press 0 to exit"<<endl <<"enter code => ";
cin>>x;
}
switch(x)
{
case 1 : while(y != 3)
{
pqr.sdisplay();
cout<<endl<<"Press 1 to send frame"<<endl <<"Press 2 to send frame (lost or incorrect)"<<endl
<<"Press 3 to go back"<<endl<<" => ";
cin>>y;
switch(y)
{
case 1 :
pqr.reqstsend();
if(pqr.sn == 0)
r->arivnot(pqr.size);
else
r->arivnot(pqr.sn-1);
break;
case 2 : pqr.reqstsend();
break;
case 3 :
break;
default :
cout<<endl<<"Caution !!! error in code !!! please enter right choice "<<endl;
break;
}
}
y=0;
break;
case 2 : while(y != 3)
{
r->rdisplay();
cout<<endl<<"Press 1 to send Acknowledgement"<<endl
<<"Press 2 to send ack (lost)"<<endl
<<"Press 3 to go back"<<endl
<<" => ";
cin>>y;
switch(y)
{
case 1 :
r->sendack(rec.rn); // cout<<endl<<"no of frames sent ="<<pqr.sent<<endl;
break;
case 2:
r->sendlostack(rec.rn);
break;
case 3:
break;
default :
cout<<endl<<"Caution !!! error in code !!!"<<endl;
break;
}
};
y=0;
break;
case 0 :
exit(0);
break;
default :
cout<<endl<<"Caution !!! error in code !!!"<<endl;
break;
}
}
getch();
return 0;
}