diff options
Diffstat (limited to 'p11p-daemon/src/p11p_client.erl')
-rw-r--r-- | p11p-daemon/src/p11p_client.erl | 76 |
1 files changed, 40 insertions, 36 deletions
diff --git a/p11p-daemon/src/p11p_client.erl b/p11p-daemon/src/p11p_client.erl index 1222505..7dc3457 100644 --- a/p11p-daemon/src/p11p_client.erl +++ b/p11p-daemon/src/p11p_client.erl @@ -6,14 +6,14 @@ %% Receive p11 requests from p11p_server, forward them to the proxy app, %% wait for a reply. If a reply is received within a timeout period, -%% forward the reply to the requesting p11p_server. If the request +%% proxy the reply to the requesting p11p_server. If the request %% times out, inform the manager (our parent). -module(p11p_client). -behaviour(gen_server). %% API. --export([start_link/4]). +-export([start_link/6]). -export([request/2, stop/2]). -include("p11p_rpc.hrl"). @@ -24,38 +24,40 @@ %% Records and types. -record(state, { + token :: string(), % Token name. + timeout :: non_neg_integer(), + port :: port(), replyto :: pid() | undefined, timer :: reference() | undefined, - token :: string(), % Token name. msg :: p11rpc:msg() | undefined, recv_count = 0 :: non_neg_integer(), send_count = 0 :: non_neg_integer() }). %% API. --spec start_link(atom(), string(), string(), list()) -> +-spec start_link(atom(), string(), pid(), string(), list(), non_neg_integer()) -> {ok, pid()} | {error, term()}. -start_link(ServName, TokName, ModPath, ModEnv) -> - lager:info("~p: p11p_client starting for ~s", [ServName, ModPath]), +start_link(ServName, TokName, Server, ModPath, ModEnv, Timeout) -> + lager:info("~p: starting p11p_client for ~s", [self(), TokName]), gen_server:start_link({local, ServName}, ?MODULE, - [TokName, ModPath, ModEnv], []). + [TokName, Server, ModPath, ModEnv, Timeout], []). -spec request(pid(), p11rpc_msg()) -> {ok, non_neg_integer()}. request(Client, Request) -> gen_server:call(Client, {request, Request}). %% Use stop/1 instead of gen_server:stop/1 if you're uncertain whether -%% we (Pid) are alive or not. An example of when that can happen is when the -%% manager receives a server_event about a lost p11 app. If the server -%% process terminated on request from us because we timed out on -%% an rpc call, chances are that we have already terminated by -%% the time the manager is to act on the lost app. +%% we (Pid) are alive or not. An example of when that can happen is +%% when the manager receives a server_event about a lost p11 app. If +%% the server process terminated on request from us because we timed +%% out on an rpc call, chances are that we have already terminated by +%% the time the manager acts on the information about the lost app. stop(Pid, Reason) -> gen_server:cast(Pid, {stop, Reason}). %% Genserver callbacks. -init([TokName, ModPath, ModEnv]) -> +init([TokName, Server, ModPath, ModEnv, Timeout]) -> ProxyAppBinPath = p11p_config:proxyapp_bin_path(), Port = open_port({spawn_executable, ProxyAppBinPath}, [stream, @@ -63,9 +65,10 @@ init([TokName, ModPath, ModEnv]) -> {env, ModEnv}, {args, [ModPath, "-v"]} % FIXME: Remove -v ]), + true = is_port(Port), lager:debug("~p: ~s: new proxy app port: ~p", [self(), ProxyAppBinPath, Port]), lager:debug("~p: ~s: module: ~s, env: ~p", [self(), ProxyAppBinPath, ModPath, ModEnv]), - {ok, #state{port = Port, token = TokName}}. + {ok, #state{port = Port, token = TokName, replyto = Server, timeout = Timeout}}. handle_call({request, Request}, {FromPid, _Tag}, #state{port = Port, send_count = Sent} = S) -> @@ -75,8 +78,9 @@ handle_call({request, Request}, {FromPid, _Tag}, 0 -> <<?RPC_VERSION:8, D/binary>>; _ -> D end, - ok = do_send(Port, Buf), - {reply, {ok, sizeBuf}, S#state{replyto = FromPid, timer = start_timer(Port), + {ok, _} = do_send(Port, Buf), + {reply, {ok, sizeBuf}, S#state{replyto = FromPid, + timer = start_timer(S#state.timeout, Port), send_count = Sent + 1}}; handle_call(Call, _From, State) -> @@ -95,7 +99,7 @@ handle_info({Port, {data, Data}}, State) when Port == State#state.port, State#state.msg == undefined -> case hd(Data) of % First octet is RPC protocol version. ?RPC_VERSION -> - {noreply, handle_proxy_app_data(State, p11p_rpc:new(), tl(Data))}; + {noreply, handle_token_data(State, p11p_rpc:new(), tl(Data))}; BadVersion -> lager:info("~p: ~p: invalid RPC version: ~p", [self(), Port, BadVersion]), @@ -105,13 +109,13 @@ handle_info({Port, {data, Data}}, State) %% Receiving more data from proxy app. handle_info({Port, {data, Data}}, #state{msg = Msg} = State) when Port == State#state.port -> - {noreply, handle_proxy_app_data(State, Msg, Data)}; + {noreply, handle_token_data(State, Msg, Data)}; %% Proxy app timed out. -handle_info({timeout, Timer, Port}, #state{token = Tok, replyto = Server} = S) +handle_info({timeout, Timer, Port}, S = #state{token = Tok}) when Port == S#state.port, Timer == S#state.timer -> - lager:info("~p: rpc request timed out, exiting", [self()]), - p11p_manager:server_event(timeout, [Tok, Server]), + lager:info("~p: rpc request for ~s timed out, exiting", [self(), Tok]), + p11p_manager:client_event(timeout, Tok), State = S#state{timer = undefined}, {stop, normal, State}; @@ -129,34 +133,34 @@ code_change(_OldVersion, State, _Extra) -> %% Private do_send(Port, Buf) -> - %%lager:debug("~p: sending ~B octets to proxy app", [self(), size(Buf)]), - - %% case rand:uniform(15) of - %% 1 -> - %% lager:debug("~p: faking unresponsive proxy app (~p) by not sending it any.", [self(), Port]); - %% _ -> - %% port_command(Port, Buf) - %% end, - - true = port_command(Port, Buf), - ok. - -handle_proxy_app_data(#state{replyto = Pid, timer = Timer, recv_count = Recv} = S, + Rand = rand:uniform(100), %% + 10, + if + Rand =< 10 -> + lager:debug("~p: faking unresponsive token (~p) by not sending", + [self(), Port]); + true -> + lager:debug("~p: sending ~B octets to token", [self(), size(Buf)]), + true = port_command(Port, Buf) + end, + {ok, size(Buf)}. + +handle_token_data(#state{replyto = Pid, timer = Timer, recv_count = Recv} = S, MsgIn, DataIn) -> case p11p_rpc:parse(MsgIn, list_to_binary(DataIn)) of {needmore, Msg} -> S#state{msg = Msg}; {done, Msg} -> cancel_timer(Timer), + lager:debug("~p: <- ~s", [self(), p11p_rpc:dump(Msg)]), {ok, _BytesSent} = p11p_server:reply(Pid, Msg), %% Saving potential data not consumed by parse/2 in new message. S#state{msg = p11p_rpc:new(Msg#p11rpc_msg.buffer), recv_count = Recv + 1} end. -start_timer(Port) -> +start_timer(Timeout, Port) -> %%lager:debug("~p: starting timer", [self()]), - erlang:start_timer(3000, self(), Port). + erlang:start_timer(Timeout, self(), Port). cancel_timer(Timer) -> %%lager:debug("~p: canceling timer", [self()]), |