forked from ValckenaersPaul/TAGP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.erl
41 lines (34 loc) · 1.05 KB
/
msg.erl
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
-module(msg).
-define(TimeOut, 5000).
-export([get/2, get/3, set_ack/2, set_ack/3]).
-export([test/0]).
get(Pid, Key) ->
Pid ! {Key, replier(R = make_ref())},
receive
{R, Info} -> {ok, Info}
after ?TimeOut -> {error, timed_out, Pid, Key, R}
end.
get(Pid, Key, P_list) ->
Pid ! {Key, P_list, replier(R = make_ref())},
receive
{R, Info} -> {ok, Info}
after ?TimeOut -> {error, timed_out, Pid, Key, R}
end.
set_ack(Pid, Key) -> % identical to get; for readability only.
Pid ! {Key, replier(R = make_ref())},
receive
{R, Info} -> {ok, Info}
after ?TimeOut -> {error, timed_out, Pid, Key, R}
end.
set_ack(Pid, Key, P_list) -> % identical to get; for readability only.
Pid ! {Key, P_list, replier(R = make_ref())},
receive
{R, Info} -> {ok, Info}
after ?TimeOut -> {error, timed_out, Pid, Key, R}
end.
replier(Ref) ->
Sender = self(),
fun(Msg) -> Sender ! {Ref, Msg} end.
test() ->
Pid = spawn(fun() -> receive {_Dummy, [P | _ ], F } -> F(2 * P) end, ok end),
msg:set_ack(Pid, dummy, [10]).