get_room_code()/1 gets a completely unique and unused room code
I log them every time I create a new child:
iex(1)> %{
id: :"718687887279",
shutdown: 10000,
start: {Sem.Room, :start_link, [%{room: :"718687887279"}]}
}
iex(1)> %{
id: :"767476717986",
shutdown: 10000,
start: {Sem.Room, :start_link, [%{room: :"767476717986"}]}
}
iex(1)>
11:31:02.192 [error] Ranch protocol #PID<0.375.0> of listener None.HTTP (connection #PID<0.374.0>, stream id 1) terminated
an exception was raised:
** (MatchError) no match of right hand side value: {:error, {:already_started, #PID<0.372.0>}}
(sem 0.1.0) lib/client.ex:18: Sem.SocketHandler.init/2
(cowboy 2.9.0) c:/Users/ebarr/Desktop/coding/stacked/kertamen/sem/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
(cowboy 2.9.0) c:/Users/ebarr/Desktop/coding/stacked/kertamen/sem/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
(cowboy 2.9.0) c:/Users/ebarr/Desktop/coding/stacked/kertamen/sem/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
(stdlib 3.15) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
here is get_room_code()/1
@spec get_room_code :: {:ok, atom()}
def get_room_code() do
rand = randomcode(6)
if (Registry.lookup(Sem.RoomRegistry, rand) != []) do
{:ok, get_room_code()}
else
{:ok, String.to_atom(rand)}
end
end