2006-10-28 04:04:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_proxy65_sm.erl
|
|
|
|
%%% Author : Evgeniy Khramtsov <xram@jabber.ru>
|
|
|
|
%%% Purpose : Bytestreams manager.
|
|
|
|
%%% Created : 12 Oct 2006 by Evgeniy Khramtsov <xram@jabber.ru>
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%% 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.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% 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.
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
2006-10-28 04:04:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_proxy65_sm).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
-author('xram@jabber.ru').
|
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% gen_server callbacks.
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_info/2, handle_call/3,
|
|
|
|
handle_cast/2, terminate/2, code_change/3]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start_link/2, register_stream/1,
|
2015-06-03 15:05:17 +02:00
|
|
|
unregister_stream/1, activate_stream/4]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-record(state, {max_connections = infinity :: non_neg_integer() | infinity}).
|
|
|
|
|
|
|
|
-include("jlib.hrl").
|
|
|
|
|
|
|
|
-record(bytestream,
|
|
|
|
{sha1 = <<"">> :: binary() | '$1',
|
|
|
|
target :: pid() | '_',
|
|
|
|
initiator :: pid() | '_',
|
|
|
|
active = false :: boolean() | '_',
|
|
|
|
jid_i = {<<"">>, <<"">>, <<"">>} :: ljid() | '_'}).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
-define(PROCNAME, ejabberd_mod_proxy65_sm).
|
|
|
|
|
|
|
|
%% Unused callbacks.
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_cast(_Request, State) -> {noreply, State}.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
|
|
|
|
|
|
|
handle_info(_Info, State) -> {noreply, State}.
|
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
%%----------------
|
|
|
|
|
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_server:start_link({local, Proc}, ?MODULE, [Opts],
|
|
|
|
[]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
init([Opts]) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
mnesia:create_table(bytestream,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, bytestream)}]),
|
2007-02-22 06:03:51 +01:00
|
|
|
mnesia:add_table_copy(bytestream, node(), ram_copies),
|
2013-03-14 10:33:02 +01:00
|
|
|
MaxConnections = gen_mod:get_opt(max_connections, Opts,
|
|
|
|
fun(I) when is_integer(I), I>0 ->
|
|
|
|
I;
|
|
|
|
(infinity) ->
|
|
|
|
infinity
|
|
|
|
end, infinity),
|
|
|
|
{ok, #state{max_connections = MaxConnections}}.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
terminate(_Reason, _State) -> ok.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
handle_call({activate, SHA1, IJid}, _From, State) ->
|
|
|
|
MaxConns = State#state.max_connections,
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2006-10-28 04:04:55 +02:00
|
|
|
case mnesia:read(bytestream, SHA1, write) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[#bytestream{target = TPid, initiator = IPid} =
|
|
|
|
ByteStream]
|
|
|
|
when is_pid(TPid), is_pid(IPid) ->
|
|
|
|
ActiveFlag = ByteStream#bytestream.active,
|
|
|
|
if ActiveFlag == false ->
|
|
|
|
ConnsPerJID = mnesia:select(bytestream,
|
|
|
|
[{#bytestream{sha1 =
|
|
|
|
'$1',
|
|
|
|
jid_i =
|
|
|
|
IJid,
|
|
|
|
_ = '_'},
|
|
|
|
[], ['$1']}]),
|
|
|
|
if length(ConnsPerJID) < MaxConns ->
|
|
|
|
mnesia:write(ByteStream#bytestream{active =
|
|
|
|
true,
|
|
|
|
jid_i =
|
|
|
|
IJid}),
|
|
|
|
{ok, IPid, TPid};
|
|
|
|
true -> {limit, IPid, TPid}
|
|
|
|
end;
|
|
|
|
true -> conflict
|
|
|
|
end;
|
|
|
|
_ -> false
|
2006-10-28 04:04:55 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
Reply = mnesia:transaction(F),
|
|
|
|
{reply, Reply, State};
|
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
{reply, ok, State}.
|
|
|
|
|
|
|
|
%%%----------------------
|
|
|
|
%%% API.
|
|
|
|
%%%----------------------
|
|
|
|
%%%---------------------------------------------------
|
|
|
|
%%% register_stream(SHA1) -> {atomic, ok} |
|
|
|
|
%%% {atomic, error} |
|
|
|
|
%%% transaction abort
|
|
|
|
%%% SHA1 = string()
|
|
|
|
%%%---------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
register_stream(SHA1) when is_binary(SHA1) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
StreamPid = self(),
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2006-10-28 04:04:55 +02:00
|
|
|
case mnesia:read(bytestream, SHA1, write) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] ->
|
|
|
|
mnesia:write(#bytestream{sha1 = SHA1,
|
|
|
|
target = StreamPid});
|
|
|
|
[#bytestream{target = Pid, initiator = undefined} =
|
|
|
|
ByteStream]
|
|
|
|
when is_pid(Pid), Pid /= StreamPid ->
|
|
|
|
mnesia:write(ByteStream#bytestream{initiator =
|
|
|
|
StreamPid});
|
|
|
|
_ -> error
|
2006-10-28 04:04:55 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
%%%----------------------------------------------------
|
|
|
|
%%% unregister_stream(SHA1) -> ok | transaction abort
|
|
|
|
%%% SHA1 = string()
|
|
|
|
%%%----------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
unregister_stream(SHA1) when is_binary(SHA1) ->
|
|
|
|
F = fun () -> mnesia:delete({bytestream, SHA1}) end,
|
2006-10-28 04:04:55 +02:00
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
%%%--------------------------------------------------------
|
|
|
|
%%% activate_stream(SHA1, IJid, TJid, Host) -> ok |
|
|
|
|
%%% false |
|
|
|
|
%%% limit |
|
|
|
|
%%% conflict |
|
|
|
|
%%% error
|
|
|
|
%%% SHA1 = string()
|
|
|
|
%%% IJid = TJid = jid()
|
|
|
|
%%% Host = string()
|
|
|
|
%%%--------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
activate_stream(SHA1, IJid, TJid, Host)
|
|
|
|
when is_binary(SHA1) ->
|
2006-10-28 04:04:55 +02:00
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch gen_server:call(Proc, {activate, SHA1, IJid})
|
|
|
|
of
|
|
|
|
{atomic, {ok, IPid, TPid}} ->
|
|
|
|
mod_proxy65_stream:activate({IPid, IJid}, {TPid, TJid});
|
|
|
|
{atomic, {limit, IPid, TPid}} ->
|
|
|
|
mod_proxy65_stream:stop(IPid),
|
|
|
|
mod_proxy65_stream:stop(TPid),
|
|
|
|
limit;
|
|
|
|
{atomic, conflict} -> conflict;
|
|
|
|
{atomic, false} -> false;
|
|
|
|
_ -> error
|
2006-10-28 04:04:55 +02:00
|
|
|
end.
|