Explicitly matching casts with 'ok'.
[cellular-automata.git] / 001 / src / life_cell.erl
index 2ffda4b..e7cebcd 100644 (file)
@@ -15,7 +15,7 @@
         ]).
 
 
--record(state, {id              :: integer()
+-record(state, {cell_id         :: integer()
                ,name            :: string()
                ,cell_state      :: 0 | 1
                ,neighbors       :: list(atom())
@@ -30,7 +30,7 @@
 %% API
 %% ============================================================================
 
-start_link({_ID, Name, _NeighborNames}=Datum) ->
+start_link({_, Name, _}=Datum) ->
     ServerName = {local, Name},
     Args = [Datum],
     Opts = [],
@@ -41,8 +41,8 @@ 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
@@ -66,21 +66,29 @@ handle_call(_Msg, _From, State) ->
 
 
 handle_cast({next_gen, GenID},
-    #state{name=Name
+    #state{cell_state=CellState
           ,neighbors=Neighbors
           ,num_neighbors=NumNeighbors
           }=State) ->
-    ok = cast_all(Neighbors, {request_state, Name}),
+
+    ok = cast_all(Neighbors, {state_broadcast, GenID, CellState}),
     {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 'state_broadcast' 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({state_broadcast, ReceivedGenID, _NeighborState}=Msg,
+    #state{gen_id=GenID, name=Name}=State) when GenID =/= ReceivedGenID->
+    ok = gen_server:cast(Name, Msg),
     {noreply, State};
 
 
-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 +104,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
This page took 0.028932 seconds and 4 git commands to generate.