summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Nordberg <linus@sunet.se>2020-01-07 22:06:28 +0100
committerLinus Nordberg <linus@sunet.se>2020-01-07 23:09:09 +0100
commitfcfe6d7a24ef5c26881ff7feb40fefe0eac792b5 (patch)
treea0b2fd5c27b313f3f771d27986e659d5d706f66b
parent6a3dd773452351c95b552c479d1bcaf276b2cc62 (diff)
Code cleanup, variable renaming
Move away from func(Foo) -> NewFoo = something(Foo) and instead use FooIn and Foo, or something else that makes sense.
-rw-r--r--p11p-daemon/src/p11p_config.erl13
-rw-r--r--p11p-daemon/src/p11p_remote.erl41
-rw-r--r--p11p-daemon/src/p11p_remote_manager.erl100
-rw-r--r--p11p-daemon/src/p11p_rpc.erl85
-rw-r--r--p11p-daemon/src/p11p_rpc.hrl10
-rw-r--r--p11p-daemon/src/p11p_server.erl71
-rw-r--r--p11p-daemon/src/p11p_sup.erl3
7 files changed, 167 insertions, 156 deletions
diff --git a/p11p-daemon/src/p11p_config.erl b/p11p-daemon/src/p11p_config.erl
index 1fc83e9..9c7749c 100644
--- a/p11p-daemon/src/p11p_config.erl
+++ b/p11p-daemon/src/p11p_config.erl
@@ -9,7 +9,8 @@
%%-export([config/0]).
-export([nameof/1]).
-export([tokens/0]).
--export([remotebin_path/0, modules_for_token/1, module_path/1, module_env/1, token_mode/1]).
+-export([remotebin_path/0, modules_for_token/1, module_path/1, module_env/1,
+ token_mode/1]).
-export_type([token_mode_t/0]).
%% Genserver callbacks.
@@ -118,8 +119,9 @@ code_change(_OldVersion, State, _Extra) ->
init_state() ->
#state {
- remotebin_path = application:get_env(p11p, remotebin_path,
- "/usr/local/libexec/p11-kit/p11-kit-remote"),
+ remotebin_path =
+ application:get_env(p11p, remotebin_path,
+ "/usr/local/libexec/p11-kit/p11-kit-remote"),
tokens = conf_tokens(application:get_env(p11p, groups, []))
}.
@@ -157,7 +159,8 @@ new_module(Name, Path, Env) ->
env = Env
}.
--spec mode(p11p_config:token_mode_t(), non_neg_integer()) -> p11p_config:token_mode_t().
+-spec mode(p11p_config:token_mode_t(), non_neg_integer()) ->
+ p11p_config:token_mode_t().
mode({balance, Args}, NModules) ->
{balance, Args ++ [1 || _ <- lists:seq(1, NModules - length(Args))]};
mode(Conf, _) ->
@@ -205,5 +208,3 @@ tokens_init_test_() ->
%% modules_for_token_test_() ->
%% {setup,
%% fun() ->
-
-
diff --git a/p11p-daemon/src/p11p_remote.erl b/p11p-daemon/src/p11p_remote.erl
index 6a050e6..d0f9184 100644
--- a/p11p-daemon/src/p11p_remote.erl
+++ b/p11p-daemon/src/p11p_remote.erl
@@ -72,10 +72,10 @@ init([TokName, ModPath, ModEnv]) ->
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} = State) ->
+handle_call({request, Request}, {FromPid, _Tag}, #state{port = Port} = S) ->
%%lager:debug("~p: sending request from ~p to remote ~p", [self(), FromPid, Port]),
- NewState = do_send(do_add_to_outbuf(p11p_rpc:serialise(Request), State)),
- {reply, ok, NewState#state{replyto = FromPid, timer = start_timer(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]),
{reply, unhandled, State}.
@@ -87,17 +87,20 @@ handle_cast(Cast, State) ->
{noreply, State}.
%% TODO: dedup code w/ p11p_server
-handle_info({Port, {data, Data}}, #state{replyto = Pid} = State) when Port == State#state.port, State#state.msg == undefined ->
+handle_info({Port, {data, Data}}, #state{replyto = Pid} = 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))};
-handle_info({Port, {data, Data}}, #state{msg = Msg} = State) when Port == State#state.port ->
+handle_info({Port, {data, Data}}, #state{msg = Msg} = State)
+ when Port == State#state.port ->
{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 ->
+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, [TokName, Server]),
- NewState = State#state{timer = undefined},
- {stop, normal, NewState};
+ 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}.
@@ -111,10 +114,9 @@ code_change(_OldVersion, State, _Extra) ->
{ok, State}.
%% Private
-do_add_to_outbuf(Data, #state{outbuf = Buf} = State) ->
+do_add_to_outbuf(Data, #state{outbuf = OutBuf} = State) ->
%%lager:debug("~p: adding ~B octets to outbuf", [self(), size(Data)]),
- NewBuf = <<Buf/binary, Data/binary>>,
- State#state{outbuf = NewBuf}.
+ State#state{outbuf = <<OutBuf/binary, Data/binary>>}.
do_send(#state{port = Port, outbuf = Buf} = State) ->
%%lager:debug("~p: sending ~B octets to remote", [self(), size(Buf)]),
@@ -127,17 +129,16 @@ do_send(#state{port = Port, outbuf = Buf} = State) ->
%% end,
port_command(Port, Buf),
-
State#state{outbuf = <<>>}.
-handle_remote_data(#state{replyto = Pid, timer = Timer} = State, Msg, Data) ->
- case p11p_rpc:parse(Msg, list_to_binary(Data)) of
- {done, NewMsg} ->
+handle_remote_data(#state{replyto = Pid, timer = Timer} = S, MsgIn, DataIn) ->
+ case p11p_rpc:parse(MsgIn, list_to_binary(DataIn)) of
+ {done, Msg} ->
cancel_timer(Timer),
- {ok, _BytesSent} = p11p_server:reply(Pid, NewMsg),
- State#state{msg = p11p_rpc:new(NewMsg#p11rpc_msg.buffer)};
- {needmore, NewMsg} ->
- State#state{msg = NewMsg}
+ {ok, _BytesSent} = p11p_server:reply(Pid, Msg),
+ S#state{msg = p11p_rpc:new(Msg#p11rpc_msg.buffer)};
+ {needmore, Msg} ->
+ S#state{msg = Msg}
end.
start_timer(Port) ->
diff --git a/p11p-daemon/src/p11p_remote_manager.erl b/p11p-daemon/src/p11p_remote_manager.erl
index eabb67f..ad7fbaf 100644
--- a/p11p-daemon/src/p11p_remote_manager.erl
+++ b/p11p-daemon/src/p11p_remote_manager.erl
@@ -9,17 +9,16 @@
%% given vtoken and spawn a p11p_remote genserver "on demand".
%%
%% Provide a client event and a server event API for servers and
-%% remotes, respectively, where events like "remote timing out" and
+%% remotes, respectively, where events like "remote timed out" and
%% "p11 client hung up" can be reported.
%%
-
-%% Keep track of (successful) p11 requests which might cause state
-%% changes in the token, like "logins". When switching token under the
+%% Keep track of successful p11 requests which might cause state
+%% changes in a token, like logins. When switching token under the
%% feet of the p11 client, replay whatever is needed to the new
-%% token. This goes for the p11-kit RPC protocol version octet too.
+%% token. This includes the p11-kit RPC protocol version octet.
%% Certain state changing p11 requests cannot be replayed, like
-%% "generate new key". Any such (successful) request invalidates all
-%% other remotes for the given token.
+%% generation of a new key. Any such (successful) request invalidates
+%% all other remotes for the given vtoken.
-module(p11p_remote_manager).
@@ -72,63 +71,70 @@ server_event(Event, Args) ->
init([]) ->
{ok, #state{tokens = init_tokens(p11p_config:tokens())}}.
-handle_call({remote_for_token, TokName}, _From, #state{tokens = Tokens} = State) ->
- #{TokName := Token} = Tokens,
- lager:debug("all remotes: ~p", [Token#token.remotes]),
- {Remotes, NewBC} =
- case Token#token.balance_count of
+handle_call({remote_for_token, TokNameIn}, _, #state{tokens = Tokens} = S) ->
+ #{TokNameIn := TokenIn} = Tokens,
+ RemotesIn = TokenIn#token.remotes,
+ lager:debug("all remotes: ~p", [RemotesIn]),
+ {Remotes, BalanceCount} =
+ case TokenIn#token.balance_count of
0 ->
lager:debug("~p: balancing: next remote", [self()]),
- R0 = rotate_remotes(Token#token.remotes),
- R = hd(R0),
- {R0, R#remote.balance - 1};
+ Rotated = rotate_remotes(RemotesIn),
+ First = hd(Rotated),
+ {Rotated, First#remote.balance - 1};
N when N > 0 ->
lager:debug("~p: balancing: ~B more invocations", [self(), N]),
- {Token#token.remotes, N - 1};
+ {RemotesIn, N - 1};
-1 ->
- {Token#token.remotes, -1}
+ {RemotesIn, -1}
end,
- #remote{tokname = TokName, servid = ServId, modpath = ModPath, modenv = ModEnv, pid = Pid} = Remote = hd(Remotes),
- case Pid of
+ #remote{tokname = TokNameIn,
+ servid = ServId,
+ modpath = ModPath,
+ modenv = ModEnv,
+ pid = PidIn} = SelectedRemote = hd(Remotes),
+ case PidIn of
undefined ->
- {ok, NewPid} = p11p_remote:start_link(ServId, TokName, ModPath, ModEnv),
- NewRemote = Remote#remote{pid = NewPid},
- NewToken = Token#token{remotes = [NewRemote | tl(Remotes)],
- balance_count = NewBC},
- NewState = State#state{tokens = Tokens#{TokName := NewToken}},
- {reply, NewPid, NewState};
+ {ok, Pid} =
+ p11p_remote:start_link(ServId, TokNameIn, ModPath, ModEnv),
+ Remote = SelectedRemote#remote{pid = Pid},
+ Token = TokenIn#token{remotes = [Remote | tl(Remotes)],
+ balance_count = BalanceCount},
+ {reply, Pid, S#state{tokens = Tokens#{TokNameIn := Token}}};
_ ->
- {reply, Pid, State}
+ {reply, PidIn, S}
end;
handle_call(Call, _From, State) ->
lager:debug("Unhandled call: ~p~n", [Call]),
{reply, unhandled, State}.
-handle_cast({server_event, timeout, [TokName, Server]}, #state{tokens = Tokens} = State) ->
- lager:debug("~p: ~s: timed out, stopping ~p", [self(), TokName, Server]),
+handle_cast({server_event, timeout, [TokNameIn, Server]},
+ #state{tokens = Tokens} = S) ->
+ lager:debug("~p: ~s: timed out, stopping ~p", [self(), TokNameIn, Server]),
gen_server:stop(Server), % Hang up on p11 client.
%% TODO: do some code dedup with remote_for_token?
- #{TokName := Token} = Tokens,
- Remotes = Token#token.remotes,
- Remote = hd(Remotes),
- NewRemote = Remote#remote{pid = undefined},
- NewToken = Token#token{remotes = tl(Remotes) ++ [NewRemote]},
- NewState = State#state{tokens = Tokens#{TokName := NewToken}},
- lager:debug("~p: ~s: updated token: ~p", [self(), TokName, NewToken]),
- {noreply, NewState};
-handle_cast({client_event, client_gone, [TokName, Pid]}, #state{tokens = Tokens} = State) ->
+ #{TokNameIn := TokenIn} = Tokens,
+ Remotes = TokenIn#token.remotes,
+ SelectedRemote = hd(Remotes),
+ Remote = SelectedRemote#remote{pid = undefined},
+ Token = TokenIn#token{remotes = tl(Remotes) ++ [Remote]},
+ lager:debug("~p: ~s: updated token: ~p", [self(), TokNameIn, Token]),
+ {noreply, S#state{tokens = Tokens#{TokNameIn := Token}}};
+
+handle_cast({client_event, client_gone, [TokName, Pid]},
+ #state{tokens = Tokens} = S) ->
lager:debug("~p: asking remote ~p to stop", [self(), Pid]),
p11p_remote:stop(Pid, normal),
- #{TokName := Token} = Tokens,
- Remotes = Token#token.remotes,
- NewRemotes = lists:map(fun(E) ->
- case E#remote.pid of
- Pid -> E#remote{pid = undefined};
- _ -> E
- end end, Remotes),
- NewToken = Token#token{remotes = NewRemotes},
- NewState = State#state{tokens = Tokens#{TokName := NewToken}},
- {noreply, NewState};
+ #{TokName := TokenIn} = Tokens,
+ Remotes = lists:map(fun(E) ->
+ case E#remote.pid of
+ Pid -> E#remote{pid = undefined};
+ _ -> E
+ end
+ end, TokenIn#token.remotes),
+ Token = TokenIn#token{remotes = Remotes},
+ {noreply, S#state{tokens = Tokens#{TokName := Token}}};
+
handle_cast(Cast, State) ->
lager:debug("Unhandled cast: ~p~n", [Cast]),
{noreply, State}.
diff --git a/p11p-daemon/src/p11p_rpc.erl b/p11p-daemon/src/p11p_rpc.erl
index 956cb34..a775d30 100644
--- a/p11p-daemon/src/p11p_rpc.erl
+++ b/p11p-daemon/src/p11p_rpc.erl
@@ -13,28 +13,30 @@ parse(M) ->
parse(M, <<>>).
-spec parse(p11rpc_msg(), binary()) -> {done, p11rpc_msg()} | {needmore, p11rpc_msg()} | {err, term()}.
-parse(#p11rpc_msg{buffer = Buf} = M, Data) when M#p11rpc_msg.state == header ->
- %% NOTE: Does _not_ consume buffer until it has at least 12 octets.
- NewBuf = <<Buf/binary, Data/binary>>,
- Msg = M#p11rpc_msg{buffer = NewBuf},
+parse(#p11rpc_msg{buffer = MsgBuf} = M, DataIn)
+ when M#p11rpc_msg.state == header ->
+ Buf = <<MsgBuf/binary, DataIn/binary>>,
+ Msg = M#p11rpc_msg{buffer = Buf},
if
- size(NewBuf) < 12 ->
+ size(Buf) < 12 ->
{needmore, Msg};
true ->
parse(consume_header(Msg))
end;
-parse(#p11rpc_msg{buffer = Buf} = M, Data) when M#p11rpc_msg.state == opts ->
- NewBuf = <<Buf/binary, Data/binary>>,
- Msg = consume_opts(M#p11rpc_msg{buffer = NewBuf}),
+parse(#p11rpc_msg{buffer = MsgBuf} = M, DataIn)
+ when M#p11rpc_msg.state == opts ->
+ Buf = <<MsgBuf/binary, DataIn/binary>>,
+ Msg = consume_opts(M#p11rpc_msg{buffer = Buf}),
case Msg#p11rpc_msg.state of
opts ->
{needmore, Msg};
data ->
parse(Msg)
end;
-parse(#p11rpc_msg{buffer = Buf} = M, Data) when M#p11rpc_msg.state == data ->
- NewBuf = <<Buf/binary, Data/binary>>,
- Msg = consume_data(M#p11rpc_msg{buffer = NewBuf}),
+parse(#p11rpc_msg{buffer = MsgBuf} = M, DataIn)
+ when M#p11rpc_msg.state == data ->
+ Buf = <<MsgBuf/binary, DataIn/binary>>,
+ Msg = consume_data(M#p11rpc_msg{buffer = Buf}),
case Msg#p11rpc_msg.state of
data ->
{needmore, Msg};
@@ -52,10 +54,11 @@ serialise(M) when M#p11rpc_msg.state == done,
DataLen = M#p11rpc_msg.data_len,
Options = M#p11rpc_msg.options,
Data = M#p11rpc_msg.data,
- <<CallCode:32/integer, OptLen:32/integer, DataLen:32/integer, Options/binary, Data/binary>>.
+ <<CallCode:32/integer,
+ OptLen:32/integer, DataLen:32/integer,
+ Options/binary, Data/binary>>.
%% Private
-
new() ->
new(<<>>).
new(Buffer) ->
@@ -68,34 +71,34 @@ consume_header(#p11rpc_msg{buffer = Buf} = Msg) ->
state = opts,
buffer = binary:part(Buf, 12, size(Buf) - 12)}.
-consume_opts(#p11rpc_msg{opt_len = Len, options = Opts, buffer = Buf} = Msg) ->
- {NewData, NewBuf} = move_between_binaries(Opts, Buf, Len - size(Opts)),
- NewState = case size(NewData) == Len of
- true ->
- data;
- false ->
- opts
- end,
- Msg#p11rpc_msg{options = NewData, buffer = NewBuf, state = NewState}.
-
-consume_data(#p11rpc_msg{data_len = Len, data = Data, buffer = Buf} = Msg) ->
- {NewData, NewBuf} = move_between_binaries(Data, Buf, Len - size(Data)),
- NewState = case size(NewData) == Len of
- true ->
- done;
- false ->
- data
- end,
- Msg#p11rpc_msg{data = NewData, buffer = NewBuf, state = NewState}.
-
-move_between_binaries(Dst, Src, N) when N == 0 ->
- {Dst, Src};
-move_between_binaries(Dst, Src, NAsk) ->
- N = min(NAsk, size(Src)),
- FromSrc = binary:part(Src, 0, N),
- NewDst = <<Dst/binary, FromSrc/binary>>,
- NewSrc = binary:part(Src, N, size(Src) - N),
- {NewDst, NewSrc}.
+consume_opts(#p11rpc_msg{opt_len = Len, options = Opts, buffer = MsgBuf} = M) ->
+ {Data, Buf} = move_between_binaries(Opts, MsgBuf, Len - size(Opts)),
+ State = case size(Data) == Len of
+ true ->
+ data;
+ false ->
+ opts
+ end,
+ M#p11rpc_msg{options = Data, buffer = Buf, state = State}.
+
+consume_data(#p11rpc_msg{data_len = Len, data = DataIn, buffer = MsgBuf} = M) ->
+ {Data, Buf} = move_between_binaries(DataIn, MsgBuf, Len - size(DataIn)),
+ State = case size(Data) == Len of
+ true ->
+ done;
+ false ->
+ data
+ end,
+ M#p11rpc_msg{data = Data, buffer = Buf, state = State}.
+
+move_between_binaries(DstIn, SrcIn, 0) ->
+ {DstIn, SrcIn};
+move_between_binaries(DstIn, SrcIn, NBytes) ->
+ N = min(NBytes, size(SrcIn)),
+ FromSrc = binary:part(SrcIn, 0, N),
+ Dst = <<DstIn/binary, FromSrc/binary>>,
+ Src = binary:part(SrcIn, N, size(SrcIn) - N),
+ {Dst, Src}.
%%%%%%%%%%%%%%
%% Unit tests.
diff --git a/p11p-daemon/src/p11p_rpc.hrl b/p11p-daemon/src/p11p_rpc.hrl
index fa73581..8ccb0d1 100644
--- a/p11p-daemon/src/p11p_rpc.hrl
+++ b/p11p-daemon/src/p11p_rpc.hrl
@@ -2,12 +2,12 @@
%%% See LICENSE for licensing information.
-record(p11rpc_msg, {
- call_code = -1 :: integer(), % len = 4
- opt_len = -1 :: integer(), % len = 4
- data_len = -1 :: integer(), % len = 4
+ call_code = -1 :: integer(), % Length is 4
+ opt_len = -1 :: integer(), % Length is 4
+ data_len = -1 :: integer(), % Length is 4
- options = <<>> :: binary(), % len = header.opt_len
- data = <<>> :: binary(), % len = header.buf_len
+ options = <<>> :: binary(), % Length is header.opt_len
+ data = <<>> :: binary(), % Length is header.buf_len
buffer = <<>> :: binary(),
state = header :: header | opts | data | done
diff --git a/p11p-daemon/src/p11p_server.erl b/p11p-daemon/src/p11p_server.erl
index 668a51c..ff1a8df 100644
--- a/p11p-daemon/src/p11p_server.erl
+++ b/p11p-daemon/src/p11p_server.erl
@@ -37,27 +37,27 @@ add_to_clientbuf(Pid, Data) ->
-spec reply(pid(), p11rpc_msg()) -> {ok, non_neg_integer()}.
reply(Pid, Response) ->
- gen_server:call(Pid, {response, Response}).
+ gen_server:call(Pid, {respond, Response}).
%% Genserver callbacks.
init([Token, Socket]) ->
lager:debug("~p: p11p_server:init", [self()]),
- process_flag(trap_exit, true), % We want terminate().
- gen_server:cast(self(), accept), % Perform accept in gen-server loop.
+ process_flag(trap_exit, true), % Need terminate/2.
+ gen_server:cast(self(), accept), % Invoke accept, returning a socket in state.
{ok, #state{tokname = Token, socket = Socket}}.
-handle_call({add_to_clientbuf, Data}, _From, #state{clientbuf = Buf} = State) ->
- NewBuf = <<Buf/binary, Data/binary>>,
- {reply, {ok, size(NewBuf)}, State#state{clientbuf = NewBuf}};
-handle_call({response, Response}, _From, #state{socket = ClientPort, clientbuf = Buf} = State) ->
- Data = p11p_rpc:serialise(Response),
- NewBuf = <<Buf/binary, Data/binary>>,
- %%lager:debug("~p: sending ~B octets back to client as reply", [self(), size(NewBuf)]),
- ok = gen_tcp:send(ClientPort, NewBuf), % TODO: what about short writes?
- {reply, {ok, size(NewBuf)}, State#state{clientbuf = <<>>}};
-handle_call(Call, _From, State) ->
+handle_call({add_to_clientbuf, Data}, _, #state{clientbuf = B} = S) ->
+ Buf = <<B/binary, Data/binary>>,
+ {reply, {ok, size(Buf)}, S#state{clientbuf = Buf}};
+handle_call({respond, R}, _, #state{socket = Client, clientbuf = B} = S) ->
+ Data = p11p_rpc:serialise(R),
+ Buf = <<B/binary, Data/binary>>,
+ %%lager:debug("~p: sending ~B octets to client as response", [self(), size(Buf)]),
+ ok = gen_tcp:send(Client, Buf), % TODO: what about short writes?
+ {reply, {ok, size(Buf)}, S#state{clientbuf = <<>>}};
+handle_call(Call, _, S) ->
lager:debug("~p: Unhandled call: ~p~n", [self(), Call]),
- {reply, unhandled, State}.
+ {reply, unhandled, S}.
handle_cast(accept, State = #state{tokname = TokName, socket = ListenSocket}) ->
%% Blocking until client connects or timeout fires.
@@ -80,25 +80,26 @@ handle_cast(Cast, State) ->
lager:debug("~p: Unhandled cast: ~p~n", [self(), Cast]),
{noreply, State}.
-handle_info({tcp, _Port, Data}, #state{tokname = TokName, remote = Remote} = State) when Remote == undefined ->
+handle_info({tcp, _Port, DataIn}, #state{tokname = TokName} = S)
+ when S#state.remote == undefined ->
%%lager:debug("~p: received ~B octets from client on socket ~p, from new client", [self(), size(Data), Port]),
- <<Version:8, NewData/binary>> = Data,
- NewRemote = p11p_remote_manager:remote_for_token(TokName),
- p11p_remote:add_to_outbuf(NewRemote, <<Version>>),
- NewState = State#state{remote = NewRemote},
- {noreply, handle_client_data(NewState, p11p_rpc:new(), NewData)};
-handle_info({tcp, _Port, Data}, #state{msg = Msg} = State) ->
+ <<Version:8, Data/binary>> = DataIn,
+ Remote = p11p_remote_manager:remote_for_token(TokName),
+ p11p_remote:add_to_outbuf(Remote, <<Version>>),
+ State = S#state{remote = Remote},
+ {noreply, handle_client_data(State, p11p_rpc:new(), Data)};
+handle_info({tcp, _Port, DataIn}, #state{msg = Msg} = S) ->
%%lager:debug("~p: received ~B octets from client on socket ~p, with ~B octets already in buffer", [self(), size(Data), Port, size(Msg#p11rpc_msg.buffer)]),
- {noreply, handle_client_data(State, Msg, Data)};
-handle_info({tcp_closed, Port}, State) ->
+ {noreply, handle_client_data(S, Msg, DataIn)};
+handle_info({tcp_closed, Port}, S) ->
lager:debug("~p: socket ~p closed", [self(), Port]),
- {stop, normal, State};
-handle_info(Info, State) ->
+ {stop, normal, S};
+handle_info(Info, S) ->
lager:debug("~p: Unhandled info: ~p~n", [self(), Info]),
- {noreply, State}.
+ {noreply, S}.
-terminate(Reason, #state{socket = Socket, tokname = TokName, remote = Remote}) ->
- gen_tcp:close(Socket),
+terminate(Reason, #state{socket = Sock, tokname = TokName, remote = Remote}) ->
+ gen_tcp:close(Sock),
p11p_remote_manager:client_event(client_gone, [TokName, Remote]),
lager:debug("~p: terminated with reason ~p", [self(), Reason]),
ignored.
@@ -107,11 +108,11 @@ code_change(_OldVersion, State, _Extra) ->
{ok, State}.
%% Private functions.
-handle_client_data(#state{remote = Remote} = State, Msg, Data) ->
- case p11p_rpc:parse(Msg, Data) of
- {done, NewMsg} ->
- ok = p11p_remote:request(Remote, NewMsg),
- State#state{msg = p11p_rpc:new(NewMsg#p11rpc_msg.buffer)};
- {needmore, NewMsg} ->
- State#state{msg = NewMsg}
+handle_client_data(#state{remote = Remote} = S, MsgIn, DataIn) ->
+ case p11p_rpc:parse(MsgIn, DataIn) of
+ {done, Msg} ->
+ ok = p11p_remote:request(Remote, Msg),
+ S#state{msg = p11p_rpc:new(Msg#p11rpc_msg.buffer)};
+ {needmore, Msg} ->
+ S#state{msg = Msg}
end.
diff --git a/p11p-daemon/src/p11p_sup.erl b/p11p-daemon/src/p11p_sup.erl
index 9eeb83b..314b958 100644
--- a/p11p-daemon/src/p11p_sup.erl
+++ b/p11p-daemon/src/p11p_sup.erl
@@ -18,9 +18,8 @@
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
--define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
-
%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
+-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
init([]) ->
{ok, {{rest_for_one, 1, 5},
[