1 |
|
%% @doc Backend module for mod_jingle_sip for CETS backend |
2 |
|
-module(mod_jingle_sip_cets). |
3 |
|
-behaviour(mod_jingle_sip_backend). |
4 |
|
|
5 |
|
-include("mongoose.hrl"). |
6 |
|
|
7 |
|
-export([init/2]). |
8 |
|
-export([read_session/1]). |
9 |
|
-export([write_new_session/2]). |
10 |
|
-export([update_session/2]). |
11 |
|
-export([remove_session/1]). |
12 |
|
|
13 |
|
-type call_id() :: mod_jingle_sip_session:call_id(). |
14 |
|
-type session() :: mod_jingle_sip_session:session(). |
15 |
|
-type update_fun() :: mod_jingle_sip_session:update_fun(). |
16 |
|
|
17 |
|
-define(TABLE, cets_jingle_sip_session). |
18 |
|
|
19 |
|
-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
20 |
|
init(_Host, _Opts) -> |
21 |
|
%% We store Erlang records, so keypos is 2 |
22 |
:-( |
cets:start(?TABLE, #{keypos => 2}), |
23 |
:-( |
cets_discovery:add_table(mongoose_cets_discovery, ?TABLE). |
24 |
|
|
25 |
|
-spec read_session(call_id()) -> [session()]. |
26 |
|
read_session(CallID) -> |
27 |
:-( |
ets:lookup(?TABLE, CallID). |
28 |
|
|
29 |
|
-spec write_new_session(call_id(), session()) -> |
30 |
|
ok | {error, conflict}. |
31 |
|
write_new_session(CallID, Session) -> |
32 |
:-( |
case read_session(CallID) of |
33 |
|
[_] -> |
34 |
:-( |
{error, conflict}; |
35 |
|
_ -> |
36 |
:-( |
case cets:insert_new(?TABLE, Session) of |
37 |
|
true -> |
38 |
:-( |
ok; |
39 |
|
false -> |
40 |
:-( |
{error, conflict} |
41 |
|
end |
42 |
|
end. |
43 |
|
|
44 |
|
-spec update_session(call_id(), update_fun()) -> ok | {error, _}. |
45 |
|
update_session(CallID, F) -> |
46 |
:-( |
case read_session(CallID) of |
47 |
|
[Session] -> |
48 |
:-( |
case F(Session) of |
49 |
:-( |
{error, _} = Err -> Err; |
50 |
:-( |
Session2 -> cets:insert(?TABLE, Session2) |
51 |
|
end; |
52 |
|
_ -> |
53 |
:-( |
{error, not_found} |
54 |
|
end. |
55 |
|
|
56 |
|
-spec remove_session(call_id()) -> ok. |
57 |
|
remove_session(CallID) -> |
58 |
:-( |
cets:delete(?TABLE, CallID). |