1%%% Copyright (C) 2009 Enrique Marcote, Miguel Rodriguez
2%%% All rights reserved.
3%%%
4%%% Redistribution and use in source and binary forms, with or without
5%%% modification, are permitted provided that the following conditions are met:
6%%%
7%%% o Redistributions of source code must retain the above copyright notice,
8%%%   this list of conditions and the following disclaimer.
9%%%
10%%% o Redistributions in binary form must reproduce the above copyright notice,
11%%%   this list of conditions and the following disclaimer in the documentation
12%%%   and/or other materials provided with the distribution.
13%%%
14%%% o Neither the name of ERLANG TRAINING AND CONSULTING nor the names of its
15%%%   contributors may be used to endorse or promote products derived from this
16%%%   software without specific prior written permission.
17%%%
18%%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19%%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20%%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21%%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22%%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23%%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24%%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25%%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26%%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27%%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28%%% POSSIBILITY OF SUCH DAMAGE.
29-module(cl_timer_srv).
30-behaviour(gen_server).
31
32%%% START/STOP EXPORTS
33-export([start_link/0]).
34
35%%% EXTERNAL EXPORTS
36-export([cancel/1, exit_after/3]).
37
38%%% INIT/TERMINATE EXPORTS
39-export([init/1, terminate/2]).
40
41%%% HANDLE MESSAGES EXPORTS
42-export([handle_call/3, handle_cast/2, handle_info/2]).
43
44%%% CODE UPDATE EXPORTS
45-export([code_change/3]).
46
47%%% RECORDS
48-record(st, {}).
49
50%%%-----------------------------------------------------------------------------
51%%% START/STOP EXPORTS
52%%%-----------------------------------------------------------------------------
53start_link() ->
54    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
55
56%%%-----------------------------------------------------------------------------
57%%% EXTERNAL EXPORTS
58%%%-----------------------------------------------------------------------------
59cancel(Ref) ->
60    erlang:cancel_timer(Ref).
61
62
63exit_after(Time, Pid, Reason) ->
64    erlang:start_timer(Time, ?MODULE, {exit, Pid, Reason}).
65
66
67%%%-----------------------------------------------------------------------------
68%%% INIT/TERMINATE EXPORTS
69%%%-----------------------------------------------------------------------------
70init([]) ->
71    {ok, #st{}}.
72
73
74terminate(_Reason, _St) ->
75    ok.
76
77%%%-----------------------------------------------------------------------------
78%%% HANDLE MESSAGES EXPORTS
79%%%-----------------------------------------------------------------------------
80handle_call(Req, From, St) ->
81    erlang:error(function_clause, [Req, From, St]).
82
83handle_cast(Req, St) ->
84    erlang:error(function_clause, [Req, St]).
85
86handle_info({timeout, _Ref, {exit, Pid, Reason}}, St) ->
87    exit(Pid, Reason),
88    {noreply, St};
89handle_info(_Info, St) ->
90    {noreply, St}.
91
92%%%-----------------------------------------------------------------------------
93%%% CODE UPDATE EXPORTS
94%%%-----------------------------------------------------------------------------
95code_change(_OldVsn, St, _Extra) ->
96    {ok, St}.
97