1 %%%-------------------------------------------------------------------
2 %%% File : task_registry.erl
6 %%% Created : 25 Mar 2011 by <>
7 %%%-------------------------------------------------------------------
8 -module(makerl
.task_registry
).
9 -include("makerl.hrl").
14 -behaviour(gen_server
).
17 -export([start_link
/0,
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
).
30 %%====================================================================
32 %%====================================================================
33 %%--------------------------------------------------------------------
34 %% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
35 %% Description: Starts the server
36 %%--------------------------------------------------------------------
38 gen_server:start_link({local
, ?MODULE
}, ?MODULE
, [], []).
40 -spec
add(#task
{}) -> true
.
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} |
65 %% Description: Initiates the server
66 %%--------------------------------------------------------------------
68 case ets:info(?MODULE
) of
69 undefined
-> ets:new(?MODULE
, [public
, named_table
]);
70 _
-> ets:delete_all_objects(?MODULE
)
76 %%--------------------------------------------------------------------
77 %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
78 %% {reply, Reply, State, Timeout} |
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};
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
96 Res
= ets:insert(?MODULE
, {Target
, Task#task
{ pid = Pid
}}),
99 _
-> {error
, ?MODULE
, set_pid
, [Target
, Pid
]}
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
) ->
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
) ->
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
) ->
143 %%--------------------------------------------------------------------
144 %%% Internal functions
145 %%--------------------------------------------------------------------
146 private_find_by_target(Target
) ->
147 case ets:lookup(?MODULE
, Target
) of
148 [{Target
, Task
}|_
] -> {ok
, Task
};