-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenBucket.m
38 lines (38 loc) · 1.04 KB
/
TokenBucket.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
classdef TokenBucket < handle
properties
Tn {mustBeNumeric}
M {mustBeNumeric}
L {mustBeNumeric}
Q {mustBeNumeric}
Fcn
end
methods
function bucket = TokenBucket(Tn, M, L, Q, Fcn)
if nargin ~= 5
disp('Missing argument')
return
end
bucket.Tn = (Tn >= M) * M + (Tn < M) * Tn;
bucket.M = M;
bucket.L = L;
bucket.Q = Q;
bucket.Fcn = Fcn;
end
function Arrive(bucket)
if (bucket.Tn > 0)
bucket.Tn = bucket.Tn - 1;
bucket.Fcn();
elseif (bucket.Tn == 0 && bucket.Q < bucket.L)
bucket.Q = bucket.Q + 1;
end
end
function ReplinishToken(bucket)
if (bucket.Q > 0)
bucket.Q = bucket.Q -1;
bucket.Fcn();
elseif (bucket.Q == 0 && bucket.Tn < bucket.M)
bucket.Tn = bucket.Tn + 1;
end
end
end
end