Discussions

Ask a Question
Back to All

rebar3, supervisor, behaviour(application)

Hi All,

I have the following straightforward UDP Server:

  • ONLY accept Data 0:32,
  • otherwise it crash

-module(server).
-export([listen/1]).

listen(Port) ->
spawn(fun()->run_it(Port) end).

run_it(Port) ->
{ok, Skt} = gen_udp:open(Port, [binary]),
loop(Skt).

loop(Skt) ->
receive
{udp, Skt, , , Bin} ->
case Bin of
0:32 ->
io:fwrite("~p~n", [{"Good Format: ", Bin}]),
loop(Skt)
end
end.

Now, Client is going to send a malformed data.

I can write a case clause to match any message and simply ignore any
malformed message.

However, that would be a catch-all-clause, which will not help me if
there is eventual bugs.

I have read somewhere: "Don't program defensively, let it crash, then fix it".

=ERROR REPORT==== 19-Jul-2020::21:15:29.872000 ===
Error in process <0.93.0> with exit value:
{{case_clause,0},[{ggsn,loop,1,[{file,"ggsn.erl"},{line,16}]}]}

Cool, it crash, But I want my server to restart automatically anyway :-)

I have read also that a process called "supervisor" can monitor my
server and restarts it when it detects that it died.

So, I DISCOVERED "rebar3" because it helped me a lot when I compile
several files with just 1 single line 'rebar3 compile'.

It creates automatically a /src/ with 3 files, but only 2 are
interesting me for now:

  • server_app.erl
  • server_sup.erl

Ok, I have read the documentations but I'm still far from understanding.

Can anyone advise please, or transform my 19 lines of code server.erl
to server_app.erl and supervised by a server_sup.erl using rebar3?

Thanks in advance,
Best Regards,