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 init([{CellID, Name, NeighborNames}]) ->
46 State = #state{cell_id = CellID
48 ,cell_state = crypto:rand_uniform(0, 2)
49 ,neighbors = NeighborNames
50 ,num_neighbors = length(NeighborNames)
58 terminate(_Reason, State) ->
62 code_change(_Old, State, _Other) ->
66 handle_call(_Msg, _From, State) ->
70 handle_cast({next_gen, GenID},
74 ,num_neighbors=NumNeighbors
78 ok = cast_one2all(Neighbors, {state_broadcast, GenID, CellState}),
80 % Put stashed messages back in the mailbox,
81 % now that we're ready to handle them
82 ok = cast_all2one(Name, EarlyMsgs),
84 NewState = State#state{replies_pending=NumNeighbors
92 %% If we receive 'state_broadcast' before we receive 'next_gen',
93 %% stash it until we do.
95 %% Took me a while to realize this, but sometimes it is possible. The more
96 %% there're cells, the more likely this is to happen.
98 handle_cast({state_broadcast, ReceivedGenID, _NeighborState}=Msg,
100 ,early_msgs=EarlyMsgs
101 }=State) when GenID =/= ReceivedGenID ->
103 {noreply, State#state{early_msgs=[Msg|EarlyMsgs]}};
106 %% Now that we can be sure that this request is for the current generation, we
108 handle_cast({state_broadcast, GenID, NeighborState},
109 #state{cell_id=CellID
111 ,replies_pending=Pending
112 ,cell_state=CellState
113 ,live_neighbors=LiveNeighbors
116 NewPending = Pending - 1,
117 NewLiveNeighbors = LiveNeighbors + NeighborState,
119 NewState = State#state{replies_pending=NewPending
120 ,live_neighbors=NewLiveNeighbors
125 NewCellState = new_state(CellState, NewLiveNeighbors),
126 ok = life_time:report_state(CellID, GenID, NewCellState),
128 {noreply, NewState#state{live_neighbors=0
129 ,cell_state=NewCellState
138 handle_cast(_Msg, State) ->
142 handle_info(_Msg, State) ->
146 %% ============================================================================
148 %% ============================================================================
150 % Cast all messages to one destination
151 cast_all2one(_, []) -> ok;
152 cast_all2one(Server, [Msg | Msgs]) ->
153 ok = gen_server:cast(Server, Msg),
154 cast_all2one(Server, Msgs).
157 % Cast one message to all destinations
158 cast_one2all([], _) -> ok;
159 cast_one2all([Server | Servers], Msg) ->
160 ok = gen_server:cast(Server, Msg),
161 cast_one2all(Servers, Msg).
164 new_state(1, LiveNeighbors) when LiveNeighbors < 2 -> 0;
165 new_state(1, LiveNeighbors) when LiveNeighbors < 4 -> 1;
166 new_state(1, LiveNeighbors) when LiveNeighbors > 3 -> 0;
167 new_state(0, LiveNeighbors) when LiveNeighbors =:= 3 -> 1;
168 new_state(State, _LiveNeighbors) -> State.