|
| 1 | +%%%----------------------------------------------------------------------------- |
| 2 | +%%% @copyright (C) 2016-2021, 2600Hz |
| 3 | +%%% @doc |
| 4 | +%%% @author Daniel Finke |
| 5 | +%%% @end |
| 6 | +%%%----------------------------------------------------------------------------- |
| 7 | +-module(acdc_recordings_map_srv). |
| 8 | + |
| 9 | +-behaviour(gen_server). |
| 10 | + |
| 11 | +%% API |
| 12 | +-export([start_link/0]). |
| 13 | +-export([register/2]). |
| 14 | + |
| 15 | +%% gen_server callbacks |
| 16 | +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, |
| 17 | + terminate/2, code_change/3]). |
| 18 | + |
| 19 | +-include("acdc.hrl"). |
| 20 | + |
| 21 | +-define(SERVER, ?MODULE). |
| 22 | + |
| 23 | +-record(state, {table_id :: ets:tid()}). |
| 24 | +-type state() :: #state{}. |
| 25 | + |
| 26 | +%%%============================================================================= |
| 27 | +%%% API |
| 28 | +%%%============================================================================= |
| 29 | + |
| 30 | +%%------------------------------------------------------------------------------ |
| 31 | +%% @doc Starts the server |
| 32 | +%% |
| 33 | +%% @end |
| 34 | +%%------------------------------------------------------------------------------ |
| 35 | +-spec start_link() -> kz_types:startlink_ret(). |
| 36 | +start_link() -> |
| 37 | + gen_server:start_link({'local', ?SERVER}, ?MODULE, [], []). |
| 38 | + |
| 39 | +-spec register(kapps_call:call(), kz_json:object()) -> pid(). |
| 40 | +register(Call, RecordingJObj) -> |
| 41 | + gen_server:call(?SERVER, {'register', Call, RecordingJObj}). |
| 42 | + |
| 43 | +%%%============================================================================= |
| 44 | +%%% gen_server callbacks |
| 45 | +%%%============================================================================= |
| 46 | + |
| 47 | +%%------------------------------------------------------------------------------ |
| 48 | +%% @doc Initializes the server |
| 49 | +%% |
| 50 | +%% @end |
| 51 | +%%------------------------------------------------------------------------------ |
| 52 | +-spec init([]) -> {'ok', state()}. |
| 53 | +init([]) -> |
| 54 | + process_flag('trap_exit', 'true'), |
| 55 | + TabId = ets:new('map', []), |
| 56 | + {'ok', #state{table_id=TabId}}. |
| 57 | + |
| 58 | +%%------------------------------------------------------------------------------ |
| 59 | +%% @doc Handling call messages |
| 60 | +%% |
| 61 | +%% @end |
| 62 | +%%------------------------------------------------------------------------------ |
| 63 | +-spec handle_call(any(), kz_term:pid_ref(), state()) -> kz_types:handle_call_ret_state(state()). |
| 64 | +handle_call({'register', Call, RecordingJObj}, _From, #state{table_id=TabId}=State) -> |
| 65 | + case ets:lookup(TabId, kapps_call:call_id(Call)) of |
| 66 | + [] -> |
| 67 | + {'ok', Pid} = acdc_recordings_sup:new(Call, RecordingJObj), |
| 68 | + link(Pid), |
| 69 | + ets:insert(TabId, {kapps_call:call_id(Call), Pid}), |
| 70 | + Pid; |
| 71 | + [{_, Pid}] -> Pid |
| 72 | + end, |
| 73 | + {'reply', Pid, State}; |
| 74 | +handle_call(_Request, _From, State) -> |
| 75 | + Reply = 'ok', |
| 76 | + {'reply', Reply, State}. |
| 77 | + |
| 78 | +%%------------------------------------------------------------------------------ |
| 79 | +%% @doc Handling cast messages |
| 80 | +%% |
| 81 | +%% @end |
| 82 | +%%------------------------------------------------------------------------------ |
| 83 | +-spec handle_cast(any(), state()) -> kz_types:handle_cast_ret_state(state()). |
| 84 | +handle_cast(_Msg, State) -> |
| 85 | + {'noreply', State}. |
| 86 | + |
| 87 | +%%------------------------------------------------------------------------------ |
| 88 | +%% @doc Handling all non call/cast messages |
| 89 | +%% |
| 90 | +%% @end |
| 91 | +%%------------------------------------------------------------------------------ |
| 92 | +-spec handle_info(any(), state()) -> kz_types:handle_info_ret_state(state()). |
| 93 | +handle_info({'EXIT', Pid, _Reason}, #state{table_id=TabId}=State) -> |
| 94 | + ets:match_delete(TabId, {'$1', Pid}), |
| 95 | + {'noreply', State}; |
| 96 | +handle_info(_Info, State) -> |
| 97 | + {'noreply', State}. |
| 98 | + |
| 99 | +%%------------------------------------------------------------------------------ |
| 100 | +%% @doc This function is called by a gen_server when it is about to |
| 101 | +%% terminate. It should be the opposite of Module:init/1 and do any |
| 102 | +%% necessary cleaning up. When it returns, the gen_server terminates |
| 103 | +%% with Reason. The return value is ignored. |
| 104 | +%% |
| 105 | +%% @end |
| 106 | +%%------------------------------------------------------------------------------ |
| 107 | +-spec terminate(any(), state()) -> 'ok'. |
| 108 | +terminate(_Reason, _State) -> |
| 109 | + 'ok'. |
| 110 | + |
| 111 | +%%------------------------------------------------------------------------------ |
| 112 | +%% @doc Convert process state when code is changed |
| 113 | +%% |
| 114 | +%% @end |
| 115 | +%%------------------------------------------------------------------------------ |
| 116 | +-spec code_change(any(), state(), any()) -> {'ok', state()}. |
| 117 | +code_change(_OldVsn, State, _Extra) -> |
| 118 | + {'ok', State}. |
| 119 | + |
| 120 | +%%%============================================================================= |
| 121 | +%%% Internal functions |
| 122 | +%%%============================================================================= |
0 commit comments