1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
#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;
}
|