painting the bike shed
This commit is contained in:
parent
c270e18ed0
commit
6bfd19e027
@ -1,10 +1,16 @@
|
||||
`gex_httpd`: Gajumaru Exchange HTTP Daemon
|
||||
=====================================================================
|
||||
|
||||
GOALS
|
||||
====================================================================
|
||||
|
||||
|
||||
GOAL STACK
|
||||
--------------------------------------------------------------------
|
||||
|
||||
- write out call paths for `gh_sfc`
|
||||
- replace `io:format` calls with zx log
|
||||
- use `gh_sfc`
|
||||
- ~~write out call paths for `gh_sfc`~~
|
||||
|
||||
|
||||
GOAL QUEUE
|
||||
|
||||
@ -10,7 +10,6 @@
|
||||
|
||||
%% for our edification
|
||||
-export([listen/1, ignore/0]).
|
||||
-export([start/0]).
|
||||
|
||||
%% erlang expects us to export these functions
|
||||
-export([start/2, stop/1]).
|
||||
@ -43,27 +42,6 @@ ignore() ->
|
||||
gh_client_man:ignore().
|
||||
|
||||
|
||||
-spec start() -> ok.
|
||||
%% @doc
|
||||
%% Start the server in an "ignore" state.
|
||||
|
||||
start() ->
|
||||
ok = application:ensure_started(sasl),
|
||||
ok = application:start(gex_httpd),
|
||||
io:format("Starting...").
|
||||
|
||||
|
||||
%-spec start(PortNum) -> ok
|
||||
% when PortNum :: inet:port_number().
|
||||
%%% @doc
|
||||
%%% Start the server and begin listening immediately. Slightly more convenient when
|
||||
%%% playing around in the shell.
|
||||
%
|
||||
%start(PortNum) ->
|
||||
% ok = start(),
|
||||
% ok = gh_client_man:listen(PortNum),
|
||||
% io:format("Startup complete, listening on ~w~n", [PortNum]).
|
||||
%
|
||||
|
||||
-spec start(normal, term()) -> {ok, pid()}.
|
||||
%% @private
|
||||
@ -73,27 +51,25 @@ start() ->
|
||||
|
||||
start(normal, _Args) ->
|
||||
Result = gh_sup:start_link(),
|
||||
% auto-listen to port 8000
|
||||
ok = hz(),
|
||||
% auto-listen to port 8000
|
||||
ok = listen(8000),
|
||||
Result.
|
||||
|
||||
|
||||
hz() ->
|
||||
ok = application:ensure_started(hakuzaru),
|
||||
ok = hz:chain_nodes([testnet_node()]),
|
||||
%TestnetIP = {84, 46, 242, 9},
|
||||
% fuck bulgaria
|
||||
% TestnetIP = "groot.testnet.gajumaru.io",
|
||||
% TestnetPort = 3013,
|
||||
% japan good
|
||||
TestnetIP = "tsuriai.jp",
|
||||
TestnetPort = 4013,
|
||||
TestnetNode = {TestnetIP, TestnetPort},
|
||||
ok = hz:chain_nodes([TestnetNode]),
|
||||
ok = tell("hz status: ~tp", [hz:status()]),
|
||||
ok.
|
||||
|
||||
testnet_ip() ->
|
||||
{84, 46, 242, 9}.
|
||||
|
||||
testnet_port() ->
|
||||
3013.
|
||||
|
||||
testnet_node() ->
|
||||
{testnet_ip(), testnet_port()}.
|
||||
|
||||
|
||||
-spec stop(term()) -> ok.
|
||||
%% @private
|
||||
|
||||
@ -24,6 +24,7 @@
|
||||
-export([system_continue/3, system_terminate/4,
|
||||
system_get_state/1, system_replace_state/2]).
|
||||
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
-include("http.hrl").
|
||||
|
||||
|
||||
@ -85,7 +86,7 @@ start_link(ListenSocket) ->
|
||||
%% call to listen/3.
|
||||
|
||||
init(Parent, ListenSocket) ->
|
||||
ok = io:format("~p Listening.~n", [self()]),
|
||||
ok = tell("~p Listening.~n", [self()]),
|
||||
Debug = sys:debug_options([]),
|
||||
ok = proc_lib:init_ack(Parent, {ok, self()}),
|
||||
listen(Parent, Debug, ListenSocket).
|
||||
@ -109,12 +110,12 @@ listen(Parent, Debug, ListenSocket) ->
|
||||
{ok, Socket} ->
|
||||
{ok, _} = start(ListenSocket),
|
||||
{ok, Peer} = inet:peername(Socket),
|
||||
ok = io:format("~p Connection accepted from: ~p~n", [self(), Peer]),
|
||||
ok = log("~p Connection accepted from: ~p~n", [self(), Peer]),
|
||||
ok = gh_client_man:enroll(),
|
||||
State = #s{socket = Socket},
|
||||
loop(Parent, Debug, State);
|
||||
{error, closed} ->
|
||||
ok = io:format("~p Retiring: Listen socket closed.~n", [self()]),
|
||||
ok = log("~p Retiring: Listen socket closed.~n", [self()]),
|
||||
exit(normal)
|
||||
end.
|
||||
|
||||
@ -131,7 +132,6 @@ loop(Parent, Debug, State = #s{socket = Socket, received = Received}) ->
|
||||
ok = inet:setopts(Socket, [{active, once}]),
|
||||
receive
|
||||
{tcp, Socket, Message} ->
|
||||
ok = io:format("~p received: ~tp~n", [self(), Message]),
|
||||
%% Received exists because web browsers usually use the same
|
||||
%% acceptor socket for sequential requests
|
||||
%%
|
||||
@ -155,17 +155,17 @@ loop(Parent, Debug, State = #s{socket = Socket, received = Received}) ->
|
||||
NewState = State#s{received = NewReceived},
|
||||
loop(Parent, Debug, NewState);
|
||||
{error, Reason} ->
|
||||
io:format("~p error: ~tp~n", [self(), Reason]),
|
||||
tell(warning, "~p ~p: http parse error: ~tp~n", [?MODULE, self(), Reason]),
|
||||
ok = http_err(Socket, 500),
|
||||
exit(normal)
|
||||
end;
|
||||
{tcp_closed, Socket} ->
|
||||
ok = io:format("~p Socket closed, retiring.~n", [self()]),
|
||||
ok = log(warning, "~p ~p: Socket closed, retiring.~n", [?MODULE, self()]),
|
||||
exit(normal);
|
||||
{system, From, Request} ->
|
||||
sys:handle_system_msg(Request, From, Parent, ?MODULE, Debug, State);
|
||||
Unexpected ->
|
||||
ok = io:format("~p Unexpected message: ~tp", [self(), Unexpected]),
|
||||
ok = tell("~p ~p: Unexpected message: ~tp", [?MODULE, self(), Unexpected]),
|
||||
loop(Parent, Debug, State)
|
||||
end.
|
||||
|
||||
@ -240,7 +240,7 @@ handle_request(Socket, #request{method = get, path = <<"/">>}) ->
|
||||
body = ResponseBody},
|
||||
respond(Socket, Response);
|
||||
Error ->
|
||||
io:format("~p error: ~p~n", [self(), Error]),
|
||||
log("~p ~p error: ~p~n", [?MODULE, self(), Error]),
|
||||
http_err(Socket, 500)
|
||||
end;
|
||||
handle_request(Socket, _) ->
|
||||
|
||||
@ -23,6 +23,8 @@
|
||||
code_change/3, terminate/2]).
|
||||
|
||||
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
%%% Type and Record Definitions
|
||||
|
||||
|
||||
@ -93,7 +95,7 @@ start_link() ->
|
||||
%% preparatory work necessary for proper function.
|
||||
|
||||
init(none) ->
|
||||
ok = io:format("Starting.~n"),
|
||||
ok = tell("~p ~p: Starting.~n", [?MODULE, self()]),
|
||||
State = #s{},
|
||||
{ok, State}.
|
||||
|
||||
@ -119,7 +121,7 @@ handle_call({listen, PortNum}, _, State) ->
|
||||
{Response, NewState} = do_listen(PortNum, State),
|
||||
{reply, Response, NewState};
|
||||
handle_call(Unexpected, From, State) ->
|
||||
ok = io:format("~p Unexpected call from ~tp: ~tp~n", [self(), From, Unexpected]),
|
||||
ok = tell("~p ~p Unexpected call from ~tp: ~tp~n", [?MODULE, self(), From, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
@ -138,7 +140,7 @@ handle_cast(ignore, State) ->
|
||||
NewState = do_ignore(State),
|
||||
{noreply, NewState};
|
||||
handle_cast(Unexpected, State) ->
|
||||
ok = io:format("~p Unexpected cast: ~tp~n", [self(), Unexpected]),
|
||||
ok = tell("~p Unexpected cast: ~tp~n", [self(), Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
@ -154,7 +156,7 @@ handle_info({'DOWN', Mon, process, Pid, Reason}, State) ->
|
||||
NewState = handle_down(Mon, Pid, Reason, State),
|
||||
{noreply, NewState};
|
||||
handle_info(Unexpected, State) ->
|
||||
ok = io:format("~p Unexpected info: ~tp~n", [self(), Unexpected]),
|
||||
ok = tell("~p Unexpected info: ~tp~n", [self(), Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
@ -214,7 +216,7 @@ do_listen(PortNum, State = #s{port_num = none}) ->
|
||||
{ok, _} = gh_client:start(Listener),
|
||||
{ok, State#s{port_num = PortNum, listener = Listener}};
|
||||
do_listen(_, State = #s{port_num = PortNum}) ->
|
||||
ok = io:format("~p Already listening on ~p~n", [self(), PortNum]),
|
||||
ok = tell("~p ~p: Already listening on ~p~n", [?MODULE, self(), PortNum]),
|
||||
{{error, {listening, PortNum}}, State}.
|
||||
|
||||
|
||||
@ -240,7 +242,7 @@ do_enroll(Pid, State = #s{clients = Clients}) ->
|
||||
case lists:member(Pid, Clients) of
|
||||
false ->
|
||||
Mon = monitor(process, Pid),
|
||||
ok = io:format("Monitoring ~tp @ ~tp~n", [Pid, Mon]),
|
||||
ok = tell("Monitoring ~tp @ ~tp~n", [Pid, Mon]),
|
||||
State#s{clients = [Pid | Clients]};
|
||||
true ->
|
||||
State
|
||||
@ -267,6 +269,6 @@ handle_down(Mon, Pid, Reason, State = #s{clients = Clients}) ->
|
||||
State#s{clients = NewClients};
|
||||
false ->
|
||||
Unexpected = {'DOWN', Mon, process, Pid, Reason},
|
||||
ok = io:format("~p Unexpected info: ~tp~n", [self(), Unexpected]),
|
||||
ok = tell("~p Unexpected info: ~tp~n", [self(), Unexpected]),
|
||||
State
|
||||
end.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user