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
2023-11-28 21:42:16 +00:00

25 lines
587 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) 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
Process.link(pid)
pid
:no ->
Process.exit(pid, :kill)
:error
end
end
end