X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=001%2Fsrc%2Flife_cell.erl;h=2ebdf7bf353b09d909bab07959ee10a213f66874;hb=b699b8a46efcf2df395bb19dc1027726ce08a9cb;hp=2ffda4b369076d96f90ed20d1e50abbb605f3904;hpb=0a9b8c1755fb6126d79e3c51b84df9ff90fbbf6a;p=cellular-automata.git diff --git a/001/src/life_cell.erl b/001/src/life_cell.erl index 2ffda4b..2ebdf7b 100644 --- a/001/src/life_cell.erl +++ b/001/src/life_cell.erl @@ -15,7 +15,7 @@ ]). --record(state, {id :: integer() +-record(state, {cell_id :: integer() ,name :: string() ,cell_state :: 0 | 1 ,neighbors :: list(atom()) @@ -23,6 +23,7 @@ ,num_neighbors :: integer() ,replies_pending :: integer() ,gen_id :: integer() + ,early_msgs :: list() }). @@ -30,7 +31,7 @@ %% API %% ============================================================================ -start_link({_ID, Name, _NeighborNames}=Datum) -> +start_link({_, Name, _}=Datum) -> ServerName = {local, Name}, Args = [Datum], Opts = [], @@ -41,14 +42,15 @@ start_link({_ID, Name, _NeighborNames}=Datum) -> %% Callbacks %% ============================================================================ -init([{ID, Name, NeighborNames}]) -> - State = #state{id = ID +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 + ,early_msgs = [] }, {ok, State}. @@ -67,20 +69,45 @@ handle_call(_Msg, _From, 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, Name}), - {noreply, State#state{replies_pending=NumNeighbors, gen_id=GenID}}; + ok = cast_all(Neighbors, {state_broadcast, GenID, CellState}), + + % Put stashed messages back in the mailbox, + % now that we're ready to handle them + ok = cast_to(Name, EarlyMsgs), + + NewState = State#state{replies_pending=NumNeighbors + ,gen_id=GenID + ,early_msgs=[] + }, + + {noreply, NewState}; -handle_cast({request_state, Requester}, State) -> - ok = gen_server:cast(Requester, {response_state, State#state.cell_state}), - {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, NeighborState}, - #state{id=ID + {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 ,cell_state=CellState ,live_neighbors=LiveNeighbors @@ -96,7 +123,7 @@ handle_cast({response_state, NeighborState}, case NewPending of 0 -> NewCellState = new_state(CellState, NewLiveNeighbors), - ok = life_time:report_state(ID, NewCellState), + ok = life_time:report_state(CellID, GenID, NewCellState), {noreply, NewState#state{live_neighbors=0 ,cell_state=NewCellState @@ -120,6 +147,14 @@ handle_info(_Msg, State) -> %% Internal %% ============================================================================ +% Cast different messages to a single destination +cast_to(_, []) -> ok; +cast_to(Server, [Msg | Msgs]) -> + ok = gen_server:cast(Server, Msg), + cast_to(Server, Msgs). + + +% Cast the same message to multiple destinations cast_all([], _) -> ok; cast_all([Server | Servers], Msg) -> ok = gen_server:cast(Server, Msg),