X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=001%2Fsrc%2Flife_cell.erl;h=4ee07acdd3b09b70fd667726b55eda7f1052e361;hb=bdfda001fb7cb77f29552bca0071ff3d389b5fd7;hp=47710645889ead365585b019aee11b7e30593005;hpb=29199e7e6e93e242c9d9fecc7db67bb3e170d6d6;p=cellular-automata.git diff --git a/001/src/life_cell.erl b/001/src/life_cell.erl index 4771064..4ee07ac 100644 --- a/001/src/life_cell.erl +++ b/001/src/life_cell.erl @@ -23,6 +23,7 @@ ,num_neighbors :: integer() ,replies_pending :: integer() ,gen_id :: integer() + ,early_msgs :: list() }). @@ -37,6 +38,16 @@ start_link({_, Name, _}=Datum) -> gen_server:start_link(ServerName, ?MODULE, Args, Opts). +%% ============================================================================ +%% Callbacks (unused) +%% ============================================================================ + +handle_call(_Msg, _From, State) -> {reply, ok, State}. +handle_info(_Msg, State) -> {noreply, State}. +code_change(_Old, State, _Other) -> {ok, State}. +terminate(_Reason, State) -> {ok, State}. + + %% ============================================================================ %% Callbacks %% ============================================================================ @@ -49,53 +60,50 @@ init([{CellID, Name, NeighborNames}]) -> ,num_neighbors = length(NeighborNames) ,live_neighbors = 0 ,replies_pending = 0 + ,early_msgs = [] }, {ok, State}. -terminate(_Reason, State) -> - {ok, State}. - - -code_change(_Old, State, _Other) -> - {ok, State}. - - -handle_call(_Msg, _From, State) -> - {reply, ok, State}. - - handle_cast({next_gen, GenID}, #state{name=Name + ,cell_state=CellState ,neighbors=Neighbors ,num_neighbors=NumNeighbors + ,early_msgs=EarlyMsgs }=State) -> - ok = cast_all(Neighbors, {request_state, GenID, Name}), - {noreply, State#state{replies_pending=NumNeighbors, gen_id=GenID}}; + ok = life_lib:cast_one2all(Neighbors, {state_broadcast, GenID, CellState}), -%% 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-> + % Put stashed messages back in the mailbox, + % now that we're ready to handle them + ok = life_lib:cast_all2one(Name, EarlyMsgs), - gen_server:cast(Name, Msg), - {noreply, State}; + NewState = State#state{replies_pending=NumNeighbors + ,gen_id=GenID + ,early_msgs=[] + }, + + {noreply, NewState}; -%% 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-> - ok = gen_server:cast(Requester, {response_state, MyGenID, CellState}), - {noreply, State}; +%% If we receive 'state_broadcast' before we receive 'next_gen', +%% stash it until we do. +%% +%% 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({state_broadcast, ReceivedGenID, _NeighborState}=Msg, + #state{gen_id=GenID + ,early_msgs=EarlyMsgs + }=State) when GenID =/= ReceivedGenID -> -handle_cast({response_state, GenID, NeighborState}, + {noreply, State#state{early_msgs=[Msg|EarlyMsgs]}}; + + +%% Now that we can be sure that this request is for the current generation, we +%% can handle it +handle_cast({state_broadcast, GenID, NeighborState}, #state{cell_id=CellID ,gen_id=GenID ,replies_pending=Pending @@ -124,11 +132,8 @@ handle_cast({response_state, GenID, NeighborState}, {noreply, NewState} end; -handle_cast(_Msg, State) -> - {noreply, State}. - -handle_info(_Msg, State) -> +handle_cast(_Msg, State) -> {noreply, State}. @@ -136,12 +141,6 @@ handle_info(_Msg, State) -> %% Internal %% ============================================================================ -cast_all([], _) -> ok; -cast_all([Server | Servers], Msg) -> - ok = gen_server:cast(Server, Msg), - cast_all(Servers, Msg). - - new_state(1, LiveNeighbors) when LiveNeighbors < 2 -> 0; new_state(1, LiveNeighbors) when LiveNeighbors < 4 -> 1; new_state(1, LiveNeighbors) when LiveNeighbors > 3 -> 0;