1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
%% A remote manager is a gen_server for coordination of remotes for
%% all tokens.
%% Spawn one remote per configured p11p module per configured virtual
%% token. Provide a lookup service for servers that need a remote to
%% send a request to, by keeping track of which module is current for
%% a given vtoken.
-module(p11p_remote_manager).
-behaviour(gen_server).
%% API.
-export([start_link/0]).
-export([remote_for_token/1]). % For servers.
-export([p11init_done/1, timeout/1]). % For remotes.
-export([send/2]). % Experiment
%% Genserver callbacks.
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]).
%% Records and types.
-record(token, {
p11init_done = false :: boolean(),
remotes :: [pid()] % Active remote in hd().
}).
-record(state, {
tokens :: #{string() => #token{}}
}).
%% API implementation.
-spec start_link() -> {ok, pid()} | {error, term()}.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-spec remote_for_token(string()) -> pid().
remote_for_token(TokName) ->
gen_server:call(?MODULE, {remote_for_token, TokName}).
p11init_done(_Done) ->
todo.
timeout(_TokName) ->
todo.
send(TokName, Data) ->
gen_server:cast(?MODULE, {send, TokName, Data}).
%% Genserver callbacks.
init([]) ->
{ok, #state{tokens = init_tokens(p11p_config:tokens())}}.
handle_call({remote_for_token, TokName}, _From, State) ->
#{TokName := Token} = State#state.tokens,
{reply, hd(Token#token.remotes), State};
handle_call(Request, _From, State) ->
lager:debug("Unhandled call: ~p~n", [Request]),
{reply, unhandled, State}.
handle_cast({send, TokName, Data}, State) ->
#{TokName := Token} = State#state.tokens,
ServerPort = hd(Token#token.remotes),
ServerPort ! {self(), {command, Data}}, % TODO: Use synchronous port_command()?
{noreply, State};
handle_cast(Request, State) ->
lager:debug("Unhandled cast: ~p~n", [Request]),
{noreply, State}.
handle_info({Port, {exit_status, Status}}, State) ->
lager:info("~p: process exited with ~p", [Port, Status]),
{stop, child_exit, State};
handle_info(Info, State) ->
lager:debug("~p: Unhandled info: ~p~n", [self(), Info]),
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVersion, State, _Extra) ->
{ok, State}.
%% Private functions
-spec init_tokens([p11p_config:token()]) -> #{string() => #token{}}.
init_tokens(ConfTokens) ->
init_tokens(ConfTokens, #{}).
init_tokens([], Acc)->
Acc;
init_tokens([H|T], Acc)->
TokName = p11p_config:nameof(H),
init_tokens(T, Acc#{TokName => new_token(TokName, H)}).
new_token(TokName, ConfToken) ->
Remotes = start_remotes(TokName, p11p_config:modules_for_token(p11p_config:nameof(ConfToken))),
#token{remotes = Remotes}.
start_remotes(TokName, ConfModules) ->
start_remotes(TokName, ConfModules, []).
start_remotes(_, [], Acc) ->
%%lists:reverse(Acc);
Acc;
start_remotes(TokName, [H|T], Acc) ->
ModName = p11p_config:nameof(H),
ServName = "p11p_remote:" ++ TokName ++ ":" ++ ModName,
ModPath = p11p_config:module_path(H),
{ok, Pid} = p11p_remote:start_link(list_to_atom(ServName), TokName, ModPath),
start_remotes(TokName, T, [Pid | Acc]).
|