Grouping unused gen_server callbacks.
[cellular-automata.git] / 001 / src / life_cell.erl
index ada70e6..9f7b171 100644 (file)
@@ -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,39 +60,45 @@ 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{cell_state=CellState
+    #state{name=Name
+          ,cell_state=CellState
           ,neighbors=Neighbors
           ,num_neighbors=NumNeighbors
+          ,early_msgs=EarlyMsgs
           }=State) ->
 
-    ok = cast_all(Neighbors, {state_broadcast, GenID, CellState}),
-    {noreply, State#state{replies_pending=NumNeighbors, gen_id=GenID}};
+    ok = cast_one2all(Neighbors, {state_broadcast, GenID, CellState}),
+
+    % 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', 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.)
+%% 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, name=Name}=State) when GenID =/= ReceivedGenID->
-    gen_server:cast(Name, Msg),
-    {noreply, State};
+    #state{gen_id=GenID
+          ,early_msgs=EarlyMsgs
+          }=State) when GenID =/= ReceivedGenID ->
+
+    {noreply, State#state{early_msgs=[Msg|EarlyMsgs]}};
 
 
 %% Now that we can be sure that this request is for the current generation, we
@@ -120,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;
This page took 0.040268 seconds and 4 git commands to generate.