mirror of
https://github.com/processone/ejabberd.git
synced 2024-11-22 16:20:52 +01:00
Do not duplicate enc_pid/dec_pid functions
This commit is contained in:
parent
bfde473c3b
commit
f5f353d90a
29
src/aux.erl
29
src/aux.erl
@ -32,7 +32,8 @@
|
||||
decode_base64/1, encode_base64/1, ip_to_list/1,
|
||||
hex_to_bin/1, hex_to_base64/1, expand_keyword/3,
|
||||
atom_to_binary/1, binary_to_atom/1, tuple_to_binary/1,
|
||||
l2i/1, i2l/1, i2l/2, expr_to_term/1, term_to_expr/1]).
|
||||
l2i/1, i2l/1, i2l/2, expr_to_term/1, term_to_expr/1,
|
||||
encode_pid/1, decode_pid/2]).
|
||||
|
||||
%%%===================================================================
|
||||
%%% API
|
||||
@ -194,6 +195,32 @@ i2l(L, N) when is_binary(L) ->
|
||||
_ -> i2l(<<$0, L/binary>>, N)
|
||||
end.
|
||||
|
||||
-spec encode_pid(pid()) -> binary().
|
||||
encode_pid(Pid) ->
|
||||
list_to_binary(erlang:pid_to_list(Pid)).
|
||||
|
||||
-spec decode_pid(binary(), binary()) -> pid().
|
||||
decode_pid(PidBin, NodeBin) ->
|
||||
PidStr = binary_to_list(PidBin),
|
||||
Pid = erlang:list_to_pid(PidStr),
|
||||
case erlang:binary_to_atom(NodeBin, latin1) of
|
||||
Node when Node == node() ->
|
||||
Pid;
|
||||
Node ->
|
||||
try set_node_id(PidStr, NodeBin)
|
||||
catch _:badarg ->
|
||||
erlang:error({node_down, Node})
|
||||
end
|
||||
end.
|
||||
|
||||
%%%===================================================================
|
||||
%%% Internal functions
|
||||
%%%===================================================================
|
||||
-spec set_node_id(string(), binary()) -> pid().
|
||||
set_node_id(PidStr, NodeBin) ->
|
||||
ExtPidStr = erlang:pid_to_list(
|
||||
binary_to_term(
|
||||
<<131,103,100,(size(NodeBin)):16,NodeBin/binary,0:72>>)),
|
||||
[H|_] = string:tokens(ExtPidStr, "."),
|
||||
[_|T] = string:tokens(PidStr, "."),
|
||||
erlang:list_to_pid(string:join([H|T], ".")).
|
||||
|
@ -51,7 +51,7 @@ init() ->
|
||||
end.
|
||||
|
||||
register_route(Domain, ServerHost, LocalHint, _, Pid) ->
|
||||
PidS = enc_pid(Pid),
|
||||
PidS = aux:encode_pid(Pid),
|
||||
LocalHintS = enc_local_hint(LocalHint),
|
||||
Node = erlang:atom_to_binary(node(Pid), latin1),
|
||||
case ?SQL_UPSERT(?MYNAME, "route",
|
||||
@ -68,7 +68,7 @@ register_route(Domain, ServerHost, LocalHint, _, Pid) ->
|
||||
end.
|
||||
|
||||
unregister_route(Domain, _, Pid) ->
|
||||
PidS = enc_pid(Pid),
|
||||
PidS = aux:encode_pid(Pid),
|
||||
Node = erlang:atom_to_binary(node(Pid), latin1),
|
||||
ejabberd_sql:sql_query(
|
||||
?MYNAME,
|
||||
@ -153,37 +153,10 @@ dec_local_hint(<<"">>) ->
|
||||
dec_local_hint(S) ->
|
||||
ejabberd_sql:decode_term(S).
|
||||
|
||||
-spec enc_pid(pid()) -> binary().
|
||||
enc_pid(Pid) ->
|
||||
list_to_binary(erlang:pid_to_list(Pid)).
|
||||
|
||||
-spec dec_pid(binary(), binary()) -> pid().
|
||||
dec_pid(PidBin, NodeBin) ->
|
||||
PidStr = binary_to_list(PidBin),
|
||||
Pid = erlang:list_to_pid(PidStr),
|
||||
case erlang:binary_to_atom(NodeBin, latin1) of
|
||||
Node when Node == node() ->
|
||||
Pid;
|
||||
Node ->
|
||||
try set_node_id(PidStr, NodeBin)
|
||||
catch _:badarg ->
|
||||
erlang:error({node_down, Node})
|
||||
end
|
||||
end.
|
||||
|
||||
-spec set_node_id(string(), binary()) -> pid().
|
||||
set_node_id(PidStr, NodeBin) ->
|
||||
ExtPidStr = erlang:pid_to_list(
|
||||
binary_to_term(
|
||||
<<131,103,100,(size(NodeBin)):16,NodeBin/binary,0:72>>)),
|
||||
[H|_] = string:tokens(ExtPidStr, "."),
|
||||
[_|T] = string:tokens(PidStr, "."),
|
||||
erlang:list_to_pid(string:join([H|T], ".")).
|
||||
|
||||
row_to_route(Domain, {ServerHost, NodeS, PidS, LocalHintS} = Row) ->
|
||||
try [#route{domain = Domain,
|
||||
server_host = ServerHost,
|
||||
pid = dec_pid(PidS, NodeS),
|
||||
pid = aux:decode_pid(PidS, NodeS),
|
||||
local_hint = dec_local_hint(LocalHintS)}]
|
||||
catch _:{node_down, _} ->
|
||||
[];
|
||||
|
@ -68,7 +68,7 @@ set_session(#session{sid = {Now, Pid}, usr = {U, LServer, R},
|
||||
InfoS = aux:term_to_expr(Info),
|
||||
PrioS = enc_priority(Priority),
|
||||
TS = now_to_timestamp(Now),
|
||||
PidS = enc_pid(Pid),
|
||||
PidS = aux:encode_pid(Pid),
|
||||
Node = erlang:atom_to_binary(node(Pid), latin1),
|
||||
case ?SQL_UPSERT(LServer, "sm",
|
||||
["!usec=%(TS)d",
|
||||
@ -194,37 +194,10 @@ enc_priority(Int) when is_integer(Int) ->
|
||||
|
||||
row_to_session(LServer, {USec, PidS, NodeS, User, Resource, PrioS, InfoS}) ->
|
||||
Now = timestamp_to_now(USec),
|
||||
Pid = dec_pid(PidS, NodeS),
|
||||
Pid = aux:decode_pid(PidS, NodeS),
|
||||
Priority = dec_priority(PrioS),
|
||||
Info = ejabberd_sql:decode_term(InfoS),
|
||||
#session{sid = {Now, Pid}, us = {User, LServer},
|
||||
usr = {User, LServer, Resource},
|
||||
priority = Priority,
|
||||
info = Info}.
|
||||
|
||||
-spec enc_pid(pid()) -> binary().
|
||||
enc_pid(Pid) ->
|
||||
list_to_binary(erlang:pid_to_list(Pid)).
|
||||
|
||||
-spec dec_pid(binary(), binary()) -> pid().
|
||||
dec_pid(PidBin, NodeBin) ->
|
||||
PidStr = binary_to_list(PidBin),
|
||||
Pid = erlang:list_to_pid(PidStr),
|
||||
case erlang:binary_to_atom(NodeBin, latin1) of
|
||||
Node when Node == node() ->
|
||||
Pid;
|
||||
Node ->
|
||||
try set_node_id(PidStr, NodeBin)
|
||||
catch _:badarg ->
|
||||
erlang:error({node_down, Node})
|
||||
end
|
||||
end.
|
||||
|
||||
-spec set_node_id(string(), binary()) -> pid().
|
||||
set_node_id(PidStr, NodeBin) ->
|
||||
ExtPidStr = erlang:pid_to_list(
|
||||
binary_to_term(
|
||||
<<131,103,100,(size(NodeBin)):16,NodeBin/binary,0:72>>)),
|
||||
[H|_] = string:tokens(ExtPidStr, "."),
|
||||
[_|T] = string:tokens(PidStr, "."),
|
||||
erlang:list_to_pid(string:join([H|T], ".")).
|
||||
|
@ -34,7 +34,7 @@ init() ->
|
||||
end.
|
||||
|
||||
open_session(SID, Pid) ->
|
||||
PidS = enc_pid(Pid),
|
||||
PidS = aux:encode_pid(Pid),
|
||||
Node = erlang:atom_to_binary(node(Pid), latin1),
|
||||
case ?SQL_UPSERT(?MYNAME, "bosh",
|
||||
["!sid=%(SID)s",
|
||||
@ -57,7 +57,7 @@ find_session(SID) ->
|
||||
?MYNAME,
|
||||
?SQL("select @(pid)s, @(node)s from bosh where sid=%(SID)s")) of
|
||||
{selected, [{Pid, Node}]} ->
|
||||
try {ok, dec_pid(Pid, Node)}
|
||||
try {ok, aux:decode_pid(Pid, Node)}
|
||||
catch _:{node_down, _} -> error
|
||||
end;
|
||||
{selected, []} ->
|
||||
@ -70,29 +70,3 @@ find_session(SID) ->
|
||||
%%%===================================================================
|
||||
%%% Internal functions
|
||||
%%%===================================================================
|
||||
-spec enc_pid(pid()) -> binary().
|
||||
enc_pid(Pid) ->
|
||||
list_to_binary(erlang:pid_to_list(Pid)).
|
||||
|
||||
-spec dec_pid(binary(), binary()) -> pid().
|
||||
dec_pid(PidBin, NodeBin) ->
|
||||
PidStr = binary_to_list(PidBin),
|
||||
Pid = erlang:list_to_pid(PidStr),
|
||||
case erlang:binary_to_atom(NodeBin, latin1) of
|
||||
Node when Node == node() ->
|
||||
Pid;
|
||||
Node ->
|
||||
try set_node_id(PidStr, NodeBin)
|
||||
catch _:badarg ->
|
||||
erlang:error({node_down, Node})
|
||||
end
|
||||
end.
|
||||
|
||||
-spec set_node_id(string(), binary()) -> pid().
|
||||
set_node_id(PidStr, NodeBin) ->
|
||||
ExtPidStr = erlang:pid_to_list(
|
||||
binary_to_term(
|
||||
<<131,103,100,(size(NodeBin)):16,NodeBin/binary,0:72>>)),
|
||||
[H|_] = string:tokens(ExtPidStr, "."),
|
||||
[_|T] = string:tokens(PidStr, "."),
|
||||
erlang:list_to_pid(string:join([H|T], ".")).
|
||||
|
@ -171,7 +171,7 @@ search_affiliation(_ServerHost, _Room, _Host, _Affiliation) ->
|
||||
{error, not_implemented}.
|
||||
|
||||
register_online_room(ServerHost, Room, Host, Pid) ->
|
||||
PidS = enc_pid(Pid),
|
||||
PidS = aux:encode_pid(Pid),
|
||||
NodeS = erlang:atom_to_binary(node(Pid), latin1),
|
||||
case ?SQL_UPSERT(ServerHost,
|
||||
"muc_online_room",
|
||||
@ -188,7 +188,7 @@ register_online_room(ServerHost, Room, Host, Pid) ->
|
||||
|
||||
unregister_online_room(ServerHost, Room, Host, Pid) ->
|
||||
%% TODO: report errors
|
||||
PidS = enc_pid(Pid),
|
||||
PidS = aux:encode_pid(Pid),
|
||||
NodeS = erlang:atom_to_binary(node(Pid), latin1),
|
||||
ejabberd_sql:sql_query(
|
||||
ServerHost,
|
||||
@ -201,7 +201,7 @@ find_online_room(ServerHost, Room, Host) ->
|
||||
?SQL("select @(pid)s, @(node)s from muc_online_room where "
|
||||
"name=%(Room)s and host=%(Host)s")) of
|
||||
{selected, [{PidS, NodeS}]} ->
|
||||
try {ok, dec_pid(PidS, NodeS)}
|
||||
try {ok, aux:decode_pid(PidS, NodeS)}
|
||||
catch _:{node_down, _} -> error
|
||||
end;
|
||||
{selected, []} ->
|
||||
@ -231,7 +231,7 @@ get_online_rooms(ServerHost, Host, _RSM) ->
|
||||
{selected, Rows} ->
|
||||
lists:flatmap(
|
||||
fun({Room, PidS, NodeS}) ->
|
||||
try [{Room, Host, dec_pid(PidS, NodeS)}]
|
||||
try [{Room, Host, aux:decode_pid(PidS, NodeS)}]
|
||||
catch _:{node_down, _} -> []
|
||||
end
|
||||
end, Rows);
|
||||
@ -328,33 +328,6 @@ import(_, _, _) ->
|
||||
%%%===================================================================
|
||||
%%% Internal functions
|
||||
%%%===================================================================
|
||||
-spec enc_pid(pid()) -> binary().
|
||||
enc_pid(Pid) ->
|
||||
list_to_binary(erlang:pid_to_list(Pid)).
|
||||
|
||||
-spec dec_pid(binary(), binary()) -> pid().
|
||||
dec_pid(PidBin, NodeBin) ->
|
||||
PidStr = binary_to_list(PidBin),
|
||||
Pid = erlang:list_to_pid(PidStr),
|
||||
case erlang:binary_to_atom(NodeBin, latin1) of
|
||||
Node when Node == node() ->
|
||||
Pid;
|
||||
Node ->
|
||||
try set_node_id(PidStr, NodeBin)
|
||||
catch _:badarg ->
|
||||
erlang:error({node_down, Node})
|
||||
end
|
||||
end.
|
||||
|
||||
-spec set_node_id(string(), binary()) -> pid().
|
||||
set_node_id(PidStr, NodeBin) ->
|
||||
ExtPidStr = erlang:pid_to_list(
|
||||
binary_to_term(
|
||||
<<131,103,100,(size(NodeBin)):16,NodeBin/binary,0:72>>)),
|
||||
[H|_] = string:tokens(ExtPidStr, "."),
|
||||
[_|T] = string:tokens(PidStr, "."),
|
||||
erlang:list_to_pid(string:join([H|T], ".")).
|
||||
|
||||
clean_tables(ServerHost) ->
|
||||
NodeS = erlang:atom_to_binary(node(), latin1),
|
||||
?INFO_MSG("Cleaning SQL muc_online_room table...", []),
|
||||
|
Loading…
Reference in New Issue
Block a user