2003-11-10 22:25:36 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_receiver.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-11-10 22:25:36 +01:00
|
|
|
%%% Purpose : Socket receiver for C2S and S2S connections
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 10 Nov 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2015-01-08 17:34:43 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2015 ProcessOne
|
2007-12-24 12:41:41 +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 12:41:41 +01:00
|
|
|
%%%
|
2003-11-10 22:25:36 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_receiver).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
2006-09-05 06:26:28 +02:00
|
|
|
-export([start_link/4,
|
2006-01-29 05:38:31 +01:00
|
|
|
start/3,
|
2006-04-22 05:35:13 +02:00
|
|
|
start/4,
|
2003-11-10 22:25:36 +01:00
|
|
|
change_shaper/2,
|
2004-08-01 22:12:20 +02:00
|
|
|
reset_stream/1,
|
2006-01-13 02:55:20 +01:00
|
|
|
starttls/2,
|
2006-01-19 03:17:31 +01:00
|
|
|
compress/2,
|
2006-09-05 06:26:28 +02:00
|
|
|
become_controller/2,
|
2006-01-13 02:55:20 +01:00
|
|
|
close/1]).
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
%% gen_server callbacks
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
|
|
|
handle_info/2, terminate/2, code_change/3]).
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(state,
|
2013-06-20 10:40:44 +02:00
|
|
|
{socket :: inet:socket() | p1_tls:tls_socket() | ezlib:zlib_socket(),
|
|
|
|
sock_mod = gen_tcp :: gen_tcp | p1_tls | ezlib,
|
2013-03-14 10:33:02 +01:00
|
|
|
shaper_state = none :: shaper:shaper(),
|
|
|
|
c2s_pid :: pid(),
|
|
|
|
max_stanza_size = infinity :: non_neg_integer() | infinity,
|
|
|
|
xml_stream_state :: xml_stream:xml_stream_state(),
|
|
|
|
timeout = infinity:: timeout()}).
|
2006-01-13 02:55:20 +01:00
|
|
|
|
2008-11-23 10:43:52 +01:00
|
|
|
-define(HIBERNATE_TIMEOUT, 90000).
|
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
2006-01-29 05:38:31 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec start_link(inet:socket(), atom(), shaper:shaper(),
|
|
|
|
non_neg_integer() | infinity) -> ignore |
|
|
|
|
{error, any()} |
|
|
|
|
{ok, pid()}.
|
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
start_link(Socket, SockMod, Shaper, MaxStanzaSize) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_server:start_link(?MODULE,
|
|
|
|
[Socket, SockMod, Shaper, MaxStanzaSize], []).
|
|
|
|
|
|
|
|
-spec start(inet:socket(), atom(), shaper:shaper()) -> undefined | pid().
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
2003-11-10 22:25:36 +01:00
|
|
|
start(Socket, SockMod, Shaper) ->
|
2006-04-22 05:35:13 +02:00
|
|
|
start(Socket, SockMod, Shaper, infinity).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec start(inet:socket(), atom(), shaper:shaper(),
|
|
|
|
non_neg_integer() | infinity) -> undefined | pid().
|
|
|
|
|
2006-04-22 05:35:13 +02:00
|
|
|
start(Socket, SockMod, Shaper, MaxStanzaSize) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Pid} =
|
|
|
|
supervisor:start_child(ejabberd_receiver_sup,
|
|
|
|
[Socket, SockMod, Shaper, MaxStanzaSize]),
|
2006-01-13 02:55:20 +01:00
|
|
|
Pid.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec change_shaper(pid(), shaper:shaper()) -> ok.
|
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
change_shaper(Pid, Shaper) ->
|
|
|
|
gen_server:cast(Pid, {change_shaper, Shaper}).
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec reset_stream(pid()) -> ok | {error, any()}.
|
|
|
|
|
|
|
|
reset_stream(Pid) -> do_call(Pid, reset_stream).
|
|
|
|
|
2013-06-24 12:04:56 +02:00
|
|
|
-spec starttls(pid(), p1_tls:tls_socket()) -> ok.
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
starttls(Pid, TLSSocket) ->
|
2012-02-14 08:03:08 +01:00
|
|
|
do_call(Pid, {starttls, TLSSocket}).
|
2006-01-13 02:55:20 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec compress(pid(), iodata() | undefined) -> {error, any()} |
|
2013-04-08 11:12:54 +02:00
|
|
|
{ok, ezlib:zlib_socket()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
compress(Pid, Data) ->
|
|
|
|
do_call(Pid, {compress, Data}).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec become_controller(pid(), pid()) -> ok | {error, any()}.
|
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
become_controller(Pid, C2SPid) ->
|
2012-02-14 08:03:08 +01:00
|
|
|
do_call(Pid, {become_controller, C2SPid}).
|
2006-01-13 02:55:20 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec close(pid()) -> ok.
|
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
close(Pid) ->
|
|
|
|
gen_server:cast(Pid, close).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
2006-09-05 06:26:28 +02:00
|
|
|
init([Socket, SockMod, Shaper, MaxStanzaSize]) ->
|
2003-11-10 22:25:36 +01:00
|
|
|
ShaperState = shaper:new(Shaper),
|
|
|
|
Timeout = case SockMod of
|
2013-03-14 10:33:02 +01:00
|
|
|
ssl -> 20;
|
|
|
|
_ -> infinity
|
2003-11-10 22:25:36 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok,
|
|
|
|
#state{socket = Socket, sock_mod = SockMod,
|
|
|
|
shaper_state = ShaperState,
|
|
|
|
max_stanza_size = MaxStanzaSize, timeout = Timeout}}.
|
2006-01-13 02:55:20 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
|
|
|
|
%% {reply, Reply, State, Timeout} |
|
|
|
|
%% {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, Reply, State} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling call messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_call({starttls, TLSSocket}, _From,
|
|
|
|
#state{xml_stream_state = XMLStreamState,
|
2006-04-22 05:35:13 +02:00
|
|
|
c2s_pid = C2SPid,
|
|
|
|
max_stanza_size = MaxStanzaSize} = State) ->
|
2008-02-11 16:36:56 +01:00
|
|
|
close_stream(XMLStreamState),
|
2013-03-14 10:33:02 +01:00
|
|
|
NewXMLStreamState = xml_stream:new(C2SPid,
|
|
|
|
MaxStanzaSize),
|
2006-01-13 02:55:20 +01:00
|
|
|
NewState = State#state{socket = TLSSocket,
|
2013-06-20 10:40:44 +02:00
|
|
|
sock_mod = p1_tls,
|
2006-01-13 02:55:20 +01:00
|
|
|
xml_stream_state = NewXMLStreamState},
|
2013-06-20 10:40:44 +02:00
|
|
|
case p1_tls:recv_data(TLSSocket, <<"">>) of
|
2006-01-13 02:55:20 +01:00
|
|
|
{ok, TLSData} ->
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, ok, process_data(TLSData, NewState), ?HIBERNATE_TIMEOUT};
|
2006-01-13 02:55:20 +01:00
|
|
|
{error, _Reason} ->
|
|
|
|
{stop, normal, ok, NewState}
|
|
|
|
end;
|
2013-04-08 11:12:54 +02:00
|
|
|
handle_call({compress, Data}, _From,
|
2006-01-19 03:17:31 +01:00
|
|
|
#state{xml_stream_state = XMLStreamState,
|
2013-04-08 11:12:54 +02:00
|
|
|
c2s_pid = C2SPid, socket = Socket, sock_mod = SockMod,
|
|
|
|
max_stanza_size = MaxStanzaSize} =
|
|
|
|
State) ->
|
|
|
|
{ok, ZlibSocket} = ezlib:enable_zlib(SockMod,
|
|
|
|
Socket),
|
|
|
|
if Data /= undefined -> do_send(State, Data);
|
|
|
|
true -> ok
|
|
|
|
end,
|
2008-02-11 16:36:56 +01:00
|
|
|
close_stream(XMLStreamState),
|
2013-03-14 10:33:02 +01:00
|
|
|
NewXMLStreamState = xml_stream:new(C2SPid,
|
|
|
|
MaxStanzaSize),
|
2006-01-19 03:17:31 +01:00
|
|
|
NewState = State#state{socket = ZlibSocket,
|
2013-04-08 11:12:54 +02:00
|
|
|
sock_mod = ezlib,
|
2006-01-19 03:17:31 +01:00
|
|
|
xml_stream_state = NewXMLStreamState},
|
2013-04-08 11:12:54 +02:00
|
|
|
case ezlib:recv_data(ZlibSocket, <<"">>) of
|
|
|
|
{ok, ZlibData} ->
|
|
|
|
{reply, {ok, ZlibSocket},
|
|
|
|
process_data(ZlibData, NewState), ?HIBERNATE_TIMEOUT};
|
|
|
|
{error, _Reason} -> {stop, normal, ok, NewState}
|
2006-01-19 03:17:31 +01:00
|
|
|
end;
|
2006-01-13 02:55:20 +01:00
|
|
|
handle_call(reset_stream, _From,
|
|
|
|
#state{xml_stream_state = XMLStreamState,
|
2013-03-14 10:33:02 +01:00
|
|
|
c2s_pid = C2SPid, max_stanza_size = MaxStanzaSize} =
|
|
|
|
State) ->
|
2008-02-11 16:36:56 +01:00
|
|
|
close_stream(XMLStreamState),
|
2013-03-14 10:33:02 +01:00
|
|
|
NewXMLStreamState = xml_stream:new(C2SPid,
|
|
|
|
MaxStanzaSize),
|
2006-01-13 02:55:20 +01:00
|
|
|
Reply = ok,
|
2013-03-14 10:33:02 +01:00
|
|
|
{reply, Reply,
|
|
|
|
State#state{xml_stream_state = NewXMLStreamState},
|
2008-11-23 10:43:52 +01:00
|
|
|
?HIBERNATE_TIMEOUT};
|
2006-09-05 06:26:28 +02:00
|
|
|
handle_call({become_controller, C2SPid}, _From, State) ->
|
|
|
|
XMLStreamState = xml_stream:new(C2SPid, State#state.max_stanza_size),
|
|
|
|
NewState = State#state{c2s_pid = C2SPid,
|
|
|
|
xml_stream_state = XMLStreamState},
|
|
|
|
activate_socket(NewState),
|
2006-01-13 02:55:20 +01:00
|
|
|
Reply = ok,
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, Reply, NewState, ?HIBERNATE_TIMEOUT};
|
2006-01-13 02:55:20 +01:00
|
|
|
handle_call(_Request, _From, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Reply = ok, {reply, Reply, State, ?HIBERNATE_TIMEOUT}.
|
2006-01-13 02:55:20 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_cast({change_shaper, Shaper}, State) ->
|
|
|
|
NewShaperState = shaper:new(Shaper),
|
2013-03-14 10:33:02 +01:00
|
|
|
{noreply, State#state{shaper_state = NewShaperState},
|
|
|
|
?HIBERNATE_TIMEOUT};
|
|
|
|
handle_cast(close, State) -> {stop, normal, State};
|
2006-01-13 02:55:20 +01:00
|
|
|
handle_cast(_Msg, State) ->
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, State, ?HIBERNATE_TIMEOUT}.
|
2006-01-13 02:55:20 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_info({Tag, _TCPSocket, Data},
|
2013-03-14 10:33:02 +01:00
|
|
|
#state{socket = Socket, sock_mod = SockMod} = State)
|
|
|
|
when (Tag == tcp) or (Tag == ssl) or
|
|
|
|
(Tag == ejabberd_xml) ->
|
2006-01-13 02:55:20 +01:00
|
|
|
case SockMod of
|
2013-06-20 10:40:44 +02:00
|
|
|
p1_tls ->
|
|
|
|
case p1_tls:recv_data(Socket, Data) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, TLSData} ->
|
|
|
|
{noreply, process_data(TLSData, State),
|
|
|
|
?HIBERNATE_TIMEOUT};
|
2015-03-03 11:32:04 +01:00
|
|
|
{error, Reason} ->
|
|
|
|
if is_binary(Reason) ->
|
|
|
|
?ERROR_MSG("TLS error = ~s", [Reason]);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{stop, normal, State}
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
2013-04-08 11:12:54 +02:00
|
|
|
ezlib ->
|
|
|
|
case ezlib:recv_data(Socket, Data) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, ZlibData} ->
|
|
|
|
{noreply, process_data(ZlibData, State),
|
|
|
|
?HIBERNATE_TIMEOUT};
|
|
|
|
{error, _Reason} -> {stop, normal, State}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{noreply, process_data(Data, State), ?HIBERNATE_TIMEOUT}
|
2006-01-13 02:55:20 +01:00
|
|
|
end;
|
|
|
|
handle_info({Tag, _TCPSocket}, State)
|
2013-03-14 10:33:02 +01:00
|
|
|
when (Tag == tcp_closed) or (Tag == ssl_closed) ->
|
2006-01-13 02:55:20 +01:00
|
|
|
{stop, normal, State};
|
|
|
|
handle_info({Tag, _TCPSocket, Reason}, State)
|
2013-03-14 10:33:02 +01:00
|
|
|
when (Tag == tcp_error) or (Tag == ssl_error) ->
|
2006-01-13 02:55:20 +01:00
|
|
|
case Reason of
|
2013-03-14 10:33:02 +01:00
|
|
|
timeout -> {noreply, State, ?HIBERNATE_TIMEOUT};
|
|
|
|
_ -> {stop, normal, State}
|
2006-01-13 02:55:20 +01:00
|
|
|
end;
|
|
|
|
handle_info({timeout, _Ref, activate}, State) ->
|
|
|
|
activate_socket(State),
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, State, ?HIBERNATE_TIMEOUT};
|
|
|
|
handle_info(timeout, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
proc_lib:hibernate(gen_server, enter_loop,
|
|
|
|
[?MODULE, [], State]),
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, State, ?HIBERNATE_TIMEOUT};
|
2006-01-13 02:55:20 +01:00
|
|
|
handle_info(_Info, State) ->
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, State, ?HIBERNATE_TIMEOUT}.
|
2006-01-13 02:55:20 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: terminate(Reason, State) -> void()
|
|
|
|
%% Description: This function is called by a gen_server when it is about to
|
|
|
|
%% terminate. It should be the opposite of Module:init/1 and do any necessary
|
|
|
|
%% cleaning up. When it returns, the gen_server terminates with Reason.
|
|
|
|
%% The return value is ignored.
|
|
|
|
%%--------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
terminate(_Reason,
|
|
|
|
#state{xml_stream_state = XMLStreamState,
|
|
|
|
c2s_pid = C2SPid} =
|
|
|
|
State) ->
|
2008-02-11 16:36:56 +01:00
|
|
|
close_stream(XMLStreamState),
|
2013-03-14 10:33:02 +01:00
|
|
|
if C2SPid /= undefined ->
|
|
|
|
gen_fsm:send_event(C2SPid, closed);
|
|
|
|
true -> ok
|
2006-12-08 21:48:26 +01:00
|
|
|
end,
|
2006-01-13 02:55:20 +01:00
|
|
|
catch (State#state.sock_mod):close(State#state.socket),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
|
|
|
|
%% Description: Convert process state when code is changed
|
|
|
|
%%--------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2006-01-13 02:55:20 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
|
|
activate_socket(#state{socket = Socket,
|
2008-07-16 14:55:29 +02:00
|
|
|
sock_mod = SockMod}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
PeerName = case SockMod of
|
|
|
|
gen_tcp ->
|
|
|
|
inet:setopts(Socket, [{active, once}]),
|
|
|
|
inet:peername(Socket);
|
|
|
|
_ ->
|
|
|
|
SockMod:setopts(Socket, [{active, once}]),
|
|
|
|
SockMod:peername(Socket)
|
|
|
|
end,
|
2007-05-07 19:52:35 +02:00
|
|
|
case PeerName of
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, _Reason} -> self() ! {tcp_closed, Socket};
|
|
|
|
{ok, _} -> ok
|
2003-11-10 22:25:36 +01:00
|
|
|
end.
|
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
%% Data processing for connectors directly generating xmlelement in
|
|
|
|
%% Erlang data structure.
|
|
|
|
%% WARNING: Shaper does not work with Erlang data structure.
|
|
|
|
process_data([], State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
activate_socket(State), State;
|
|
|
|
process_data([Element | Els],
|
|
|
|
#state{c2s_pid = C2SPid} = State)
|
|
|
|
when element(1, Element) == xmlel;
|
|
|
|
element(1, Element) == xmlstreamstart;
|
|
|
|
element(1, Element) == xmlstreamelement;
|
|
|
|
element(1, Element) == xmlstreamend ->
|
|
|
|
if C2SPid == undefined -> State;
|
|
|
|
true ->
|
|
|
|
catch gen_fsm:send_event(C2SPid,
|
|
|
|
element_wrapper(Element)),
|
|
|
|
process_data(Els, State)
|
2009-08-31 20:37:52 +02:00
|
|
|
end;
|
|
|
|
%% Data processing for connectors receivind data as string.
|
2006-01-13 02:55:20 +01:00
|
|
|
process_data(Data,
|
|
|
|
#state{xml_stream_state = XMLStreamState,
|
2013-03-14 10:33:02 +01:00
|
|
|
shaper_state = ShaperState, c2s_pid = C2SPid} =
|
|
|
|
State) ->
|
|
|
|
?DEBUG("Received XML on stream = ~p", [(Data)]),
|
2006-01-13 02:55:20 +01:00
|
|
|
XMLStreamState1 = xml_stream:parse(XMLStreamState, Data),
|
2013-03-14 10:33:02 +01:00
|
|
|
{NewShaperState, Pause} = shaper:update(ShaperState, byte_size(Data)),
|
2006-01-13 02:55:20 +01:00
|
|
|
if
|
2008-05-04 09:38:24 +02:00
|
|
|
C2SPid == undefined ->
|
|
|
|
ok;
|
2006-01-13 02:55:20 +01:00
|
|
|
Pause > 0 ->
|
|
|
|
erlang:start_timer(Pause, self(), activate);
|
|
|
|
true ->
|
|
|
|
activate_socket(State)
|
|
|
|
end,
|
|
|
|
State#state{xml_stream_state = XMLStreamState1,
|
|
|
|
shaper_state = NewShaperState}.
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
%% Element coming from XML parser are wrapped inside xmlstreamelement
|
|
|
|
%% When we receive directly xmlelement tuple (from a socket module
|
|
|
|
%% speaking directly Erlang XML), we wrap it inside the same
|
|
|
|
%% xmlstreamelement coming from the XML parser.
|
|
|
|
element_wrapper(XMLElement)
|
2013-03-14 10:33:02 +01:00
|
|
|
when element(1, XMLElement) == xmlel ->
|
2009-08-31 20:37:52 +02:00
|
|
|
{xmlstreamelement, XMLElement};
|
2013-03-14 10:33:02 +01:00
|
|
|
element_wrapper(Element) -> Element.
|
2009-08-31 20:37:52 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
close_stream(undefined) -> ok;
|
2008-02-11 16:36:56 +01:00
|
|
|
close_stream(XMLStreamState) ->
|
|
|
|
xml_stream:close(XMLStreamState).
|
2012-02-14 08:03:08 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
do_send(State, Data) ->
|
|
|
|
(State#state.sock_mod):send(State#state.socket, Data).
|
|
|
|
|
2012-02-14 08:03:08 +01:00
|
|
|
do_call(Pid, Msg) ->
|
|
|
|
case catch gen_server:call(Pid, Msg) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', Why} -> {error, Why};
|
|
|
|
Res -> Res
|
2012-02-14 08:03:08 +01:00
|
|
|
end.
|