X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=001%2Fsrc%2Flife_cell.erl;h=9f7b1717d3fd6eafd0f713bb13ac103a1d052c63;hb=90456d0857c0e6ac3f9550cc18f27ad158fa8dd8;hp=3f4669d790a796022a0377dd3e44b818950a8ee3;hpb=a88b29167618be4d288304b8d07ce62502217e74;p=cellular-automata.git diff --git a/001/src/life_cell.erl b/001/src/life_cell.erl index 3f4669d..9f7b171 100644 --- a/001/src/life_cell.erl +++ b/001/src/life_cell.erl @@ -15,14 +15,15 @@ ]). --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() - ,generation :: 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 = [], @@ -38,49 +39,73 @@ start_link({_ID, Name, _NeighborNames}=Datum) -> %% ============================================================================ -%% Callbacks +%% Callbacks (unused) %% ============================================================================ -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 - }, - {ok, State}. - +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}. -terminate(_Reason, State) -> - {ok, State}. +%% ============================================================================ +%% Callbacks +%% ============================================================================ -code_change(_Old, State, _Other) -> +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}. -handle_call(_Msg, _From, State) -> - {reply, ok, State}. - - -handle_cast({tick, Generation}, +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, generation=Generation}}; + ok = cast_one2all(Neighbors, {state_broadcast, GenID, CellState}), -handle_cast({request_state, Requester}, State) -> - ok = gen_server:cast(Requester, {response_state, State#state.cell_state}), - {noreply, State}; + % Put stashed messages back in the mailbox, + % now that we're ready to handle them + ok = cast_all2one(Name, EarlyMsgs), + + NewState = State#state{replies_pending=NumNeighbors + ,gen_id=GenID + ,early_msgs=[] + }, + + {noreply, NewState}; + + +%% 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 -> + + {noreply, State#state{early_msgs=[Msg|EarlyMsgs]}}; -handle_cast({response_state, NeighborState}, - #state{id=ID +%% 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 +121,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 @@ -112,18 +137,22 @@ handle_cast(_Msg, State) -> {noreply, State}. -handle_info(_Msg, State) -> - {noreply, State}. - - %% ============================================================================ %% Internal %% ============================================================================ -cast_all([], _) -> ok; -cast_all([Server | Servers], Msg) -> +% Cast all messages to one destination +cast_all2one(_, []) -> ok; +cast_all2one(Server, [Msg | Msgs]) -> + ok = gen_server:cast(Server, Msg), + cast_all2one(Server, Msgs). + + +% Cast one message to all destinations +cast_one2all([], _) -> ok; +cast_one2all([Server | Servers], Msg) -> ok = gen_server:cast(Server, Msg), - cast_all(Servers, Msg). + cast_one2all(Servers, Msg). new_state(1, LiveNeighbors) when LiveNeighbors < 2 -> 0;