I have the following straightforward UDP Server:
ONLY accept Binary <<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, my peer UDP Client is going to send a malformed data intentionally.
I can write a case clause to match any message and simply ignore any malformed message.
However, it 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>>},[{server,loop,1,[{file,"server.erl"},{line,16}]}]}
Cool, it crash, But I want my server to restart automatically now :-)
I have read 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
In addition, 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,