1%%------------------------------------------------------------------- 2%% 3%% Copyright (c) 2015, James Fish <james@fishcakez.com> 4%% 5%% This file is provided to you under the Apache License, 6%% Version 2.0 (the "License"); you may not use this file 7%% except in compliance with the License. You may obtain 8%% a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, 13%% software distributed under the License is distributed on an 14%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15%% KIND, either express or implied. See the License for the 16%% specific language governing permissions and limitations 17%% under the License. 18%% 19%%------------------------------------------------------------------- 20-module(sbroker_test_handler). 21 22-behaviour(gen_event). 23 24%% public api 25 26-export([add_handler/0]). 27-export([get_alarms/0]). 28-export([reset/0]). 29-export([delete_handler/0]). 30 31%% gen_event api 32-export([init/1]). 33-export([handle_event/2]). 34-export([handle_call/2]). 35-export([handle_info/2]). 36-export([code_change/3]). 37-export([terminate/2]). 38 39%% public api 40 41add_handler() -> 42 swap_handler(alarm_handler, ?MODULE). 43 44get_alarms() -> 45 gen_event:call(alarm_handler, ?MODULE, get_alarms). 46 47reset() -> 48 gen_event:call(alarm_handler, ?MODULE, reset). 49 50delete_handler() -> 51 swap_handler(?MODULE, alarm_handler). 52 53%% gen_event api 54 55init(_) -> 56 {ok, []}. 57 58handle_event({set_alarm, Alarm}, Alarms) -> 59 {ok, [Alarm | Alarms]}; 60handle_event({clear_alarm, Name}, Alarms) -> 61 {ok, lists:keydelete(Name, 1, Alarms)}. 62 63handle_call(get_alarms, Alarms) -> 64 {ok, Alarms, Alarms}; 65handle_call(reset, Alarms) -> 66 {ok, Alarms, []}. 67 68handle_info(_Info, State) -> 69 {ok, State}. 70 71code_change(_OldVsn, State, _Extra) -> 72 {ok, State}. 73 74terminate(swap, Alarms) -> 75 {?MODULE, Alarms}; 76terminate(_Reason, _State) -> 77 ok. 78 79%% Internal 80 81swap_handler(From, To) -> 82 ok = gen_event:add_handler(alarm_handler, To, []), 83 gen_event:delete_handler(alarm_handler, From, swap). 84