function [Umas,Pak,Ack]=selectiverepeat(amas,Nsimul,Ploss,SWS);
%function [Umas,Pak,Ack]=gobackn(amas,Nsimul,Ploss,SWS);
% function [Umas,Pak,Ack]=selectiverepeat(amas,Nsimul,Ploss,SWS);
%
% GoBackN algoritnm simulation
% MaxSeqNum=SWS+1
%
%
% Input:
%
% amas - t_prop/t_transm - can be an array, in which case
% simulation is repeated for each value of an array and
% cirresponding utilization is recorded in corresponding element of Umas
%
% Nsimul - number of sent frames to simulate
% time required is proportional to Nsimul
% quality of utilization estimates improves with Nsimul
% recommended Nsimul=100;
%
% Ploss - probability of packet loss (or receiving with an error)
% this probability is assumed the same for data packets and acknowledgments
%
% SWS - sender window size
% note: receiver window size - 1 in this algorith
%
% Output:
%
% Umas - vector containing channel utilization estimates for values of a
% in amas parameter. Length(Umas)=Length(amas)
%
% In case of the length(amas)>1 the following 2 output parameters
% output the result for the simulation for the last entry of vector amas
%
% Pak - matrix, each raw contains a signature of the simulated data packet sent.
% structure of the raw: (Pak# Time_sent Time_received sequence#)
% Time_received = Inf if the packet is lost
%
% Ack - matrix, each raw contains a signature of the simulated acknowledgment
% packet sent.
% structure of the raw: (Ack# Time_sent Time_received sequence#)
% Time_received = Inf if the ack is lost
%
if Ploss<0 || Ploss>1
error('Ploss not in limits [0,1]');
end
if SWS<1
error('wrong SWS');
end
Umas=[];
for j=1:length(amas)
a=amas(j);
Tprop=1;
Ttran=Tprop/a;
Pak=[];
% Pak - matrix, each raw contains a signature of the simulated data packet sent.
% structure of the raw: (Pak# Time_sent Time_received sequence#)
% Time_received = Inf if the packet is lost
Ack=[];
% Ack - matrix, each raw contains a signature of the simulated acknowledgment
% packet sent.
% structure of the raw: (Ack# Time_sent Time_received sequence#)
% Time_received = Inf if the ack is lost
time=0;
framen=0;
U=0;
Maxfrn=floor(2*SWS-1); %for SR SWS<(MaxSeqNum+1)/2 (definition)
LFR=-1; %last frame recv
LFA=-1; %last frame acked
LFS=-1; %last frame sent
busysender=0;
stackarrive=[0 0];
stackarrive(1,:)=[];
stackack=[0 0];
stackack(1,:)=[];
stacktimeout=[0 0]; %every packed goes in timeout array until it is received, then overwritten
stacktimeout(1,:)=[];
timestep=min(Tprop,Ttran)/50;
Timeout=Ttran+2*Tprop+3*timestep; %make timeout be slightly greater than the normal time to send packet and receive ack
paknum = 0;
%----------------------------------------------------------
time2=time;
while paknum<Nsimul
time=time2;
for i=1:Maxfrn
test = rand>Ploss;
framen=rem(paknum,Maxfrn);
if test
Pak = [Pak; paknum time time+Ttran+Tprop framen];
else
Pak = [Pak; paknum time Inf framen];
Pak = [Pak; paknum time+Timeout time+2*Timeout-Tprop framen];
end
time=time+Ttran;
time2=time+Tprop;
paknum=paknum+1;
i=i+1;
end
for j=paknum-Maxfrn+1:paknum
Ack = [Ack; Pak(j,1) Pak(j,3) Pak(j,3)+Tprop Pak(j,4)];
j=j+1;
end;
end;
Umas(1)=LFS*Ttran/time;
end;
Nsimul=busysender-1;
while paknum<Nsimul %iterate until all frames have been sent (Nsimul=total frames, LFS=last frame sent)
framen=rem(paknum,Maxfrn);
test = rand>Ploss;
if test
Pak = [Pak; paknum time time+Ttran+Tprop framen];
else
Pak = [Pak; paknum time Inf framen];
Pak = [Pak; paknum time+Timeout time+2*Timeout-Tprop framen];
end
time=time+Ttran+Tprop;
paknum=paknum+1;
end;
i=1;
while i<=length(Pak)
Ack = [Ack; Pak(i,1) Pak(i,3) Pak(i,3)+Tprop Pak(i,4)];
i=i+1;
%end;
Umas(1)=LFS*Ttran/time;
end;
%function [Umas,Pak,Ack]=selectiverepeat(amas,Nsimul,Ploss,SWS);
while LFS<Nsimul %iterate until all frames have been sent (Nsimul=total frames, LFS=last frame sent)
% disp([time LFS LFA]);
% disp(stacktimeout)
% resend packets if timeout
ind=find(stacktimeout(:,2)<=time);% find() this returns the packet that just timed out
%ind is an array of timedout (row)
if length(ind)
nopak=stacktimeout(1,1);
if time>=busysender
busysender=time+Ttran; %beginning to send and gives time for when time is up
stacktimeout=[nopak time+Timeout]; %overwri
framen=rem(nopak,Maxfrn); %assign the packet a frame number 0 sws
gotit=rand>Ploss;
LFS=nopak; %TO DELETE
if gotit
Pak=[Pak; nopak time time+Tprop+Ttran framen];
stackarrive=[stackarrive; nopak time+Tprop+Ttran];
else
Pak=[Pak; nopak time Inf framen];
end
end
end
% if no timeout we can send new packet
if time>=busysender & LFS<LFA+SWS % not transimitting, and can fit in receiver window
busysender=time+Ttran; %start transmitting next packet
LFS=LFS+1; %indicate that you sent packet
stacktimeout=[stacktimeout; LFS time+Timeout]; %%appending and indicating when it will get timedout
framen=rem(LFS,Maxfrn); % assign packet a frame number
gotit=rand>Ploss; % see if the packet was recieved
if gotit
Pak=[Pak; LFS time time+Tprop+Ttran framen]; %if you received it record in PAK array [packet, sent time , arrival time, frame#)
stackarrive=[stackarrive; LFS time+Tprop+Ttran]; %updating the the packet you receive and the time you received it
else
Pak=[Pak; LFS time Inf framen]; %if you don't receive it then go to the PAK array and set to INF
end
end
% check of arrival at receiver
ind=find(stackarrive(:,2)<=time); % return packet (row) which has arrived (time=time+prop+tran)
if length(ind) %if there are any packet arrivals
newpak=stackarrive(1,1); %if arrival take the first one and update in stack arrive
% if newpak==LFR | newpak==LFR+1 | newpak<LFR
if newpak<LFR+SWS
if newpak>=LFR LFR=newpak;
end;
framen=rem(newpak,Maxfrn);%assign the packet a frame number
gotack=rand>Ploss;
if gotack
Ack=[Ack; newpak time time+Tprop framen];%ack being sent with packet # and timesent time received frame number
stackack=[stackack; newpak time+Tprop]; %ack is in array set for timed simulated arrival
else
Ack=[Ack; newpak time Inf framen]; %this means that ack is in array with inf simulating lost
end
else %if there was no arrived packet
gotack=rand>Ploss;
framen=rem(LFR,Maxfrn);%this is the ack number
if gotack
% Ack=[Ack; LFR time time+Tprop framen]; % packet
% ack, time
% packe