Question

I have a server which is created like this :

gateway.erl (supervisor of the supervisor) -> gateway_sup.erl (supervisor of gen_servers) -> gateway_serv.erl (Where every client are handled).

This is pretty basic as I saw over the internet, most people are doing like this.

The Listen Socket is created on the gateway_sup.erl, and I would like to listen over multiple sockets in case some client port restrictions.

So here's my code so far.

gateway.erl

-export([start_link/0, init/1, startWithPort/1]). 

start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init([]) ->
    spawn_link(?MODULE, startWithPort, [8080]),
    spawn_link(?MODULE, startWithPort, [443]),
    {ok, {{simple_one_for_one, 3600, 3600},
         [{socket,
          {gateway_sup, start_link, []},
          permanent, 1000, supervisor, [gateway_sup]}
         ]}}.

startWithPort(Port) ->
    io:fwrite("Starting...: ~p~n", [Port]),
    supervisor:start_child(?MODULE, [Port]).

gateway_sup.erl

-export([start_socket/0, init/1, start_link/1]).

    start_link(Port) ->
        io:fwrite("gateway_sup start_link Port: ~p // ~p~n", [list_to_atom(atom_to_list(?MODULE) ++ atom_to_list(Port)), Port])
        supervisor:start_link({local, list_to_atom(atom_to_list(?MODULE) ++ atom_to_list(Port))}, ?MODULE, [Port]).

    init([Port]) ->
        io:fwrite("gateway_sup Init with port: ~p~n", [Port]),
        R = ssl:listen(Port, ?SSL_OPTIONS),
        {ok, LSocket} = R,
        spawn_link(fun empty_listeners/0),
        {ok, {{simple_one_for_one, 3600, 3600},
             [{socket,
               {gateway_serv, start_link, [LSocket]},
               temporary, 1000, worker, [gateway_serv]}
             ]}}.

    empty_listeners() ->
        [start_socket() || _ <- lists:seq(1,128)],
        ok.

    start_socket() ->
        supervisor:start_child(?MODULE, []).

the start_link() function on gateway_sup.erl is never called. If the gateway is one_for_one and I'm not trying to pass a parameter, everything works fine, but I only do listen over one hardcoded port.

I can't see why it won't call the gateway_sup:start_link/1 ?

Was it helpful?

Solution

Ok, found it ! took me over a night for such minor mistake !

The error is when I'm creating the role within the start_link

list_to_atom(atom_to_list(?MODULE) ++ atom_to_list(Port))

has been replaced with

list_to_atom(atom_to_list(?MODULE) ++ lists:flatten(io_lib:format("~B", [Port])))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top