aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeniy Khramtsov <ekhramtsov@process-one.net>2017-03-28 20:33:57 +0300
committerEvgeniy Khramtsov <ekhramtsov@process-one.net>2017-03-28 20:33:57 +0300
commite5815553cbaa0a2ca829a6b7fdb799ee820fa967 (patch)
tree7ff3b0408ba05f9c14cd842578aa81f27b1de8cf
parentAdd Redis as router RAM backend (diff)
Add SQL as mod_bosh RAM backend
-rw-r--r--sql/lite.sql8
-rw-r--r--sql/mssql.sql10
-rw-r--r--sql/mysql.sql8
-rw-r--r--sql/pg.sql8
-rw-r--r--src/mod_bosh_sql.erl98
5 files changed, 132 insertions, 0 deletions
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 <ekhramtsov@process-one.net>
+%%% @copyright (C) 2017, Evgeny Khramtsov
+%%% @doc
+%%%
+%%% @end
+%%% Created : 28 Mar 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
+%%%-------------------------------------------------------------------
+-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], ".")).