diff --git a/src/fd_client.erl b/src/fd_client.erl index cd93919..efb6fc2 100644 --- a/src/fd_client.erl +++ b/src/fd_client.erl @@ -312,7 +312,7 @@ ws_tetris2(Sock, Request, Received) -> case fd_ws:handshake(Request) of {ok, Response} -> respond(Sock, Response), - tetris_init(Sock), + {ok, _} = fd_tetris:start_link(), ws_tetris_loop(Sock, [], Received); Error -> tell("ws_tetris: error: ~tp", [Error]), @@ -334,11 +334,11 @@ ws_tetris_loop(Sock, Frames, Received) -> receive {tcp, Sock, Bin} -> Rcv1 = <>, - tell("~p:~p rcv1: ~tp", [?MODULE, ?LINE, Rcv1]), + ok = tell("~p:~p rcv1: ~tp", [?MODULE, ?LINE, Rcv1]), ws_tetris_loop(Sock, Frames, <<>>); - {tetris, State} -> - tell("tetris: ~p", [State]), - fd_ws:send(Sock, {text, State}), + {tetris, Message} -> + ok = tell("tetris: ~p", [Message]), + ok = fd_ws:send(Sock, {text, Message}), ws_tetris_loop(Sock, Frames, Received); {tcp_closed, Sock} -> {error, tcp_closed}; {tcp_error, Sock, Reason} -> {error, {tcp_error, Reason}} @@ -349,18 +349,6 @@ ws_tetris_loop(Sock, Frames, Received) -> {error, {inet, Reason}} end. -tetris_init(_Sock) -> - tell("~p tetris_init", [self()]), - Parent = self(), - _Child = spawn_link(fun() -> poop(Parent) end), - ok. - -poop(Parent) -> - tell("~p poop(~p)", [self(), Parent]), - Parent ! {tetris, "poop\n"}, - timer:sleep(3_000), - poop(Parent). - %% ------------------------------ %% echo %% ------------------------------ @@ -390,7 +378,7 @@ ws_echo_loop(Sock) -> ws_echo_loop(Sock, Frames, Received) -> tell("~p ws_echo_loop(Sock, ~tp, ~tp)", [self(), Frames, Received]), case fd_ws:recv(Sock, Received, 5*fd_ws:min(), Frames) of - Result = {ok, Message, NewFrames, NewReceived} -> + {ok, Message, NewFrames, NewReceived} -> tell("~p echo message: ~tp", [self(), Message]), % send the same message back ok = fd_ws:send(Sock, Message), diff --git a/src/fd_sfc.erl b/src/fd_sfc.erl index 04a76d0..c273451 100644 --- a/src/fd_sfc.erl +++ b/src/fd_sfc.erl @@ -62,7 +62,7 @@ start_link() -> %% gen_server callbacks init(none) -> - log(info, "starting fd_cache"), + tell("starting fd_sfc"), InitState = #s{}, erlang:send_after(InitState#s.auto_renew, self(), auto_renew), {ok, InitState}. diff --git a/src/fd_tetris.erl b/src/fd_tetris.erl new file mode 100644 index 0000000..a00aa42 --- /dev/null +++ b/src/fd_tetris.erl @@ -0,0 +1,76 @@ +% @doc tetris +% +% manages state for a single game of tetris +% +% https://www.erlang.org/docs/24/man/gen_server +-module(fd_tetris). + +-behavior(gen_server). + +-export([ + %% caller context + start_link/0, + %% process context + %% gen_server callbacks + init/1, handle_call/3, handle_cast/2, handle_info/2, + code_change/3, terminate/2 +]). + +-include("$zx_include/zx_logger.hrl"). + + +-record(s, {parent :: pid()}). + +-type state() :: #s{}. + +%%----------------------------------------------------------------------------- +%% caller context below this line +%%----------------------------------------------------------------------------- + +-spec start_link() -> {ok, pid()} | {error, term()}. +start_link() -> + gen_server:start_link(?MODULE, [self()], []). + + +%%----------------------------------------------------------------------------- +%% process context below this line +%%----------------------------------------------------------------------------- + +%% gen_server callbacks + +-spec init(Args) -> {ok, State} + when Args :: [Parent], + Parent :: pid(), + State :: state(). + +init([Parent]) -> + tell("~tp:~tp starting fd_tetris with parent ~p", [?MODULE, self(), Parent]), + self() ! {poop, 0}, + InitState = #s{parent = Parent}, + {ok, InitState}. + + +handle_call(Unexpected, From, State) -> + tell("~tp:~tp unexpected call from ~tp: ~tp", [?MODULE, self(), From, Unexpected]), + {noreply, State}. + + +handle_cast(Unexpected, State) -> + tell("~tp:~tp unexpected cast: ~tp", [?MODULE, self(), Unexpected]), + {noreply, State}. + + +handle_info({poop, N}, State = #s{parent = Parent}) -> + Parent ! {tetris, io_lib:format("poop~p", [N])}, + erlang:send_after(1_000, self(), {poop, N+1}), + {noreply, State}; +handle_info(Unexpected, State) -> + tell("~tp:~tp unexpected info: ~tp", [?MODULE, self(), Unexpected]), + {noreply, State}. + + +code_change(_, State, _) -> + {ok, State}. + +terminate(_, _) -> + ok.