📄 gobackn.m
字号:
function [Umas,Pak,Ack]=gobackn(amas,Nsimul,Ploss,SWS);
% function [Umas,Pak,Ack]=gobackn(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
%
% 15/11/01
% A. Litvin
% e-mail: litvin@bu.edu
%
if length(Ploss)>1
error('Ploss size');
end
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=[];
Ack=[];
time=0;
framen=0;
U=0;
Maxfrn=SWS+1;
LFR=-1;
LFA=-1;
LFS=-1;
busysender=0;
stackarrive=[0 0]; stackarrive(1,:)=[];
stackack=[0 0]; stackack(1,:)=[];
stacktimeout=[0 0]; stacktimeout(1,:)=[];
timestep=min(Tprop,Ttran)/50;
Timeout=Ttran+2*Tprop+3*timestep;
while LFS<Nsimul
% disp([time LFS LFA]);
% disp(stacktimeout)
% resend packets if timeout
ind=find(stacktimeout(:,2)<=time);
if length(ind)
nopak=stacktimeout(1,1);
if time>=busysender
busysender=time+Ttran;
stacktimeout=[nopak time+Timeout];
framen=rem(nopak,Maxfrn);
gotit=rand>Ploss;
LFS=nopak;
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
busysender=time+Ttran;
LFS=LFS+1;
stacktimeout=[stacktimeout; LFS time+Timeout];
framen=rem(LFS,Maxfrn);
gotit=rand>Ploss;
if gotit
Pak=[Pak; LFS time time+Tprop+Ttran framen];
stackarrive=[stackarrive; LFS time+Tprop+Ttran];
else
Pak=[Pak; LFS time Inf framen];
end
end
% check of arrival at receiver
ind=find(stackarrive(:,2)<=time);
if length(ind)
newpak=stackarrive(1,1);
if newpak==LFR | newpak==LFR+1 | newpak<LFR
if newpak>=LFR LFR=newpak; end;
framen=rem(newpak,Maxfrn);
gotack=1;
if gotack
Ack=[Ack; newpak time time+Tprop framen];
stackack=[stackack; newpak time+Tprop];
else
Ack=[Ack; newpak time Inf framen];
end
else
gotack=1;
framen=rem(LFR,Maxfrn);
if gotack
Ack=[Ack; LFR time time+Tprop framen];
stackack=[stackack; LFR time+Tprop];
else
Ack=[Ack; LFR time Inf framen];
end
end
stackarrive(1,:)=[];
end
% check of arrival of ack
ind=find(stackack(:,2)<=time);
if length(ind)
newack=stackack(1,1);
if newack==LFA | newack==LFA+1
LFA=newack;
ind=find(stacktimeout(:,1)==LFA);
if length(ind)
stacktimeout(ind,:)=[];
end;
end
stackack(1,:)=[];
end
time=time+timestep;
end;
Umas(j)=LFS*Ttran/time;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -