diff options
author | Alexey Shchepin <alexey@process-one.net> | 2006-02-02 05:00:27 +0000 |
---|---|---|
committer | Alexey Shchepin <alexey@process-one.net> | 2006-02-02 05:00:27 +0000 |
commit | 971001e7aa04a4691356f4ae85e00526506c8a6f (patch) | |
tree | 5e1e99779531a4c0eb4953fe9cc08add9674d59d /src/mod_pubsub | |
parent | * src/odbc/pg.sql: Fixed syntax error (diff) |
* src/mod_pubsub/mod_pubsub.erl: Updated to use gen_server
behaviour and ejabberd supervision tree
* src/mod_muc/mod_muc.erl: Added a supervisor for conference room
processes
* src/mod_muc/mod_muc_room.erl: Likewise
SVN Revision: 496
Diffstat (limited to 'src/mod_pubsub')
-rw-r--r-- | src/mod_pubsub/mod_pubsub.erl | 203 |
1 files changed, 126 insertions, 77 deletions
diff --git a/src/mod_pubsub/mod_pubsub.erl b/src/mod_pubsub/mod_pubsub.erl index c957c9ac..7c3353a5 100644 --- a/src/mod_pubsub/mod_pubsub.erl +++ b/src/mod_pubsub/mod_pubsub.erl @@ -10,15 +10,13 @@ -author('alexey@sevcom.net'). -vsn('$Revision$ '). +-behaviour(gen_server). -behaviour(gen_mod). --export([start/2, - init/4, - loop/2, - stop/1, - system_continue/3, - system_terminate/4, - system_code_change/4]). +%% API +-export([start_link/2, + start/2, + stop/1]). -export([delete_item/3, set_entities/4, @@ -28,9 +26,15 @@ get_node_config/4, set_node_config/4]). +%% gen_server callbacks +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, + terminate/2, code_change/3]). + -include("ejabberd.hrl"). -include("jlib.hrl"). +-record(state, {host}). + -define(DICT, dict). -define(MAXITEMS, 20). -define(MAX_PAYLOAD_SIZE, 100000). @@ -45,60 +49,35 @@ -record(item, {id, publisher, payload}). -define(PROCNAME, ejabberd_mod_pubsub). - -start(Host, Opts) -> - mnesia:create_table(pubsub_node, - [{disc_only_copies, [node()]}, - {attributes, record_info(fields, pubsub_node)}]), - MyHost = gen_mod:get_opt(host, Opts, "pubsub." ++ Host), - update_table(MyHost), - mnesia:add_table_index(pubsub_node, host_parent), - ServedHosts = gen_mod:get_opt(served_hosts, Opts, []), - register(gen_mod:get_module_proc(Host, ?PROCNAME), - proc_lib:spawn_link(?MODULE, init, - [MyHost, Host, ServedHosts, self()])). - - -define(MYJID, #jid{user = "", server = Host, resource = "", luser = "", lserver = Host, lresource = ""}). -init(Host, ServerHost, ServedHosts, Parent) -> - ejabberd_router:register_route(Host), - create_new_node(Host, ["pubsub"], ?MYJID), - create_new_node(Host, ["pubsub", "nodes"], ?MYJID), - create_new_node(Host, ["home"], ?MYJID), - create_new_node(Host, ["home", ServerHost], ?MYJID), - lists:foreach(fun(H) -> - create_new_node(Host, ["home", H], ?MYJID) - end, ServedHosts), - ets:new(gen_mod:get_module_proc(Host, pubsub_presence), [set, named_table]), - loop(Host, Parent). +%%==================================================================== +%% API +%%==================================================================== +%%-------------------------------------------------------------------- +%% Function: start_link() -> {ok,Pid} | ignore | {error,Error} +%% Description: Starts the server +%%-------------------------------------------------------------------- +start_link(Host, Opts) -> + Proc = gen_mod:get_module_proc(Host, ?PROCNAME), + gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []). -loop(Host, Parent) -> - receive - {route, From, To, Packet} -> - case catch do_route(To#jid.lserver, From, To, Packet) of - {'EXIT', Reason} -> - ?ERROR_MSG("~p", [Reason]); - _ -> - ok - end, - loop(Host, Parent); - {room_destroyed, Room} -> - ets:delete(muc_online_room, Room), - loop(Host, Parent); - stop -> - ejabberd_router:unregister_route(Host), - ok; - reload -> - ?MODULE:loop(Host, Parent); - {system, From, Request} -> - sys:handle_system_msg(Request, From, Parent, ?MODULE, [], Host); - _ -> - loop(Host, Parent) - end. +start(Host, Opts) -> + Proc = gen_mod:get_module_proc(Host, ?PROCNAME), + ChildSpec = + {Proc, + {?MODULE, start_link, [Host, Opts]}, + temporary, + 1000, + worker, + [?MODULE]}, + supervisor:start_child(ejabberd_sup, ChildSpec). -%%% API functions +stop(Host) -> + Proc = gen_mod:get_module_proc(Host, ?PROCNAME), + gen_server:call(Proc, stop), + supervisor:stop_child(ejabberd_sup, Proc). delete_item(From, Node, ItemID) -> delete_item(get_host(), From, Node, ItemID). @@ -124,8 +103,98 @@ get_host() -> timeout end. -%%% Internal functions +%%==================================================================== +%% gen_server callbacks +%%==================================================================== + +%%-------------------------------------------------------------------- +%% Function: init(Args) -> {ok, State} | +%% {ok, State, Timeout} | +%% ignore | +%% {stop, Reason} +%% Description: Initiates the server +%%-------------------------------------------------------------------- +init([Host, Opts]) -> + mnesia:create_table(pubsub_node, + [{disc_only_copies, [node()]}, + {attributes, record_info(fields, pubsub_node)}]), + MyHost = gen_mod:get_opt(host, Opts, "pubsub." ++ Host), + update_table(MyHost), + mnesia:add_table_index(pubsub_node, host_parent), + ServedHosts = gen_mod:get_opt(served_hosts, Opts, []), + + ejabberd_router:register_route(MyHost), + create_new_node(MyHost, ["pubsub"], ?MYJID), + create_new_node(MyHost, ["pubsub", "nodes"], ?MYJID), + create_new_node(MyHost, ["home"], ?MYJID), + create_new_node(MyHost, ["home", Host], ?MYJID), + lists:foreach(fun(H) -> + create_new_node(MyHost, ["home", H], ?MYJID) + end, ServedHosts), + ets:new(gen_mod:get_module_proc(MyHost, pubsub_presence), + [set, named_table]), + {ok, #state{host = MyHost}}. + +%%-------------------------------------------------------------------- +%% 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) -> + {stop, normal, ok, 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 +%%-------------------------------------------------------------------- +handle_info({route, From, To, Packet}, State) -> + case catch do_route(To#jid.lserver, From, To, Packet) of + {'EXIT', Reason} -> + ?ERROR_MSG("~p", [Reason]); + _ -> + ok + end, + {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) -> + ejabberd_router:unregister_route(State#state.host), + 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 +%%-------------------------------------------------------------------- do_route(Host, From, To, Packet) -> {xmlelement, Name, Attrs, Els} = Packet, case To of @@ -236,14 +305,6 @@ do_route(Host, From, To, Packet) -> - -stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - Proc ! stop, - {wait, Proc}. - - - node_to_string(Node) -> string:strip(lists:flatten(lists:map(fun(S) -> [S, "/"] end, Node)), right, $/). @@ -1261,18 +1322,6 @@ broadcast_config_notification(Host, Node, Lang) -> -system_continue(Parent, _, State) -> - loop(State, Parent). - -system_terminate(Reason, Parent, _, State) -> - exit(Reason). - -system_code_change(State, _Mod, Ver, _Extra) -> - {ok, State}. - - - - iq_pubsub_owner(Host, From, Type, Lang, SubEl) -> {xmlelement, _, _, SubEls} = SubEl, case xml:remove_cdata(SubEls) of |