runs
This commit is contained in:
parent
cbfc496057
commit
e160701403
@ -23,11 +23,17 @@ start_link() ->
|
|||||||
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
RestartStrategy = {one_for_one, 1, 60},
|
RestartStrategy = {one_for_one, 1, 60},
|
||||||
Clerks = {fd_httpd_clerks,
|
FileCache = {fd_httpd_sfc,
|
||||||
{fd_httpd_clerks, start_link, []},
|
{fd_httpd_sfc, start_link, []},
|
||||||
|
permanent,
|
||||||
|
5000,
|
||||||
|
worker,
|
||||||
|
[fd_httpd_sfc]},
|
||||||
|
Clients = {fd_httpd_clients,
|
||||||
|
{fd_httpd_clients, start_link, []},
|
||||||
permanent,
|
permanent,
|
||||||
5000,
|
5000,
|
||||||
supervisor,
|
supervisor,
|
||||||
[fd_httpd_clerks]},
|
[fd_httpd_clients]},
|
||||||
Children = [Clerks],
|
Children = [FileCache, Clients],
|
||||||
{ok, {RestartStrategy, Children}}.
|
{ok, {RestartStrategy, Children}}.
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
%%% http://erlang.org/doc/design_principles/spec_proc.html
|
%%% http://erlang.org/doc/design_principles/spec_proc.html
|
||||||
%%% @end
|
%%% @end
|
||||||
|
|
||||||
-module(fd_http_client).
|
-module(fd_httpd_client).
|
||||||
-vsn("0.1.0").
|
-vsn("0.1.0").
|
||||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||||
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
|
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
|
||||||
@ -52,11 +52,11 @@
|
|||||||
| {shutdown, term()}
|
| {shutdown, term()}
|
||||||
| term().
|
| term().
|
||||||
%% @private
|
%% @private
|
||||||
%% How the fd_client_man or a prior fd_client kicks things off.
|
%% How the fd_httpd_client_man or a prior fd_httpd_client kicks things off.
|
||||||
%% This is called in the context of fd_client_man or the prior fd_client.
|
%% This is called in the context of fd_httpd_client_man or the prior fd_httpd_client.
|
||||||
|
|
||||||
start(ListenSocket) ->
|
start(ListenSocket) ->
|
||||||
fd_client_sup:start_acceptor(ListenSocket).
|
fd_httpd_client_sup:start_acceptor(ListenSocket).
|
||||||
|
|
||||||
|
|
||||||
-spec start_link(ListenSocket) -> Result
|
-spec start_link(ListenSocket) -> Result
|
||||||
@ -67,7 +67,7 @@ start(ListenSocket) ->
|
|||||||
| {shutdown, term()}
|
| {shutdown, term()}
|
||||||
| term().
|
| term().
|
||||||
%% @private
|
%% @private
|
||||||
%% This is called by the fd_client_sup. While start/1 is called to iniate a startup
|
%% This is called by the fd_httpd_client_sup. While start/1 is called to iniate a startup
|
||||||
%% (essentially requesting a new worker be started by the supervisor), this is
|
%% (essentially requesting a new worker be started by the supervisor), this is
|
||||||
%% actually called in the context of the supervisor.
|
%% actually called in the context of the supervisor.
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ start_link(ListenSocket) ->
|
|||||||
%% call to listen/3.
|
%% call to listen/3.
|
||||||
|
|
||||||
init(Parent, ListenSocket) ->
|
init(Parent, ListenSocket) ->
|
||||||
ok = io:format("~p Listening.~n", [self()]),
|
ok = tell("~p Listening.~n", [self()]),
|
||||||
Debug = sys:debug_options([]),
|
Debug = sys:debug_options([]),
|
||||||
ok = proc_lib:init_ack(Parent, {ok, self()}),
|
ok = proc_lib:init_ack(Parent, {ok, self()}),
|
||||||
listen(Parent, Debug, ListenSocket).
|
listen(Parent, Debug, ListenSocket).
|
||||||
@ -98,7 +98,7 @@ init(Parent, ListenSocket) ->
|
|||||||
ListenSocket :: gen_tcp:socket().
|
ListenSocket :: gen_tcp:socket().
|
||||||
%% @private
|
%% @private
|
||||||
%% This function waits for a TCP connection. The owner of the socket is still
|
%% This function waits for a TCP connection. The owner of the socket is still
|
||||||
%% the fd_client_man (so it can still close it on a call to fd_client_man:ignore/0),
|
%% the fd_httpd_client_man (so it can still close it on a call to fd_httpd_client_man:ignore/0),
|
||||||
%% but the only one calling gen_tcp:accept/1 on it is this process. Closing the socket
|
%% but the only one calling gen_tcp:accept/1 on it is this process. Closing the socket
|
||||||
%% is one way a manager process can gracefully unblock child workers that are blocking
|
%% is one way a manager process can gracefully unblock child workers that are blocking
|
||||||
%% on a network accept.
|
%% on a network accept.
|
||||||
@ -110,12 +110,12 @@ listen(Parent, Debug, ListenSocket) ->
|
|||||||
{ok, Socket} ->
|
{ok, Socket} ->
|
||||||
{ok, _} = start(ListenSocket),
|
{ok, _} = start(ListenSocket),
|
||||||
{ok, Peer} = inet:peername(Socket),
|
{ok, Peer} = inet:peername(Socket),
|
||||||
ok = io:format("~p Connection accepted from: ~p~n", [self(), Peer]),
|
ok = tell("~p Connection accepted from: ~p~n", [self(), Peer]),
|
||||||
ok = fd_client_man:enroll(),
|
ok = fd_httpd_client_man:enroll(),
|
||||||
State = #s{socket = Socket},
|
State = #s{socket = Socket},
|
||||||
loop(Parent, Debug, State);
|
loop(Parent, Debug, State);
|
||||||
{error, closed} ->
|
{error, closed} ->
|
||||||
ok = io:format("~p Retiring: Listen socket closed.~n", [self()]),
|
ok = tell("~p Retiring: Listen socket closed.~n", [self()]),
|
||||||
exit(normal)
|
exit(normal)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@ -142,17 +142,17 @@ loop(Parent, Debug, State = #s{socket = Socket, next = Next0}) ->
|
|||||||
%% should trigger bad request
|
%% should trigger bad request
|
||||||
tell(error, "~p QHL parse error: ~tp", [?LINE, Error]),
|
tell(error, "~p QHL parse error: ~tp", [?LINE, Error]),
|
||||||
tell(error, "~p bad request:~n~ts", [?LINE, Received]),
|
tell(error, "~p bad request:~n~ts", [?LINE, Received]),
|
||||||
fd_http_utils:http_err(Socket, 400),
|
fd_httpd_utils:http_err(Socket, 400),
|
||||||
gen_tcp:shutdown(Socket, read_write),
|
gen_tcp:shutdown(Socket, read_write),
|
||||||
exit(normal)
|
exit(normal)
|
||||||
end;
|
end;
|
||||||
{tcp_closed, Socket} ->
|
{tcp_closed, Socket} ->
|
||||||
ok = io:format("~p Socket closed, retiring.~n", [self()]),
|
ok = tell("~p Socket closed, retiring.~n", [self()]),
|
||||||
exit(normal);
|
exit(normal);
|
||||||
{system, From, Request} ->
|
{system, From, Request} ->
|
||||||
sys:handle_system_msg(Request, From, Parent, ?MODULE, Debug, State);
|
sys:handle_system_msg(Request, From, Parent, ?MODULE, Debug, State);
|
||||||
Unexpected ->
|
Unexpected ->
|
||||||
ok = io:format("~p Unexpected message: ~tp", [self(), Unexpected]),
|
ok = tell("~p Unexpected message: ~tp", [self(), Unexpected]),
|
||||||
loop(Parent, Debug, State)
|
loop(Parent, Debug, State)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@ -232,7 +232,6 @@ handle_request(Sock, R = #request{method = M, path = P}, Received) when M =/= un
|
|||||||
|
|
||||||
route(Sock, get, Route, Request, Received) ->
|
route(Sock, get, Route, Request, Received) ->
|
||||||
case Route of
|
case Route of
|
||||||
<<"/ws/tetris">> -> ws_tetris(Sock, Request, Received);
|
|
||||||
<<"/ws/echo">> -> ws_echo(Sock, Request) , Received;
|
<<"/ws/echo">> -> ws_echo(Sock, Request) , Received;
|
||||||
<<"/">> -> route_static(Sock, <<"/index.html">>) , Received;
|
<<"/">> -> route_static(Sock, <<"/index.html">>) , Received;
|
||||||
_ -> route_static(Sock, Route) , Received
|
_ -> route_static(Sock, Route) , Received
|
||||||
@ -240,10 +239,10 @@ route(Sock, get, Route, Request, Received) ->
|
|||||||
route(Sock, post, Route, Request, Received) ->
|
route(Sock, post, Route, Request, Received) ->
|
||||||
case Route of
|
case Route of
|
||||||
<<"/wfcin">> -> wfcin(Sock, Request) , Received;
|
<<"/wfcin">> -> wfcin(Sock, Request) , Received;
|
||||||
_ -> fd_http_utils:http_err(Sock, 404) , Received
|
_ -> fd_httpd_utils:http_err(Sock, 404) , Received
|
||||||
end;
|
end;
|
||||||
route(Sock, _, _, _, Received) ->
|
route(Sock, _, _, _, Received) ->
|
||||||
fd_http_utils:http_err(Sock, 404),
|
fd_httpd_utils:http_err(Sock, 404),
|
||||||
Received.
|
Received.
|
||||||
|
|
||||||
|
|
||||||
@ -253,13 +252,13 @@ route(Sock, _, _, _, Received) ->
|
|||||||
Route :: binary().
|
Route :: binary().
|
||||||
|
|
||||||
route_static(Sock, Route) ->
|
route_static(Sock, Route) ->
|
||||||
respond_static(Sock, fd_sfc:query(Route)).
|
respond_static(Sock, fd_httpd_sfc:query(Route)).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-spec respond_static(Sock, MaybeEty) -> ok
|
-spec respond_static(Sock, MaybeEty) -> ok
|
||||||
when Sock :: gen_tcp:socket(),
|
when Sock :: gen_tcp:socket(),
|
||||||
MaybeEty :: fd_sfc:maybe_entry().
|
MaybeEty :: fd_httpd_sfc:maybe_entry().
|
||||||
|
|
||||||
respond_static(Sock, {found, Entry}) ->
|
respond_static(Sock, {found, Entry}) ->
|
||||||
% -record(e, {fs_path :: file:filename(),
|
% -record(e, {fs_path :: file:filename(),
|
||||||
@ -268,87 +267,18 @@ respond_static(Sock, {found, Entry}) ->
|
|||||||
% encoding :: encoding(),
|
% encoding :: encoding(),
|
||||||
% contents :: binary()}).
|
% contents :: binary()}).
|
||||||
Headers0 =
|
Headers0 =
|
||||||
case fd_sfc_entry:encoding(Entry) of
|
case fd_httpd_sfc_entry:encoding(Entry) of
|
||||||
gzip -> [{"content-encoding", "gzip"}];
|
gzip -> [{"content-encoding", "gzip"}];
|
||||||
none -> []
|
none -> []
|
||||||
end,
|
end,
|
||||||
Headers1 = [{"content-type", fd_sfc_entry:mime_type(Entry)} | Headers0],
|
Headers1 = [{"content-type", fd_httpd_sfc_entry:mime_type(Entry)} | Headers0],
|
||||||
Response = #response{headers = Headers1,
|
Response = #response{headers = Headers1,
|
||||||
body = fd_sfc_entry:contents(Entry)},
|
body = fd_httpd_sfc_entry:contents(Entry)},
|
||||||
fd_http_utils:respond(Sock, Response);
|
fd_httpd_utils:respond(Sock, Response);
|
||||||
respond_static(Sock, not_found) ->
|
respond_static(Sock, not_found) ->
|
||||||
fd_http_utils:http_err(Sock, 404).
|
fd_httpd_utils:http_err(Sock, 404).
|
||||||
|
|
||||||
|
|
||||||
%% ------------------------------
|
|
||||||
%% tetris
|
|
||||||
%% ------------------------------
|
|
||||||
|
|
||||||
-spec ws_tetris(Sock, Request, Received) -> NewReceived
|
|
||||||
when Sock :: gen_tcp:socket(),
|
|
||||||
Request :: request(),
|
|
||||||
Received :: binary(),
|
|
||||||
NewReceived :: binary().
|
|
||||||
|
|
||||||
ws_tetris(Sock, Request, Received) ->
|
|
||||||
error(nyi).
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-spec ws_tetris2(Sock, Request, Received) -> NewReceived
|
|
||||||
when Sock :: gen_tcp:socket(),
|
|
||||||
Request :: request(),
|
|
||||||
Received :: binary(),
|
|
||||||
NewReceived :: binary().
|
|
||||||
|
|
||||||
ws_tetris2(Sock, Request, Received) ->
|
|
||||||
%tell("~p: ws_tetris request: ~tp", [?LINE, Request]),
|
|
||||||
case qhl_ws:handshake(Request) of
|
|
||||||
{ok, Response} ->
|
|
||||||
fd_http_utils:respond(Sock, Response),
|
|
||||||
{ok, TetrisPid} = fd_tetris:start_link(),
|
|
||||||
ws_tetris_loop(Sock, TetrisPid, [], Received);
|
|
||||||
Error ->
|
|
||||||
tell("ws_tetris: error: ~tp", [Error]),
|
|
||||||
fd_http_utils:http_err(Sock, 400)
|
|
||||||
end.
|
|
||||||
|
|
||||||
|
|
||||||
-spec ws_tetris_loop(Sock, Tetris, Frames, Received) -> NewReceived
|
|
||||||
when Sock :: gen_tcp:socket(),
|
|
||||||
Tetris :: pid(),
|
|
||||||
Frames :: [qhl_ws:frame()],
|
|
||||||
Received :: binary(),
|
|
||||||
NewReceived :: binary().
|
|
||||||
|
|
||||||
ws_tetris_loop(Sock, Tetris, Frames, Received) ->
|
|
||||||
tell("~p:ws_tetris_loop(Sock, ~p, ~p, ~p)", [?MODULE, Tetris, Frames, Received]),
|
|
||||||
%% create tetris state
|
|
||||||
case inet:setopts(Sock, [{active, once}]) of
|
|
||||||
ok ->
|
|
||||||
receive
|
|
||||||
{tcp, Sock, Bin} ->
|
|
||||||
Rcv1 = <<Received/binary, Bin/binary>>,
|
|
||||||
case qhl_ws:recv(Sock, Rcv1, 3_000, Frames) of
|
|
||||||
{ok, WsMsg, NewFrames, Rcv2} ->
|
|
||||||
ok = fd_tetris:ws_msg(Tetris, WsMsg),
|
|
||||||
ws_tetris_loop(Sock, Tetris, NewFrames, Rcv2);
|
|
||||||
Error ->
|
|
||||||
error(Error)
|
|
||||||
end;
|
|
||||||
{tetris, Message} ->
|
|
||||||
ok = log(info, "~p tetris: ~p", [self(), Message]),
|
|
||||||
ok = qhl_ws:send(Sock, {text, Message}),
|
|
||||||
ws_tetris_loop(Sock, Tetris, Frames, Received);
|
|
||||||
{tcp_closed, Sock} -> {error, tcp_closed};
|
|
||||||
{tcp_error, Sock, Reason} -> {error, {tcp_error, Reason}}
|
|
||||||
after 30_000 ->
|
|
||||||
{error, timeout}
|
|
||||||
end;
|
|
||||||
{error, Reason} ->
|
|
||||||
{error, {inet, Reason}}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% ------------------------------
|
%% ------------------------------
|
||||||
%% echo
|
%% echo
|
||||||
%% ------------------------------
|
%% ------------------------------
|
||||||
@ -359,17 +289,17 @@ ws_echo(Sock, Request) ->
|
|||||||
catch
|
catch
|
||||||
X:Y:Z ->
|
X:Y:Z ->
|
||||||
tell(error, "CRASH ws_echo: ~tp:~tp:~tp", [X, Y, Z]),
|
tell(error, "CRASH ws_echo: ~tp:~tp:~tp", [X, Y, Z]),
|
||||||
fd_http_utils:http_err(Sock, 500)
|
fd_httpd_utils:http_err(Sock, 500)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
ws_echo2(Sock, Request) ->
|
ws_echo2(Sock, Request) ->
|
||||||
case qhl_ws:handshake(Request) of
|
case qhl_ws:handshake(Request) of
|
||||||
{ok, Response} ->
|
{ok, Response} ->
|
||||||
fd_http_utils:respond(Sock, Response),
|
fd_httpd_utils:respond(Sock, Response),
|
||||||
ws_echo_loop(Sock);
|
ws_echo_loop(Sock);
|
||||||
Error ->
|
Error ->
|
||||||
tell("ws_echo: error: ~tp", [Error]),
|
tell("ws_echo: error: ~tp", [Error]),
|
||||||
fd_http_utils:http_err(Sock, 400)
|
fd_httpd_utils:http_err(Sock, 400)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
ws_echo_loop(Sock) ->
|
ws_echo_loop(Sock) ->
|
||||||
@ -405,17 +335,17 @@ wfcin(Sock, #request{enctype = json,
|
|||||||
case wfc_read:expr(Input) of
|
case wfc_read:expr(Input) of
|
||||||
{ok, Expr, _Rest} ->
|
{ok, Expr, _Rest} ->
|
||||||
case wfc_eval:eval(Expr, Ctx0) of
|
case wfc_eval:eval(Expr, Ctx0) of
|
||||||
{ok, noop, Ctx1} -> {fd_http_utils:jsgud("<noop>"), Ctx1};
|
{ok, noop, Ctx1} -> {fd_httpd_utils:jsgud("<noop>"), Ctx1};
|
||||||
{ok, Sentence, Ctx1} -> {fd_http_utils:jsgud(wfc_pp:sentence(Sentence)), Ctx1};
|
{ok, Sentence, Ctx1} -> {fd_httpd_utils:jsgud(wfc_pp:sentence(Sentence)), Ctx1};
|
||||||
{error, Message} -> {fd_http_utils:jsbad(Message), Ctx0}
|
{error, Message} -> {fd_httpd_utils:jsbad(Message), Ctx0}
|
||||||
end;
|
end;
|
||||||
{error, Message} ->
|
{error, Message} ->
|
||||||
{fd_http_utils:jsbad(Message), Ctx0}
|
{fd_httpd_utils:jsbad(Message), Ctx0}
|
||||||
end
|
end
|
||||||
catch
|
catch
|
||||||
error:E:S ->
|
error:E:S ->
|
||||||
ErrorMessage = unicode:characters_to_list(io_lib:format("parser crashed: ~p:~p", [E, S])),
|
ErrorMessage = unicode:characters_to_list(io_lib:format("parser crashed: ~p:~p", [E, S])),
|
||||||
{fd_http_utils:jsbad(ErrorMessage), Ctx0}
|
{fd_httpd_utils:jsbad(ErrorMessage), Ctx0}
|
||||||
end,
|
end,
|
||||||
% update cache with new context
|
% update cache with new context
|
||||||
ok = fd_cache:set(Cookie, NewCtx),
|
ok = fd_cache:set(Cookie, NewCtx),
|
||||||
@ -423,10 +353,10 @@ wfcin(Sock, #request{enctype = json,
|
|||||||
Response = #response{headers = [{"content-type", "application/json"},
|
Response = #response{headers = [{"content-type", "application/json"},
|
||||||
{"set-cookie", ["wfc=", Cookie]}],
|
{"set-cookie", ["wfc=", Cookie]}],
|
||||||
body = Body},
|
body = Body},
|
||||||
fd_http_utils:respond(Sock, Response);
|
fd_httpd_utils:respond(Sock, Response);
|
||||||
wfcin(Sock, Request) ->
|
wfcin(Sock, Request) ->
|
||||||
tell("wfcin: bad request: ~tp", [Request]),
|
tell("wfcin: bad request: ~tp", [Request]),
|
||||||
fd_http_utils:http_err(Sock, 400).
|
fd_httpd_utils:http_err(Sock, 400).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -441,7 +371,7 @@ ctx(#{<<"wfc">> := Cookie}) ->
|
|||||||
error -> {Cookie, wfc_eval_context:default()}
|
error -> {Cookie, wfc_eval_context:default()}
|
||||||
end;
|
end;
|
||||||
ctx(_) ->
|
ctx(_) ->
|
||||||
{fd_http_utils:new_cookie(), wfc_eval_context:default()}.
|
{fd_httpd_utils:new_cookie(), wfc_eval_context:default()}.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -9,7 +9,7 @@
|
|||||||
%%% OTP should take care of for us.
|
%%% OTP should take care of for us.
|
||||||
%%% @end
|
%%% @end
|
||||||
|
|
||||||
-module(fd_http_client_man).
|
-module(fd_httpd_client_man).
|
||||||
-vsn("0.1.0").
|
-vsn("0.1.0").
|
||||||
-behavior(gen_server).
|
-behavior(gen_server).
|
||||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||||
@ -23,6 +23,8 @@
|
|||||||
code_change/3, terminate/2]).
|
code_change/3, terminate/2]).
|
||||||
|
|
||||||
|
|
||||||
|
-include("$zx_include/zx_logger.hrl").
|
||||||
|
|
||||||
%%% Type and Record Definitions
|
%%% Type and Record Definitions
|
||||||
|
|
||||||
|
|
||||||
@ -92,7 +94,7 @@ echo(Message) ->
|
|||||||
when Result :: {ok, pid()}
|
when Result :: {ok, pid()}
|
||||||
| {error, Reason :: term()}.
|
| {error, Reason :: term()}.
|
||||||
%% @private
|
%% @private
|
||||||
%% This should only ever be called by fd_clients (the service-level supervisor).
|
%% This should only ever be called by fd_httpd_clients (the service-level supervisor).
|
||||||
|
|
||||||
start_link() ->
|
start_link() ->
|
||||||
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
|
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
|
||||||
@ -104,8 +106,9 @@ start_link() ->
|
|||||||
%% preparatory work necessary for proper function.
|
%% preparatory work necessary for proper function.
|
||||||
|
|
||||||
init(none) ->
|
init(none) ->
|
||||||
ok = io:format("Starting.~n"),
|
ok = tell("Starting fd_httpd_client_man."),
|
||||||
State = #s{},
|
State = #s{},
|
||||||
|
ok = tell("fd_httpd_client_man init state: ~tp", [State]),
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
|
|
||||||
@ -130,7 +133,7 @@ handle_call({listen, PortNum}, _, State) ->
|
|||||||
{Response, NewState} = do_listen(PortNum, State),
|
{Response, NewState} = do_listen(PortNum, State),
|
||||||
{reply, Response, NewState};
|
{reply, Response, NewState};
|
||||||
handle_call(Unexpected, From, State) ->
|
handle_call(Unexpected, From, State) ->
|
||||||
ok = io:format("~p Unexpected call from ~tp: ~tp~n", [self(), From, Unexpected]),
|
ok = tell("~p Unexpected call from ~tp: ~tp~n", [self(), From, Unexpected]),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
|
|
||||||
@ -152,7 +155,7 @@ handle_cast(ignore, State) ->
|
|||||||
NewState = do_ignore(State),
|
NewState = do_ignore(State),
|
||||||
{noreply, NewState};
|
{noreply, NewState};
|
||||||
handle_cast(Unexpected, State) ->
|
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}.
|
{noreply, State}.
|
||||||
|
|
||||||
|
|
||||||
@ -168,7 +171,7 @@ handle_info({'DOWN', Mon, process, Pid, Reason}, State) ->
|
|||||||
NewState = handle_down(Mon, Pid, Reason, State),
|
NewState = handle_down(Mon, Pid, Reason, State),
|
||||||
{noreply, NewState};
|
{noreply, NewState};
|
||||||
handle_info(Unexpected, State) ->
|
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}.
|
{noreply, State}.
|
||||||
|
|
||||||
|
|
||||||
@ -225,10 +228,10 @@ do_listen(PortNum, State = #s{port_num = none}) ->
|
|||||||
{keepalive, true},
|
{keepalive, true},
|
||||||
{reuseaddr, true}],
|
{reuseaddr, true}],
|
||||||
{ok, Listener} = gen_tcp:listen(PortNum, SocketOptions),
|
{ok, Listener} = gen_tcp:listen(PortNum, SocketOptions),
|
||||||
{ok, _} = fd_client:start(Listener),
|
{ok, _} = fd_httpd_client:start(Listener),
|
||||||
{ok, State#s{port_num = PortNum, listener = Listener}};
|
{ok, State#s{port_num = PortNum, listener = Listener}};
|
||||||
do_listen(_, State = #s{port_num = PortNum}) ->
|
do_listen(_, State = #s{port_num = PortNum}) ->
|
||||||
ok = io:format("~p Already listening on ~p~n", [self(), PortNum]),
|
ok = tell("~p Already listening on ~p~n", [self(), PortNum]),
|
||||||
{{error, {listening, PortNum}}, State}.
|
{{error, {listening, PortNum}}, State}.
|
||||||
|
|
||||||
|
|
||||||
@ -254,7 +257,7 @@ do_enroll(Pid, State = #s{clients = Clients}) ->
|
|||||||
case lists:member(Pid, Clients) of
|
case lists:member(Pid, Clients) of
|
||||||
false ->
|
false ->
|
||||||
Mon = monitor(process, Pid),
|
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]};
|
State#s{clients = [Pid | Clients]};
|
||||||
true ->
|
true ->
|
||||||
State
|
State
|
||||||
@ -292,6 +295,6 @@ handle_down(Mon, Pid, Reason, State = #s{clients = Clients}) ->
|
|||||||
State#s{clients = NewClients};
|
State#s{clients = NewClients};
|
||||||
false ->
|
false ->
|
||||||
Unexpected = {'DOWN', Mon, process, Pid, Reason},
|
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
|
State
|
||||||
end.
|
end.
|
||||||
@ -2,8 +2,8 @@
|
|||||||
%%% front end web development lab Client Supervisor
|
%%% front end web development lab Client Supervisor
|
||||||
%%%
|
%%%
|
||||||
%%% This process supervises the client socket handlers themselves. It is a peer of the
|
%%% This process supervises the client socket handlers themselves. It is a peer of the
|
||||||
%%% fd_client_man (the manager interface to this network service component),
|
%%% fd_httpd_client_man (the manager interface to this network service component),
|
||||||
%%% and a child of the supervisor named fd_clients.
|
%%% and a child of the supervisor named fd_httpd_clients.
|
||||||
%%%
|
%%%
|
||||||
%%% Because we don't know (or care) how many client connections the server may end up
|
%%% Because we don't know (or care) how many client connections the server may end up
|
||||||
%%% handling this is a simple_one_for_one supervisor which can spawn and manage as
|
%%% handling this is a simple_one_for_one supervisor which can spawn and manage as
|
||||||
@ -13,7 +13,7 @@
|
|||||||
%%% http://erlang.org/doc/design_principles/sup_princ.html#id79244
|
%%% http://erlang.org/doc/design_principles/sup_princ.html#id79244
|
||||||
%%% @end
|
%%% @end
|
||||||
|
|
||||||
-module(fd_http_client_sup).
|
-module(fd_httpd_client_sup).
|
||||||
-vsn("0.1.0").
|
-vsn("0.1.0").
|
||||||
-behaviour(supervisor).
|
-behaviour(supervisor).
|
||||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||||
@ -35,9 +35,9 @@
|
|||||||
| {shutdown, term()}
|
| {shutdown, term()}
|
||||||
| term().
|
| term().
|
||||||
%% @private
|
%% @private
|
||||||
%% Spawns the first listener at the request of the fd_client_man when
|
%% Spawns the first listener at the request of the fd_httpd_client_man when
|
||||||
%% fewd:listen/1 is called, or the next listener at the request of the
|
%% fewd:listen/1 is called, or the next listener at the request of the
|
||||||
%% currently listening fd_client when a connection is made.
|
%% currently listening fd_httpd_client when a connection is made.
|
||||||
%%
|
%%
|
||||||
%% Error conditions, supervision strategies and other important issues are
|
%% Error conditions, supervision strategies and other important issues are
|
||||||
%% explained in the supervisor module docs:
|
%% explained in the supervisor module docs:
|
||||||
@ -61,10 +61,10 @@ start_link() ->
|
|||||||
|
|
||||||
init(none) ->
|
init(none) ->
|
||||||
RestartStrategy = {simple_one_for_one, 1, 60},
|
RestartStrategy = {simple_one_for_one, 1, 60},
|
||||||
Client = {fd_client,
|
Client = {fd_httpd_client,
|
||||||
{fd_client, start_link, []},
|
{fd_httpd_client, start_link, []},
|
||||||
temporary,
|
temporary,
|
||||||
brutal_kill,
|
brutal_kill,
|
||||||
worker,
|
worker,
|
||||||
[fd_client]},
|
[fd_httpd_client]},
|
||||||
{ok, {RestartStrategy, [Client]}}.
|
{ok, {RestartStrategy, [Client]}}.
|
||||||
@ -1,14 +1,14 @@
|
|||||||
%%% @doc
|
%%% @doc
|
||||||
%%% front end web development lab Clerk Service Supervisor
|
%%% front end web development lab Client Service Supervisor
|
||||||
%%%
|
%%%
|
||||||
%%% This is the service-level supervisor of the system. It is the parent of both the
|
%%% This is the service-level supervisor of the system. It is the parent of both the
|
||||||
%%% clerk connection handlers and the clerk manager (which manages the clerk
|
%%% client connection handlers and the client manager (which manages the client
|
||||||
%%% connection handlers). This is the child of fd_sup.
|
%%% connection handlers). This is the child of fd_sup.
|
||||||
%%%
|
%%%
|
||||||
%%% See: http://erlang.org/doc/apps/kernel/application.html
|
%%% See: http://erlang.org/doc/apps/kernel/application.html
|
||||||
%%% @end
|
%%% @end
|
||||||
|
|
||||||
-module(fd_httpd_clerks).
|
-module(fd_httpd_clients).
|
||||||
-vsn("0.1.0").
|
-vsn("0.1.0").
|
||||||
-behavior(supervisor).
|
-behavior(supervisor).
|
||||||
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
-author("Peter Harpending <peterharpending@qpq.swiss>").
|
||||||
@ -32,17 +32,17 @@ start_link() ->
|
|||||||
|
|
||||||
init(none) ->
|
init(none) ->
|
||||||
RestartStrategy = {rest_for_one, 1, 60},
|
RestartStrategy = {rest_for_one, 1, 60},
|
||||||
HttpClerkMan = {fd_http_clerk_man,
|
HttpClientMan = {fd_httpd_client_man,
|
||||||
{fd_http_clerk_man, start_link, []},
|
{fd_httpd_client_man, start_link, []},
|
||||||
permanent,
|
permanent,
|
||||||
5000,
|
5000,
|
||||||
worker,
|
worker,
|
||||||
[fd_http_clerk_man]},
|
[fd_httpd_client_man]},
|
||||||
HttpClerkSup = {fd_http_clerk_sup,
|
HttpClientSup = {fd_httpd_client_sup,
|
||||||
{fd_http_clerk_sup, start_link, []},
|
{fd_httpd_client_sup, start_link, []},
|
||||||
permanent,
|
permanent,
|
||||||
5000,
|
5000,
|
||||||
supervisor,
|
supervisor,
|
||||||
[fd_http_clerk_sup]},
|
[fd_httpd_client_sup]},
|
||||||
Children = [HttpClerkSup, HttpClerkMan],
|
Children = [HttpClientSup, HttpClientMan],
|
||||||
{ok, {RestartStrategy, Children}}.
|
{ok, {RestartStrategy, Children}}.
|
||||||
@ -21,12 +21,12 @@
|
|||||||
|
|
||||||
-include("$zx_include/zx_logger.hrl").
|
-include("$zx_include/zx_logger.hrl").
|
||||||
|
|
||||||
-type entry() :: fd_sfc_entry:entry().
|
-type entry() :: fd_httpd_sfc_entry:entry().
|
||||||
-type maybe_entry() :: {found, fd_sfc_entry:entry()} | not_found.
|
-type maybe_entry() :: {found, fd_httpd_sfc_entry:entry()} | not_found.
|
||||||
|
|
||||||
|
|
||||||
-record(s, {base_path = base_path() :: file:filename(),
|
-record(s, {base_path = base_path() :: file:filename(),
|
||||||
cache = fd_sfc_cache:new(base_path()) :: fd_sfc_cache:cache(),
|
cache = fd_httpd_sfc_cache:new(base_path()) :: fd_httpd_sfc_cache:cache(),
|
||||||
auto_renew = 0_500 :: pos_integer()}).
|
auto_renew = 0_500 :: pos_integer()}).
|
||||||
%-type state() :: #s{}.
|
%-type state() :: #s{}.
|
||||||
|
|
||||||
@ -62,14 +62,14 @@ start_link() ->
|
|||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
|
|
||||||
init(none) ->
|
init(none) ->
|
||||||
tell("starting fd_sfc"),
|
tell("starting fd_httpd_sfc"),
|
||||||
InitState = #s{},
|
InitState = #s{},
|
||||||
erlang:send_after(InitState#s.auto_renew, self(), auto_renew),
|
erlang:send_after(InitState#s.auto_renew, self(), auto_renew),
|
||||||
{ok, InitState}.
|
{ok, InitState}.
|
||||||
|
|
||||||
|
|
||||||
handle_call({query, Path}, _, State = #s{cache = Cache}) ->
|
handle_call({query, Path}, _, State = #s{cache = Cache}) ->
|
||||||
Reply = fd_sfc_cache:query(Path, Cache),
|
Reply = fd_httpd_sfc_cache:query(Path, Cache),
|
||||||
{reply, Reply, State};
|
{reply, Reply, State};
|
||||||
handle_call(Unexpected, From, State) ->
|
handle_call(Unexpected, From, State) ->
|
||||||
tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]),
|
tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]),
|
||||||
@ -106,6 +106,6 @@ terminate(_, _) ->
|
|||||||
%%-----------------------------------------------------------------------------
|
%%-----------------------------------------------------------------------------
|
||||||
|
|
||||||
i_renew(State = #s{base_path = BasePath}) ->
|
i_renew(State = #s{base_path = BasePath}) ->
|
||||||
NewCache = fd_sfc_cache:new(BasePath),
|
NewCache = fd_httpd_sfc_cache:new(BasePath),
|
||||||
NewState = State#s{cache = NewCache},
|
NewState = State#s{cache = NewCache},
|
||||||
NewState.
|
NewState.
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
% @doc
|
% @doc
|
||||||
% cache data management
|
% cache data management
|
||||||
-module(fd_sfc_cache).
|
-module(fd_httpd_sfc_cache).
|
||||||
|
|
||||||
-export_type([
|
-export_type([
|
||||||
cache/0
|
cache/0
|
||||||
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
-include("$zx_include/zx_logger.hrl").
|
-include("$zx_include/zx_logger.hrl").
|
||||||
|
|
||||||
-type cache() :: #{HttpPath :: binary() := Entry :: fd_sfc_entry:entry()}.
|
-type cache() :: #{HttpPath :: binary() := Entry :: fd_httpd_sfc_entry:entry()}.
|
||||||
|
|
||||||
|
|
||||||
-spec query(HttpPath, Cache) -> Result
|
-spec query(HttpPath, Cache) -> Result
|
||||||
@ -21,7 +21,7 @@
|
|||||||
Cache :: cache(),
|
Cache :: cache(),
|
||||||
Result :: {found, Entry}
|
Result :: {found, Entry}
|
||||||
| not_found,
|
| not_found,
|
||||||
Entry :: fd_sfc_entry:entry().
|
Entry :: fd_httpd_sfc_entry:entry().
|
||||||
|
|
||||||
query(HttpPath, Cache) ->
|
query(HttpPath, Cache) ->
|
||||||
case maps:find(HttpPath, Cache) of
|
case maps:find(HttpPath, Cache) of
|
||||||
@ -63,7 +63,7 @@ new2(BasePath) ->
|
|||||||
BAbsPath = unicode:characters_to_binary(AbsPath),
|
BAbsPath = unicode:characters_to_binary(AbsPath),
|
||||||
HttpPath = remove_prefix(BBaseDir, BAbsPath),
|
HttpPath = remove_prefix(BBaseDir, BAbsPath),
|
||||||
NewCache =
|
NewCache =
|
||||||
case fd_sfc_entry:new(AbsPath) of
|
case fd_httpd_sfc_entry:new(AbsPath) of
|
||||||
{found, Entry} -> maps:put(HttpPath, Entry, AccCache);
|
{found, Entry} -> maps:put(HttpPath, Entry, AccCache);
|
||||||
not_found -> AccCache
|
not_found -> AccCache
|
||||||
end,
|
end,
|
||||||
@ -1,7 +1,7 @@
|
|||||||
% @doc non-servery functions for static file caching
|
% @doc non-servery functions for static file caching
|
||||||
%
|
%
|
||||||
% this spams the filesystem, so it's not "pure" code
|
% this spams the filesystem, so it's not "pure" code
|
||||||
-module(fd_sfc_entry).
|
-module(fd_httpd_sfc_entry).
|
||||||
|
|
||||||
-export_type([
|
-export_type([
|
||||||
encoding/0,
|
encoding/0,
|
||||||
@ -1,5 +1,5 @@
|
|||||||
% @doc http utility functions
|
% @doc http utility functions
|
||||||
-module(fd_http_utils).
|
-module(fd_httpd_utils).
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
new_cookie/0,
|
new_cookie/0,
|
||||||
@ -36,17 +36,17 @@ start_link() ->
|
|||||||
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
RestartStrategy = {one_for_one, 1, 60},
|
RestartStrategy = {one_for_one, 1, 60},
|
||||||
Httpd = {fd_httpd,
|
|
||||||
{fd_httpd, start_link, []},
|
|
||||||
permanent,
|
|
||||||
5000,
|
|
||||||
supervisor,
|
|
||||||
[fd_httpd]},
|
|
||||||
Cache = {fd_cache,
|
Cache = {fd_cache,
|
||||||
{fd_cache, start_link, []},
|
{fd_cache, start_link, []},
|
||||||
permanent,
|
permanent,
|
||||||
5000,
|
5000,
|
||||||
worker,
|
worker,
|
||||||
[fd_cache]},
|
[fd_cache]},
|
||||||
Children = [Httpd, Cache],
|
Httpd = {fd_httpd,
|
||||||
|
{fd_httpd, start_link, []},
|
||||||
|
permanent,
|
||||||
|
5000,
|
||||||
|
supervisor,
|
||||||
|
[fd_httpd]},
|
||||||
|
Children = [Cache, Httpd],
|
||||||
{ok, {RestartStrategy, Children}}.
|
{ok, {RestartStrategy, Children}}.
|
||||||
|
|||||||
@ -24,7 +24,7 @@
|
|||||||
%% Returns an {error, Reason} tuple if it is already listening.
|
%% Returns an {error, Reason} tuple if it is already listening.
|
||||||
|
|
||||||
listen(PortNum) ->
|
listen(PortNum) ->
|
||||||
fd_http:listen(PortNum).
|
fd_httpd_client_man:listen(PortNum).
|
||||||
|
|
||||||
|
|
||||||
-spec ignore() -> ok.
|
-spec ignore() -> ok.
|
||||||
@ -32,7 +32,7 @@ listen(PortNum) ->
|
|||||||
%% Make the server stop listening if it is, or continue to do nothing if it isn't.
|
%% Make the server stop listening if it is, or continue to do nothing if it isn't.
|
||||||
|
|
||||||
ignore() ->
|
ignore() ->
|
||||||
fd_http:ignore().
|
fd_httpd_client_man:ignore().
|
||||||
|
|
||||||
|
|
||||||
-spec start(normal, term()) -> {ok, pid()}.
|
-spec start(normal, term()) -> {ok, pid()}.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user