2002-11-27 21:46:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_router.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% 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,
|
|
|
|
register_local_route/1,
|
|
|
|
unregister_route/1,
|
|
|
|
unregister_local_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
|
|
|
|
|
|
|
-record(route, {domain, node, pid}).
|
2002-11-30 19:46:16 +01:00
|
|
|
-record(local_route, {domain, pid}).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
|
|
|
{ok, proc_lib:spawn_link(ejabberd_router, init, [])}.
|
2002-11-27 21:46:29 +01:00
|
|
|
|
|
|
|
init() ->
|
|
|
|
register(ejabberd_router, self()),
|
2002-11-29 21:55:12 +01:00
|
|
|
mnesia:create_table(route,
|
2002-11-27 21:46:29 +01:00
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{attributes,
|
2002-11-29 21:55:12 +01:00
|
|
|
record_info(fields, route)}]),
|
2002-11-30 19:46:16 +01:00
|
|
|
mnesia:create_table(local_route,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{local_content, true},
|
|
|
|
{attributes,
|
|
|
|
record_info(fields, local_route)}]),
|
2003-01-21 21:36:55 +01:00
|
|
|
mnesia:add_table_copy(local_route, node(), ram_copies),
|
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();
|
2002-11-29 21:55:12 +01:00
|
|
|
{register_route, Domain, Pid, Node} ->
|
|
|
|
F = fun() ->
|
2003-01-28 20:45:13 +01:00
|
|
|
%case mnesia:wread({route, Domain}) of
|
|
|
|
% [] ->
|
|
|
|
% ok;
|
|
|
|
% [Old] ->
|
|
|
|
% % TODO: notify
|
|
|
|
% ok
|
|
|
|
%end,
|
2002-11-29 21:55:12 +01:00
|
|
|
mnesia:write(#route{domain = Domain,
|
|
|
|
node = Node,
|
|
|
|
pid = Pid})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
2002-11-27 21:46:29 +01:00
|
|
|
loop();
|
2002-11-30 19:46:16 +01:00
|
|
|
{register_local_route, Domain, Pid} ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(#local_route{domain = Domain,
|
|
|
|
pid = Pid})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
|
|
|
loop();
|
2002-11-29 21:55:12 +01:00
|
|
|
{unregister_route, Domain} ->
|
|
|
|
F = fun() ->
|
2003-01-28 20:45:13 +01:00
|
|
|
%case mnesia:wread({route, Domain}) of
|
|
|
|
% [] ->
|
|
|
|
% ok;
|
|
|
|
% [Old] ->
|
|
|
|
% % TODO: notify
|
|
|
|
% ok
|
|
|
|
%end,
|
2002-11-29 21:55:12 +01:00
|
|
|
mnesia:delete({route, Domain})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
2002-11-27 21:46:29 +01:00
|
|
|
loop();
|
2003-01-07 20:10:35 +01:00
|
|
|
{unregister_local_route, Domain} ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({local_route, Domain})
|
|
|
|
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-10-07 22:31:44 +02:00
|
|
|
#jid{lserver = LDstDomain} = To,
|
2003-01-18 20:42:48 +01:00
|
|
|
case mnesia:dirty_read({local_route, LDstDomain}) of
|
2003-01-15 20:54:44 +01:00
|
|
|
[] ->
|
2003-01-18 20:42:48 +01:00
|
|
|
case mnesia:dirty_read({route, LDstDomain}) of
|
2003-01-15 20:54:44 +01:00
|
|
|
[] ->
|
|
|
|
ejabberd_s2s ! {route, From, To, Packet};
|
|
|
|
[R] ->
|
|
|
|
Node = R#route.node,
|
2003-01-16 21:24:53 +01:00
|
|
|
case node() of
|
|
|
|
Node ->
|
2003-02-16 21:07:21 +01:00
|
|
|
Pid = R#route.pid,
|
2003-01-16 21:24:53 +01:00
|
|
|
?DEBUG("routed to process ~p~n", [Pid]),
|
|
|
|
Pid ! {route, From, To, Packet};
|
|
|
|
_ ->
|
|
|
|
?DEBUG("routed to node ~p~n", [Node]),
|
|
|
|
{ejabberd_router, Node} ! {route, From, To, Packet}
|
|
|
|
end
|
2002-11-30 19:46:16 +01:00
|
|
|
end;
|
2003-01-15 20:54:44 +01:00
|
|
|
[R] ->
|
|
|
|
Pid = R#local_route.pid,
|
|
|
|
?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) ->
|
|
|
|
ejabberd_router ! {register_route, Domain, self(), node()}.
|
|
|
|
|
|
|
|
register_local_route(Domain) ->
|
|
|
|
ejabberd_router ! {register_local_route, Domain, self()}.
|
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
unregister_route(Domain) ->
|
|
|
|
ejabberd_router ! {unregister_route, Domain}.
|
|
|
|
|
|
|
|
unregister_local_route(Domain) ->
|
|
|
|
ejabberd_router ! {unregister_local_route, Domain}.
|
|
|
|
|
2003-01-02 22:01:12 +01:00
|
|
|
|
|
|
|
dirty_get_all_routes() ->
|
|
|
|
lists:delete(?MYNAME,
|
|
|
|
lists:umerge(lists:sort(mnesia:dirty_all_keys(route)),
|
|
|
|
lists:sort(mnesia:dirty_all_keys(local_route)))).
|
|
|
|
|
2003-01-04 21:09:25 +01:00
|
|
|
dirty_get_all_domains() ->
|
|
|
|
lists:umerge(lists:sort(mnesia:dirty_all_keys(route)),
|
|
|
|
lists:sort(mnesia:dirty_all_keys(local_route))).
|
|
|
|
|