aboutsummaryrefslogtreecommitdiff
path: root/src/ejabberd_router_multicast.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/ejabberd_router_multicast.erl')
-rw-r--r--src/ejabberd_router_multicast.erl58
1 files changed, 30 insertions, 28 deletions
diff --git a/src/ejabberd_router_multicast.erl b/src/ejabberd_router_multicast.erl
index fa32c8ed7..5a0988448 100644
--- a/src/ejabberd_router_multicast.erl
+++ b/src/ejabberd_router_multicast.erl
@@ -5,7 +5,7 @@
%%% Created : 11 Aug 2007 by Badlop <badlop@process-one.net>
%%%
%%%
-%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
+%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
@@ -41,11 +41,11 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
--include("ejabberd.hrl").
-include("logger.hrl").
--include("jlib.hrl").
+-include("xmpp.hrl").
--record(route_multicast, {domain, pid}).
+-record(route_multicast, {domain = <<"">> :: binary() | '_',
+ pid = self() :: pid()}).
-record(state, {}).
%%====================================================================
@@ -58,9 +58,9 @@
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-
+-spec route_multicast(jid(), binary(), [jid()], stanza()) -> ok.
route_multicast(From, Domain, Destinations, Packet) ->
- case catch do_route(From, Domain, Destinations, Packet) of
+ case catch do_route(Domain, Destinations, xmpp:set_from(Packet, From)) of
{'EXIT', Reason} ->
?ERROR_MSG("~p~nwhen processing: ~p",
[Reason, {From, Domain, Destinations, Packet}]);
@@ -68,6 +68,7 @@ route_multicast(From, Domain, Destinations, Packet) ->
ok
end.
+-spec register_route(binary()) -> any().
register_route(Domain) ->
case jid:nameprep(Domain) of
error ->
@@ -81,6 +82,7 @@ register_route(Domain) ->
mnesia:transaction(F)
end.
+-spec unregister_route(binary()) -> any().
unregister_route(Domain) ->
case jid:nameprep(Domain) of
error ->
@@ -112,12 +114,11 @@ unregister_route(Domain) ->
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([]) ->
- mnesia:create_table(route_multicast,
+ ejabberd_mnesia:create(?MODULE, route_multicast,
[{ram_copies, [node()]},
{type, bag},
{attributes,
record_info(fields, route_multicast)}]),
- mnesia:add_table_copy(route_multicast, node(), ram_copies),
mnesia:subscribe({table, route_multicast, simple}),
lists:foreach(
fun(Pid) ->
@@ -135,9 +136,9 @@ init([]) ->
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
-handle_call(_Request, _From, State) ->
- Reply = ok,
- {reply, Reply, State}.
+handle_call(Request, From, State) ->
+ ?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
+ {noreply, State}.
%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
@@ -145,7 +146,8 @@ handle_call(_Request, _From, State) ->
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
-handle_cast(_Msg, State) ->
+handle_cast(Msg, State) ->
+ ?WARNING_MSG("Unexpected cast: ~p", [Msg]),
{noreply, State}.
%%--------------------------------------------------------------------
@@ -154,11 +156,11 @@ handle_cast(_Msg, State) ->
%% {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
-handle_info({route_multicast, From, Domain, Destinations, Packet}, State) ->
- case catch do_route(From, Domain, Destinations, Packet) of
+handle_info({route_multicast, Domain, Destinations, Packet}, State) ->
+ case catch do_route(Domain, Destinations, Packet) of
{'EXIT', Reason} ->
?ERROR_MSG("~p~nwhen processing: ~p",
- [Reason, {From, Domain, Destinations, Packet}]);
+ [Reason, {Domain, Destinations, Packet}]);
_ ->
ok
end,
@@ -181,7 +183,8 @@ handle_info({'DOWN', _Ref, _Type, Pid, _Info}, State) ->
end,
mnesia:transaction(F),
{noreply, State};
-handle_info(_Info, State) ->
+handle_info(Info, State) ->
+ ?WARNING_MSG("Unexpected info: ~p", [Info]),
{noreply, State}.
%%--------------------------------------------------------------------
@@ -206,26 +209,24 @@ code_change(_OldVsn, State, _Extra) ->
%%--------------------------------------------------------------------
%% From = #jid
%% Destinations = [#jid]
-do_route(From, Domain, Destinations, Packet) ->
-
- ?DEBUG("route_multicast~n\tfrom ~s~n\tdomain ~s~n\tdestinations ~p~n\tpacket ~p~n",
- [jid:to_string(From),
- Domain,
- [jid:to_string(To) || To <- Destinations],
- Packet]),
-
+-spec do_route(binary(), [jid()], stanza()) -> any().
+do_route(Domain, Destinations, Packet) ->
+ ?DEBUG("Route multicast:~n~ts~nDomain: ~ts~nDestinations: ~ts~n",
+ [xmpp:pp(Packet), Domain,
+ str:join([jid:encode(To) || To <- Destinations], <<", ">>)]),
%% Try to find an appropriate multicast service
case mnesia:dirty_read(route_multicast, Domain) of
%% If no multicast service is available in this server, send manually
- [] -> do_route_normal(From, Destinations, Packet);
+ [] -> do_route_normal(Destinations, Packet);
%% If some is available, send the packet using multicast service
Rs when is_list(Rs) ->
Pid = pick_multicast_pid(Rs),
- Pid ! {route_trusted, From, Destinations, Packet}
+ Pid ! {route_trusted, Destinations, Packet}
end.
+-spec pick_multicast_pid([#route_multicast{}]) -> pid().
pick_multicast_pid(Rs) ->
List = case [R || R <- Rs, node(R#route_multicast.pid) == node()] of
[] -> Rs;
@@ -233,5 +234,6 @@ pick_multicast_pid(Rs) ->
end,
(hd(List))#route_multicast.pid.
-do_route_normal(From, Destinations, Packet) ->
- [ejabberd_router:route(From, To, Packet) || To <- Destinations].
+-spec do_route_normal([jid()], stanza()) -> any().
+do_route_normal(Destinations, Packet) ->
+ [ejabberd_router:route(xmpp:set_to(Packet, To)) || To <- Destinations].