%% Create an AF_UNIX socket and accept connections. On connect, spawn %% another p11p_server process. -module(p11p_server). -behaviour(gen_server). -include("p11p-rpc.hrl"). %% API. -export([start_link/1]). -export([reply/2]). %% Genserver callbacks. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %% Records and types. -record(state, { tokname :: string(), sockpath :: string(), % FIXME: filename(3erl) socket :: gen_tcp:socket(), msg :: p11rpc_msg() | undefined }). %% API. -spec start_link(gen_tcp:socket()) -> {ok, pid()} | {error, term()}. start_link(Args) -> gen_server:start_link(?MODULE, Args, []). reply(Pid, Data) -> gen_server:cast(Pid, {response, Data}). %% Genserver callbacks. init([Token, SocketPath, Socket]) -> lager:debug("~p: p11p_server:init: ~s", [self(), SocketPath]), process_flag(trap_exit, true), % We want terminate(). gen_server:cast(self(), accept), % Perform accept in gen-server loop. {ok, #state{tokname = Token, sockpath = SocketPath, socket = Socket}}. handle_call(Request, _From, State) -> lager:debug("~p: Unhandled call: ~p~n", [self(), Request]), {reply, unhandled, State}. handle_cast(accept, State = #state{tokname = TokName, sockpath = SocketPath, socket = ListenSocket}) -> %% Blocking until client connects or timeout fires -- without a %% timeout our supervisor cannot terminate us. case gen_tcp:accept(ListenSocket, 900) of {ok, Sock} -> lager:debug("~p: ~p: new connection accepted", [self(), Sock]), p11p_server_sup:start_server([TokName, SocketPath, ListenSocket]), % Start a new acceptor. {noreply, State#state{socket = Sock}}; % Use the new socket. {error, timeout} -> gen_server:cast(self(), accept), % Try again. {noreply, State}; {error, closed} -> lager:debug("~p: listening socket closed", [self()]), {stop, normal, State} end; handle_cast({response, Data}, #state{socket = ClientPort} = State) -> lager:debug("~p: received ~B octets from remote", [self(), length(Data)]), ok = gen_tcp:send(ClientPort, Data), {noreply, State}; handle_cast(Cast, State) -> lager:debug("~p: Unhandled cast: ~p~n", [self(), Cast]), {noreply, State}. handle_info({tcp, Port, Data}, #state{tokname = TokName, msg = Msg} = State) when Msg == undefined -> lager:debug("~p: received ~B octets from client on socket ~p, from new client", [self(), size(Data), Port]), <> = Data, %% FIXME: don't send, just add to outbuf ok = p11p_remote:send(self(), p11p_remote_manager:remote_for_token(TokName), <>), NewState = handle_client_data(State, p11p_rpc:new(), NewData), {noreply, NewState}; handle_info({tcp, Port, Data}, #state{msg = Msg} = State) -> 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)]), NewState = handle_client_data(State, Msg, Data), {noreply, NewState}; handle_info({tcp_closed, Port}, State) -> lager:debug("~p: socket ~p closed", [self(), Port]), {stop, {shutdown, close_by_client}, State}; handle_info(Info, State) -> lager:debug("~p: Unhandled info: ~p~n", [self(), Info]), {noreply, State}. terminate(_Reason, #state{sockpath = _SocketPath, socket = Socket}) -> lager:debug("~p: terminated", [self()]), gen_tcp:close(Socket), ok. code_change(_OldVersion, State, _Extra) -> {ok, State}. %% Private functions. handle_client_data(#state{tokname = TokName} = State, Msg, Data) -> case p11p_rpc:parse(Msg, Data) of {done, NewMsg} -> Remote = p11p_remote_manager:remote_for_token(TokName), ok = p11p_remote:send(self(), Remote, p11p_rpc:serialise(NewMsg)), State#state{msg = p11p_rpc:new(NewMsg#p11rpc_msg.buffer)}; {needmore, NewMsg} -> State#state{msg = NewMsg} end.