Do not duplicate enc_pid/dec_pid functions

This commit is contained in:
Evgeniy Khramtsov 2017-03-30 17:44:43 +03:00
parent bfde473c3b
commit f5f353d90a
5 changed files with 39 additions and 119 deletions

View File

@ -32,7 +32,8 @@
decode_base64/1, encode_base64/1, ip_to_list/1, decode_base64/1, encode_base64/1, ip_to_list/1,
hex_to_bin/1, hex_to_base64/1, expand_keyword/3, hex_to_bin/1, hex_to_base64/1, expand_keyword/3,
atom_to_binary/1, binary_to_atom/1, tuple_to_binary/1, 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 %%% API
@ -194,6 +195,32 @@ i2l(L, N) when is_binary(L) ->
_ -> i2l(<<$0, L/binary>>, N) _ -> i2l(<<$0, L/binary>>, N)
end. 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 %%% 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], ".")).

View File

@ -51,7 +51,7 @@ init() ->
end. end.
register_route(Domain, ServerHost, LocalHint, _, Pid) -> register_route(Domain, ServerHost, LocalHint, _, Pid) ->
PidS = enc_pid(Pid), PidS = aux:encode_pid(Pid),
LocalHintS = enc_local_hint(LocalHint), LocalHintS = enc_local_hint(LocalHint),
Node = erlang:atom_to_binary(node(Pid), latin1), Node = erlang:atom_to_binary(node(Pid), latin1),
case ?SQL_UPSERT(?MYNAME, "route", case ?SQL_UPSERT(?MYNAME, "route",
@ -68,7 +68,7 @@ register_route(Domain, ServerHost, LocalHint, _, Pid) ->
end. end.
unregister_route(Domain, _, Pid) -> unregister_route(Domain, _, Pid) ->
PidS = enc_pid(Pid), PidS = aux:encode_pid(Pid),
Node = erlang:atom_to_binary(node(Pid), latin1), Node = erlang:atom_to_binary(node(Pid), latin1),
ejabberd_sql:sql_query( ejabberd_sql:sql_query(
?MYNAME, ?MYNAME,
@ -153,37 +153,10 @@ dec_local_hint(<<"">>) ->
dec_local_hint(S) -> dec_local_hint(S) ->
ejabberd_sql:decode_term(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) -> row_to_route(Domain, {ServerHost, NodeS, PidS, LocalHintS} = Row) ->
try [#route{domain = Domain, try [#route{domain = Domain,
server_host = ServerHost, server_host = ServerHost,
pid = dec_pid(PidS, NodeS), pid = aux:decode_pid(PidS, NodeS),
local_hint = dec_local_hint(LocalHintS)}] local_hint = dec_local_hint(LocalHintS)}]
catch _:{node_down, _} -> catch _:{node_down, _} ->
[]; [];

View File

@ -68,7 +68,7 @@ set_session(#session{sid = {Now, Pid}, usr = {U, LServer, R},
InfoS = aux:term_to_expr(Info), InfoS = aux:term_to_expr(Info),
PrioS = enc_priority(Priority), PrioS = enc_priority(Priority),
TS = now_to_timestamp(Now), TS = now_to_timestamp(Now),
PidS = enc_pid(Pid), PidS = aux:encode_pid(Pid),
Node = erlang:atom_to_binary(node(Pid), latin1), Node = erlang:atom_to_binary(node(Pid), latin1),
case ?SQL_UPSERT(LServer, "sm", case ?SQL_UPSERT(LServer, "sm",
["!usec=%(TS)d", ["!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}) -> row_to_session(LServer, {USec, PidS, NodeS, User, Resource, PrioS, InfoS}) ->
Now = timestamp_to_now(USec), Now = timestamp_to_now(USec),
Pid = dec_pid(PidS, NodeS), Pid = aux:decode_pid(PidS, NodeS),
Priority = dec_priority(PrioS), Priority = dec_priority(PrioS),
Info = ejabberd_sql:decode_term(InfoS), Info = ejabberd_sql:decode_term(InfoS),
#session{sid = {Now, Pid}, us = {User, LServer}, #session{sid = {Now, Pid}, us = {User, LServer},
usr = {User, LServer, Resource}, usr = {User, LServer, Resource},
priority = Priority, priority = Priority,
info = Info}. 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], ".")).

View File

@ -34,7 +34,7 @@ init() ->
end. end.
open_session(SID, Pid) -> open_session(SID, Pid) ->
PidS = enc_pid(Pid), PidS = aux:encode_pid(Pid),
Node = erlang:atom_to_binary(node(Pid), latin1), Node = erlang:atom_to_binary(node(Pid), latin1),
case ?SQL_UPSERT(?MYNAME, "bosh", case ?SQL_UPSERT(?MYNAME, "bosh",
["!sid=%(SID)s", ["!sid=%(SID)s",
@ -57,7 +57,7 @@ find_session(SID) ->
?MYNAME, ?MYNAME,
?SQL("select @(pid)s, @(node)s from bosh where sid=%(SID)s")) of ?SQL("select @(pid)s, @(node)s from bosh where sid=%(SID)s")) of
{selected, [{Pid, Node}]} -> {selected, [{Pid, Node}]} ->
try {ok, dec_pid(Pid, Node)} try {ok, aux:decode_pid(Pid, Node)}
catch _:{node_down, _} -> error catch _:{node_down, _} -> error
end; end;
{selected, []} -> {selected, []} ->
@ -70,29 +70,3 @@ find_session(SID) ->
%%%=================================================================== %%%===================================================================
%%% Internal functions %%% 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], ".")).

View File

@ -171,7 +171,7 @@ search_affiliation(_ServerHost, _Room, _Host, _Affiliation) ->
{error, not_implemented}. {error, not_implemented}.
register_online_room(ServerHost, Room, Host, Pid) -> register_online_room(ServerHost, Room, Host, Pid) ->
PidS = enc_pid(Pid), PidS = aux:encode_pid(Pid),
NodeS = erlang:atom_to_binary(node(Pid), latin1), NodeS = erlang:atom_to_binary(node(Pid), latin1),
case ?SQL_UPSERT(ServerHost, case ?SQL_UPSERT(ServerHost,
"muc_online_room", "muc_online_room",
@ -188,7 +188,7 @@ register_online_room(ServerHost, Room, Host, Pid) ->
unregister_online_room(ServerHost, Room, Host, Pid) -> unregister_online_room(ServerHost, Room, Host, Pid) ->
%% TODO: report errors %% TODO: report errors
PidS = enc_pid(Pid), PidS = aux:encode_pid(Pid),
NodeS = erlang:atom_to_binary(node(Pid), latin1), NodeS = erlang:atom_to_binary(node(Pid), latin1),
ejabberd_sql:sql_query( ejabberd_sql:sql_query(
ServerHost, ServerHost,
@ -201,7 +201,7 @@ find_online_room(ServerHost, Room, Host) ->
?SQL("select @(pid)s, @(node)s from muc_online_room where " ?SQL("select @(pid)s, @(node)s from muc_online_room where "
"name=%(Room)s and host=%(Host)s")) of "name=%(Room)s and host=%(Host)s")) of
{selected, [{PidS, NodeS}]} -> {selected, [{PidS, NodeS}]} ->
try {ok, dec_pid(PidS, NodeS)} try {ok, aux:decode_pid(PidS, NodeS)}
catch _:{node_down, _} -> error catch _:{node_down, _} -> error
end; end;
{selected, []} -> {selected, []} ->
@ -231,7 +231,7 @@ get_online_rooms(ServerHost, Host, _RSM) ->
{selected, Rows} -> {selected, Rows} ->
lists:flatmap( lists:flatmap(
fun({Room, PidS, NodeS}) -> fun({Room, PidS, NodeS}) ->
try [{Room, Host, dec_pid(PidS, NodeS)}] try [{Room, Host, aux:decode_pid(PidS, NodeS)}]
catch _:{node_down, _} -> [] catch _:{node_down, _} -> []
end end
end, Rows); end, Rows);
@ -328,33 +328,6 @@ import(_, _, _) ->
%%%=================================================================== %%%===================================================================
%%% Internal functions %%% 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) -> clean_tables(ServerHost) ->
NodeS = erlang:atom_to_binary(node(), latin1), NodeS = erlang:atom_to_binary(node(), latin1),
?INFO_MSG("Cleaning SQL muc_online_room table...", []), ?INFO_MSG("Cleaning SQL muc_online_room table...", []),