diff options
Diffstat (limited to 'p11p-daemon/src/p11p_remote.erl')
-rw-r--r-- | p11p-daemon/src/p11p_remote.erl | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/p11p-daemon/src/p11p_remote.erl b/p11p-daemon/src/p11p_remote.erl index 84299c5..9047cad 100644 --- a/p11p-daemon/src/p11p_remote.erl +++ b/p11p-daemon/src/p11p_remote.erl @@ -14,7 +14,7 @@ %% API. -export([start_link/3]). --export([request/2, add_to_outbuf/2]). +-export([request/2, add_to_outbuf/2, stop/2]). -include("p11p_rpc.hrl"). @@ -48,11 +48,20 @@ request(Remote, 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 +%% process terminated on request from a remote which has timed out on +%% an rpc call, chances are that the remote has already terminated by +%% the time the manager is to act on the lost client. +stop(Pid, Reason) -> + gen_server:cast(Pid, {stop, Reason}). + %% Genserver callbacks. init([TokName, ModPath]) -> Port = open_port({spawn_executable, ?P11KITREMOTE_PATH}, [stream, exit_status, {args, [ModPath, "-v"]}]), - lager:debug("~p: ~s: New port: ~p", [self(), ?P11KITREMOTE_PATH, Port]), + lager:debug("~p: ~s: new remote port: ~p", [self(), ?P11KITREMOTE_PATH, Port]), {ok, #state{port = Port, token = TokName}}. handle_call({add_to_outbuf, Data}, _From, State) -> @@ -65,8 +74,10 @@ handle_call(Request, _From, State) -> lager:debug("~p: Unhandled call: ~p~n", [self(), Request]), {reply, unhandled, State}. -handle_cast(Request, State) -> - lager:debug("~p: Unhandled cast: ~p~n", [self(), Request]), +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 @@ -78,7 +89,7 @@ handle_info({Port, {data, Data}}, #state{msg = Msg} = State) when Port == State# {noreply, handle_remote_data(State, Msg, Data)}; handle_info({timeout, Timer, Port}, #state{token = TokName, replyto = Server} = State) when Port == State#state.port, Timer == State#state.timer -> lager:info("~p: rpc request timed out, exiting", [self()]), - ok = p11p_remote_manager:server_event(timeout, [TokName, Server]), + p11p_remote_manager:server_event(timeout, [TokName, Server]), NewState = State#state{timer = undefined}, {stop, normal, NewState}; handle_info(Info, State) -> |