selectiverepeat.m~

来自「本程序是用java编写的一些关于arq协议实现的小程序」· M~ 代码 · 共 502 行

M~
502
字号
% Tuesday 10-28
% focus on why it receiver still sends acks for the last correctly received
% packet




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
%
% 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=[];
    % 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;

% 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;

%----------------------------------------------------------
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;
if(SWS==5)
Pak = [0	0	Inf	0
1	2	5	1
2	4	7	0
0	6.02	9.02	2
3	8.04	11.04	3
4	10.06	Inf	4
5	12.08	15.08	5
6	14.1	17.1	4
4	16.12	19.12	6
7	18.14	21.14	7
8	20.16	23.16	8
9	22.18	25.18	0
10	24.2	27.2	1
11	26.22	29.22	2
12	28.24	31.24	3
13	30.26	33.26	4
14	32.26	35.26	5
15	34.26	37.26	6
16	36.26	39.26	7
17	38.26	41.26	8
18	40.26	43.26	0
19	42.26	45.26	1
20	44.26	47.26	2
21	46.26	49.26	3
22	48.26	Inf	4
23	50.26	53.26	1
24	52.26	Inf	2];


Ack = [-1	5.02	6.02	-1
-1	7.02	8.02	-1
0	9.04	10.04	0
1	11.06	12.06	1
1	15.1	16.1	1
1	17.12	18.12	1
2	19.14	20.14	2
3	21.16	22.16	3
4	23.18	24.18	4
5	25.2	26.2	5
6	27.22	28.22	0
7	29.24	30.24	1
8	31.26	32.26	2
9	33.26	34.26	3
10	35.26	36.26	4
11	37.26	38.26	5
12	39.26	40.26	0
13	41.26	42.26	1
14	43.26	44.26	2
15	45.26	46.26	3
16	47.26	48.26	4
17	49.26	50.26	5];
end


if(SWS == 2)
   Pak = [0	0	3	0
1	2	5	1
2	4	7	2
3	6.02	9.02	3
4	8.04	11.04	0
5	10.06	Inf	1
6	12.08	Inf	2
5	14.1	17.1	1
6	16.12	19.12	2
7	18.14	21.14	3
8	20.16	23.16	0
8	22.18	Inf	3
9	24.2	27.2	0
10	26.22	29.22	1
8	28.24	31.24	2
9	30.26	33.26	3
10	32.26	35.26	0
11	34.26	37.26	1
12	36.26	Inf	0
13	38.26	Inf	1
14	40.26	43.26	2
12	42.26	45.26	0
13	44.26	47.26	1
14	46.26	49.26	2
15	48.26	51.26	3
16	50.26	53.26	4
17	52.26	Inf	5
18	54.26	Inf	0
19	56.26	Inf	1
17	58.26	61.26	5
18	60.26	63.26	0
19	62.26	65.26	1
20	64.26	67.26	2]; 
   
   Ack = [0	3	4	0
1	5.02	6.02	1
2	7.02	8.02	2
3	9.04	10.04	3
4	11.06	12.06	4
4	17.12	18.12	4
5	19.14	20.14	5
6	21.16	22.16	0
7	23.18	24.18	1
7	27.22	28.22	1
7	29.24	30.24	1
8	31.26	32.26	2
9	33.26	34.26	3
10	35.26	36.26	4
11	37.26	38.26	5
11	43.26	44.26	5
12	45.26	46.26	0
13	47.26	48.26	1
14	49.26	50.26	2
15	51.26	52.26	3
16	53.26	54.26	4
17	61.26	62.26	5
18	63.26	64.26	0];

end
    


function [Umas,Pak,Ack]=selectiverepeat(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
%
% 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=[];
%     % 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
% 
% 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
%                                                                 % packet act,  time  received, ack framenumber
%                                                                 % and replaced with next line  
%                Ack=[Ack; newpak time time+Tprop framen];
%                %%xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
%                stackack=[stackack; LFR time+Tprop];              
%                %xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
%                
%             else
%               % Ack=[Ack; LFR time Inf framen];                 %took out
%                                                                 % and replaced with next line
%               Ack=[Ack; newpak time Inf framen]; % ack is not received and put it into the ACK's
%             end 
%         end         
%           stackarrive(1,:)=[]; %this clears out the packet that was received 
%       end
%       
%       % check of arrival of ack
%       
%       ind=find(stackack(:,2)<=time);% find the acks that have been received time is up, return row number
%       if length(ind)    %if there was one recieved
%          newack=stackack(1,1);  % them most recent recieved ack
%          if newack==LFA | newack==LFA+1 %if you recieved the LFA or the 
%             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 + =
减小字号Ctrl + -
显示快捷键?