首页 > 其他 > 详细

ejabberd_iq_sup

时间:2015-12-18 18:23:54      阅读:255      评论:0      收藏:0      [点我收藏+]

ejabberd_iq_sup作为supervisor启动,子程序启动的模块为gen_iq_handler

gen_iq_handler:

根据配置参数iqdisc,可选项为no_queue, one_queue, {quques, N}和parallel

技术分享
add_iq_handler(Component, Host, NS, Module, Function,
           Type) ->
    case Type of
    no_queue ->
        Component:register_iq_handler(Host, NS, Module,
        Function, no_queue);
    one_queue ->
        {ok, Pid} = supervisor:start_child(ejabberd_iq_sup,
            [Host, Module, Function]),
        Component:register_iq_handler(Host, NS, Module,
        Function, {one_queue, Pid});
    N when is_integer(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});
    parallel ->
        Component:register_iq_handler(Host, NS, Module,
        Function, parallel)
    end.
View Code

add_iq_handler根据选项可分为四种启动方式,这是根据IQ的业务性质决定的

因为IQ业务的处理时间不同,有的耗时,有的不耗时,所以有的直接执行,有的另起进程

具体操作为handle函数

技术分享
handle(Host, Module, Function, Opts, From, To, IQ) ->
    case Opts of
    no_queue ->
        process_iq(Host, Module, Function, From, To, IQ);
    {one_queue, Pid} ->
        Pid ! {process_iq, From, To, IQ};
    {queues, Pids} ->
        Pid = lists:nth(erlang:phash(now(), length(Pids)), Pids),
        Pid ! {process_iq, From, To, IQ};
    parallel ->
        spawn(?MODULE, process_iq,
        [Host, Module, Function, From, To, IQ]);
    _ -> todo
    end.
View Code

gen_iq_handler的域分为 ejabberd_sm | ejabberd_local

-type component() :: ejabberd_sm | ejabberd_local.

意思为只向ejabberd_sm 、ejabberd_local注册

这样则主业务逻辑和模块分开,主业务逻辑只需调用gen_iq_handler提供的接口,所有的操作细节则有gen_iq_handler处理。

所以gen_iq_handler屏蔽了各个模块的差异

ejabberd_iq_sup

原文:http://www.cnblogs.com/lawen/p/5057556.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!