-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathered_node_delay.erl
More file actions
131 lines (118 loc) · 3.45 KB
/
ered_node_delay.erl
File metadata and controls
131 lines (118 loc) · 3.45 KB
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
-module(ered_node_delay).
-behaviour(ered_node).
-export([start/2]).
-export([handle_msg/2]).
-export([handle_event/2]).
%%
%% Delay pauses the travels of a message by XX units of time.
%%
%%
%% "type": "delay",
%% "pauseType": "random",
%% "timeout": "2",
%% "timeoutUnits": "seconds",
%% "rate": "1",
%% "nbRateUnits": "1",
%% "rateUnits": "second",
%% "randomFirst": "1",
%% "randomLast": "5",
%% "randomUnits": "seconds",
%% "drop": false,
%% "allowrate": false,
%%
-import(ered_nodered_comm, [
node_status/5,
unsupported/3,
ws_from/1
]).
-import(ered_nodes, [
jstr/2,
send_msg_to_connected_nodes/2
]).
-import(ered_messages, [
convert_to_num/1,
convert_units_to_milliseconds/2
]).
-define(CNTSTATUS(CNT), node_status(ws_from(Msg), NodeDef, CNT, "blue", "dot")).
-define(TO_MILLIS(FName, TUnits),
convert_units_to_milliseconds(
maps:find(TUnits, NodeDef),
maps:find(FName, NodeDef)
)
).
%%
%%
start(NodeDef, _WsName) ->
ered_node:start(NodeDef, ?MODULE).
%%
%% this must return a millisecond value.
compute_pause({ok, <<"delay">>}, NodeDef, Msg) ->
ConversionResult = ?TO_MILLIS(<<"timeout">>, <<"timeoutUnits">>),
case ConversionResult of
{ok, V} ->
V;
{error, ErrMsg} ->
unsupported(NodeDef, Msg, ErrMsg),
0
end;
compute_pause({ok, <<"delayv">>}, NodeDef, Msg) ->
case maps:find(<<"delay">>, Msg) of
{ok, V} ->
%% msg.delay is assumed to be milliseconds --> RTFM.
convert_to_num(V);
_ ->
%% if the delay isn't set, then revert to the node configuration
compute_pause({ok, <<"delay">>}, NodeDef, Msg)
end;
compute_pause({ok, <<"random">>}, NodeDef, Msg) ->
Val1 = ?TO_MILLIS(<<"randomFirst">>, <<"randomUnits">>),
Val2 = ?TO_MILLIS(<<"randomLast">>, <<"randomUnits">>),
case {Val1, Val2} of
{{ok, V1}, {ok, V2}} ->
delay_random(V1, V2);
_ ->
unsupported(NodeDef, Msg, "not supported"),
0
end;
compute_pause(PType, NodeDef, Msg) ->
unsupported(NodeDef, Msg, jstr("PauseType: '~p'", [PType])),
0.
%%
%%
handle_event({registered, _WsName, _Pid}, NodeDef) ->
maps:put('_delay_counter', 0, NodeDef);
handle_event(_, NodeDef) ->
NodeDef.
%%
%%
handle_msg({delay_push_out, Msg}, NodeDef) ->
% Push message out through the delay node to maintain the delay
% counter even though it might well delay the message since its
% using the same message queue as incoming messages.
send_msg_to_connected_nodes(NodeDef, Msg),
DelayCnt = maps:get('_delay_counter', NodeDef) - 1,
?CNTSTATUS(integer_to_binary(DelayCnt)),
{handled, maps:put('_delay_counter', DelayCnt, NodeDef), Msg};
handle_msg({incoming, Msg}, NodeDef) ->
DelayCnt = maps:get('_delay_counter', NodeDef) + 1,
PauseFor = compute_pause(maps:find(<<"pauseType">>, NodeDef), NodeDef, Msg),
NodePid = self(),
spawn(fun() -> delay_sending(Msg, PauseFor, NodePid) end),
?CNTSTATUS(integer_to_binary(DelayCnt)),
{handled, maps:put('_delay_counter', DelayCnt, NodeDef),
dont_send_complete_msg};
handle_msg(_, NodeDef) ->
{unhandled, NodeDef}.
%%
%%
delay_sending(Msg, PauseFor, NodePid) ->
timer:sleep(PauseFor),
gen_server:cast(NodePid, {delay_push_out, Msg}).
%%
%%
delay_random(V1, V2) when V1 > V2 ->
rand:uniform(V1 - V2);
delay_random(V1, V2) when V2 > V1 ->
rand:uniform(V2 - V1);
delay_random(_V1, _V2) ->
0.