-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingpong.erl
executable file
·63 lines (50 loc) · 1.16 KB
/
pingpong.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
%%%-------------------------------------------------------------------
%%% @author andrz
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 07. Apr 2020 7:22 PM
%%%-------------------------------------------------------------------
-module(pingpong).
-author("andrz").
%% API
-export([start/0, stop/0, play/1, pingFun/0, pongFun/0]).
start() ->
register (ping, spawn (?MODULE, pingFun, [])),
register (pong, spawn(?MODULE, pongFun, [])),
ok.
play(N) ->
pong ! start,
ping ! N,
ok.
stop() ->
ping ! stop,
pong ! stop.
pingFun() ->
receive
stop -> ok;
N -> sendNMsg(N), pingFun()
end.
pongFun() ->
receive
stop -> ok;
start -> receiveAndResponse(), pongFun()
end.
sendNMsg(0)-> pong ! term, ok;
sendNMsg(N) ->
io:format("Ping sending: ~B~n",[N]),
pong ! N,
receive
M->io:format("Ping received: ~B~n~n",[M]),
timer:sleep(1000),
sendNMsg(N-1)
end.
receiveAndResponse()->
receive
term -> ok;
N->io:format("Pong received: ~B~n",[N]),
io:format("Pong sending: ~B~n",[N]),
ping ! N,
receiveAndResponse()
end.