diff options
Diffstat (limited to 'step-04/tut17.erl')
-rw-r--r-- | step-04/tut17.erl | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/step-04/tut17.erl b/step-04/tut17.erl new file mode 100644 index 0000000..6808340 --- /dev/null +++ b/step-04/tut17.erl @@ -0,0 +1,31 @@ +-module(tut17). + +-export([start_ping/1, start_pong/0, ping/2, pong/0]). + +ping(0, Pong_Node) -> + {pong, Pong_Node} ! finished, + io:format("ping finished~n", []); + +ping(N, Pong_Node) -> + {pong, Pong_Node} ! {ping, self()}, + receive + pong -> + io:format("Ping received pong~n", []) + end, + ping(N - 1, Pong_Node). + +pong() -> + receive + finished -> + io:format("Pong finished~n", []); + {ping, Ping_PID} -> + io:format("Pong received ping~n", []), + Ping_PID ! pong, + pong() + end. + +start_pong() -> + register(pong, spawn(tut17, pong, [])). + +start_ping(Pong_Node) -> + spawn(tut17, ping, [3, Pong_Node]). |