summaryrefslogtreecommitdiff
path: root/src/code/2/pingpong.exs
blob: 9d05b38af24a9c30d1b8d44ba85db3f3c4bad343 (plain)
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
defmodule PingPong do
  def start_link do
    {:ok, spawn_link(fn -> loop() end)}
  end

  defp loop do
    receive do
      {:ping, sender} ->
        IO.puts "[PingPong]: Received Ping"
        IO.puts "[PingPong]: Sending Pong..."
        send sender, {:pong, self}
    end
    loop
  end
end

{:ok, pid} = PingPong.start_link
IO.puts "Sending Ping..."
send pid, {:ping, self}

receive do
  {:pong, ^pid} ->
    IO.puts "Received Pong"
  after 5000 ->
    IO.puts "Never Received Pong"
end