parent
e56c6545fa
commit
e1f01f0c8a
|
@ -47,7 +47,7 @@
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
-spec init() -> ok | {error, any()}.
|
-spec init() -> ok | {error, any()}.
|
||||||
init() ->
|
init() ->
|
||||||
Node = jlib:atom_to_binary(node()),
|
Node = erlang:atom_to_binary(node(), latin1),
|
||||||
?INFO_MSG("Cleaning SQL SM table...", []),
|
?INFO_MSG("Cleaning SQL SM table...", []),
|
||||||
lists:foldl(
|
lists:foldl(
|
||||||
fun(Host, ok) ->
|
fun(Host, ok) ->
|
||||||
|
@ -68,8 +68,8 @@ set_session(#session{sid = {Now, Pid}, usr = {U, LServer, R},
|
||||||
InfoS = jlib:term_to_expr(Info),
|
InfoS = jlib:term_to_expr(Info),
|
||||||
PrioS = enc_priority(Priority),
|
PrioS = enc_priority(Priority),
|
||||||
TS = now_to_timestamp(Now),
|
TS = now_to_timestamp(Now),
|
||||||
PidS = list_to_binary(erlang:pid_to_list(Pid)),
|
PidS = enc_pid(Pid),
|
||||||
Node = jlib:atom_to_binary(node(Pid)),
|
Node = erlang:atom_to_binary(node(Pid), latin1),
|
||||||
case ?SQL_UPSERT(LServer, "sm",
|
case ?SQL_UPSERT(LServer, "sm",
|
||||||
["!usec=%(TS)d",
|
["!usec=%(TS)d",
|
||||||
"!pid=%(PidS)s",
|
"!pid=%(PidS)s",
|
||||||
|
@ -89,7 +89,7 @@ delete_session(_LUser, LServer, _LResource, {Now, Pid}) ->
|
||||||
PidS = list_to_binary(erlang:pid_to_list(Pid)),
|
PidS = list_to_binary(erlang:pid_to_list(Pid)),
|
||||||
case ejabberd_sql:sql_query(
|
case ejabberd_sql:sql_query(
|
||||||
LServer,
|
LServer,
|
||||||
?SQL("select @(usec)d, @(pid)s, @(username)s,"
|
?SQL("select @(usec)d, @(pid)s, @(node)s, @(username)s,"
|
||||||
" @(resource)s, @(priority)s, @(info)s "
|
" @(resource)s, @(priority)s, @(info)s "
|
||||||
"from sm where usec=%(TS)d and pid=%(PidS)s")) of
|
"from sm where usec=%(TS)d and pid=%(PidS)s")) of
|
||||||
{selected, [Row]} ->
|
{selected, [Row]} ->
|
||||||
|
@ -97,7 +97,9 @@ delete_session(_LUser, LServer, _LResource, {Now, Pid}) ->
|
||||||
LServer,
|
LServer,
|
||||||
?SQL("delete from sm"
|
?SQL("delete from sm"
|
||||||
" where usec=%(TS)d and pid=%(PidS)s")),
|
" where usec=%(TS)d and pid=%(PidS)s")),
|
||||||
{ok, row_to_session(LServer, Row)};
|
try {ok, row_to_session(LServer, Row)}
|
||||||
|
catch _:{node_down, _} -> {error, notfound}
|
||||||
|
end;
|
||||||
{selected, []} ->
|
{selected, []} ->
|
||||||
{error, notfound};
|
{error, notfound};
|
||||||
Err ->
|
Err ->
|
||||||
|
@ -114,10 +116,15 @@ get_sessions() ->
|
||||||
get_sessions(LServer) ->
|
get_sessions(LServer) ->
|
||||||
case ejabberd_sql:sql_query(
|
case ejabberd_sql:sql_query(
|
||||||
LServer,
|
LServer,
|
||||||
?SQL("select @(usec)d, @(pid)s, @(username)s,"
|
?SQL("select @(usec)d, @(pid)s, @(node)s, @(username)s,"
|
||||||
" @(resource)s, @(priority)s, @(info)s from sm")) of
|
" @(resource)s, @(priority)s, @(info)s from sm")) of
|
||||||
{selected, Rows} ->
|
{selected, Rows} ->
|
||||||
[row_to_session(LServer, Row) || Row <- Rows];
|
lists:flatmap(
|
||||||
|
fun(Row) ->
|
||||||
|
try [row_to_session(LServer, Row)]
|
||||||
|
catch _:{node_down, _} -> []
|
||||||
|
end
|
||||||
|
end, Rows);
|
||||||
Err ->
|
Err ->
|
||||||
?ERROR_MSG("failed to select from 'sm' table: ~p", [Err]),
|
?ERROR_MSG("failed to select from 'sm' table: ~p", [Err]),
|
||||||
[]
|
[]
|
||||||
|
@ -126,11 +133,16 @@ get_sessions(LServer) ->
|
||||||
get_sessions(LUser, LServer) ->
|
get_sessions(LUser, LServer) ->
|
||||||
case ejabberd_sql:sql_query(
|
case ejabberd_sql:sql_query(
|
||||||
LServer,
|
LServer,
|
||||||
?SQL("select @(usec)d, @(pid)s, @(username)s,"
|
?SQL("select @(usec)d, @(pid)s, @(node)s, @(username)s,"
|
||||||
" @(resource)s, @(priority)s, @(info)s from sm"
|
" @(resource)s, @(priority)s, @(info)s from sm"
|
||||||
" where username=%(LUser)s")) of
|
" where username=%(LUser)s")) of
|
||||||
{selected, Rows} ->
|
{selected, Rows} ->
|
||||||
[row_to_session(LServer, Row) || Row <- Rows];
|
lists:flatmap(
|
||||||
|
fun(Row) ->
|
||||||
|
try [row_to_session(LServer, Row)]
|
||||||
|
catch _:{node_down, _} -> []
|
||||||
|
end
|
||||||
|
end, Rows);
|
||||||
Err ->
|
Err ->
|
||||||
?ERROR_MSG("failed to select from 'sm' table: ~p", [Err]),
|
?ERROR_MSG("failed to select from 'sm' table: ~p", [Err]),
|
||||||
[]
|
[]
|
||||||
|
@ -139,11 +151,16 @@ get_sessions(LUser, LServer) ->
|
||||||
get_sessions(LUser, LServer, LResource) ->
|
get_sessions(LUser, LServer, LResource) ->
|
||||||
case ejabberd_sql:sql_query(
|
case ejabberd_sql:sql_query(
|
||||||
LServer,
|
LServer,
|
||||||
?SQL("select @(usec)d, @(pid)s, @(username)s,"
|
?SQL("select @(usec)d, @(pid)s, @(node)s, @(username)s,"
|
||||||
" @(resource)s, @(priority)s, @(info)s from sm"
|
" @(resource)s, @(priority)s, @(info)s from sm"
|
||||||
" where username=%(LUser)s and resource=%(LResource)s")) of
|
" where username=%(LUser)s and resource=%(LResource)s")) of
|
||||||
{selected, Rows} ->
|
{selected, Rows} ->
|
||||||
[row_to_session(LServer, Row) || Row <- Rows];
|
lists:flatmap(
|
||||||
|
fun(Row) ->
|
||||||
|
try [row_to_session(LServer, Row)]
|
||||||
|
catch _:{node_down, _} -> []
|
||||||
|
end
|
||||||
|
end, Rows);
|
||||||
Err ->
|
Err ->
|
||||||
?ERROR_MSG("failed to select from 'sm' table: ~p", [Err]),
|
?ERROR_MSG("failed to select from 'sm' table: ~p", [Err]),
|
||||||
[]
|
[]
|
||||||
|
@ -175,12 +192,39 @@ enc_priority(undefined) ->
|
||||||
enc_priority(Int) when is_integer(Int) ->
|
enc_priority(Int) when is_integer(Int) ->
|
||||||
integer_to_binary(Int).
|
integer_to_binary(Int).
|
||||||
|
|
||||||
row_to_session(LServer, {USec, PidS, 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 = erlang:list_to_pid(binary_to_list(PidS)),
|
Pid = dec_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], ".")).
|
||||||
|
|
Loading…
Reference in New Issue