diff --git a/src/ejabberd_router.erl b/src/ejabberd_router.erl index eeb9b45b0..879a88c0e 100644 --- a/src/ejabberd_router.erl +++ b/src/ejabberd_router.erl @@ -65,7 +65,6 @@ -callback is_my_route(binary()) -> boolean(). -callback is_my_host(binary()) -> boolean(). -callback get_all_routes() -> [binary()]. --callback handle_event(term()) -> any(). -record(state, {}). @@ -251,9 +250,8 @@ handle_info({route, From, To, Packet}, 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) -> diff --git a/src/ejabberd_router_mnesia.erl b/src/ejabberd_router_mnesia.erl index f53541f24..d9946cef5 100644 --- a/src/ejabberd_router_mnesia.erl +++ b/src/ejabberd_router_mnesia.erl @@ -8,32 +8,32 @@ %%%------------------------------------------------------------------- -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 () -> @@ -135,10 +135,35 @@ get_all_routes() -> 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, _ = '_'}, [], ['$_']}]), @@ -158,10 +183,18 @@ handle_event({'DOWN', _Ref, _Type, Pid, _Info}) -> 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 %%%=================================================================== diff --git a/src/mod_muc.erl b/src/mod_muc.erl index 149c25f46..e75fb3893 100644 --- a/src/mod_muc.erl +++ b/src/mod_muc.erl @@ -105,7 +105,6 @@ -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 @@ -377,9 +376,8 @@ handle_info({room_destroyed, {Room, Host}, Pid}, State) -> 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}) -> diff --git a/src/mod_muc_mnesia.erl b/src/mod_muc_mnesia.erl index 0552184b4..14c048863 100644 --- a/src/mod_muc_mnesia.erl +++ b/src/mod_muc_mnesia.erl @@ -17,51 +17,29 @@ -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) -> @@ -247,11 +225,6 @@ get_online_rooms(Action, Key, Host, Count, Max, Items) -> Items end. -handle_event({mnesia_system_event, {mnesia_down, Node}}) -> - clean_table_from_bad_node(Node); -handle_event(_) -> - ok. - rsm_supported() -> true. @@ -294,6 +267,64 @@ import(_LServer, <<"muc_registered">>, #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 %%%=================================================================== diff --git a/src/mod_muc_riak.erl b/src/mod_muc_riak.erl index f53d945fe..47e50f86c 100644 --- a/src/mod_muc_riak.erl +++ b/src/mod_muc_riak.erl @@ -17,8 +17,7 @@ -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]). @@ -140,9 +139,6 @@ count_online_rooms(_) -> get_online_rooms(_, _) -> erlang:error(not_implemented). -handle_event(_) -> - ok. - rsm_supported() -> false. diff --git a/src/mod_muc_sql.erl b/src/mod_muc_sql.erl index 08b8bf9e0..6a37451c0 100644 --- a/src/mod_muc_sql.erl +++ b/src/mod_muc_sql.erl @@ -20,8 +20,7 @@ -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]). @@ -165,9 +164,6 @@ count_online_rooms(_) -> get_online_rooms(_, _) -> erlang:error(not_implemented). -handle_event(_) -> - ok. - rsm_supported() -> false.