From: Siraaj Khandkar Date: Mon, 23 Jul 2012 15:06:04 +0000 (-0400) Subject: API tweeks, bangs to casts. X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=172421cbe00ddf82a0b92fcb04bd73b4b14e2338;p=cellular-automata.git API tweeks, bangs to casts. --- diff --git a/001/src/cell.erl b/001/src/cell.erl index 6090fa0..227f673 100644 --- a/001/src/cell.erl +++ b/001/src/cell.erl @@ -64,28 +64,21 @@ handle_call(_Msg, _From, State) -> {reply, ok, State}. -handle_cast(_Msg, State) -> - {noreply, State}. - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -handle_info(tick, +handle_cast(tick, #state{name=Name ,neighbors=Neighbors ,num_neighbors=NumNeighbors }=State) -> - - ok = send_all(Neighbors, {request_state, Name}), + ok = cast_all(Neighbors, {request_state, Name}), {noreply, State#state{replies_pending=NumNeighbors}}; -handle_info({request_state, Requester}, State) -> - Requester ! {response_state, State#state.cell_state}, +handle_cast({request_state, Requester}, State) -> + ok = gen_server:cast(Requester, {response_state, State#state.cell_state}), {noreply, State}; -handle_info({response_state, NeighborState}, +handle_cast({response_state, NeighborState}, #state{id=ID ,replies_pending=Pending ,cell_state=CellState @@ -102,17 +95,20 @@ handle_info({response_state, NeighborState}, case NewPending of 0 -> NewCellState = new_state(CellState, NewLiveNeighbors), - ok = time:cast({tock, {ID, NewCellState}}), + ok = time:tock(ID, NewCellState), {noreply, NewState#state{live_neighbors=0 ,cell_state=NewCellState - }}; + } + }; _N -> {noreply, NewState} end; -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +handle_cast(_Msg, State) -> + {noreply, State}. handle_info(_Msg, State) -> @@ -123,10 +119,10 @@ handle_info(_Msg, State) -> %% Internal %% ============================================================================ -send_all([], _) -> ok; -send_all([PID | PIDs], Msg) -> - PID ! Msg, - send_all(PIDs, Msg). +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; diff --git a/001/src/time.erl b/001/src/time.erl index fb947e3..d933215 100644 --- a/001/src/time.erl +++ b/001/src/time.erl @@ -4,7 +4,7 @@ %% API -export([start_link/2 - ,cast/1 + ,tock/2 ]). %% Callbacks @@ -44,8 +44,8 @@ start_link(X, Cells) -> gen_server:start_link(ServerName, ?MODULE, Args, Opts). -cast(Msg) -> - gen_server:cast(?MODULE, Msg). +tock(CellID, CellState) -> + gen_server:cast(?MODULE, {tock, {CellID, CellState}}). %% ============================================================================ @@ -59,7 +59,7 @@ init([X, Cells]) -> ,state_pairs=[] ,replies_pending=0 }, - cast(next_tick), + schedule_next_tick(), {ok, State}. @@ -75,8 +75,13 @@ handle_call(_Msg, _From, State) -> {reply, ok, State}. -handle_cast(next_tick, #state{cells=Cells, num_cells=NumCells, state_pairs=[]}=State) -> - ok = send_all(Cells, tick), +handle_cast(next_tick, + #state{cells=Cells + ,num_cells=NumCells + ,state_pairs=[] + }=State) -> + + ok = cast_all(Cells, tick), {noreply, State#state{replies_pending=NumCells}}; handle_cast({tock, {ID, CellState}}, @@ -101,7 +106,7 @@ handle_cast({tock, {ID, CellState}}, ok = do_print_state_chars(X, StateChars), ok = do_print_bar(X), ok = timer:sleep(?INTERVAL), - cast(next_tick), + schedule_next_tick(), {noreply, NewState#state{state_pairs=[], gen_count=NewGenCount}}; _N -> @@ -120,10 +125,14 @@ handle_info(_Msg, State) -> %% Internal %% ============================================================================ -send_all([], _) -> ok; -send_all([PID | PIDs], Msg) -> - PID ! Msg, - send_all(PIDs, Msg). +schedule_next_tick() -> + gen_server:cast(?MODULE, next_tick). + + +cast_all([], _) -> ok; +cast_all([Server | Servers], Msg) -> + ok = gen_server:cast(Server, Msg), + cast_all(Servers, Msg). state_to_char(0) -> ?CHAR_DEAD;