2 -behaviour(gen_server).
6 -export([start_link/1]).
18 -record(state, {cell_id :: integer()
21 ,neighbors :: list(atom())
22 ,live_neighbors :: integer()
23 ,num_neighbors :: integer()
24 ,replies_pending :: integer()
30 %% ============================================================================
32 %% ============================================================================
34 start_link({_, Name, _}=Datum) ->
35 ServerName = {local, Name},
38 gen_server:start_link(ServerName, ?MODULE, Args, Opts).
41 %% ============================================================================
43 %% ============================================================================
45 handle_call(_Msg, _From, State) -> {reply, ok, State}.
46 handle_info(_Msg, State) -> {noreply, State}.
47 code_change(_Old, State, _Other) -> {ok, State}.
48 terminate(_Reason, State) -> {ok, State}.
51 %% ============================================================================
53 %% ============================================================================
55 init([{CellID, Name, NeighborNames}]) ->
56 State = #state{cell_id = CellID
58 ,cell_state = crypto:rand_uniform(0, 2)
59 ,neighbors = NeighborNames
60 ,num_neighbors = length(NeighborNames)
68 handle_cast({next_gen, GenID},
72 ,num_neighbors=NumNeighbors
76 ok = life_lib:cast_one2all(Neighbors, {state_broadcast, GenID, CellState}),
78 % Put stashed messages back in the mailbox,
79 % now that we're ready to handle them
80 ok = life_lib:cast_all2one(Name, EarlyMsgs),
82 NewState = State#state{replies_pending=NumNeighbors
90 %% If we receive 'state_broadcast' before we receive 'next_gen',
91 %% stash it until we do.
93 %% Took me a while to realize this, but sometimes it is possible. The more
94 %% there're cells, the more likely this is to happen.
96 handle_cast({state_broadcast, ReceivedGenID, _NeighborState}=Msg,
99 }=State) when GenID =/= ReceivedGenID ->
101 {noreply, State#state{early_msgs=[Msg|EarlyMsgs]}};
104 %% Now that we can be sure that this request is for the current generation, we
106 handle_cast({state_broadcast, GenID, NeighborState},
107 #state{cell_id=CellID
109 ,replies_pending=Pending
110 ,cell_state=CellState
111 ,live_neighbors=LiveNeighbors
114 NewPending = Pending - 1,
115 NewLiveNeighbors = LiveNeighbors + NeighborState,
117 NewState = State#state{replies_pending=NewPending
118 ,live_neighbors=NewLiveNeighbors
123 NewCellState = new_state(CellState, NewLiveNeighbors),
124 ok = life_time:report_state(CellID, GenID, NewCellState),
126 {noreply, NewState#state{live_neighbors=0
127 ,cell_state=NewCellState
136 handle_cast(_Msg, State) ->
140 %% ============================================================================
142 %% ============================================================================
144 new_state(1, LiveNeighbors) when LiveNeighbors < 2 -> 0;
145 new_state(1, LiveNeighbors) when LiveNeighbors < 4 -> 1;
146 new_state(1, LiveNeighbors) when LiveNeighbors > 3 -> 0;
147 new_state(0, LiveNeighbors) when LiveNeighbors =:= 3 -> 1;
148 new_state(State, _LiveNeighbors) -> State.