1 |
|
%% Mnesia backend module for mod_jingle_sip module |
2 |
|
-module(mod_jingle_sip_mnesia). |
3 |
|
-behaviour(mod_jingle_sip_backend). |
4 |
|
|
5 |
|
-include("mongoose.hrl"). |
6 |
|
-include("mod_jingle_sip_session.hrl"). |
7 |
|
|
8 |
|
-export([init/2]). |
9 |
|
-export([read_session/1]). |
10 |
|
-export([write_new_session/2]). |
11 |
|
-export([update_session/2]). |
12 |
|
-export([remove_session/1]). |
13 |
|
|
14 |
|
-type call_id() :: mod_jingle_sip_session:call_id(). |
15 |
|
-type session() :: mod_jingle_sip_session:session(). |
16 |
|
-type update_fun() :: mod_jingle_sip_session:update_fun(). |
17 |
|
|
18 |
|
-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
19 |
|
init(_Host, _Opts) -> |
20 |
:-( |
mongoose_mnesia:create_table(jingle_sip_session, |
21 |
|
[{ram_copies, [node()]}, |
22 |
|
{attributes, record_info(fields, jingle_sip_session)}]), |
23 |
:-( |
ok. |
24 |
|
|
25 |
|
-spec read_session(call_id()) -> [session()]. |
26 |
|
read_session(CallID) -> |
27 |
:-( |
mnesia:dirty_read(jingle_sip_session, CallID). |
28 |
|
|
29 |
|
-spec write_new_session(call_id(), session()) -> |
30 |
|
ok | {error, conflict}. |
31 |
|
write_new_session(CallID, Session) -> |
32 |
:-( |
run_transaction(fun() -> write_new_session_tr(CallID, Session) end). |
33 |
|
|
34 |
|
write_new_session_tr(CallID, Session) -> |
35 |
:-( |
case mnesia:wread({jingle_sip_session, CallID}) of |
36 |
|
[_] -> |
37 |
:-( |
{error, conflict}; |
38 |
|
_ -> |
39 |
:-( |
mnesia:write(Session) |
40 |
|
end. |
41 |
|
|
42 |
|
-spec update_session(call_id(), update_fun()) -> ok | {error, _}. |
43 |
|
update_session(CallID, F) -> |
44 |
:-( |
run_transaction(fun() -> update_session_tr(CallID, F) end). |
45 |
|
|
46 |
|
update_session_tr(CallID, F) -> |
47 |
:-( |
case mnesia:wread({jingle_sip_session, CallID}) of |
48 |
|
[Session] -> |
49 |
:-( |
case F(Session) of |
50 |
:-( |
{error, _} = Err -> Err; |
51 |
:-( |
Session2 -> mnesia:write(Session2) |
52 |
|
end; |
53 |
|
_ -> |
54 |
:-( |
{error, not_found} |
55 |
|
end. |
56 |
|
|
57 |
|
run_transaction(TFun) -> |
58 |
:-( |
case mnesia:transaction(TFun) of |
59 |
|
{atomic, Result} -> |
60 |
:-( |
Result; |
61 |
|
{aborted, Reason} -> |
62 |
:-( |
{error, Reason} |
63 |
|
end. |
64 |
|
|
65 |
|
-spec remove_session(call_id()) -> ok. |
66 |
|
remove_session(CallID) -> |
67 |
:-( |
mnesia:dirty_delete(jingle_sip_session, CallID). |