2017-03-28 15:31:37 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% Created : 28 Mar 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(ejabberd_router_sql).
|
|
|
|
-behaviour(ejabberd_router).
|
|
|
|
|
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([init/0, register_route/5, unregister_route/3, find_routes/1,
|
2017-04-14 12:57:52 +02:00
|
|
|
get_all_routes/0]).
|
2017-03-28 15:31:37 +02:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("logger.hrl").
|
|
|
|
-include("ejabberd_sql_pt.hrl").
|
|
|
|
-include("ejabberd_router.hrl").
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init() ->
|
|
|
|
Node = erlang:atom_to_binary(node(), latin1),
|
2017-04-15 14:47:00 +02:00
|
|
|
?DEBUG("Cleaning SQL 'route' table...", []),
|
2017-03-28 15:31:37 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
?MYNAME, ?SQL("delete from route where node=%(Node)s")) of
|
|
|
|
{updated, _} ->
|
|
|
|
ok;
|
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("failed to clean 'route' table: ~p", [Err]),
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
|
|
|
register_route(Domain, ServerHost, LocalHint, _, Pid) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
PidS = misc:encode_pid(Pid),
|
2017-03-28 15:31:37 +02:00
|
|
|
LocalHintS = enc_local_hint(LocalHint),
|
|
|
|
Node = erlang:atom_to_binary(node(Pid), latin1),
|
|
|
|
case ?SQL_UPSERT(?MYNAME, "route",
|
|
|
|
["!domain=%(Domain)s",
|
|
|
|
"!server_host=%(ServerHost)s",
|
|
|
|
"!node=%(Node)s",
|
|
|
|
"!pid=%(PidS)s",
|
|
|
|
"local_hint=%(LocalHintS)s"]) of
|
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("failed to update 'route' table: ~p", [Err]),
|
2017-04-14 12:57:52 +02:00
|
|
|
{error, db_failure}
|
2017-03-28 15:31:37 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
unregister_route(Domain, _, Pid) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
PidS = misc:encode_pid(Pid),
|
2017-03-28 15:31:37 +02:00
|
|
|
Node = erlang:atom_to_binary(node(Pid), latin1),
|
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
?MYNAME,
|
2017-04-14 12:57:52 +02:00
|
|
|
?SQL("delete from route where domain=%(Domain)s "
|
|
|
|
"and pid=%(PidS)s and node=%(Node)s")) of
|
|
|
|
{updated, _} ->
|
|
|
|
ok;
|
2017-03-28 15:31:37 +02:00
|
|
|
Err ->
|
2017-04-14 12:57:52 +02:00
|
|
|
?ERROR_MSG("failed to delete from 'route' table: ~p", [Err]),
|
|
|
|
{error, db_failure}
|
2017-03-28 15:31:37 +02:00
|
|
|
end.
|
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
find_routes(Domain) ->
|
2017-03-28 15:31:37 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
?MYNAME,
|
2017-04-14 12:57:52 +02:00
|
|
|
?SQL("select @(server_host)s, @(node)s, @(pid)s, @(local_hint)s "
|
|
|
|
"from route where domain=%(Domain)s")) of
|
|
|
|
{selected, Rows} ->
|
|
|
|
{ok, lists:flatmap(
|
|
|
|
fun(Row) ->
|
|
|
|
row_to_route(Domain, Row)
|
|
|
|
end, Rows)};
|
2017-03-28 15:31:37 +02:00
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("failed to select from 'route' table: ~p", [Err]),
|
2017-04-14 12:57:52 +02:00
|
|
|
{error, db_failure}
|
2017-03-28 15:31:37 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_all_routes() ->
|
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
?MYNAME,
|
|
|
|
?SQL("select @(domain)s from route where domain <> server_host")) of
|
|
|
|
{selected, Domains} ->
|
2017-04-14 12:57:52 +02:00
|
|
|
{ok, [Domain || {Domain} <- Domains]};
|
2017-03-28 18:34:04 +02:00
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("failed to select from 'route' table: ~p", [Err]),
|
2017-04-14 12:57:52 +02:00
|
|
|
{error, db_failure}
|
2017-03-28 18:34:04 +02:00
|
|
|
end.
|
|
|
|
|
2017-03-28 15:31:37 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
enc_local_hint(undefined) ->
|
|
|
|
<<"">>;
|
|
|
|
enc_local_hint(LocalHint) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
misc:term_to_expr(LocalHint).
|
2017-03-28 15:31:37 +02:00
|
|
|
|
|
|
|
dec_local_hint(<<"">>) ->
|
|
|
|
undefined;
|
|
|
|
dec_local_hint(S) ->
|
|
|
|
ejabberd_sql:decode_term(S).
|
|
|
|
|
|
|
|
row_to_route(Domain, {ServerHost, NodeS, PidS, LocalHintS} = Row) ->
|
|
|
|
try [#route{domain = Domain,
|
|
|
|
server_host = ServerHost,
|
2017-04-11 12:13:58 +02:00
|
|
|
pid = misc:decode_pid(PidS, NodeS),
|
2017-03-28 15:31:37 +02:00
|
|
|
local_hint = dec_local_hint(LocalHintS)}]
|
2017-03-30 16:51:37 +02:00
|
|
|
catch _:{bad_node, _} ->
|
2017-03-28 15:31:37 +02:00
|
|
|
[];
|
|
|
|
E:R ->
|
|
|
|
?ERROR_MSG("failed to decode row from 'route' table:~n"
|
|
|
|
"Row = ~p~n"
|
|
|
|
"Domain = ~s~n"
|
|
|
|
"Reason = ~p",
|
|
|
|
[Row, Domain, {E, {R, erlang:get_stacktrace()}}]),
|
|
|
|
[]
|
|
|
|
end.
|