-callback is_my_route(binary()) -> boolean().
-callback is_my_host(binary()) -> boolean().
-callback get_all_routes() -> [binary()].
--callback handle_event(term()) -> any().
-record(state, {}).
_ -> ok
end,
{noreply, State};
-handle_info(Event, State) ->
- Mod = get_backend(),
- Mod:handle_event(Event),
+handle_info(Info, State) ->
+ ?ERROR_MSG("unexpected info: ~p", [Info]),
{noreply, State}.
terminate(_Reason, _State) ->
%%%-------------------------------------------------------------------
-module(ejabberd_router_mnesia).
-behaviour(ejabberd_router).
+-behaviour(gen_server).
%% API
-export([init/0, register_route/4, unregister_route/2, find_routes/1,
- host_of_route/1, is_my_route/1, is_my_host/1, get_all_routes/0,
- handle_event/1]).
+ host_of_route/1, is_my_route/1, is_my_host/1, get_all_routes/0]).
+%% gen_server callbacks
+-export([init/1, handle_cast/2, handle_call/3, handle_info/2,
+ terminate/2, code_change/3]).
-include("ejabberd.hrl").
-include("ejabberd_router.hrl").
-include("logger.hrl").
-include_lib("stdlib/include/ms_transform.hrl").
+-record(state, {}).
+
%%%===================================================================
%%% API
%%%===================================================================
init() ->
- update_tables(),
- ejabberd_mnesia:create(?MODULE, route,
- [{ram_copies, [node()]},
- {type, bag},
- {attributes, record_info(fields, route)}]),
- mnesia:add_table_copy(route, node(), ram_copies),
- mnesia:subscribe({table, route, simple}),
- lists:foreach(
- fun (Pid) -> erlang:monitor(process, Pid) end,
- mnesia:dirty_select(route,
- [{{route, '_', '$1', '_'}, [], ['$1']}])).
+ case gen_server:start_link({local, ?MODULE}, ?MODULE, [], []) of
+ {ok, _Pid} ->
+ ok;
+ Err ->
+ Err
+ end.
register_route(Domain, ServerHost, LocalHint, undefined) ->
F = fun () ->
when Domain /= ServerHost -> Domain
end)).
-handle_event({mnesia_table_event,
- {write, #route{pid = Pid}, _ActivityId}}) ->
- erlang:monitor(process, Pid);
-handle_event({'DOWN', _Ref, _Type, Pid, _Info}) ->
+%%%===================================================================
+%%% gen_server callbacks
+%%%===================================================================
+init([]) ->
+ update_tables(),
+ ejabberd_mnesia:create(?MODULE, route,
+ [{ram_copies, [node()]},
+ {type, bag},
+ {attributes, record_info(fields, route)}]),
+ mnesia:add_table_copy(route, node(), ram_copies),
+ mnesia:subscribe({table, route, simple}),
+ lists:foreach(
+ fun (Pid) -> erlang:monitor(process, Pid) end,
+ mnesia:dirty_select(route,
+ [{{route, '_', '$1', '_'}, [], ['$1']}])),
+ {ok, #state{}}.
+
+handle_call(_Request, _From, State) ->
+ Reply = ok,
+ {reply, Reply, State}.
+
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+handle_info({mnesia_table_event,
+ {write, #route{pid = Pid}, _ActivityId}}, State) ->
+ erlang:monitor(process, Pid),
+ {noreply, State};
+handle_info({'DOWN', _Ref, _Type, Pid, _Info}, State) ->
F = fun () ->
Es = mnesia:select(route,
[{#route{pid = Pid, _ = '_'}, [], ['$_']}]),
end
end, Es)
end,
- transaction(F);
-handle_event(_Event) ->
+ transaction(F),
+ {noreply, State};
+handle_info(Info, State) ->
+ ?ERROR_MSG("unexpected info: ~p", [Info]),
+ {noreply, State}.
+
+terminate(_Reason, _State) ->
ok.
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
%%%===================================================================
%%% Internal functions
%%%===================================================================
-callback unregister_online_user(ljid(), binary(), binary()) -> any().
-callback count_online_rooms_by_user(binary(), binary()) -> non_neg_integer().
-callback get_online_rooms_by_user(binary(), binary()) -> [{binary(), binary()}].
--callback handle_event(term()) -> any().
%%====================================================================
%% API
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
RMod:unregister_online_room(Room, Host, Pid),
{noreply, State};
-handle_info(Event, #state{server_host = LServer} = State) ->
- RMod = gen_mod:ram_db_mod(LServer, ?MODULE),
- RMod:handle_event(Event),
+handle_info(Info, State) ->
+ ?ERROR_MSG("unexpected info: ~p", [Info]),
{noreply, State}.
terminate(_Reason, #state{host = MyHost}) ->
-export([register_online_room/3, unregister_online_room/3, find_online_room/2,
get_online_rooms/2, count_online_rooms/1, rsm_supported/0,
register_online_user/3, unregister_online_user/3,
- count_online_rooms_by_user/2, get_online_rooms_by_user/2,
- handle_event/1]).
+ count_online_rooms_by_user/2, get_online_rooms_by_user/2]).
-export([set_affiliation/6, set_affiliations/4, get_affiliation/5,
get_affiliations/3, search_affiliation/4]).
+%% gen_server callbacks
+-export([init/1, handle_cast/2, handle_call/3, handle_info/2,
+ terminate/2, code_change/3]).
-include("mod_muc.hrl").
-include("logger.hrl").
-include("xmpp.hrl").
-include_lib("stdlib/include/ms_transform.hrl").
+-record(state, {}).
+
%%%===================================================================
%%% API
%%%===================================================================
init(Host, Opts) ->
- MyHost = proplists:get_value(host, Opts),
- case gen_mod:db_mod(Host, Opts, mod_muc) of
- ?MODULE ->
- ejabberd_mnesia:create(?MODULE, muc_room,
- [{disc_copies, [node()]},
- {attributes,
- record_info(fields, muc_room)}]),
- ejabberd_mnesia:create(?MODULE, muc_registered,
- [{disc_copies, [node()]},
- {attributes,
- record_info(fields, muc_registered)}]),
- update_tables(MyHost),
- mnesia:add_table_index(muc_registered, nick);
- _ ->
- ok
- end,
- case gen_mod:ram_db_mod(Host, Opts, mod_muc) of
- ?MODULE ->
- update_muc_online_table(),
- ejabberd_mnesia:create(?MODULE, muc_online_room,
- [{ram_copies, [node()]},
- {type, ordered_set},
- {attributes,
- record_info(fields, muc_online_room)}]),
- mnesia:add_table_copy(muc_online_room, node(), ram_copies),
- catch ets:new(muc_online_users,
- [bag, named_table, public, {keypos, 2}]),
- clean_table_from_bad_node(node(), MyHost),
- mnesia:subscribe(system);
- _ ->
- ok
+ case gen_server:start_link({local, ?MODULE}, ?MODULE, [Host, Opts], []) of
+ {ok, _Pid} ->
+ ok;
+ Err ->
+ Err
end.
store_room(_LServer, Host, Name, Opts) ->
Items
end.
-handle_event({mnesia_system_event, {mnesia_down, Node}}) ->
- clean_table_from_bad_node(Node);
-handle_event(_) ->
- ok.
-
rsm_supported() ->
true.
#muc_registered{us_host = {{U, S}, RoomHost},
nick = Nick}).
+%%%===================================================================
+%%% gen_server callbacks
+%%%===================================================================
+init([Host, Opts]) ->
+ MyHost = proplists:get_value(host, Opts),
+ case gen_mod:db_mod(Host, Opts, mod_muc) of
+ ?MODULE ->
+ ejabberd_mnesia:create(?MODULE, muc_room,
+ [{disc_copies, [node()]},
+ {attributes,
+ record_info(fields, muc_room)}]),
+ ejabberd_mnesia:create(?MODULE, muc_registered,
+ [{disc_copies, [node()]},
+ {attributes,
+ record_info(fields, muc_registered)}]),
+ update_tables(MyHost),
+ mnesia:add_table_index(muc_registered, nick);
+ _ ->
+ ok
+ end,
+ case gen_mod:ram_db_mod(Host, Opts, mod_muc) of
+ ?MODULE ->
+ update_muc_online_table(),
+ ejabberd_mnesia:create(?MODULE, muc_online_room,
+ [{ram_copies, [node()]},
+ {type, ordered_set},
+ {attributes,
+ record_info(fields, muc_online_room)}]),
+ mnesia:add_table_copy(muc_online_room, node(), ram_copies),
+ catch ets:new(muc_online_users,
+ [bag, named_table, public, {keypos, 2}]),
+ clean_table_from_bad_node(node(), MyHost),
+ mnesia:subscribe(system);
+ _ ->
+ ok
+ end,
+ {ok, #state{}}.
+
+handle_call(_Request, _From, State) ->
+ Reply = ok,
+ {reply, Reply, State}.
+
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+handle_info({mnesia_system_event, {mnesia_down, Node}}, State) ->
+ clean_table_from_bad_node(Node),
+ {noreply, State};
+handle_info(Info, State) ->
+ ?ERROR_MSG("unexpected info: ~p", [Info]),
+ {noreply, State}.
+
+terminate(_Reason, _State) ->
+ ok.
+
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
%%%===================================================================
%%% Internal functions
%%%===================================================================
-export([register_online_room/3, unregister_online_room/3, find_online_room/2,
get_online_rooms/2, count_online_rooms/1, rsm_supported/0,
register_online_user/3, unregister_online_user/3,
- count_online_rooms_by_user/2, get_online_rooms_by_user/2,
- handle_event/1]).
+ count_online_rooms_by_user/2, get_online_rooms_by_user/2]).
-export([set_affiliation/6, set_affiliations/4, get_affiliation/5,
get_affiliations/3, search_affiliation/4]).
get_online_rooms(_, _) ->
erlang:error(not_implemented).
-handle_event(_) ->
- ok.
-
rsm_supported() ->
false.
-export([register_online_room/3, unregister_online_room/3, find_online_room/2,
get_online_rooms/2, count_online_rooms/1, rsm_supported/0,
register_online_user/3, unregister_online_user/3,
- count_online_rooms_by_user/2, get_online_rooms_by_user/2,
- handle_event/1]).
+ count_online_rooms_by_user/2, get_online_rooms_by_user/2]).
-export([set_affiliation/6, set_affiliations/4, get_affiliation/5,
get_affiliations/3, search_affiliation/4]).
get_online_rooms(_, _) ->
erlang:error(not_implemented).
-handle_event(_) ->
- ok.
-
rsm_supported() ->
false.