X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=001%2Fsrc%2Flife_cell.erl;h=47710645889ead365585b019aee11b7e30593005;hb=29199e7e6e93e242c9d9fecc7db67bb3e170d6d6;hp=6f477cf5edd1aba9e47bc49a9938a101913047c9;hpb=982ec72043e6de6b6b22e49acb3fe13f1f54bc93;p=cellular-automata.git diff --git a/001/src/life_cell.erl b/001/src/life_cell.erl index 6f477cf..4771064 100644 --- a/001/src/life_cell.erl +++ b/001/src/life_cell.erl @@ -15,13 +15,14 @@ ]). --record(state, {id :: integer() +-record(state, {cell_id :: integer() ,name :: string() ,cell_state :: 0 | 1 ,neighbors :: list(atom()) ,live_neighbors :: integer() ,num_neighbors :: integer() ,replies_pending :: integer() + ,gen_id :: integer() }). @@ -29,7 +30,7 @@ %% API %% ============================================================================ -start_link({_ID, Name, _NeighborNames}=Datum) -> +start_link({_, Name, _}=Datum) -> ServerName = {local, Name}, Args = [Datum], Opts = [], @@ -40,14 +41,14 @@ start_link({_ID, Name, _NeighborNames}=Datum) -> %% Callbacks %% ============================================================================ -init([{ID, Name, NeighborNames}]) -> - State = #state{id=ID - ,name=Name - ,cell_state=crypto:rand_uniform(0, 2) - ,neighbors=NeighborNames - ,num_neighbors=length(NeighborNames) - ,live_neighbors=0 - ,replies_pending=0 +init([{CellID, Name, NeighborNames}]) -> + State = #state{cell_id = CellID + ,name = Name + ,cell_state = crypto:rand_uniform(0, 2) + ,neighbors = NeighborNames + ,num_neighbors = length(NeighborNames) + ,live_neighbors = 0 + ,replies_pending = 0 }, {ok, State}. @@ -64,22 +65,39 @@ handle_call(_Msg, _From, State) -> {reply, ok, State}. -handle_cast(tick, +handle_cast({next_gen, GenID}, #state{name=Name ,neighbors=Neighbors ,num_neighbors=NumNeighbors }=State) -> - ok = cast_all(Neighbors, {request_state, Name}), - {noreply, State#state{replies_pending=NumNeighbors}}; + ok = cast_all(Neighbors, {request_state, GenID, Name}), + {noreply, State#state{replies_pending=NumNeighbors, gen_id=GenID}}; -handle_cast({request_state, Requester}, State) -> - ok = gen_server:cast(Requester, {response_state, State#state.cell_state}), +%% If we receive this before we receive next_gen, throw it back in the queue. +%% (Took me a while to realize this, but sometimes it is possible. The more +%% there're cells, the more likely this is to happen.) +handle_cast({request_state, GenID, _Requester}=Msg, + #state{gen_id=MyGenID + ,name=Name + }=State) when GenID =/= MyGenID-> + + gen_server:cast(Name, Msg), {noreply, State}; +%% Now that we can be sure that this request is for the current generation, we +%% can handle it +handle_cast({request_state, GenID, Requester}, + #state{gen_id=MyGenID + ,cell_state=CellState + }=State) when GenID =:= MyGenID-> -handle_cast({response_state, NeighborState}, - #state{id=ID + ok = gen_server:cast(Requester, {response_state, MyGenID, CellState}), + {noreply, State}; + +handle_cast({response_state, GenID, NeighborState}, + #state{cell_id=CellID + ,gen_id=GenID ,replies_pending=Pending ,cell_state=CellState ,live_neighbors=LiveNeighbors @@ -95,7 +113,7 @@ handle_cast({response_state, NeighborState}, case NewPending of 0 -> NewCellState = new_state(CellState, NewLiveNeighbors), - ok = life_time:tock(ID, NewCellState), + ok = life_time:report_state(CellID, GenID, NewCellState), {noreply, NewState#state{live_neighbors=0 ,cell_state=NewCellState @@ -106,7 +124,6 @@ handle_cast({response_state, NeighborState}, {noreply, NewState} end; - handle_cast(_Msg, State) -> {noreply, State}.