25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-28 16:34:13 +01:00

* src/ejabberd_local.erl: Replaced register_local_route to

register_route
* src/ejabberd_service.erl: Likewise
* src/mod_echo.erl: Likewise
* src/mod_vcard.erl: Likewise

* src/ejabberd_router.erl: Partially rewrited
* src/ejabberd_sm.erl: Likewise

SVN Revision: 185
This commit is contained in:
Alexey Shchepin 2003-12-14 20:51:01 +00:00
parent 4413036585
commit 4768cd2f26
7 changed files with 107 additions and 152 deletions

View File

@ -1,3 +1,14 @@
2003-12-14 Alexey Shchepin <alexey@sevcom.net>
* src/ejabberd_local.erl: Replaced register_local_route to
register_route
* src/ejabberd_service.erl: Likewise
* src/mod_echo.erl: Likewise
* src/mod_vcard.erl: Likewise
* src/ejabberd_router.erl: Partially rewrited
* src/ejabberd_sm.erl: Likewise
2003-12-13 Alexey Shchepin <alexey@sevcom.net> 2003-12-13 Alexey Shchepin <alexey@sevcom.net>
* src/ejabberd_s2s.erl: Partially rewrited * src/ejabberd_s2s.erl: Partially rewrited

View File

@ -29,7 +29,7 @@ start_link() ->
init() -> init() ->
MyDomain = ?MYNAME, MyDomain = ?MYNAME,
ejabberd_router:register_local_route(MyDomain), ejabberd_router:register_route(MyDomain),
catch ets:new(local_iqtable, [named_table, public]), catch ets:new(local_iqtable, [named_table, public]),
loop(#state{mydomain = MyDomain, loop(#state{mydomain = MyDomain,
iqtable = local_iqtable}). iqtable = local_iqtable}).

View File

@ -1,7 +1,7 @@
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
%%% File : ejabberd_router.erl %%% File : ejabberd_router.erl
%%% Author : Alexey Shchepin <alexey@sevcom.net> %%% Author : Alexey Shchepin <alexey@sevcom.net>
%%% Purpose : %%% Purpose : Main router
%%% Created : 27 Nov 2002 by Alexey Shchepin <alexey@sevcom.net> %%% Created : 27 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
%%% Id : $Id$ %%% Id : $Id$
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
@ -12,9 +12,7 @@
-export([route/3, -export([route/3,
register_route/1, register_route/1,
register_local_route/1,
unregister_route/1, unregister_route/1,
unregister_local_route/1,
dirty_get_all_routes/0, dirty_get_all_routes/0,
dirty_get_all_domains/0 dirty_get_all_domains/0
]). ]).
@ -24,25 +22,22 @@
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-include("jlib.hrl"). -include("jlib.hrl").
-record(route, {domain, node, pid}). -record(route, {domain, pid}).
-record(local_route, {domain, pid}).
start_link() -> start_link() ->
{ok, proc_lib:spawn_link(ejabberd_router, init, [])}. Pid = proc_lib:spawn_link(ejabberd_router, init, []),
register(ejabberd_router, Pid),
{ok, Pid}.
init() -> init() ->
register(ejabberd_router, self()), update_tables(),
mnesia:create_table(route, mnesia:create_table(route,
[{ram_copies, [node()]}, [{ram_copies, [node()]},
{type, bag},
{attributes, {attributes,
record_info(fields, route)}]), record_info(fields, route)}]),
mnesia:create_table(local_route, mnesia:add_table_copy(route, node(), ram_copies),
[{ram_copies, [node()]},
{local_content, true},
{attributes,
record_info(fields, local_route)}]),
mnesia:add_table_copy(local_route, node(), ram_copies),
loop(). loop().
loop() -> loop() ->
@ -56,73 +51,37 @@ loop() ->
ok ok
end, end,
loop(); loop();
{register_route, Domain, Pid, Node} -> {register_route, Domain, Pid} ->
F = fun() -> F = fun() ->
%case mnesia:wread({route, Domain}) of
% [] ->
% ok;
% [Old] ->
% % TODO: notify
% ok
%end,
mnesia:write(#route{domain = Domain, mnesia:write(#route{domain = Domain,
node = Node,
pid = Pid}) pid = Pid})
end, end,
mnesia:transaction(F), mnesia:transaction(F),
loop(); loop();
{register_local_route, Domain, Pid} -> {unregister_route, Domain, Pid} ->
F = fun() -> F = fun() ->
mnesia:write(#local_route{domain = Domain, mnesia:delete_object(#route{domain = Domain,
pid = Pid}) pid = Pid})
end, end,
mnesia:transaction(F), mnesia:transaction(F),
loop(); loop();
{unregister_route, Domain} ->
F = fun() ->
%case mnesia:wread({route, Domain}) of
% [] ->
% ok;
% [Old] ->
% % TODO: notify
% ok
%end,
mnesia:delete({route, Domain})
end,
mnesia:transaction(F),
loop();
{unregister_local_route, Domain} ->
F = fun() ->
mnesia:delete({local_route, Domain})
end,
mnesia:transaction(F),
loop();
_ -> _ ->
loop() loop()
end. end.
do_route(From, To, Packet) -> do_route(From, To, Packet) ->
?DEBUG("route~n\tfrom ~p~n\tto ~p~n\tpacket ~p~n", [From, To, Packet]), ?DEBUG("route~n\tfrom ~p~n\tto ~p~n\tpacket ~p~n", [From, To, Packet]),
#jid{lserver = LDstDomain} = To, LDstDomain = To#jid.lserver,
case mnesia:dirty_read({local_route, LDstDomain}) of case mnesia:dirty_read(route, LDstDomain) of
[] ->
case mnesia:dirty_read({route, LDstDomain}) of
[] -> [] ->
ejabberd_s2s ! {route, From, To, Packet}; ejabberd_s2s ! {route, From, To, Packet};
[R] -> [R] ->
Node = R#route.node,
case node() of
Node ->
Pid = R#route.pid, Pid = R#route.pid,
?DEBUG("routed to process ~p~n", [Pid]), ?DEBUG("routed to process ~p~n", [Pid]),
Pid ! {route, From, To, Packet}; Pid ! {route, From, To, Packet};
_ -> Rs ->
?DEBUG("routed to node ~p~n", [Node]), R = lists:nth(erlang:phash(now(), length(Rs)), Rs),
{ejabberd_router, Node} ! {route, From, To, Packet} Pid = R#route.pid,
end
end;
[R] ->
Pid = R#local_route.pid,
?DEBUG("routed to process ~p~n", [Pid]), ?DEBUG("routed to process ~p~n", [Pid]),
Pid ! {route, From, To, Packet} Pid ! {route, From, To, Packet}
end. end.
@ -132,24 +91,33 @@ route(From, To, Packet) ->
ejabberd_router ! {route, From, To, Packet}. ejabberd_router ! {route, From, To, Packet}.
register_route(Domain) -> register_route(Domain) ->
ejabberd_router ! {register_route, Domain, self(), node()}. ejabberd_router ! {register_route, Domain, self()}.
register_local_route(Domain) ->
ejabberd_router ! {register_local_route, Domain, self()}.
unregister_route(Domain) -> unregister_route(Domain) ->
ejabberd_router ! {unregister_route, Domain}. ejabberd_router ! {unregister_route, Domain, self()}.
unregister_local_route(Domain) ->
ejabberd_router ! {unregister_local_route, Domain}.
dirty_get_all_routes() -> dirty_get_all_routes() ->
lists:delete(?MYNAME, lists:delete(?MYNAME, lists:usort(mnesia:dirty_all_keys(route))).
lists:umerge(lists:sort(mnesia:dirty_all_keys(route)),
lists:sort(mnesia:dirty_all_keys(local_route)))).
dirty_get_all_domains() -> dirty_get_all_domains() ->
lists:umerge(lists:sort(mnesia:dirty_all_keys(route)), lists:usort(mnesia:dirty_all_keys(route)).
lists:sort(mnesia:dirty_all_keys(local_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.

View File

@ -164,7 +164,7 @@ wait_for_handshake({xmlstreamelement, El}, StateData) ->
send_text(StateData, "<handshake/>"), send_text(StateData, "<handshake/>"),
lists:foreach( lists:foreach(
fun(H) -> fun(H) ->
ejabberd_router:register_local_route(H) ejabberd_router:register_route(H)
end, StateData#state.hosts), end, StateData#state.hosts),
{next_state, stream_established, StateData}; {next_state, stream_established, StateData};
_ -> _ ->
@ -299,7 +299,7 @@ terminate(_Reason, StateName, StateData) ->
stream_established -> stream_established ->
lists:foreach( lists:foreach(
fun(H) -> fun(H) ->
ejabberd_router:unregister_local_route(H) ejabberd_router:unregister_route(H)
end, StateData#state.hosts); end, StateData#state.hosts);
_ -> _ ->
ok ok

View File

@ -1,7 +1,7 @@
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
%%% File : ejabberd_sm.erl %%% File : ejabberd_sm.erl
%%% Author : Alexey Shchepin <alexey@sevcom.net> %%% Author : Alexey Shchepin <alexey@sevcom.net>
%%% Purpose : %%% Purpose : Session manager
%%% Created : 24 Nov 2002 by Alexey Shchepin <alexey@sevcom.net> %%% Created : 24 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
%%% Id : $Id$ %%% Id : $Id$
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
@ -21,28 +21,23 @@
unregister_iq_handler/1 unregister_iq_handler/1
]). ]).
-include_lib("mnemosyne/include/mnemosyne.hrl").
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-include("jlib.hrl"). -include("jlib.hrl").
-record(session, {ur, user, node}). -record(session, {ur, user, pid}).
-record(local_session, {ur, pid}).
-record(presence, {ur, user, priority}). -record(presence, {ur, user, priority}).
start_link() -> start_link() ->
{ok, proc_lib:spawn_link(ejabberd_sm, init, [])}. Pid = proc_lib:spawn_link(ejabberd_sm, init, []),
register(ejabberd_sm, Pid),
{ok, Pid}.
init() -> init() ->
register(ejabberd_sm, self()), update_tables(),
mnesia:create_table(session, [{ram_copies, [node()]}, mnesia:create_table(session, [{ram_copies, [node()]},
{attributes, record_info(fields, session)}]), {attributes, record_info(fields, session)}]),
mnesia:add_table_index(session, user), mnesia:add_table_index(session, user),
mnesia:add_table_index(session, node), mnesia:add_table_copy(session, node(), ram_copies),
mnesia:create_table(local_session,
[{ram_copies, [node()]},
{local_content, true},
{attributes, record_info(fields, local_session)}]),
mnesia:add_table_copy(local_session, node(), ram_copies),
mnesia:create_table(presence, mnesia:create_table(presence,
[{ram_copies, [node()]}, [{ram_copies, [node()]},
{attributes, record_info(fields, presence)}]), {attributes, record_info(fields, presence)}]),
@ -68,9 +63,6 @@ loop() ->
{close_session, User, Resource} -> {close_session, User, Resource} ->
remove_connection(User, Resource), remove_connection(User, Resource),
loop(); loop();
{replace, User, Resource} ->
replace_my_connection(User, Resource),
loop();
{mnesia_system_event, {mnesia_down, Node}} -> {mnesia_system_event, {mnesia_down, Node}} ->
clean_table_from_bad_node(Node), clean_table_from_bad_node(Node),
loop(); loop();
@ -108,46 +100,15 @@ register_connection(User, Resource, Pid) ->
UR = {LUser, LResource}, UR = {LUser, LResource},
F = fun() -> F = fun() ->
Ss = mnesia:wread({session, UR}), Ss = mnesia:wread({session, UR}),
Ls = mnesia:wread({local_session, UR}), mnesia:write(#session{ur = UR, user = LUser, pid = Pid}),
mnesia:write(#session{ur = UR, user = LUser, node = node()}), Ss
mnesia:write(#local_session{ur = UR, pid = Pid}),
{Ss, Ls}
end, end,
case mnesia:transaction(F) of case mnesia:transaction(F) of
{atomic, {Ss, Ls}} -> {atomic, Ss} ->
lists:foreach( lists:foreach(
fun(R) -> fun(R) ->
if R#session.node /= node() -> R#session.pid ! replaced
{ejabberd_sm, R#session.node} ! end, Ss);
{replace, User, Resource};
true ->
ok
end
end, Ss),
lists:foreach(
fun(R) ->
R#local_session.pid ! replaced
end, Ls);
_ ->
false
end.
replace_my_connection(User, Resource) ->
LUser = jlib:nodeprep(User),
LResource = jlib:resourceprep(Resource),
UR = {LUser, LResource},
F = fun() ->
Es = mnesia:read({local_session, UR}),
mnesia:delete({local_session, UR}),
Es
end,
case mnesia:transaction(F) of
{atomic, Rs} ->
lists:foreach(
fun(R) ->
R#local_session.pid ! replaced
end, Rs);
_ -> _ ->
false false
end. end.
@ -156,9 +117,8 @@ replace_my_connection(User, Resource) ->
remove_connection(User, Resource) -> remove_connection(User, Resource) ->
LUser = jlib:nodeprep(User), LUser = jlib:nodeprep(User),
LResource = jlib:resourceprep(Resource), LResource = jlib:resourceprep(Resource),
F = fun() ->
UR = {LUser, LResource}, UR = {LUser, LResource},
mnesia:delete({local_session, UR}), F = fun() ->
mnesia:delete({session, UR}) mnesia:delete({session, UR})
end, end,
mnesia:transaction(F). mnesia:transaction(F).
@ -166,11 +126,13 @@ remove_connection(User, Resource) ->
clean_table_from_bad_node(Node) -> clean_table_from_bad_node(Node) ->
F = fun() -> F = fun() ->
Es = mnesia:index_read(session, Node, #session.node), Es = mnesia:select(
session,
[{#session{pid = '$1', _ = '_'},
[{'==', {node, '$1'}, Node}],
['$_']}]),
lists:foreach(fun(E) -> lists:foreach(fun(E) ->
mnesia:delete_object(session, E, write), mnesia:delete_object(E)
mnesia:delete(
{user_resource, E#session.ur})
end, Es) end, Es)
end, end,
mnesia:transaction(F). mnesia:transaction(F).
@ -261,8 +223,7 @@ do_route(From, To, Packet) ->
end; end;
_ -> _ ->
LUR = {LUser, LResource}, LUR = {LUser, LResource},
Sess = mnesia:dirty_read({session, LUR}), case mnesia:dirty_read({session, LUR}) of
case Sess of
[] -> [] ->
case Name of case Name of
"message" -> "message" ->
@ -280,18 +241,11 @@ do_route(From, To, Packet) ->
_ -> _ ->
?DEBUG("packet droped~n", []) ?DEBUG("packet droped~n", [])
end; end;
[Ses] -> [Sess] ->
case mnesia:dirty_read({local_session, LUR}) of Pid = Sess#session.pid,
[] ->
Node = Ses#session.node,
?DEBUG("sending to node ~p~n", [Node]),
{ejabberd_sm, Node} ! {route, From, To, Packet};
[El] ->
Pid = El#local_session.pid,
?DEBUG("sending to process ~p~n", [Pid]), ?DEBUG("sending to process ~p~n", [Pid]),
Pid ! {route, From, To, Packet} Pid ! {route, From, To, Packet}
end end
end
end. end.
route_message(From, To, Packet) -> route_message(From, To, Packet) ->
@ -375,7 +329,11 @@ dirty_get_sessions_list() ->
mnesia:dirty_all_keys(session). mnesia:dirty_all_keys(session).
dirty_get_my_sessions_list() -> dirty_get_my_sessions_list() ->
mnesia:dirty_all_keys(local_session). mnesia:dirty_select(
session,
[{#session{pid = '$1', _ = '_'},
[{'==', {node, '$1'}, node()}],
['$_']}]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -421,3 +379,21 @@ register_iq_handler(XMLNS, Module, Fun, Opts) ->
unregister_iq_handler(XMLNS) -> unregister_iq_handler(XMLNS) ->
ejabberd_sm ! {unregister_iq_handler, XMLNS}. ejabberd_sm ! {unregister_iq_handler, XMLNS}.
update_tables() ->
case catch mnesia:table_info(session, attributes) of
[ur, user, node] ->
mnesia:delete_table(session);
[ur, user, pid] ->
ok;
{'EXIT', _} ->
ok
end,
case lists:member(local_session, mnesia:system_info(tables)) of
true ->
mnesia:delete_table(local_session);
false ->
ok
end.

View File

@ -25,7 +25,7 @@ start(Opts) ->
register(ejabberd_mod_echo, spawn(?MODULE, init, [Host])). register(ejabberd_mod_echo, spawn(?MODULE, init, [Host])).
init(Host) -> init(Host) ->
ejabberd_router:register_local_route(Host), ejabberd_router:register_route(Host),
loop(Host). loop(Host).
loop(Host) -> loop(Host) ->
@ -34,7 +34,7 @@ loop(Host) ->
ejabberd_router:route(To, From, Packet), ejabberd_router:route(To, From, Packet),
loop(Host); loop(Host);
stop -> stop ->
ejabberd_router:unregister_local_route(Host), ejabberd_router:unregister_route(Host),
ok; ok;
_ -> _ ->
loop(Host) loop(Host)

View File

@ -68,7 +68,7 @@ start(Opts) ->
init(Host) -> init(Host) ->
ejabberd_router:register_local_route(Host), ejabberd_router:register_route(Host),
loop(). loop().
loop() -> loop() ->