2 -behaviour(gen_server).
20 -define(GEN_INTERVAL, 100). % In milliseconds
22 -define(CHAR_DEAD, 32). % " "
23 -define(CHAR_ALIVE, 111). % "o"
24 -define(CHAR_BAR, 45). % "-"
27 -record(state, {x :: integer()
30 ,num_cells :: integer()
31 ,num_dead :: integer()
32 ,num_alive :: integer()
33 ,state_pairs :: [{integer(), integer()}]
34 ,replies_pending :: integer()
36 ,gen_began :: erlang:timestamp()
40 %% ============================================================================
42 %% ============================================================================
44 start_link(X, Y, Cells) ->
45 ServerName = {local, ?MODULE},
48 gen_server:start_link(ServerName, ?MODULE, Args, Opts).
51 report_state(CellID, GenID, CellState) ->
52 gen_server:cast(?MODULE, {report_state, {CellID, GenID, CellState}}).
55 %% ============================================================================
57 %% ============================================================================
59 handle_call(_Msg, _From, State) -> {reply, ok, State}.
60 code_change(_Old, State, _Other) -> {ok, State}.
61 terminate(_Reason, State) -> {ok, State}.
64 %% ============================================================================
66 %% ============================================================================
68 init([X, Y, Cells]) ->
72 ,num_cells = length(Cells)
78 ok = schedule_next_gen(NextGenDelay),
89 GenBegan = os:timestamp(),
91 ok = life_lib:cast_one2all(Cells, {next_gen, NewGenID}),
92 NewState = State#state{replies_pending=NumCells
100 handle_info(_Msg, State) ->
104 handle_cast({report_state, {CellID, GenID, CellState}},
109 ,state_pairs=StatePairs
110 ,replies_pending=RepliesPending
116 NewStatePairs = [{CellID, CellState} | StatePairs],
117 NewRepliesPending = RepliesPending - 1,
118 {NewNDead, NewNAlive} = increment_dead_or_alive(CellState, NDead, NAlive),
119 NewState = State#state{replies_pending=NewRepliesPending
124 case NewRepliesPending of
126 SortedStatePairs = lists:sort(
127 fun({A, _}, {B, _}) -> A < B end,
130 StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
132 GenDurationMic = timer:now_diff(os:timestamp(), GenBegan),
133 GenDurationMil = GenDurationMic / 1000,
134 GenDurationSec = GenDurationMic / 1000000,
135 NextGenDelay = round(?GEN_INTERVAL - GenDurationMil),
137 ok = life_observer:log_generation(GenID, GenDurationSec, NewNDead, NewNAlive),
140 "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b DURATION: ~f~n",
141 [X, Y, NumCells, NewNDead, NewNAlive, GenID, GenDurationSec]
143 ok = do_print_bar(X),
144 ok = do_print_state_chars(X, StateChars),
146 ok = schedule_next_gen(NextGenDelay),
147 {noreply, NewState#state{state_pairs=[]}};
150 {noreply, NewState#state{state_pairs=NewStatePairs}}
153 handle_cast(_Msg, State) ->
157 %% ============================================================================
159 %% ============================================================================
161 increment_dead_or_alive(0, NDead, NAlive) -> {NDead + 1, NAlive};
162 increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}.
165 schedule_next_gen(Delay) when Delay > 0 ->
166 erlang:send_after(Delay, self(), next_gen),
169 schedule_next_gen(_) ->
170 erlang:send(self(), next_gen),
174 state_to_char(0) -> ?CHAR_DEAD;
175 state_to_char(1) -> ?CHAR_ALIVE.
178 do_print_state_chars(_, []) -> ok;
179 do_print_state_chars(X, Chars) ->
180 {XChars, RestChars} = lists:split(X, Chars),
181 ok = io:format([XChars, $\n]),
182 do_print_state_chars(X, RestChars).
186 io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]).