No BSD License  

Event based Stop and Wait ARQ Simulation

by

 

04 Sep 2006 (Updated )

simulation of ARQ Techniques

newsim(dummy)
function newsim(dummy)

% event based TDMA network simulation

initialize;
%simulate;



% ===========================
function initialize

global stoptime
global starttime
global No_stations
global lambda
global TDM_frame_duration
global Tpropagation


sim_duration=1000; % sec
init_scheduler;
starttime=0;
stoptime=100;
Tpropagation=0.5;
No_stations=10;
lambda=0.1; % possion parameter
TDM_frame_duration=0.02; % sec.


%============================
function simulate

global starttime stoptime
global ScheduleAt

do=1;
while do==1,
    temp=[];
    temp=[temp, ScheduleAt.time]; % holds all schedule times
    [SimTime,i]=min(temp); % find 1st event to be done and its index
    if SimTime>stoptime,
        do=0; % stop simulation
    end
    ProcessEvent(ScheduleAt(i).time, ScheduleAt(i).event,ScheduleAt(i).Station_No)
end
%============================
function ScheduleAt=init_scheduler

global No_stations
global TDM_frame_duration
global ScheduleAt

ScheduleAt=struct('time',[],'event', [], 'Station_No', [])

% init traffic generators for each station
s_i=0; % schedulaeAt index
for i=1:No_stations,
    s_i=s_i+1;
    ScheduleAt(s_i).time=0.5.*rand(1);
    ScheduleAt(s_i).event='generate_packet';
    ScheduleAt(s_i).Station_No=i;
end
% init TDMA transmit times
s_i=0; % schedulaeAt index
% for i=1:No_stations,
%     s_i=s_i+1;
%     TDM_frame_duration./No_stations.*(i+1_
%     ScheduleAt(s_i).time=TDM_frame_duration./No_stations;
%     ScheduleAt(s_i).event='transmit_packet';
%     ScheduleAt(s_i).Station_No=i;
% end

%================================
function ProcessEvent(time, eventtype, Station)

global ScheduleAt

dummy1=0;

switch eventtype
    case 'generate_packet'
        newtime=new_packet_time(time);
        NewScheduledEvent(newtime, 'generate_packet',Station); % put a new packet generation event in scheduler
        Remove_Event_From_Scheduler(time, eventtype, Station)
        NewScheduledEvent(time, 'computeCRC',Station); % put a new packet generation event in scheduler
        dummy2=0;                                    
switch eventtype 
    case 'computeCRC'
    T_frame_transmission=ComputeCRC(time)
    NewScheduledEvent(newtime, 'compose frame',Station);
    Remove_Event_From_Scheduler(time, eventtype, Station)
    NewScheduledEvent(newtime, 'transmit_packet',Station
switch eventtype    
    case 'transmit_packet'
       Receive_time=Tx_packet(time);
         NewScheduledEvent(newtime, 'receive_packet',Station);
         Remove_Event_From_Scheduler(time, eventtype, Station)
        NewScheduledEvent(newtime, 'packet_received',Station
switch eventtype
    case 'receive packet'
        NewScheduledEvent(newtime, 'check frame',Station
        Remove_Event_From_Scheduler(time, eventtype, Station)
end
%===============================

function Remove_Event_From_Scheduler(time, eventtype, Station)
% this function removes event that have been processed by ProcessEvent

global ScheduleAt


N=length(ScheduleAt); % no of entries in the Scheduler
temp=[];
for i=1:N,
    temp=[temp ScheduleAt(i).time];
end
I=find(temp==time); % finds indx in temp equal to current time
ScheduleAt(I)=[];

 dummy=1;

%=============================.
 function Receive_time=Tx_packet(time)
% 
 global Tpropagation
% 
% Receive_time=time+Tpropagation;Receive_time=time+Tpropagation;
 global T_frame_Transmission
%=============================
function newtime =new_packet_time(time)

global lambda

newtime=exprnd(lambda)+time;

%=============================
function NewScheduledEvent(time, event, stationNo)

global ScheduleAt

s_i=size(ScheduleAt,2)+1;
ScheduleAt(s_i).time=time;
ScheduleAt(s_i).event=event;
ScheduleAt(s_i).Station_No=stationNo;
%==============================

Contact us