diff options
Diffstat (limited to 'p11p-daemon/src/p11p_remote.erl')
-rw-r--r-- | p11p-daemon/src/p11p_remote.erl | 84 |
1 files changed, 50 insertions, 34 deletions
diff --git a/p11p-daemon/src/p11p_remote.erl b/p11p-daemon/src/p11p_remote.erl index d0f9184..b27b333 100644 --- a/p11p-daemon/src/p11p_remote.erl +++ b/p11p-daemon/src/p11p_remote.erl @@ -10,14 +10,14 @@ %% times out, inform the remote manager (our parent). %% TODO: "remote" is not a great name and we shouldn't just inherit it -%% from p11p-kit +%% from p11p-kit. Let's use "client" or "proxy_client". -module(p11p_remote). -behaviour(gen_server). %% API. -export([start_link/4]). --export([request/2, add_to_outbuf/2, stop/2]). +-export([request/2, stop/2]). -include("p11p_rpc.hrl"). @@ -30,24 +30,24 @@ port :: port(), replyto :: pid() | undefined, timer :: reference() | undefined, - token :: string(), % Name - outbuf = <<>> :: binary(), - msg :: p11rpc:msg() | 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()) -> {ok, pid()} | {error, term()}. +-spec start_link(atom(), string(), string(), list()) -> + {ok, pid()} | {error, term()}. start_link(ServName, TokName, ModPath, ModEnv) -> lager:info("~p: p11p_remote starting for ~s", [ServName, ModPath]), - gen_server:start_link({local, ServName}, ?MODULE, [TokName, ModPath, ModEnv], []). + gen_server:start_link({local, ServName}, ?MODULE, + [TokName, ModPath, ModEnv], []). --spec request(pid(), p11rpc_msg()) -> ok. +-spec request(pid(), p11rpc_msg()) -> {ok, non_neg_integer()}. request(Remote, Request) -> gen_server:call(Remote, {request, Request}). -add_to_outbuf(Remote, Data) -> - gen_server:call(Remote, {add_to_outbuf, Data}). - %% Use stop/1 instead of gen_server:stop/1 if you're uncertain whether %% Pid is alive or not. An example of when that can happen is when the %% manager receiving a server_event about a lost client. If the server @@ -70,37 +70,54 @@ init([TokName, ModPath, ModEnv]) -> lager:debug("~p: ~s: module: ~s, env: ~p", [self(), RemoteBinPath, ModPath, ModEnv]), {ok, #state{port = Port, token = TokName}}. -handle_call({add_to_outbuf, Data}, _From, State) -> - {reply, ok, do_add_to_outbuf(Data, State)}; -handle_call({request, Request}, {FromPid, _Tag}, #state{port = Port} = S) -> +handle_call({request, Request}, {FromPid, _Tag}, + #state{port = Port, send_count = Sent} = S) -> %%lager:debug("~p: sending request from ~p to remote ~p", [self(), FromPid, Port]), - State = do_send(do_add_to_outbuf(p11p_rpc:serialise(Request), S)), - {reply, ok, State#state{replyto = FromPid, timer = start_timer(Port)}}; -handle_call(Request, _From, State) -> - lager:debug("~p: Unhandled call: ~p~n", [self(), Request]), + D = p11p_rpc:serialise(Request), + Buf = case Sent of + 0 -> <<?RPC_VERSION:8, D/binary>>; + _ -> D + end, + ok = do_send(Port, Buf), + {reply, {ok, sizeBuf}, S#state{replyto = FromPid, timer = start_timer(Port), + send_count = Sent + 1}}; + +handle_call(Call, _From, State) -> + lager:debug("~p: Unhandled call: ~p~n", [self(), Call]), {reply, unhandled, State}. handle_cast({stop, Reason}, State) -> {stop, Reason, State}; + handle_cast(Cast, State) -> lager:debug("~p: unhandled cast: ~p~n", [self(), Cast]), {noreply, State}. -%% TODO: dedup code w/ p11p_server -handle_info({Port, {data, Data}}, #state{replyto = Pid} = State) +%% Receiving the very first response from remote since it was started. +handle_info({Port, {data, Data}}, State) when Port == State#state.port, State#state.msg == undefined -> - Version = hd(Data), % First octet is version. - {ok, _BytesAdded} = p11p_server:add_to_clientbuf(Pid, <<Version>>), - {noreply, handle_remote_data(State, p11p_rpc:new(), tl(Data))}; + case hd(Data) of % First octet is RPC protocol version. + ?RPC_VERSION -> + {noreply, handle_remote_data(State, p11p_rpc:new(), tl(Data))}; + BadVersion -> + lager:info("~p: ~p: invalid RPC version: ~p", [self(), Port, + BadVersion]), + {noreply, State} + end; + +%% Receiving more data from remote. handle_info({Port, {data, Data}}, #state{msg = Msg} = State) when Port == State#state.port -> {noreply, handle_remote_data(State, Msg, Data)}; + +%% Remote timed out. handle_info({timeout, Timer, Port}, #state{token = Tok, replyto = Server} = S) when Port == S#state.port, Timer == S#state.timer -> lager:info("~p: rpc request timed out, exiting", [self()]), p11p_remote_manager:server_event(timeout, [Tok, Server]), State = S#state{timer = undefined}, {stop, normal, State}; + handle_info(Info, State) -> lager:debug("~p: Unhandled info: ~p~n", [self(), Info]), {noreply, State}. @@ -114,11 +131,7 @@ code_change(_OldVersion, State, _Extra) -> {ok, State}. %% Private -do_add_to_outbuf(Data, #state{outbuf = OutBuf} = State) -> - %%lager:debug("~p: adding ~B octets to outbuf", [self(), size(Data)]), - State#state{outbuf = <<OutBuf/binary, Data/binary>>}. - -do_send(#state{port = Port, outbuf = Buf} = State) -> +do_send(Port, Buf) -> %%lager:debug("~p: sending ~B octets to remote", [self(), size(Buf)]), %% case rand:uniform(15) of @@ -128,17 +141,20 @@ do_send(#state{port = Port, outbuf = Buf} = State) -> %% port_command(Port, Buf) %% end, - port_command(Port, Buf), - State#state{outbuf = <<>>}. + true = port_command(Port, Buf), + ok. -handle_remote_data(#state{replyto = Pid, timer = Timer} = S, MsgIn, DataIn) -> +handle_remote_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), {ok, _BytesSent} = p11p_server:reply(Pid, Msg), - S#state{msg = p11p_rpc:new(Msg#p11rpc_msg.buffer)}; - {needmore, Msg} -> - S#state{msg = 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) -> |