diff --git a/big_tests/dynamic_domains.spec b/big_tests/dynamic_domains.spec index be64140486..6b04cd5e2c 100644 --- a/big_tests/dynamic_domains.spec +++ b/big_tests/dynamic_domains.spec @@ -15,6 +15,8 @@ {skip_cases, "tests", carboncopy_SUITE, [discovering_support], "at the moment mod_disco doesn't support dynamic domains"}. +{suites, "tests", mod_ping_SUITE}. + {suites, "tests", inbox_SUITE}. {skip_cases, "tests", inbox_SUITE, [disco_service], "at the moment mod_disco doesn't support dynamic domains"}. diff --git a/big_tests/tests/domain_helper.erl b/big_tests/tests/domain_helper.erl index 6f3c56a6c0..f5942bfd44 100644 --- a/big_tests/tests/domain_helper.erl +++ b/big_tests/tests/domain_helper.erl @@ -4,10 +4,11 @@ delete_configured_domains/0, insert_domain/3, delete_domain/2, + make_metrics_prefix/1, host_type/1, host_type/2]). --import(distributed_helper, [get_or_fail/1, rpc/4]). +-import(distributed_helper, [get_or_fail/1, rpc/4, mim/0]). host_type(NodeKey) -> host_type(NodeKey, domain). @@ -18,6 +19,9 @@ host_type(NodeKey, DomainKey) -> {ok, HostType} = rpc(Node, mongoose_domain_core, get_host_type, [Domain]), HostType. +make_metrics_prefix(HostType) -> + rpc(mim(), mongoose_metrics, make_host_type_name, [HostType]). + insert_configured_domains() -> for_each_configured_domain(fun insert_domain/3). diff --git a/big_tests/tests/mod_ping_SUITE.erl b/big_tests/tests/mod_ping_SUITE.erl index ac2e1db901..13975bfe71 100644 --- a/big_tests/tests/mod_ping_SUITE.erl +++ b/big_tests/tests/mod_ping_SUITE.erl @@ -54,10 +54,10 @@ suite() -> escalus:suite(). ping_interval() -> - 3. + timer:seconds(3). ping_req_timeout() -> - 2. + timer:seconds(2). init_per_suite(Config) -> mongoose_helper:inject_module(?MODULE), @@ -83,8 +83,8 @@ init_per_group(server_ping_kill, Config) -> [{timeout_action, kill} | Config]. end_per_group(_GroupName, Config) -> - Domain = ct:get_config({hosts, mim, domain}), - dynamic_modules:stop(Domain, mod_ping), + HostType = domain_helper:host_type(mim), + dynamic_modules:stop(HostType, mod_ping), Config. init_per_testcase(server_ping_pong = CN, Config) -> @@ -100,39 +100,41 @@ end_per_testcase(CaseName, Config) -> escalus:end_per_testcase(CaseName, Config). start_mod_ping(Opts) -> - Domain = ct:get_config({hosts, mim, domain}), - dynamic_modules:start(Domain, mod_ping, Opts). + HostType = domain_helper:host_type(mim), + dynamic_modules:start(HostType, mod_ping, Opts). setup_pong_hook(Config) -> Pid = self(), - Host = escalus_users:get_host(Config, alice), - Handler = mongoose_helper:successful_rpc(?MODULE, setup_pong_hook, [Host, Pid]), + HostType = domain_helper:host_type(mim), + Handler = mongoose_helper:successful_rpc(?MODULE, setup_pong_hook, [HostType, Pid]), [{pong_handler, Handler} | Config]. -setup_pong_hook(Host, Pid) -> - Handler = fun(_Acc, JID, _Response, _TDelta) -> +setup_pong_hook(HostType, Pid) -> + Handler = fun(_Acc, _HostType, JID, _Response, _TDelta) -> Pid ! {pong, jid:to_binary(jid:to_lower(JID))} end, - ejabberd_hooks:add(user_ping_response, Host, Handler, 50), + ejabberd_hooks:add(user_ping_response, HostType, Handler, 50), Handler. clear_pong_hook(Config) -> {value, {_, Handler}, NConfig} = lists:keytake(pong_handler, 1, Config), - Host = escalus_users:get_host(Config, alice), - mongoose_helper:successful_rpc(?MODULE, clear_pong_hook, [Host, Handler]), + HostType = domain_helper:host_type(mim), + mongoose_helper:successful_rpc(?MODULE, clear_pong_hook, [HostType, Handler]), NConfig. -clear_pong_hook(Host, Handler) -> - ejabberd_hooks:delete(user_ping_response, Host, Handler, 50). +clear_pong_hook(HostType, Handler) -> + ejabberd_hooks:delete(user_ping_response, HostType, Handler, 50). %%-------------------------------------------------------------------- %% Ping tests %%-------------------------------------------------------------------- ping(ConfigIn) -> Domain = ct:get_config({hosts, mim, domain}), + HostType = domain_helper:host_type(mim), + HostTypePrefix = domain_helper:make_metrics_prefix(HostType), Metrics = [ - {[Domain, mod_ping, ping_response],0}, - {[Domain, mod_ping, ping_response_timeout],0} + {[HostTypePrefix, mod_ping, ping_response],0}, + {[HostTypePrefix, mod_ping, ping_response_timeout],0} ], Config = [{mongoose_metrics, Metrics} | ConfigIn], escalus:fresh_story(Config, [{alice, 1}], @@ -146,23 +148,25 @@ ping(ConfigIn) -> wrong_ping(Config) -> escalus:fresh_story(Config, [{alice, 1}], - fun(Alice) -> - Domain = ct:get_config({hosts, mim, domain}), - IQ = escalus_stanza:iq(<<"get">>, [#xmlel{name = <<"unsupported">>, - attrs = [{<<"xmlns">>, ?NS_PING}] - }]), - PingReq = escalus_stanza:to(IQ, Domain), - escalus_client:send(Alice, PingReq), - - PingResp = escalus_client:wait_for_stanza(Alice), - escalus:assert(is_iq_error, [PingReq], PingResp) - end). + fun(Alice) -> + Domain = ct:get_config({hosts, mim, domain}), + IQ = escalus_stanza:iq(<<"get">>, [#xmlel{name = <<"unsupported">>, + attrs = [{<<"xmlns">>, ?NS_PING}] + }]), + PingReq = escalus_stanza:to(IQ, Domain), + escalus_client:send(Alice, PingReq), + + PingResp = escalus_client:wait_for_stanza(Alice), + escalus:assert(is_iq_error, [PingReq], PingResp) + end). active(ConfigIn) -> Domain = ct:get_config({hosts, mim, domain}), + HostType = domain_helper:host_type(mim), + HostTypePrefix = domain_helper:make_metrics_prefix(HostType), Metrics = [ - {[Domain, mod_ping, ping_response],0}, - {[Domain, mod_ping, ping_response_timeout],0} + {[HostTypePrefix, mod_ping, ping_response],0}, + {[HostTypePrefix, mod_ping, ping_response_timeout],0} ], Config = [{mongoose_metrics, Metrics} | ConfigIn], escalus:fresh_story(Config, [{alice, 1}], @@ -177,10 +181,11 @@ active(ConfigIn) -> end). active_keep_alive(ConfigIn) -> - Domain = ct:get_config({hosts, mim, domain}), + HostType = domain_helper:host_type(mim), + HostTypePrefix = domain_helper:make_metrics_prefix(HostType), Metrics = [ - {[Domain, mod_ping, ping_response],0}, - {[Domain, mod_ping, ping_response_timeout],0} + {[HostTypePrefix, mod_ping, ping_response],0}, + {[HostTypePrefix, mod_ping, ping_response_timeout],0} ], Config = [{mongoose_metrics, Metrics} | ConfigIn], escalus:fresh_story(Config, [{alice, 1}], @@ -193,11 +198,12 @@ active_keep_alive(ConfigIn) -> end). server_ping_pong(ConfigIn) -> - Domain = ct:get_config({hosts, mim, domain}), + HostType = domain_helper:host_type(mim), + HostTypePrefix = domain_helper:make_metrics_prefix(HostType), Metrics = [ - {[Domain, mod_ping, ping_response], 5}, - {[Domain, mod_ping, ping_response_timeout], 0}, - {[Domain, mod_ping, ping_response_time], changed} + {[HostTypePrefix, mod_ping, ping_response], 5}, + {[HostTypePrefix, mod_ping, ping_response_timeout], 0}, + {[HostTypePrefix, mod_ping, ping_response_time], changed} ], Config = [{mongoose_metrics, Metrics} | ConfigIn], %% We use 5 Alices because with just 1 sample the histogram may look like it hasn't changed @@ -213,24 +219,25 @@ server_ping_pong(ConfigIn) -> end). server_ping_pang(ConfigIn) -> - Domain = ct:get_config({hosts, mim, domain}), + HostType = domain_helper:host_type(mim), + HostTypePrefix = domain_helper:make_metrics_prefix(HostType), Metrics = [ - {[Domain, mod_ping, ping_response], 0}, - {[Domain, mod_ping, ping_response_timeout], 1} + {[HostTypePrefix, mod_ping, ping_response], 0}, + {[HostTypePrefix, mod_ping, ping_response_timeout], 1} ], Config = [{mongoose_metrics, Metrics} | ConfigIn], escalus:fresh_story(Config, [{alice, 1}], fun(Alice) -> wait_for_ping_req(Alice), %% do not resp to ping req - ct:sleep(timer:seconds(ping_req_timeout() + 0.5)), + ct:sleep(ping_req_timeout() + timer:seconds(1)/2), TimeoutAction = ?config(timeout_action, Config), check_connection(TimeoutAction, Alice), escalus_client:kill_connection(Config, Alice) end). wait_ping_interval(Ration) -> - WaitTime = timer:seconds(ping_interval()) * Ration, + WaitTime = ping_interval() * Ration, ct:sleep(WaitTime). check_connection(kill, Client) -> diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index 96d0041a98..779df85b5f 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -893,11 +893,10 @@ process_outgoing_stanza(Acc, StateData) -> process_outgoing_stanza(Acc, ToJID, <<"presence">>, StateData) -> #jid{user = User, server = Server} = FromJID = mongoose_acc:from_jid(Acc), - %% TODO: recheck that Acc has sender's host type here. HostType = mongoose_acc:host_type(Acc), Res = mongoose_hooks:c2s_update_presence(HostType, Acc), El = mongoose_acc:element(Res), - Res1 = mongoose_hooks:user_send_packet(HostType, Res, FromJID, ToJID, El), + Res1 = mongoose_hooks:user_send_packet(Res, FromJID, ToJID, El), {_Acc1, NState} = case ToJID of #jid{user = User, server = Server, @@ -910,7 +909,6 @@ process_outgoing_stanza(Acc, ToJID, <<"presence">>, StateData) -> process_outgoing_stanza(Acc0, ToJID, <<"iq">>, StateData) -> {XMLNS, Acc} = mongoose_iq:xmlns(Acc0), FromJID = mongoose_acc:from_jid(Acc), - HostType = mongoose_acc:host_type(Acc), El = mongoose_acc:element(Acc), {_Acc, NState} = case XMLNS of ?NS_PRIVACY -> @@ -918,17 +916,16 @@ process_outgoing_stanza(Acc0, ToJID, <<"iq">>, StateData) -> ?NS_BLOCKING -> process_privacy_iq(Acc, ToJID, StateData); _ -> - Acc2 = mongoose_hooks:user_send_packet(HostType, Acc, - FromJID, ToJID, El), + Acc2 = mongoose_hooks:user_send_packet(Acc, FromJID, + ToJID, El), Acc3 = check_privacy_and_route(Acc2, StateData), {Acc3, StateData} end, NState; process_outgoing_stanza(Acc, ToJID, <<"message">>, StateData) -> FromJID = mongoose_acc:from_jid(Acc), - HostType = mongoose_acc:host_type(Acc), El = mongoose_acc:element(Acc), - Acc1 = mongoose_hooks:user_send_packet(HostType, Acc, FromJID, ToJID, El), + Acc1 = mongoose_hooks:user_send_packet(Acc, FromJID, ToJID, El), _Acc2 = check_privacy_and_route(Acc1, StateData), StateData; process_outgoing_stanza(_Acc, _ToJID, _Name, StateData) -> diff --git a/src/metrics/mongoose_metrics.erl b/src/metrics/mongoose_metrics.erl index a264be18d2..74c68bfd11 100644 --- a/src/metrics/mongoose_metrics.erl +++ b/src/metrics/mongoose_metrics.erl @@ -23,6 +23,7 @@ create_global_metrics/0, init_predefined_host_type_metrics/1, init_subscriptions/0, + make_host_type_name/1, create_generic_hook_metric/2, ensure_db_pool_metric/1, update/3, diff --git a/src/mod_commands.erl b/src/mod_commands.erl index 568c9d061a..8588d2c61d 100644 --- a/src/mod_commands.erl +++ b/src/mod_commands.erl @@ -313,13 +313,11 @@ send_stanza(BinStanza) -> do_send_packet(From, To, Packet) -> case mongoose_domain_api:get_domain_host_type(From#jid.lserver) of {ok, HostType} -> - Acc0 = mongoose_acc:new(#{location => ?LOCATION, + Acc = mongoose_acc:new(#{location => ?LOCATION, host_type => HostType, lserver => From#jid.lserver, element => Packet}), - mongoose_hooks:user_send_packet(From#jid.lserver, - Acc0, - From, To, Packet), + mongoose_hooks:user_send_packet(Acc, From, To, Packet), ejabberd_router:route(From, To, Packet), ok; {error, not_found} -> diff --git a/src/mod_ping.erl b/src/mod_ping.erl index df55a1bc1d..330272b892 100644 --- a/src/mod_ping.erl +++ b/src/mod_ping.erl @@ -15,18 +15,21 @@ -include("mongoose_config_spec.hrl"). -define(DEFAULT_SEND_PINGS, false). % bool() --define(DEFAULT_PING_INTERVAL, 60). % seconds --define(DEFAULT_PING_REQ_TIMEOUT, 32). +-define(DEFAULT_PING_INTERVAL, (60*1000)). % 60 seconds +-define(DEFAULT_PING_REQ_TIMEOUT, (32*1000)).% 32 seconds %% gen_mod callbacks --export([start/2, stop/1, config_spec/0]). +-export([start/2, + stop/1, + config_spec/0, + supported_features/0]). %% Hook callbacks --export([iq_ping/4, +-export([iq_ping/5, user_online/5, user_offline/5, user_send/4, - user_ping_response/4, + user_ping_response/5, user_keep_alive/2]). %% Remote hook callback @@ -36,9 +39,9 @@ %% Info Handler %%==================================================================== -route_ping_iq(JID, Server) -> - PingReqTimeout = timer:seconds(gen_mod:get_module_opt(Server, ?MODULE, ping_req_timeout, - ?DEFAULT_PING_REQ_TIMEOUT)), +route_ping_iq(JID, Server, HostType) -> + PingReqTimeout = gen_mod:get_module_opt(HostType, ?MODULE, ping_req_timeout, + ?DEFAULT_PING_REQ_TIMEOUT), IQ = #iq{type = get, sub_el = [#xmlel{name = <<"ping">>, attrs = [{<<"xmlns">>, ?NS_PING}]}]}, @@ -46,19 +49,20 @@ route_ping_iq(JID, Server) -> T0 = erlang:monotonic_time(millisecond), F = fun(_From, _To, Acc, timeout) -> ejabberd_c2s:run_remote_hook(Pid, mod_ping, timeout), - NewAcc = mongoose_hooks:user_ping_response(Server, + NewAcc = mongoose_hooks:user_ping_response(HostType, Acc, JID, timeout, 0), NewAcc; (_From, _To, Acc, Response) -> % received a pong from client TDelta = erlang:monotonic_time(millisecond) - T0, - NewAcc = mongoose_hooks:user_ping_response(Server, + NewAcc = mongoose_hooks:user_ping_response(HostType, Acc, JID, Response, TDelta), NewAcc end, From = jid:make_noprep(<<"">>, Server, <<"">>), Acc = mongoose_acc:new(#{ location => ?LOCATION, lserver => Server, + host_type => HostType, from_jid => From, to_jid => JID, element => jlib:iq_to_xml(IQ) }), @@ -68,69 +72,76 @@ route_ping_iq(JID, Server) -> %% utility %%==================================================================== -hooks(Host) -> - [{sm_register_connection_hook, Host, ?MODULE, user_online, 100}, - {sm_remove_connection_hook, Host, ?MODULE, user_offline, 100}, - {user_send_packet, Host, ?MODULE, user_send, 100}, - {user_sent_keep_alive, Host, ?MODULE, user_keep_alive, 100}, - {user_ping_response, Host, ?MODULE, user_ping_response, 100}, - {c2s_remote_hook, Host, ?MODULE, handle_remote_hook, 100}]. +hooks(HostType) -> + [{sm_register_connection_hook, HostType, ?MODULE, user_online, 100}, + {sm_remove_connection_hook, HostType, ?MODULE, user_offline, 100}, + {user_send_packet, HostType, ?MODULE, user_send, 100}, + {user_sent_keep_alive, HostType, ?MODULE, user_keep_alive, 100}, + {user_ping_response, HostType, ?MODULE, user_ping_response, 100}, + {c2s_remote_hook, HostType, ?MODULE, handle_remote_hook, 100}]. -ensure_metrics(Host) -> - mongoose_metrics:ensure_metric(Host, [mod_ping, ping_response], spiral), - mongoose_metrics:ensure_metric(Host, [mod_ping, ping_response_timeout], spiral), - mongoose_metrics:ensure_metric(Host, [mod_ping, ping_response_time], histogram). +ensure_metrics(HostType) -> + mongoose_metrics:ensure_metric(HostType, [mod_ping, ping_response], spiral), + mongoose_metrics:ensure_metric(HostType, [mod_ping, ping_response_timeout], spiral), + mongoose_metrics:ensure_metric(HostType, [mod_ping, ping_response_time], histogram). %%==================================================================== %% gen_mod callbacks %%==================================================================== -start(Host, Opts) -> - ensure_metrics(Host), +start(HostType, Opts) -> + ensure_metrics(HostType), SendPings = gen_mod:get_opt(send_pings, Opts, ?DEFAULT_SEND_PINGS), IQDisc = gen_mod:get_opt(iqdisc, Opts, no_queue), - mod_disco:register_feature(Host, ?NS_PING), - gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_PING, - ?MODULE, iq_ping, IQDisc), - gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_PING, - ?MODULE, iq_ping, IQDisc), - maybe_add_hooks_handlers(Host, SendPings). + %% TODO: update code related to mod_disco + mod_disco:register_feature(HostType, ?NS_PING), + gen_iq_handler:add_iq_handler_for_domain(HostType, ?NS_PING, ejabberd_sm, + fun ?MODULE:iq_ping/5, #{}, IQDisc), + gen_iq_handler:add_iq_handler_for_domain(HostType, ?NS_PING, ejabberd_local, + fun ?MODULE:iq_ping/5, #{}, IQDisc), + maybe_add_hooks_handlers(HostType, SendPings). maybe_add_hooks_handlers(Host, true) -> ejabberd_hooks:add(hooks(Host)); maybe_add_hooks_handlers(_, _) -> ok. -stop(Host) -> +stop(HostType) -> %% a word of warning: timers are installed in c2s processes, so stopping mod_ping %% won't stop currently running timers. They'll run one more time, and then stop. - ejabberd_hooks:delete(hooks(Host)), - gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_PING), - gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_PING), - mod_disco:unregister_feature(Host, ?NS_PING). + ejabberd_hooks:delete(hooks(HostType)), + gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_PING, ejabberd_local), + gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_PING, ejabberd_sm), + %% TODO: update code related to mod_disco + mod_disco:unregister_feature(HostType, ?NS_PING). -spec config_spec() -> mongoose_config_spec:config_section(). config_spec() -> #section{ items = #{<<"send_pings">> => #option{type = boolean}, <<"ping_interval">> => #option{type = integer, - validate = positive}, + validate = positive, + process = fun timer:seconds/1}, <<"timeout_action">> => #option{type = atom, validate = {enum, [none, kill]}}, <<"ping_req_timeout">> => #option{type = integer, - validate = positive}, + validate = positive, + process = fun timer:seconds/1}, <<"iqdisc">> => mongoose_config_spec:iqdisc() } }. +supported_features() -> [dynamic_domains]. + %%==================================================================== %% IQ handlers %%==================================================================== -iq_ping(_From, _To, Acc, #iq{type = get, sub_el = #xmlel{name = <<"ping">>}} = IQ) -> +iq_ping(Acc, _From, _To, #iq{type = get, sub_el = #xmlel{name = <<"ping">>}} = IQ, _) -> {Acc, IQ#iq{type = result, sub_el = []}}; -iq_ping(_From, _To, Acc, #iq{sub_el = SubEl} = IQ) -> - {Acc, IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:feature_not_implemented()]}}. +iq_ping(Acc, _From, _To, #iq{sub_el = SubEl} = IQ, _) -> + NewSubEl = [SubEl, mongoose_xmpp_errors:feature_not_implemented()], + {Acc, IQ#iq{type = error, sub_el = NewSubEl}}. %%==================================================================== %% Hook callbacks @@ -140,6 +151,7 @@ handle_remote_hook(HandlerState, mod_ping, Args, C2SState) -> handle_remote_call(Args, ejabberd_c2s_state:jid(C2SState), ejabberd_c2s_state:server(C2SState), + ejabberd_c2s_state:host_type(C2SState), HandlerState); handle_remote_hook(HandlerState, _, _, _) -> HandlerState. @@ -161,42 +173,44 @@ user_keep_alive(Acc, _JID) -> Acc. -spec user_ping_response(Acc :: mongoose_acc:t(), + HostType :: mongooseim:host_type(), JID :: jid:jid(), Response :: timeout | jlib:iq(), TDelta :: pos_integer()) -> mongoose_acc:t(). -user_ping_response(Acc, #jid{server = Server}, timeout, _TDelta) -> - mongoose_metrics:update(Server, [mod_ping, ping_response_timeout], 1), +user_ping_response(Acc, HostType, _JID, timeout, _TDelta) -> + mongoose_metrics:update(HostType, [mod_ping, ping_response_timeout], 1), Acc; -user_ping_response(Acc, #jid{server = Server}, _Response, TDelta) -> - mongoose_metrics:update(Server, [mod_ping, ping_response_time], TDelta), - mongoose_metrics:update(Server, [mod_ping, ping_response], 1), +user_ping_response(Acc, HostType, _JID, _Response, TDelta) -> + mongoose_metrics:update(HostType, [mod_ping, ping_response_time], TDelta), + mongoose_metrics:update(HostType, [mod_ping, ping_response], 1), Acc. %%==================================================================== %% Implementation %%==================================================================== -handle_remote_call(init, _JID, Server, HandlerState) -> - start_ping_timer(HandlerState, Server); -handle_remote_call(send_ping, JID, Server, HandlerState) -> - route_ping_iq(JID, Server), +handle_remote_call(init, _JID, _Server, HostType, HandlerState) -> + start_ping_timer(HandlerState, HostType); +handle_remote_call(send_ping, JID, Server, HostType, HandlerState) -> + route_ping_iq(JID, Server, HostType), start_ping_timer(HandlerState, Server); -handle_remote_call(timeout, JID, Server, HandlerState) -> - mongoose_hooks:user_ping_timeout(Server, JID), - case gen_mod:get_module_opt(Server, ?MODULE, timeout_action, none) of +handle_remote_call(timeout, JID, _Server, HostType, HandlerState) -> + mongoose_hooks:user_ping_timeout(HostType, JID), + case gen_mod:get_module_opt(HostType, ?MODULE, timeout_action, none) of kill -> ejabberd_c2s:stop(self()); _ -> ok end, HandlerState; -handle_remote_call(remove_timer, _JID, _Server, HandlerState) -> +handle_remote_call(remove_timer, _JID, _Server, _HostType, HandlerState) -> cancel_timer(HandlerState), empty_state. -spec start_ping_timer(term(), jid:server()) -> reference(). -start_ping_timer(HandlerState, Server) -> +start_ping_timer(HandlerState, HostType) -> cancel_timer(HandlerState), - PingInterval = gen_mod:get_module_opt(Server, ?MODULE, ping_interval, ?DEFAULT_PING_INTERVAL), - ejabberd_c2s:run_remote_hook_after(timer:seconds(PingInterval), self(), mod_ping, send_ping). + PingInterval = gen_mod:get_module_opt(HostType, ?MODULE, ping_interval, + ?DEFAULT_PING_INTERVAL), + ejabberd_c2s:run_remote_hook_after(PingInterval, self(), mod_ping, send_ping). cancel_timer(empty_state) -> do_nothing; diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 5650df69ea..604b8ecb80 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -40,7 +40,7 @@ user_ping_timeout/2, user_receive_packet/6, user_sent_keep_alive/2, - user_send_packet/5, + user_send_packet/4, vcard_set/3, xmpp_send_element/3, xmpp_stanza_dropped/4]). @@ -452,25 +452,25 @@ user_available_hook(HostType, Acc, JID) -> ejabberd_hooks:run_for_host_type(user_available_hook, HostType, Acc, [JID]). %%% @doc The `user_ping_response' hook is called when a user responds to a ping. --spec user_ping_response(Server, Acc, JID, Response, TDelta) -> Result when - Server :: jid:server(), +-spec user_ping_response(HostType, Acc, JID, Response, TDelta) -> Result when + HostType :: mongooseim:host_type(), Acc :: mongoose_acc:t(), JID :: jid:jid(), Response :: timeout | jlib:iq(), TDelta :: non_neg_integer(), Result :: mongoose_acc:t(). -user_ping_response(Server, Acc, JID, Response, TDelta) -> - ejabberd_hooks:run_for_host_type(user_ping_response, Server, Acc, - [JID, Response, TDelta]). +user_ping_response(HostType, Acc, JID, Response, TDelta) -> + ejabberd_hooks:run_for_host_type(user_ping_response, HostType, Acc, + [HostType, JID, Response, TDelta]). %%% @doc The `user_ping_timeout' hook is called when there is a timeout %%% when waiting for a ping response from a user. --spec user_ping_timeout(Server, JID) -> Result when - Server :: jid:server(), +-spec user_ping_timeout(HostType, JID) -> Result when + HostType :: mongooseim:host_type(), JID :: jid:jid(), Result :: any(). -user_ping_timeout(Server, JID) -> - ejabberd_hooks:run_for_host_type(user_ping_timeout, Server, ok, [JID]). +user_ping_timeout(HostType, JID) -> + ejabberd_hooks:run_for_host_type(user_ping_timeout, HostType, ok, [JID]). -spec user_receive_packet(HostType, Acc, JID, From, To, El) -> Result when HostType :: binary(), @@ -495,15 +495,14 @@ user_sent_keep_alive(HostType, JID) -> %%% The hook's handler is expected to accept four parameters: %%% `Acc', `From', `To' and `Packet' %%% The arguments and the return value types correspond to the following spec. --spec user_send_packet(HostType, Acc, From, To, Packet) -> Result when - HostType :: binary(), +-spec user_send_packet(Acc, From, To, Packet) -> Result when Acc :: mongoose_acc:t(), From :: jid:jid(), To :: jid:jid(), Packet :: exml:element(), Result :: mongoose_acc:t(). -user_send_packet(HostType, Acc, From, To, Packet) -> - %% TODO: ejabberd_c2s calls this hook with host type, fix other places. +user_send_packet(Acc, From, To, Packet) -> + HostType = mongoose_acc:host_type(Acc), ejabberd_hooks:run_for_host_type(user_send_packet, HostType, Acc, [From, To, Packet]). diff --git a/test/config_parser_SUITE.erl b/test/config_parser_SUITE.erl index e22aba6691..b031988093 100644 --- a/test/config_parser_SUITE.erl +++ b/test/config_parser_SUITE.erl @@ -2415,11 +2415,11 @@ mod_ping(_Config) -> M = fun(Cfg) -> modopts(mod_ping, Cfg) end, ?eqf(M([{send_pings, true}]), T(#{<<"send_pings">> => true})), - ?eqf(M([{ping_interval, 10}]), + ?eqf(M([{ping_interval, timer:seconds(10)}]), T(#{<<"ping_interval">> => 10})), ?eqf(M([{timeout_action, kill}]), T(#{<<"timeout_action">> => <<"kill">>})), - ?eqf(M([{ping_req_timeout, 20}]), + ?eqf(M([{ping_req_timeout, timer:seconds(20)}]), T(#{<<"ping_req_timeout">> => 20})), ?errf(T(#{<<"send_pings">> => 1})), ?errf(T(#{<<"ping_interval">> => 0})), diff --git a/test/config_parser_SUITE_data/modules.options b/test/config_parser_SUITE_data/modules.options index ccd0bf4a92..f7508013d9 100644 --- a/test/config_parser_SUITE_data/modules.options +++ b/test/config_parser_SUITE_data/modules.options @@ -60,8 +60,8 @@ {host,{fqdn,<<"muc.example.com">>}}, {http_auth_pool,my_auth_pool}]}, {mod_ping, - [{ping_interval,60}, - {ping_req_timeout,32}, + [{ping_interval,60000}, + {ping_req_timeout,32000}, {send_pings,true}, {timeout_action,none}]}, {mod_mam, @@ -324,8 +324,8 @@ {host,{fqdn,<<"muc.example.com">>}}, {http_auth_pool,my_auth_pool}]}, {mod_ping, - [{ping_interval,60}, - {ping_req_timeout,32}, + [{ping_interval,60000}, + {ping_req_timeout,32000}, {send_pings,true}, {timeout_action,none}]}, {mod_mam,