Canviant namespaces jerarquic
[makerl.git] / src / task_registry.erl
blob239eeb0e61d25d79e0591411ae85531b8210c23a
1 %%%-------------------------------------------------------------------
2 %%% File : task_registry.erl
3 %%% Author : <>
4 %%% Description :
5 %%%
6 %%% Created : 25 Mar 2011 by <>
7 %%%-------------------------------------------------------------------
8 -module(makerl.task_registry).
9 -include("makerl.hrl").
11 -import(gen_server).
12 -import(ets).
14 -behaviour(gen_server).
16 %% API
17 -export([start_link/0,
18 add/1,
19 set_pid/2,
20 find_by_target/1,
21 find_pid_by_target/1]).
23 %% gen_server callbacks
24 -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
25 terminate/2, code_change/3]).
27 -define(TIMEOUT, infinity).
28 -record(state, {}).
30 %%====================================================================
31 %% API
32 %%====================================================================
33 %%--------------------------------------------------------------------
34 %% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
35 %% Description: Starts the server
36 %%--------------------------------------------------------------------
37 start_link() ->
38 gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
40 -spec add(#task{}) -> true.
41 add(Task) ->
42 gen_server:call(?MODULE, {register, Task}, ?TIMEOUT).
44 -spec set_pid(string(), pid()) -> {ok, pid()} | {error, any()}.
45 set_pid(Target, Pid) ->
46 gen_server:call(?MODULE, {set_pid, Target, Pid}, ?TIMEOUT).
48 -spec find_pid_by_target(string()) -> {ok, pid()} | {error, any()}.
49 find_pid_by_target(Target) ->
50 gen_server:call(?MODULE, {find_pid_by_target, Target}, ?TIMEOUT).
52 -spec find_by_target(string()) -> {ok, #task{} } | {error, any()}.
53 find_by_target(Target) ->
54 gen_server:call(?MODULE, {find_by_target, Target}, ?TIMEOUT).
56 %%====================================================================
57 %% gen_server callbacks
58 %%====================================================================
60 %%--------------------------------------------------------------------
61 %% Function: init(Args) -> {ok, State} |
62 %% {ok, State, Timeout} |
63 %% ignore |
64 %% {stop, Reason}
65 %% Description: Initiates the server
66 %%--------------------------------------------------------------------
67 init([]) ->
68 case ets:info(?MODULE) of
69 undefined -> ets:new(?MODULE, [public, named_table]);
70 _ -> ets:delete_all_objects(?MODULE)
71 end,
72 {ok, #state{}}.
76 %%--------------------------------------------------------------------
77 %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
78 %% {reply, Reply, State, Timeout} |
79 %% {noreply, State} |
80 %% {noreply, State, Timeout} |
81 %% {stop, Reason, Reply, State} |
82 %% {stop, Reason, State}
83 %% Description: Handling call messages
84 %%--------------------------------------------------------------------
85 handle_call({find_pid_by_target, Target}, _From, State) ->
86 Reply = case private_find_by_target(Target) of
87 {ok, Task} -> {ok, Task#task.pid};
88 X -> X
89 end,
90 {reply, Reply, State};
91 handle_call({find_by_target, Target}, _From, State) ->
92 {reply, private_find_by_target(Target), State};
93 handle_call({set_pid, Target, Pid}, _From, State) ->
94 Reply = case private_find_by_target(Target) of
95 {ok, Task} ->
96 Res = ets:insert(?MODULE, {Target, Task#task{ pid = Pid }}),
97 case Res of
98 true -> {ok, self()};
99 _ -> {error, ?MODULE, set_pid, [Target, Pid]}
100 end;
101 X -> X
102 end,
103 {reply, Reply, State};
104 handle_call({register, Task}, _From, State) ->
105 Reply = ets:insert(?MODULE, {Task#task.target, Task}),
106 {reply, Reply, State}.
108 %%--------------------------------------------------------------------
109 %% Function: handle_cast(Msg, State) -> {noreply, State} |
110 %% {noreply, State, Timeout} |
111 %% {stop, Reason, State}
112 %% Description: Handling cast messages
113 %%--------------------------------------------------------------------
114 handle_cast(_Msg, State) ->
115 {noreply, State}.
117 %%--------------------------------------------------------------------
118 %% Function: handle_info(Info, State) -> {noreply, State} |
119 %% {noreply, State, Timeout} |
120 %% {stop, Reason, State}
121 %% Description: Handling all non call/cast messages
122 %%--------------------------------------------------------------------
123 handle_info(_Info, State) ->
124 {noreply, State}.
126 %%--------------------------------------------------------------------
127 %% Function: terminate(Reason, State) -> void()
128 %% Description: This function is called by a gen_server when it is about to
129 %% terminate. It should be the opposite of Module:init/1 and do any necessary
130 %% cleaning up. When it returns, the gen_server terminates with Reason.
131 %% The return value is ignored.
132 %%--------------------------------------------------------------------
133 terminate(_Reason, _State) ->
136 %%--------------------------------------------------------------------
137 %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
138 %% Description: Convert process state when code is changed
139 %%--------------------------------------------------------------------
140 code_change(_OldVsn, State, _Extra) ->
141 {ok, State}.
143 %%--------------------------------------------------------------------
144 %%% Internal functions
145 %%--------------------------------------------------------------------
146 private_find_by_target(Target) ->
147 case ets:lookup(?MODULE, Target) of
148 [{Target, Task}|_] -> {ok, Task};
149 X -> {error, X}
150 end.