aboutsummaryrefslogtreecommitdiff
path: root/src/ejabberd_listener.erl
blob: 3ed030c7b9df43015d5d56dedd6d6b25eb971d74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
%%%----------------------------------------------------------------------
%%% File    : ejabberd_listener.erl
%%% Author  : Alexey Shchepin <alexey@sevcom.net>
%%% Purpose : 
%%% Created : 16 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
%%% Id      : $Id$
%%%----------------------------------------------------------------------

-module(ejabberd_listener).
-author('alexey@sevcom.net').
-vsn('$Revision$ ').

-export([start_link/0, init/1, start/4, init/4]).

start_link() ->
    supervisor:start_link({local, ejabberd_listeners}, ?MODULE, []).


init(_) ->
    case ejabberd_config:get_local_option(listen) of
	undefined ->
	    ignore;
	Ls ->
	    {ok, {{one_for_one, 10, 1},
		  lists:map(
		    fun({Port, Module, Fun, Opts}) ->
			    {Port,
			     {?MODULE, start, [Port, Module, Fun, [Opts]]},
			     permanent,
			     brutal_kill,
			     worker,
			     [Module]}
		    end, Ls)}}
    end.


start(Port, Module, Fun, Args) ->
    {ok, spawn_link(?MODULE, init, [Port, Module, Fun, Args])}.

init(Port, Module, Fun, Args) ->
    {ok, ListenSocket} = gen_tcp:listen(Port, [binary,
					       {packet, 0}, 
					       {active, false},
					       {reuseaddr, true}]),
    accept(ListenSocket, Module, Fun, Args).

accept(ListenSocket, Module, Fun, Args) ->
    case gen_tcp:accept(ListenSocket) of
	{ok,Socket} ->
	    apply(Module, Fun, [Socket] ++ Args),
	    %ejabberd_c2s:start(Socket),
	    accept(ListenSocket, Module, Fun, Args)
    end.