2002-11-27 21:46:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_router.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
2003-12-14 21:51:01 +01:00
|
|
|
%%% Purpose : Main router
|
2002-11-27 21:46:29 +01:00
|
|
|
%%% Created : 27 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_router).
|
|
|
|
-author('alexey@sevcom.net').
|
2002-11-29 21:55:12 +01:00
|
|
|
-vsn('$Revision$ ').
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
-export([route/3,
|
|
|
|
register_route/1,
|
|
|
|
unregister_route/1,
|
2003-01-04 21:09:25 +01:00
|
|
|
dirty_get_all_routes/0,
|
|
|
|
dirty_get_all_domains/0
|
|
|
|
]).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
-export([start_link/0, init/0]).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2002-11-29 21:55:12 +01:00
|
|
|
-include("ejabberd.hrl").
|
2003-10-07 22:31:44 +02:00
|
|
|
-include("jlib.hrl").
|
2002-11-29 21:55:12 +01:00
|
|
|
|
2003-12-14 21:51:01 +01:00
|
|
|
-record(route, {domain, pid}).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
2003-12-14 21:51:01 +01:00
|
|
|
Pid = proc_lib:spawn_link(ejabberd_router, init, []),
|
|
|
|
register(ejabberd_router, Pid),
|
|
|
|
{ok, Pid}.
|
2002-11-27 21:46:29 +01:00
|
|
|
|
|
|
|
init() ->
|
2003-12-14 21:51:01 +01:00
|
|
|
update_tables(),
|
2002-11-29 21:55:12 +01:00
|
|
|
mnesia:create_table(route,
|
2002-11-27 21:46:29 +01:00
|
|
|
[{ram_copies, [node()]},
|
2003-12-14 21:51:01 +01:00
|
|
|
{type, bag},
|
2002-11-27 21:46:29 +01:00
|
|
|
{attributes,
|
2002-11-29 21:55:12 +01:00
|
|
|
record_info(fields, route)}]),
|
2003-12-14 21:51:01 +01:00
|
|
|
mnesia:add_table_copy(route, node(), ram_copies),
|
2004-01-06 21:37:14 +01:00
|
|
|
mnesia:subscribe({table, route, simple}),
|
2002-11-27 21:46:29 +01:00
|
|
|
loop().
|
|
|
|
|
|
|
|
loop() ->
|
|
|
|
receive
|
|
|
|
{route, From, To, Packet} ->
|
2003-01-15 20:54:44 +01:00
|
|
|
case catch do_route(From, To, Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
2003-10-29 21:09:09 +01:00
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
2003-01-15 20:54:44 +01:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2002-11-27 21:46:29 +01:00
|
|
|
loop();
|
2003-12-14 21:51:01 +01:00
|
|
|
{register_route, Domain, Pid} ->
|
2002-11-29 21:55:12 +01:00
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(#route{domain = Domain,
|
|
|
|
pid = Pid})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
2002-11-27 21:46:29 +01:00
|
|
|
loop();
|
2003-12-14 21:51:01 +01:00
|
|
|
{unregister_route, Domain, Pid} ->
|
2003-01-07 20:10:35 +01:00
|
|
|
F = fun() ->
|
2003-12-14 21:51:01 +01:00
|
|
|
mnesia:delete_object(#route{domain = Domain,
|
|
|
|
pid = Pid})
|
2003-01-07 20:10:35 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
|
|
|
loop();
|
2004-01-06 21:37:14 +01:00
|
|
|
{mnesia_table_event, {write, #route{pid = Pid}, _ActivityId}} ->
|
|
|
|
erlang:monitor(process, Pid),
|
|
|
|
loop();
|
|
|
|
{'DOWN', _Ref, _Type, Pid, _Info} ->
|
|
|
|
F = fun() ->
|
|
|
|
Es = mnesia:select(
|
|
|
|
route,
|
|
|
|
[{#route{pid = Pid, _ = '_'},
|
|
|
|
[],
|
|
|
|
['$_']}]),
|
|
|
|
lists:foreach(fun(E) ->
|
|
|
|
mnesia:delete_object(E)
|
|
|
|
end, Es)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
|
|
|
loop();
|
2002-11-27 21:46:29 +01:00
|
|
|
_ ->
|
|
|
|
loop()
|
|
|
|
end.
|
2002-11-29 21:55:12 +01:00
|
|
|
|
|
|
|
do_route(From, To, Packet) ->
|
|
|
|
?DEBUG("route~n\tfrom ~p~n\tto ~p~n\tpacket ~p~n", [From, To, Packet]),
|
2003-12-14 21:51:01 +01:00
|
|
|
LDstDomain = To#jid.lserver,
|
|
|
|
case mnesia:dirty_read(route, LDstDomain) of
|
2003-01-15 20:54:44 +01:00
|
|
|
[] ->
|
2003-12-14 21:51:01 +01:00
|
|
|
ejabberd_s2s ! {route, From, To, Packet};
|
2003-01-15 20:54:44 +01:00
|
|
|
[R] ->
|
2003-12-14 21:51:01 +01:00
|
|
|
Pid = R#route.pid,
|
|
|
|
?DEBUG("routed to process ~p~n", [Pid]),
|
|
|
|
Pid ! {route, From, To, Packet};
|
|
|
|
Rs ->
|
2004-01-03 18:25:21 +01:00
|
|
|
Rs1 = case [R || R <- Rs, node(R#route.pid) == node()] of
|
|
|
|
[] -> Rs;
|
|
|
|
LRs -> LRs
|
|
|
|
end,
|
|
|
|
R = lists:nth(erlang:phash(now(), length(Rs1)), Rs1),
|
2003-12-14 21:51:01 +01:00
|
|
|
Pid = R#route.pid,
|
2003-01-15 20:54:44 +01:00
|
|
|
?DEBUG("routed to process ~p~n", [Pid]),
|
|
|
|
Pid ! {route, From, To, Packet}
|
2002-11-29 21:55:12 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
route(From, To, Packet) ->
|
|
|
|
ejabberd_router ! {route, From, To, Packet}.
|
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
register_route(Domain) ->
|
2003-12-14 21:51:01 +01:00
|
|
|
ejabberd_router ! {register_route, Domain, self()}.
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
unregister_route(Domain) ->
|
2003-12-14 21:51:01 +01:00
|
|
|
ejabberd_router ! {unregister_route, Domain, self()}.
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2003-01-02 22:01:12 +01:00
|
|
|
|
|
|
|
dirty_get_all_routes() ->
|
2003-12-14 21:51:01 +01:00
|
|
|
lists:delete(?MYNAME, lists:usort(mnesia:dirty_all_keys(route))).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2003-01-04 21:09:25 +01:00
|
|
|
dirty_get_all_domains() ->
|
2003-12-14 21:51:01 +01:00
|
|
|
lists:usort(mnesia:dirty_all_keys(route)).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
update_tables() ->
|
|
|
|
case catch mnesia:table_info(route, attributes) of
|
|
|
|
[domain, node, pid] ->
|
|
|
|
mnesia:delete_table(route);
|
|
|
|
[domain, pid] ->
|
|
|
|
ok;
|
|
|
|
{'EXIT', _} ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
case lists:member(local_route, mnesia:system_info(tables)) of
|
|
|
|
true ->
|
|
|
|
mnesia:delete_table(local_route);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
2003-01-04 21:09:25 +01:00
|
|
|
|