Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / test / java / rep / fiddler / registry.erl
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.
5 %%%
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.
10
11 -module(registry).
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]).
17
18 start() ->
19     {ok,_Pid} = gen_server:start({local,registry}, registry, [], []),
20     ok.
21
22 init(_) ->
23     {ok,{[],0}}.
24
25 handle_call({register,Path, Pid}, _From, State) ->
26     {Paths,Count} = State,
27     Num = Count + 1,
28     {reply, ok, {[{Num,Path,Pid}|Paths],Count+1}};
29
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)],
34     {reply, Ans, State};
35
36 handle_call({unregister,Path}, _, State) ->
37     {Paths,Count} = State,
38     {reply, ok, {lists:keydelete(Path, 2, Paths),Count}};
39
40 handle_call(all, _, State) ->
41     {Paths,_Count} = State,
42     {reply, Paths, State}.
43
44 lookup(Query) ->
45     gen_server:call(registry, {lookup, Query}).
46
47 register(Path) ->
48     gen_server:call(registry, {register, Path, self()}).
49
50 unregister(Path) ->
51     gen_server:call(registry, {unregister, Path}).
52
53 all() ->
54     gen_server:call(registry, all).
55
56 %%%
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.
60 %%% 
61 code_change(_,_,_) ->
62     ok.
63
64 handle_cast(_,_) ->
65     ok.
66
67 handle_info(_,_) ->
68     ok.
69
70 terminate(_,_) ->
71     ok.