-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes-3.exs
57 lines (50 loc) · 1.41 KB
/
nodes-3.exs
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
# Exercise:Nodes-3
# Alter the code so that successive ticks are sent to each registered client
# (so the first goes to the client, the second to the next client and so on).
# Once the last client receives a tick, the process starts back at the
# first. The solution should deal with new clients being added at any time.
defmodule Ticker do
@interval 2000
@name :ticker
def start do
pid = spawn(__MODULE__, :generator, [[], 0, 0])
:global.register_name(@name, pid)
end
def register(client_pid) do
send :global.whereis_name(@name), {:register, client_pid}
end
def generator([], _, _) do
receive do
{:register, pid} ->
IO.puts "registering #{inspect pid}"
generator([pid], 1, 0)
end
end
def generator(clients, num_clients, index) when num_clients == index, do: generator(clients, num_clients, 0)
def generator(clients, num_clients, index) do
receive do
{:register, pid} ->
IO.puts "registering #{inspect pid}"
generator([pid | clients], num_clients + 1, index)
after
@interval ->
clients
|> Enum.at(index)
|> send({:tick})
generator(clients, num_clients, index + 1)
end
end
end
defmodule Client do
def start do
pid = spawn(__MODULE__, :receiver, [])
Ticker.register(pid)
end
def receiver do
receive do
{:tick} ->
IO.puts "tock in client"
receiver()
end
end
end