forked from kenny4uk/DSRC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalg_onoe1.m
executable file
·184 lines (161 loc) · 7.7 KB
/
alg_onoe1.m
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
function alg_onoe1
global Sim App Mac Phy Rate Arf Onoe;
global Pk St Trace_time Trace_rate Trace_sc Trace_fc Trace_fail Trace_col Trace_suc Trace_per Static;
par_init;
% Simulation stops when all packets have been transmitted. Each iteration corresponds to a transmission attempt
Sim.tstart = clock;
Sim.time = 0.0; % simulation time
Sim.ratetime=0.0;
t=0;
x_max=1000;
v=0;
old_pos=0;
Phy.Ts=0.0;
while sum([Pk.suc])<=Sim.pk,
% Rate.level
% Phy.snr_per
% pause;
if (Sim.time-Sim.ratetime)>Onoe.period
% Rate.level
Sim.ratetime=Sim.time; % - mod(Sim.time, Onoe.period);
% process the link rate adaptation for every Onoe.period.
for ii=1:Sim.n
if (Onoe.win_nretry(ii)>Onoe.ratedec_retthr* Onoe.win_tx_all(ii)) & (Onoe.win_tx_all(ii)>=10) % | (Onoe.win_tx==0 & Onoe.win_nretry>0 ))
if 0
Sim.time
ii
Rate
Onoe
pause;
end
if Rate.level(ii)>1
Rate.level(ii)=Rate.level(ii)-1;
Rate.curr(ii)=Rate.set(Rate.level(ii));
Onoe.credit(ii)=0;
end % if Rate.curr(ii)
end % if (Onoe.win_nretry
if Onoe.win_nretry(ii)>=(Onoe.creditinc_retthr*Onoe.win_tx_all(ii))
if Onoe.credit(ii)>0; Onoe.credit(ii)=max(0,Onoe.credit(ii)-1); end;
elseif Onoe.win_nretry(ii)<(Onoe.creditinc_retthr*Onoe.win_tx_all(ii))
Onoe.credit(ii)=Onoe.credit(ii)+1;
end % if (Onoe.win_retired
if Onoe.credit(ii)>=Onoe.rateinc_creditthr;
if Rate.level(ii)<Rate.level_max;
Rate.level(ii)=Rate.level(ii)+1;
Rate.curr(ii)=Rate.set(Rate.level(ii));
Onoe.credit(ii)=0;
end
end
Onoe.win_tx_all(ii)=0; % including both new and retransmitted packets.
Onoe.win_tx(ii)=0; % new packets only
Onoe.win_nretry(ii)=0;
Onoe.win_retried(ii)=0;
end % for ii=1:n
end % if Sim.time
% process the transmission events
if (rem(sum([Pk.tx]),10000)==0) & 0,
deltaT= etime(clock,Sim.tstart);
disp(['Expected time to conclusion: ', num2str(round(deltaT/sum([Pk.suc])*(Sim.pk- sum([Pk.suc])))),' sec...'])
end; % if rem...
dt_temp = min(Mac.Bk_cnt); % Txnode = IDs of the nodes that attempt the transmission
Phy.Ts=0.0001;
Txnode = find(Mac.Bk_cnt==dt_temp); % find the time of the first transmission attempt
Mac.Bk_cnt=Mac.Bk_cnt-dt_temp-1; % all backoff counters are decremented
Sim.time = Sim.time+ dt_temp*Phy.sigma; % update the simulation time accordingly
sTxnode = length(Txnode); % sTxnode = number of simultaneously transmitting nodes
w=p_mob(Phy.Ts,v,old_pos,x_max);
Pk.tx(Txnode)=Pk.tx(Txnode)+1;
old_pos=w;
Onoe.win_tx_all(Txnode)=Onoe.win_tx_all(Txnode)+1;
% we distringuish two possible events at this slot time
if sTxnode>1 % if sTxnode > 1 => Collision occurs
St.fail(Txnode)=1;
St.col(Txnode)=1;
Pk.col(Txnode)= Pk.col(Txnode)+ 1; % total number of collided packets is updated;
Phy.Tc(Txnode)=(Phy.Lc_over+ 8*App.lave)./Rate.curr(Txnode);
Pk.power(Txnode)=Pk.power(Txnode)+Phy.Tc(Txnode)*Phy.power;
maxTc=max(Phy.Tc(Txnode)); % we need to know how long the collision is going to last
Sim.time= Sim.time + maxTc; % and update the simulation time subsequently
elseif sTxnode==1
% process BER and check if pkt can be accepted due to ber.
Bper=0;
Per_temp= Phy.snr_per(Rate.level(Txnode));
if rand()<Per_temp; Bper=1; end;
if Bper==1
St.fail(Txnode)=1;
St.col(Txnode)=0;
St.per(Txnode)=1;
w=p_mob(Phy.Ts,v,old_pos,x_max);
Pk.per(Txnode)=Pk.per(Txnode)+1;
old_pos=w;
Phy.Tc(Txnode)=(Phy.Lc_over+8*App.lave)./Rate.curr(Txnode); % how long does it take to transmit it with success?
Pk.power(Txnode)=Pk.power(Txnode)+Phy.Tc(Txnode)*Phy.power;
Sim.time = Sim.time + Phy.Tc(Txnode); % update the simulation time
else % if sTxnode == 1 & Bper==0 => Successfull transmission occurs
St.fail(Txnode)=0;
St.col(Txnode)=0;
St.per(Txnode)=0;
Phy.Ts=0.002;
Pk.suc(Txnode)= Pk.suc(Txnode)+1; % update number of sent packets
Phy.Ts(Txnode)=(Phy.Ls_over+8*App.lave)./Rate.curr(Txnode); % how long does it take to transmit it with success?
Pk.bit(Txnode)=Pk.bit(Txnode)+8*App.lave;
Pk.power(Txnode)=Pk.power(Txnode)+Phy.Ts(Txnode)*Phy.power;
w=p_mob(Phy.Ts,v,old_pos,x_max);
Sim.time= Sim.time + Phy.Ts(Txnode); % update the simulation time
% ws(Pksuc) = Sim.time-birthtime(Txnode); % compute the service time of this packet
App.birthtime(Txnode)= Sim.time; % and store the time this packet entered service
end; % if Bper
end % if sTxnode>1
for ii=1:sTxnode
iTx=Txnode(ii);
Rate.timer(iTx)=Rate.timer(iTx)-1;
Trace_time(iTx).list=[Trace_time(iTx).list Sim.time(iTx)];
Trace_rate(iTx).list=[Trace_rate(iTx).list Rate.level(iTx)];
Trace_fail(iTx).list=[Trace_fail(iTx).list St.fail(iTx)];
Trace_col(iTx).list=[Trace_col(iTx).list St.col(iTx)];
Trace_per(iTx).list=[Trace_per(iTx).list St.per(iTx)];
check_more_pk=0;
if Mac.nRetry(iTx)==0;
Onoe.win_tx(iTx)=Onoe.win_tx(iTx)+1;
% disp('tx+1');
end
if St.fail(iTx)==0
check_more_pk=1;
else % if St_tx(Txnode...
Mac.nRetry(iTx)=Mac.nRetry(iTx)+1;
Onoe.win_nretry(iTx)=Onoe.win_nretry(iTx)+1;
if Mac.nRetry(iTx)==1;
Onoe.win_retried(iTx)=Onoe.win_retried(iTx)+1;
end
if Mac.nRetry(iTx)>Mac.nRetry_max
check_more_pk=1;
Pk.drop(iTx)=Pk.drop(iTx)+1;
else
Mac.W(iTx)=min(Mac.Wmin*2^Mac.nRetry(iTx), Mac.Wmax);
Mac.Bk_cnt(iTx)=floor(rand()*Mac.W(iTx));
end % if Mac.nRetry>Ret_thr
end
if check_more_pk==1
if 1 % if more pk available in queue
Mac.nRetry(iTx)=0;
Mac.W(iTx)=Mac.Wmin;
Mac.Bk_cnt(iTx)=floor(rand()*Mac.W(iTx));
else
Mac.Bk_cnt(iTx)=10^20;
end
end % if check_more_pk
end % for iTx
end; % while sum(Pksuc)<n*mpck,...,end
Static.pk_col = sum([Pk.col])/( sum([Pk.tx])); % collision probability
Static.pk_suc = sum([Pk.suc])/( sum([Pk.tx])); % collision probability
Static.pk_per = sum([Pk.per])/( sum([Pk.tx])); % collision probability
Static.through=sum([Pk.suc])*App.lave*8/Sim.time; % average throughput.
Static.energyeff=sum([Pk.power])/sum([Pk.bit]); % average energy efficiency.
if Sim.debug_onoe_sim==1
figure(2);
for ii=1:Sim.n;
plot(Trace_time(ii).list, Trace_rate(ii).list);
pause;
end; % hold on; end; hold off;
end
return;