25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-04 16:40:36 +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>
* src/ejabberd_s2s.erl: Partially rewrited

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
%%%----------------------------------------------------------------------
%%% File : ejabberd_sm.erl
%%% Author : Alexey Shchepin <alexey@sevcom.net>
%%% Purpose :
%%% Purpose : Session manager
%%% Created : 24 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
%%% Id : $Id$
%%%----------------------------------------------------------------------
@ -21,28 +21,23 @@
unregister_iq_handler/1
]).
-include_lib("mnemosyne/include/mnemosyne.hrl").
-include("ejabberd.hrl").
-include("jlib.hrl").
-record(session, {ur, user, node}).
-record(local_session, {ur, pid}).
-record(session, {ur, user, pid}).
-record(presence, {ur, user, priority}).
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() ->
register(ejabberd_sm, self()),
update_tables(),
mnesia:create_table(session, [{ram_copies, [node()]},
{attributes, record_info(fields, session)}]),
mnesia:add_table_index(session, user),
mnesia:add_table_index(session, node),
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:add_table_copy(session, node(), ram_copies),
mnesia:create_table(presence,
[{ram_copies, [node()]},
{attributes, record_info(fields, presence)}]),
@ -68,9 +63,6 @@ loop() ->
{close_session, User, Resource} ->
remove_connection(User, Resource),
loop();
{replace, User, Resource} ->
replace_my_connection(User, Resource),
loop();
{mnesia_system_event, {mnesia_down, Node}} ->
clean_table_from_bad_node(Node),
loop();
@ -108,46 +100,15 @@ register_connection(User, Resource, Pid) ->
UR = {LUser, LResource},
F = fun() ->
Ss = mnesia:wread({session, UR}),
Ls = mnesia:wread({local_session, UR}),
mnesia:write(#session{ur = UR, user = LUser, node = node()}),
mnesia:write(#local_session{ur = UR, pid = Pid}),
{Ss, Ls}
mnesia:write(#session{ur = UR, user = LUser, pid = Pid}),
Ss
end,
case mnesia:transaction(F) of
{atomic, {Ss, Ls}} ->
{atomic, Ss} ->
lists:foreach(
fun(R) ->
if R#session.node /= node() ->
{ejabberd_sm, R#session.node} !
{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);
R#session.pid ! replaced
end, Ss);
_ ->
false
end.
@ -156,9 +117,8 @@ replace_my_connection(User, Resource) ->
remove_connection(User, Resource) ->
LUser = jlib:nodeprep(User),
LResource = jlib:resourceprep(Resource),
UR = {LUser, LResource},
F = fun() ->
UR = {LUser, LResource},
mnesia:delete({local_session, UR}),
mnesia:delete({session, UR})
end,
mnesia:transaction(F).
@ -166,11 +126,13 @@ remove_connection(User, Resource) ->
clean_table_from_bad_node(Node) ->
F = fun() ->
Es = mnesia:index_read(session, Node, #session.node),
Es = mnesia:select(
session,
[{#session{pid = '$1', _ = '_'},
[{'==', {node, '$1'}, Node}],
['$_']}]),
lists:foreach(fun(E) ->
mnesia:delete_object(session, E, write),
mnesia:delete(
{user_resource, E#session.ur})
mnesia:delete_object(E)
end, Es)
end,
mnesia:transaction(F).
@ -261,8 +223,7 @@ do_route(From, To, Packet) ->
end;
_ ->
LUR = {LUser, LResource},
Sess = mnesia:dirty_read({session, LUR}),
case Sess of
case mnesia:dirty_read({session, LUR}) of
[] ->
case Name of
"message" ->
@ -280,17 +241,10 @@ do_route(From, To, Packet) ->
_ ->
?DEBUG("packet droped~n", [])
end;
[Ses] ->
case mnesia:dirty_read({local_session, LUR}) of
[] ->
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]),
Pid ! {route, From, To, Packet}
end
[Sess] ->
Pid = Sess#session.pid,
?DEBUG("sending to process ~p~n", [Pid]),
Pid ! {route, From, To, Packet}
end
end.
@ -375,7 +329,11 @@ dirty_get_sessions_list() ->
mnesia:dirty_all_keys(session).
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) ->
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])).
init(Host) ->
ejabberd_router:register_local_route(Host),
ejabberd_router:register_route(Host),
loop(Host).
loop(Host) ->
@ -34,7 +34,7 @@ loop(Host) ->
ejabberd_router:route(To, From, Packet),
loop(Host);
stop ->
ejabberd_router:unregister_local_route(Host),
ejabberd_router:unregister_route(Host),
ok;
_ ->
loop(Host)

View File

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