diff --git a/sql/lite.sql b/sql/lite.sql index bc6a6e706..c17d80ec5 100644 --- a/sql/lite.sql +++ b/sql/lite.sql @@ -330,3 +330,11 @@ CREATE TABLE route ( CREATE UNIQUE INDEX i_route ON route(domain, server_host, node, pid); CREATE INDEX i_route_domain ON route(domain); + +CREATE TABLE bosh ( + sid text NOT NULL, + node text NOT NULL, + pid text NOT NULL +); + +CREATE UNIQUE INDEX i_bosh_sid ON bosh(sid); diff --git a/sql/mssql.sql b/sql/mssql.sql index 06f73aea9..7535da5ac 100644 --- a/sql/mssql.sql +++ b/sql/mssql.sql @@ -504,3 +504,13 @@ WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW CREATE INDEX [route_domain] ON [route] (domain) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON); + +CREATE TABLE [dbo].[bosh] ( + [sid] [varchar] (255) NOT NULL, + [node] [varchar] (255) NOT NULL, + [pid] [varchar](100) NOT NULL + CONSTRAINT [bosh_PRIMARY] PRIMARY KEY CLUSTERED +( + [sid] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) +) TEXTIMAGE_ON [PRIMARY]; diff --git a/sql/mysql.sql b/sql/mysql.sql index c4f3d1f02..982df7ff5 100644 --- a/sql/mysql.sql +++ b/sql/mysql.sql @@ -346,3 +346,11 @@ CREATE TABLE route ( CREATE UNIQUE INDEX i_route ON route(domain(75), server_host(75), node(75), pid(75)); CREATE INDEX i_route_domain ON route(domain(75)); + +CREATE TABLE bosh ( + sid text NOT NULL, + node text NOT NULL, + pid text NOT NULL +) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +CREATE UNIQUE INDEX i_bosh_sid ON bosh(sid(75)); diff --git a/sql/pg.sql b/sql/pg.sql index fac806e8a..8a8b77b1f 100644 --- a/sql/pg.sql +++ b/sql/pg.sql @@ -350,3 +350,11 @@ CREATE TABLE route ( CREATE UNIQUE INDEX i_route ON route USING btree (domain, server_host, node, pid); CREATE INDEX i_route_domain ON route USING btree (domain); + +CREATE TABLE bosh ( + sid text NOT NULL, + node text NOT NULL, + pid text NOT NULL +); + +CREATE UNIQUE INDEX i_bosh_sid ON bosh USING btree (sid); diff --git a/src/mod_bosh_sql.erl b/src/mod_bosh_sql.erl new file mode 100644 index 000000000..05df4dc73 --- /dev/null +++ b/src/mod_bosh_sql.erl @@ -0,0 +1,98 @@ +%%%------------------------------------------------------------------- +%%% @author Evgeny Khramtsov +%%% @copyright (C) 2017, Evgeny Khramtsov +%%% @doc +%%% +%%% @end +%%% Created : 28 Mar 2017 by Evgeny Khramtsov +%%%------------------------------------------------------------------- +-module(mod_bosh_sql). +-behaviour(mod_bosh). + +-compile([{parse_transform, ejabberd_sql_pt}]). + +%% API +-export([init/0, open_session/2, close_session/1, find_session/1]). + +-include("ejabberd.hrl"). +-include("logger.hrl"). +-include("ejabberd_sql_pt.hrl"). + +%%%=================================================================== +%%% API +%%%=================================================================== +init() -> + Node = erlang:atom_to_binary(node(), latin1), + ?INFO_MSG("Cleaning SQL 'bosh' table...", []), + case ejabberd_sql:sql_query( + ?MYNAME, ?SQL("delete from bosh where node=%(Node)s")) of + {updated, _} -> + ok; + Err -> + ?ERROR_MSG("failed to clean 'route' table: ~p", [Err]), + Err + end. + +open_session(SID, Pid) -> + PidS = enc_pid(Pid), + Node = erlang:atom_to_binary(node(Pid), latin1), + case ?SQL_UPSERT(?MYNAME, "bosh", + ["!sid=%(SID)s", + "node=%(Node)s", + "pid=%(PidS)s"]) of + ok -> + ok; + Err -> + ?ERROR_MSG("failed to update 'bosh' table: ~p", [Err]), + {error, Err} + end. + +close_session(SID) -> + %% TODO: report errors + ejabberd_sql:sql_query( + ?MYNAME, ?SQL("delete from bosh where sid=%(SID)s")). + +find_session(SID) -> + case ejabberd_sql:sql_query( + ?MYNAME, + ?SQL("select @(pid)s, @(node)s from bosh where sid=%(SID)s")) of + {selected, [{Pid, Node}]} -> + try {ok, dec_pid(Pid, Node)} + catch _:{node_down, _} -> error + end; + {selected, []} -> + error; + Err -> + ?ERROR_MSG("failed to select 'bosh' table: ~p", [Err]), + error + end. + +%%%=================================================================== +%%% 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], ".")).