summaryrefslogtreecommitdiff
path: root/src/code/2/pingpong.exs
diff options
context:
space:
mode:
Diffstat (limited to 'src/code/2/pingpong.exs')
-rw-r--r--src/code/2/pingpong.exs26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/code/2/pingpong.exs b/src/code/2/pingpong.exs
new file mode 100644
index 0000000..9d05b38
--- /dev/null
+++ b/src/code/2/pingpong.exs
@@ -0,0 +1,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