I have a main controller controller.erl that acts with gen_server behavior, that controller is supposed create 4 other processes called tower.erl, on 4 different pcs and communicate with them afterwards.
The init method for the tower module is called, and inside there is a timer to a periodic function that will communicate with the controller at handle_info/2, but it never reaches there because the 4 towers crash instantly, and I can't understand why, the sys:trace doesn't work either.
The controller keeps working as intented, and the terminate function for the towers doesn't get called, so I cant catch the crash reason.
What could be the reasons they crash?
The controller code for init:
start_controller() ->
gen_server:start({global, ?MODULE}, ?MODULE, [], []).
init(_) ->
global:register_name(controller,self()),
% Start additional servers on remote nodes
Node1 = '[email protected]',
Node2 = '[email protected]',
Node3 = '[email protected]',
Node4 = '[email protected]',
BORDERS_1=[XMIN_1,XMAX_1,YMIN_1,YMAX_1] =[0, 400,0,400],
BORDERS_2 = [XMIN_2,XMAX_2,YMIN_2,YMAX_2] =[401, 800,0,400],
BORDERS_3 = [XMIN_3,XMAX_3,YMIN_3,YMAX_3] =[0, 400,401,800],
BORDERS_4 = [XMIN_4,XMAX_4,YMIN_4,YMAX_4] =[401, 800,401,800],
ETS_1 = ets:new(ets1,[set,named_table]),
ETS_2 = ets:new(ets2,[set,named_table]),
ETS_3 = ets:new(ets3,[set,named_table]),
ETS_4 = ets:new(ets4,[set,named_table]),
{ok,PID_1} = rpc:call(Node1, tower, start_tower, [BORDERS_1]),
{ok,PID_2} = rpc:call(Node2, tower, start_tower, [BORDERS_2]),
{ok,PID_3} = rpc:call(Node3, tower, start_tower, [BORDERS_3]),
{ok,PID_4} = rpc:call(Node4, tower, start_tower, [BORDERS_4]),
REF_1 = erlang:monitor(process,PID_1),
REF_2 = erlang:monitor(process,PID_2),
REF_3 = erlang:monitor(process,PID_3),
REF_4 = erlang:monitor(process,PID_4),
TimerInterval = 500, % milliseconds
erlang:send_after(TimerInterval, self(), {send_to_graphics}),
%{ok,State} where State consists of [PID_i,REF_i,ETS_i,XMIN_i,XMAX_i,YMIN_i,YMAX_i]
State = [{PID_1,REF_1,ETS_1,XMIN_1,XMAX_1,YMIN_1,YMAX_1},{PID_2,REF_2,ETS_2,XMIN_2,XMAX_2,YMIN_2,YMAX_2},{PID_3,REF_3,ETS_3,XMIN_3,XMAX_3,YMIN_3,YMAX_3},{PID_4,REF_4,ETS_4,XMIN_4,XMAX_4,YMIN_4,YMAX_4}],
{ok, State}.
The tower code which gets called:
init(Borders) ->
io:format("The borders for PID ~p are ~p ~n",[self(),Borders]),
% {ok,FD} = file:open("tower_init.txt",[write]),
% file:write(FD,"initiated a tower with an rpc call"),
% file:close(FD),
[Xmin, Xmax, Ymin, Ymax] = Borders,
Table = ets:new(planes, [set,public, named_table]),
StripBusy = 0,
XStart = 100,
XEnd = 300,
YStart = 100,
YEnd = 300,
Strip = {XStart,XEnd,YStart,YEnd},
State = {Table,Strip,{Xmin,Xmax,Ymin,Ymax},StripBusy},
timer:send_interval(1000, {self(), send_to_controller}), %% Start the timer for plane creation
{ok,State}.
start_tower([Xmin, Xmax, Ymin, Ymax]) ->
gen_server:start_link({global, ?MODULE}, ?MODULE, [Xmin, Xmax, Ymin, Ymax], []).
handle_info({_PID, send_to_controller},State) ->
io:format("Sending to main controller ~p ~n",[self()]),
Controller_PID = global:whereis_name(controller),
ETS_as_list = tab2list(planes),
gen_server:cast(Controller_PID,{self(),ETS_as_list}),
timer:send_interval(1000, {self(), send_to_controller}),
{noreply,State};