2003-01-22 21:40:40 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : gen_iq_handler.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2006-01-29 05:38:31 +01:00
|
|
|
%%% Purpose : IQ handler support
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 22 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2008-09-16 16:39:57 +02:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2008 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.
|
|
|
|
%%%
|
|
|
|
%%% 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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-01-22 21:40:40 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(gen_iq_handler).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([start_link/3,
|
2005-06-20 05:18:13 +02:00
|
|
|
add_iq_handler/6,
|
|
|
|
remove_iq_handler/3,
|
2003-01-29 18:12:23 +01:00
|
|
|
stop_iq_handler/3,
|
2005-06-20 05:18:13 +02:00
|
|
|
handle/7,
|
2006-01-29 05:38:31 +01:00
|
|
|
process_iq/6]).
|
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2008-07-01 12:21:35 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2003-01-22 21:40:40 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-record(state, {host,
|
|
|
|
module,
|
|
|
|
function}).
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
start_link(Host, Module, Function) ->
|
|
|
|
gen_server:start_link(?MODULE, [Host, Module, Function], []).
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
add_iq_handler(Component, Host, NS, Module, Function, Type) ->
|
2003-01-22 21:40:40 +01:00
|
|
|
case Type of
|
|
|
|
no_queue ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Component:register_iq_handler(Host, NS, Module, Function, no_queue);
|
2003-01-22 21:40:40 +01:00
|
|
|
one_queue ->
|
2003-07-20 22:35:35 +02:00
|
|
|
{ok, Pid} = supervisor:start_child(ejabberd_iq_sup,
|
2005-06-20 05:18:13 +02:00
|
|
|
[Host, Module, Function]),
|
|
|
|
Component:register_iq_handler(Host, NS, Module, Function,
|
2003-01-22 21:40:40 +01:00
|
|
|
{one_queue, Pid});
|
2006-03-14 05:26:15 +01:00
|
|
|
{queues, N} ->
|
|
|
|
Pids =
|
|
|
|
lists:map(
|
|
|
|
fun(_) ->
|
|
|
|
{ok, Pid} = supervisor:start_child(
|
|
|
|
ejabberd_iq_sup,
|
|
|
|
[Host, Module, Function]),
|
|
|
|
Pid
|
|
|
|
end, lists:seq(1, N)),
|
|
|
|
Component:register_iq_handler(Host, NS, Module, Function,
|
|
|
|
{queues, Pids});
|
2003-01-22 21:40:40 +01:00
|
|
|
parallel ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Component:register_iq_handler(Host, NS, Module, Function, parallel)
|
2003-01-22 21:40:40 +01:00
|
|
|
end.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
remove_iq_handler(Component, Host, NS) ->
|
2005-10-16 22:42:24 +02:00
|
|
|
Component:unregister_iq_handler(Host, NS).
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
stop_iq_handler(_Module, _Function, Opts) ->
|
2003-01-29 18:12:23 +01:00
|
|
|
case Opts of
|
|
|
|
{one_queue, Pid} ->
|
2006-01-29 05:38:31 +01:00
|
|
|
gen_server:call(Pid, stop);
|
2006-03-14 05:26:15 +01:00
|
|
|
{queues, Pids} ->
|
|
|
|
lists:foreach(fun(Pid) ->
|
|
|
|
catch gen_server:call(Pid, stop)
|
|
|
|
end, Pids);
|
2003-01-29 18:12:23 +01:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2008-08-14 15:34:30 +02:00
|
|
|
handle(Host, Module, Function, Opts, From, To, IQ_Rec) ->
|
2003-01-22 21:40:40 +01:00
|
|
|
case Opts of
|
|
|
|
no_queue ->
|
2008-08-14 15:34:30 +02:00
|
|
|
process_iq(Host, Module, Function, From, To, IQ_Rec);
|
2003-01-22 21:40:40 +01:00
|
|
|
{one_queue, Pid} ->
|
2008-08-14 15:34:30 +02:00
|
|
|
Pid ! {process_iq, From, To, IQ_Rec};
|
2006-03-14 05:26:15 +01:00
|
|
|
{queues, Pids} ->
|
|
|
|
Pid = lists:nth(erlang:phash(now(), length(Pids)), Pids),
|
2008-08-14 15:34:30 +02:00
|
|
|
Pid ! {process_iq, From, To, IQ_Rec};
|
2003-01-22 21:40:40 +01:00
|
|
|
parallel ->
|
2008-07-21 17:28:44 +02:00
|
|
|
spawn(?MODULE, process_iq,
|
2008-08-14 15:34:30 +02:00
|
|
|
[Host, Module, Function, From, To, IQ_Rec]);
|
2003-01-22 21:40:40 +01:00
|
|
|
_ ->
|
|
|
|
todo
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2008-08-14 15:34:30 +02:00
|
|
|
process_iq(_Host, Module, Function, From, To, IQ_Rec) ->
|
|
|
|
try Module:Function(From, To, IQ_Rec) of
|
2008-07-21 17:28:44 +02:00
|
|
|
ignore ->
|
|
|
|
ok;
|
2008-08-14 15:34:30 +02:00
|
|
|
ResIQ ->
|
|
|
|
Reply = exmpp_iq:iq_to_xmlel(ResIQ, To, From),
|
2008-07-21 17:28:44 +02:00
|
|
|
ejabberd_router:route(To, From, Reply)
|
2008-08-14 15:34:30 +02:00
|
|
|
catch
|
|
|
|
_Class:Reason ->
|
2008-12-05 16:08:03 +01:00
|
|
|
?ERROR_MSG("~s:~s/3 crashed: ~p~n~p~n",
|
|
|
|
[Module, Function, Reason, erlang:get_stacktrace()])
|
2003-01-22 21:40:40 +01:00
|
|
|
end.
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
2003-07-20 22:35:35 +02:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
init([Host, Module, Function]) ->
|
|
|
|
{ok, #state{host = Host,
|
|
|
|
module = Module,
|
|
|
|
function = Function}}.
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2006-01-29 05:38:31 +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(stop, _From, State) ->
|
|
|
|
Reply = ok,
|
|
|
|
{stop, normal, Reply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
2008-08-14 15:34:30 +02:00
|
|
|
handle_info({process_iq, From, To, IQ_Rec},
|
2006-01-29 05:38:31 +01:00
|
|
|
#state{host = Host,
|
|
|
|
module = Module,
|
|
|
|
function = Function} = State) ->
|
2008-08-14 15:34:30 +02:00
|
|
|
process_iq(Host, Module, Function, From, To, IQ_Rec),
|
2006-01-29 05:38:31 +01:00
|
|
|
{noreply, State};
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% 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.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
|
|
|
|
%% Description: Convert process state when code is changed
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|