supervisor, behaviour(application), rebar3

Leonard B leonard.boyce@REDACTED
Sun Jul 19 22:07:31 CEST 2020


Suggestion:
https://learnyousomeerlang.com/content


On Sun, Jul 19, 2020 at 2:42 PM Papa Tana <papa.tana101@REDACTED> wrote:
>
> 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 have used "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?
>
> N.B: I'm not looking for a gen_server, I see it a lot but am I
> obligated to transform this to a gen_server also, or only
> application+supervisor is ok for my requirement?
>
> Thanks in advance,
> Best Regards,


More information about the erlang-questions mailing list