Skip to content
This repository was archived by the owner on Sep 22, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/grisp/grisp_base/files/erl_inetrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
% Add hosts
{host, {X,X,X,X}, ["hostname"]}.
{host, {10,152,0,13}, ["mainframe1"]}.

% Do not monitor the hosts file
{hosts_file, ""}.
Expand Down
2 changes: 1 addition & 1 deletion backend/grisp/grisp_base/files/grisp.ini.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ args = erl.rtems -- -home . -pa . -root conway_game -boot conway_game/releases/0
[network]
ip_self = dhcp
wlan = enable
hostname = boardX
hostname = board0002
wpa = wpa_supplicant.conf
2 changes: 1 addition & 1 deletion backend/master_vars.config
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{node_name, 'master@host'}.
{node_name, master}.
26 changes: 26 additions & 0 deletions backend/rebar.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{"1.1.0",
[{<<"cowboy">>,
{git,"git://github.com/ninenines/cowboy.git",
{ref,"d846827b2a70317914621328bd617e4e5d4d13d8"}},
0},
{<<"cowlib">>,
{git,"https://github.com/ninenines/cowlib",
{ref,"75aaeb8415033b07303febbfeba66d6d3413ad06"}},
1},
{<<"epmd">>,
{git,"https://github.com/erlang/epmd",
{ref,"4d1a595b9d5c32fc0e55f462da381de62de23bf0"}},
0},
{<<"grisp">>,{pkg,<<"grisp">>,<<"1.1.4">>},0},
{<<"jiffy">>,{pkg,<<"jiffy">>,<<"0.15.2">>},0},
{<<"mapz">>,{pkg,<<"mapz">>,<<"0.3.0">>},1},
{<<"ranch">>,
{git,"https://github.com/ninenines/ranch",
{ref,"3190aef88aea04d6dce8545fe9b4574288903f44"}},
1}]}.
[
{pkg_hash,[
{<<"grisp">>, <<"5C22D6F9C0B47A90F132E56AC9D85659B5AC469BAF04B4C98EE1E0EF5894319B">>},
{<<"jiffy">>, <<"DE266C390111FD4EA28B9302F0BC3D7472468F3B8E0ACEABFBEFA26D08CD73B7">>},
{<<"mapz">>, <<"438D24746CE5A252101E00B2032EFDF7FC69EB32689D3B805DE5E6DD7F52614F">>}]}
].
2 changes: 1 addition & 1 deletion backend/slave_vars.config
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{node_name, 'slave@host'}.
{node_name, 'slave@127.0.0.1'}.
2 changes: 1 addition & 1 deletion backend/src/conway_game.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
jiffy,
runtime_tools
]},
{env,[ {masterhost, 'master@host'},
{env,[ {masterhost, 'master@mainframe1'},
{slave_n, 1}]},
{modules, []},

Expand Down
7 changes: 5 additions & 2 deletions backend/src/conway_master_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ reset_state() -> gen_server:call(noderef(), reset_state).
get_state_raw() -> gen_server:call(noderef(), get_state_raw).

init([SlaveN]) ->
io:format("Master node ~p~n", [node()]),
{ok, #state{max_slaves = SlaveN, started = true}}.

handle_call(reset_state, _, #state{ max_slaves = MaxSl }) ->
Expand Down Expand Up @@ -102,7 +103,9 @@ handle_cast({update, NodeName, Rect, G}, #state{state = computing,
{noreply, State#state{current_rects = NewCurrentRects}}
end;
handle_cast(M, S) ->
io:format("[WARN] Unknown cast: ~p in state ~p~n", [M, S]),
io:format("[WARN] Unknown cast: ~p ~n", [M]),
io:format("[WARN] in state ~p~n", [S]),
io:format("[WARN] State: ~p ~n", [S#state.state]),
{noreply, S}.

handle_info({'DOWN', Ref, _, _, Reason} = M, S) ->
Expand All @@ -116,7 +119,7 @@ handle_info(_, S) ->
noderef() ->
{ok, NodeName} = application:get_env(conway_game, masterhost),
{?MODULE, NodeName}.

inc_generation(#state{goal_generations = N, generation = M} = State) when N == M ->
NewState = State#state{state = finished, generation = M},
ws_h:update_state(format_state(NewState)),
Expand Down
127 changes: 122 additions & 5 deletions backend/src/conway_slave.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,125 @@
%%% @author zofiapolkowska
%%% @copyright (C) 2019, zofiapolkowska
%%% @doc
%%%
%%% @end
%%% Created : 2019-06-04 18:06:41.713061

-module(conway_slave).

-export([loop/0]).
-behaviour(gen_server).

%% API
-export([start_link/0,
init_state/5]).

%% Callbacks
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).

-define(SERVER, ?MODULE).
-define(PRINT(Var), io:format("DEBUG: ~p:~p - ~p~n~n ~p~n~n", [?MODULE, ?LINE, ??Var, Var])).

-record(state, {
rect = null,
current_gen = 0,
max_gen = 1,
node_name = ""
}).

%%% API

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

init_state(Node, StartingRect, Gens, _Slaves, _W) ->
io:format("Init State~n", []),
gen_server:call({?MODULE, Node}, {init_state, Node, StartingRect, Gens}),
gen_server:cast({?MODULE, Node}, {tick}).

%%% gen_server callbacks

init([]) ->
io:format("Slave initialized~p~n", [node()]),
conway_master_worker:register_slave(node()),

{ok, #state{}}.

handle_call({init_state, NodeName, StartingRect, Gens}, _From, State) ->
Rect = [],
NewState = #state{
rect = StartingRect,
current_gen = 0,
max_gen = Gens,
node_name = NodeName
},
{reply, ok, NewState}.

handle_cast({tick}, #state{max_gen = MaxGen, current_gen = CurrentGen} = State) when CurrentGen >= MaxGen ->
{noreply, State};

handle_cast({tick}, State) ->
NewGen = State#state.current_gen + 1,

NewRect = rect:map(fun(X, Y, Value) -> update_cell(X, Y, Value, State#state.rect) end, State#state.rect),

NewState = State#state{
current_gen = NewGen,
rect = NewRect},

conway_master_worker:update_state(
NewState#state.node_name,
NewState#state.rect,
NewState#state.current_gen),

io:format("Tick: Generation ~p ~n", [NewGen]),

timer:sleep(300),
gen_server:cast({?MODULE, State#state.node_name}, {tick}),

{noreply, NewState};

handle_cast(_Msg, State) ->
{noreply, State}.

handle_info(_Info, State) ->
{noreply, State}.

terminate(_Reason, _State) ->
ok.

code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%%% Internal functions

update_cell(X, Y, Value, Rect) ->
Neighbours = [
rect:get(Rect, {X-1, Y-1}),
rect:get(Rect, {X, Y-1}),
rect:get(Rect, {X+1, Y-1}),
rect:get(Rect, {X-1, Y}),
rect:get(Rect, {X+1, Y}),
rect:get(Rect, {X-1, Y+1}),
rect:get(Rect, {X, Y+1}),
rect:get(Rect, {X+1, Y+1})
],

Count = length(lists:filter(fun(Value) ->
case Value of
true -> true;
_ -> false
end
end, Neighbours)),

NewValue = case {Count, Value} of
{2, true} -> true;
{3, _} -> true;
_ -> false
end,

loop() ->
io:format("I am alive~n", []),
timer:sleep(10000),
loop().
{{X, Y}, NewValue}.
2 changes: 1 addition & 1 deletion backend/src/conway_slave_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ start_link() ->
%% Before OTP 18 tuples must be used to specify a child. e.g.
%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
init([]) ->
ConwayWorker = #{id => conway_slave, start => {conway_slave, loop, []}},
ConwayWorker = #{id => conway_slave, start => {conway_slave, start_link, []}},
{ok, { {one_for_all, 0, 1}, [ConwayWorker]} }.
11 changes: 6 additions & 5 deletions backend/src/rest_endpoint.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
-export([init/2]).
-export([content_types_provided/2]).
-export([content_types_accepted/2]).
-export([content_types_accepted/2]).
-export([allowed_methods/2]).
-export([to_json/2]).

init(Req, Opts) ->
ReqWithCORS = add_cors_headers(Req),
{cowboy_rest, ReqWithCORS, Opts}.
ReqWithCORS = add_cors_headers(Req),
{cowboy_rest, ReqWithCORS, Opts}.

allowed_methods(Req, State) -> {[<<"POST">>, <<"GET">>, <<"OPTIONS">>], Req, State}.

Expand All @@ -29,7 +30,7 @@ to_json(Req, State) ->
<<"GET">> ->
get_state(Req, State);
<<"OPTIONS">> ->
{true, Req, State}
{true, Req, State}
end.

start_the_game(Req, State) ->
Expand Down Expand Up @@ -72,8 +73,8 @@ to_int(B) when is_list(B) ->
%% Helpers

add_cors_headers(Req) ->
Req1 = cowboy_req:set_resp_header(<<"access-control-allow-origin">>, <<"*">>, Req),
cowboy_req:set_resp_header(<<"access-control-allow-headers">>, <<"Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With">>, Req1).
Req1 = cowboy_req:set_resp_header(<<"access-control-allow-origin">>, <<"*">>, Req),
cowboy_req:set_resp_header(<<"access-control-allow-headers">>, <<"Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With">>, Req1).

is_master_node_started() ->
case maps:get(start_state, conway_master_worker:get_state()) of
Expand Down
2 changes: 1 addition & 1 deletion backend/vm.args
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-name {{node_name}}
-sname {{node_name}}

## Cookie for distributed erlang
-setcookie conway_game
Expand Down
Loading