25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00
xmpp.chapril.org-ejabberd/src/ejabberd_sm_mnesia.erl

167 lines
5.2 KiB
Erlang
Raw Normal View History

2015-03-09 12:57:33 +01:00
%%%-------------------------------------------------------------------
2016-12-27 10:44:07 +01:00
%%% File : ejabberd_sm_mnesia.erl
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
2015-03-09 12:57:33 +01:00
%%% Created : 9 Mar 2015 by Evgeny Khramtsov <ekhramtsov@process-one.net>
2016-12-27 10:44:07 +01:00
%%%
%%%
2024-01-22 16:40:01 +01:00
%%% ejabberd, Copyright (C) 2002-2024 ProcessOne
2016-12-27 10:44:07 +01:00
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%----------------------------------------------------------------------
2015-03-09 12:57:33 +01:00
-module(ejabberd_sm_mnesia).
-behaviour(gen_server).
-behaviour(ejabberd_sm).
%% API
-export([init/0,
use_cache/1,
2015-03-09 12:57:33 +01:00
set_session/1,
delete_session/1,
2015-03-09 12:57:33 +01:00
get_sessions/0,
get_sessions/1,
get_sessions/2]).
2015-03-09 12:57:33 +01:00
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
2017-02-26 13:10:59 +01:00
terminate/2, code_change/3, start_link/0]).
2015-03-09 12:57:33 +01:00
-include("ejabberd_sm.hrl").
2019-07-12 10:55:36 +02:00
-include("logger.hrl").
2015-03-09 12:57:33 +01:00
-include_lib("stdlib/include/ms_transform.hrl").
-record(state, {}).
%%%===================================================================
%%% API
%%%===================================================================
-spec init() -> ok | {error, any()}.
init() ->
2017-02-26 13:10:59 +01:00
Spec = {?MODULE, {?MODULE, start_link, []},
transient, 5000, worker, [?MODULE]},
case supervisor:start_child(ejabberd_backend_sup, Spec) of
{ok, _Pid} -> ok;
Err -> Err
2015-03-09 12:57:33 +01:00
end.
2017-02-26 13:10:59 +01:00
-spec start_link() -> {ok, pid()} | {error, any()}.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-spec use_cache(binary()) -> boolean().
use_cache(_LServer) ->
false.
2015-03-09 12:57:33 +01:00
-spec set_session(#session{}) -> ok.
set_session(Session) ->
mnesia:dirty_write(Session).
-spec delete_session(#session{}) -> ok.
delete_session(#session{sid = SID}) ->
mnesia:dirty_delete(session, SID).
2015-03-09 12:57:33 +01:00
-spec get_sessions() -> [#session{}].
get_sessions() ->
ets:tab2list(session).
-spec get_sessions(binary()) -> [#session{}].
get_sessions(LServer) ->
mnesia:dirty_select(session,
[{#session{usr = '$1', _ = '_'},
[{'==', {element, 2, '$1'}, LServer}], ['$_']}]).
-spec get_sessions(binary(), binary()) -> {ok, [#session{}]}.
2015-03-09 12:57:33 +01:00
get_sessions(LUser, LServer) ->
{ok, mnesia:dirty_index_read(session, {LUser, LServer}, #session.us)}.
2015-03-09 12:57:33 +01:00
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
init([]) ->
update_tables(),
2016-11-30 11:09:17 +01:00
ejabberd_mnesia:create(?MODULE, session,
2015-03-09 12:57:33 +01:00
[{ram_copies, [node()]},
2017-01-17 15:05:14 +01:00
{attributes, record_info(fields, session)},
{index, [usr,us]}]),
2016-11-30 11:09:17 +01:00
ejabberd_mnesia:create(?MODULE, session_counter,
2015-03-09 12:57:33 +01:00
[{ram_copies, [node()]},
{attributes, record_info(fields, session_counter)}]),
mnesia:subscribe(system),
{ok, #state{}}.
2019-07-12 10:55:36 +02:00
handle_call(Request, From, State) ->
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
{noreply, State}.
2015-03-09 12:57:33 +01:00
2019-07-12 10:55:36 +02:00
handle_cast(Msg, State) ->
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
2015-03-09 12:57:33 +01:00
{noreply, State}.
handle_info({mnesia_system_event, {mnesia_down, Node}}, State) ->
?INFO_MSG("Node ~p has left our Mnesia SM tables", [Node]),
2017-11-30 11:31:23 +01:00
Sessions =
ets:select(
session,
ets:fun2ms(
fun(#session{sid = {_, Pid}} = S)
when node(Pid) == Node ->
S
end)),
lists:foreach(
fun(S) ->
mnesia:dirty_delete_object(S)
end, Sessions),
2015-03-09 12:57:33 +01:00
{noreply, State};
handle_info({mnesia_system_event, {mnesia_up, Node}}, State) ->
?INFO_MSG("Node ~p joined our Mnesia SM tables", [Node]),
{noreply, State};
2019-07-12 10:55:36 +02:00
handle_info(Info, State) ->
?WARNING_MSG("Unexpected info: ~p", [Info]),
2015-03-09 12:57:33 +01:00
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
update_tables() ->
case catch mnesia:table_info(session, attributes) of
[ur, user, node] -> mnesia:delete_table(session);
[ur, user, pid] -> mnesia:delete_table(session);
[usr, us, pid] -> mnesia:delete_table(session);
[usr, us, sid, priority, info] -> mnesia:delete_table(session);
[sid, usr, us, priority] ->
mnesia:delete_table(session);
[sid, usr, us, priority, info] -> ok;
{'EXIT', _} -> ok
end,
case lists:member(presence, mnesia:system_info(tables))
of
true -> mnesia:delete_table(presence);
false -> ok
end,
case lists:member(local_session, mnesia:system_info(tables)) of
true ->
mnesia:delete_table(local_session);
false ->
ok
end.