Current section

Files

Jump to
double lib double registry.ex
Raw

lib/double/registry.ex

defmodule Double.Registry do
use GenServer
# API
def start do
GenServer.start(__MODULE__, nil, name: :registry)
end
def whereis_double(double_id) do
GenServer.call(:registry, {:whereis_double, double_id})
end
def whereis_test(double_id) do
GenServer.call(:registry, {:whereis_test, double_id})
end
def register_double(double_id, pid, test_pid) do
GenServer.call(:registry, {:register_id, double_id, pid, test_pid})
end
# SERVER
def init(_) do
{:ok, Map.new}
end
def handle_call({:whereis_double, double_id}, _from, state) do
{double_pid, _} = state
|> Map.get(double_id, {:undefined, nil})
{:reply, double_pid, state}
end
def handle_call({:whereis_test, double_id}, _from, state) do
{_, test_pid} = state
|> Map.get(double_id, {:undefined, nil})
{:reply, test_pid, state}
end
def handle_call({:register_id, double_id, pid, test_pid}, _from, state) do
case Map.get(state, double_id) do
nil ->
{:reply, :yes, Map.put(state, double_id, {pid, test_pid})}
_ ->
{:reply, :no, state}
end
end
end