This repository has been archived on 2024-01-29. You can view files and clone it, but cannot push or open issues or pull requests.
distributed_system_coursework/lib/utils.ex

28 lines
635 B
Elixir

defmodule Utils do
def unicast(m, p) do
case :global.whereis_name(p) do
pid when is_pid(pid) -> send(pid, m)
:undefined -> :ok
end
end
def beb_broadcast(m, dest), do: for(p <- dest, do: unicast(m, p))
def register_name(name, pid, link \\ true) do
case :global.re_register_name(name, pid) do
:yes ->
# Note this is running on the parent so we are linking the parent to the rb
# so that when we close the parent the rb also dies
if link do
Process.link(pid)
end
pid
:no ->
Process.exit(pid, :kill)
:error
end
end
end