1 %%% The Registry is a gen_server process that keeps track of the
2 %%% currently active set of connection-path processes. The listeners
3 %%% add the processes to the list as they're created. The manager
4 %%% looks stuff up in order to send commands to the proper place.
6 %%% Note that "Count" is a count of the number of paths we've *ever*
7 %%% registered, not the current length of the list. (Otherwise I
8 %%% would have simply called lists:length/1 when needed.) It's used
9 %%% to tag each entry with an identifying serial number, I guess.
12 -behaviour(gen_server).
13 -export([start/0,lookup/1,register/1,unregister/1,all/0]).
14 -export([init/1, handle_call/3]).
15 -export([code_change/3,handle_cast/2,handle_info/2,terminate/2]).
16 -import(lists,[dropwhile/2]).
19 {ok,_Pid} = gen_server:start({local,registry}, registry, [], []),
25 handle_call({register,Path, Pid}, _From, State) ->
26 {Paths,Count} = State,
28 {reply, ok, {[{Num,Path,Pid}|Paths],Count+1}};
30 %% Someday we may want to look up by Id, but for now no one is using it.
31 handle_call({lookup,Query}, _, State) ->
32 {Paths,_Count} = State,
33 Ans = [Pid || {_Num, Path, Pid} <- Paths, util:match(Path, Query)],
36 handle_call({unregister,Path}, _, State) ->
37 {Paths,Count} = State,
38 {reply, ok, {lists:keydelete(Path, 2, Paths),Count}};
40 handle_call(all, _, State) ->
41 {Paths,_Count} = State,
42 {reply, Paths, State}.
45 gen_server:call(registry, {lookup, Query}).
48 gen_server:call(registry, {register, Path, self()}).
51 gen_server:call(registry, {unregister, Path}).
54 gen_server:call(registry, all).
57 %%% The following functions defined in the gen_server behavior are not
58 %%% used. But we include them in order to avoid distracting
59 %%% compilation warnings.