2 -behaviour(gen_server).
6 -export([start_link/1]).
18 -record(state, {id :: integer()
21 ,neighbors :: list(atom())
22 ,live_neighbors :: integer()
23 ,num_neighbors :: integer()
24 ,replies_pending :: integer()
28 %% ============================================================================
30 %% ============================================================================
32 start_link({_ID, Name, _NeighborNames}=Datum) ->
33 ServerName = {local, Name},
36 gen_server:start_link(ServerName, ?MODULE, Args, Opts).
39 %% ============================================================================
41 %% ============================================================================
43 init([{ID, Name, NeighborNames}]) ->
46 ,cell_state=crypto:rand_uniform(0, 2)
47 ,neighbors=NeighborNames
48 ,num_neighbors=length(NeighborNames)
55 terminate(_Reason, State) ->
59 code_change(_Old, State, _Other) ->
63 handle_call(_Msg, _From, State) ->
70 ,num_neighbors=NumNeighbors
72 ok = cast_all(Neighbors, {request_state, Name}),
73 {noreply, State#state{replies_pending=NumNeighbors}};
76 handle_cast({request_state, Requester}, State) ->
77 ok = gen_server:cast(Requester, {response_state, State#state.cell_state}),
81 handle_cast({response_state, NeighborState},
83 ,replies_pending=Pending
85 ,live_neighbors=LiveNeighbors
88 NewPending = Pending - 1,
89 NewLiveNeighbors = LiveNeighbors + NeighborState,
91 NewState = State#state{replies_pending=NewPending
92 ,live_neighbors=NewLiveNeighbors
97 NewCellState = new_state(CellState, NewLiveNeighbors),
98 ok = life_time:tock(ID, NewCellState),
100 {noreply, NewState#state{live_neighbors=0
101 ,cell_state=NewCellState
110 handle_cast(_Msg, State) ->
114 handle_info(_Msg, State) ->
118 %% ============================================================================
120 %% ============================================================================
122 cast_all([], _) -> ok;
123 cast_all([Server | Servers], Msg) ->
124 ok = gen_server:cast(Server, Msg),
125 cast_all(Servers, Msg).
128 new_state(1, LiveNeighbors) when LiveNeighbors < 2 -> 0;
129 new_state(1, LiveNeighbors) when LiveNeighbors < 4 -> 1;
130 new_state(1, LiveNeighbors) when LiveNeighbors > 3 -> 0;
131 new_state(0, LiveNeighbors) when LiveNeighbors =:= 3 -> 1;
132 new_state(State, _LiveNeighbors) -> State.