summaryrefslogtreecommitdiff
path: root/deps/rabbit/src/tcp_listener.erl
blob: 712280fce1977fb6a4d8bf449ed80638e4b8ff59 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2021 VMware, Inc. or its affiliates.  All rights reserved.
%%

-module(tcp_listener).

%% Represents a running TCP listener (a process that listens for inbound
%% TCP or TLS connections). Every protocol supported typically has one
%% or two listeners, plain TCP and (optionally) TLS, but there can
%% be more, e.g. when multiple network interfaces are involved.
%%
%% A listener has 6 properties (is a tuple of 6):
%%
%%  * IP address
%%  * Port
%%  * Node
%%  * Label (human-friendly name, e.g. AMQP 0-9-1)
%%  * Startup callback
%%  * Shutdown callback
%%
%% Listeners use Ranch in embedded mode to accept and "bridge" client
%% connections with protocol entry points such as rabbit_reader.
%%
%% Listeners are tracked in a Mnesia table so that they can be
%%
%%  * Shut down
%%  * Listed (e.g. in the management UI)
%%
%% Every tcp_listener process has callbacks that are executed on start
%% and termination. Those must take care of listener registration
%% among other things.
%%
%% Listeners are supervised by tcp_listener_sup (one supervisor per protocol).
%%
%% See also rabbit_networking and tcp_listener_sup.

-behaviour(gen_server).

-export([start_link/5]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-record(state, {on_startup, on_shutdown, label, ip, port}).

%%----------------------------------------------------------------------------

-type mfargs() :: {atom(), atom(), [any()]}.

-spec start_link
        (inet:ip_address(), inet:port_number(),
         mfargs(), mfargs(), string()) ->
                           rabbit_types:ok_pid_or_error().

start_link(IPAddress, Port,
           OnStartup, OnShutdown, Label) ->
    gen_server:start_link(
      ?MODULE, {IPAddress, Port,
                OnStartup, OnShutdown, Label}, []).

%%--------------------------------------------------------------------

init({IPAddress, Port, {M,F,A} = OnStartup, OnShutdown, Label}) ->
    process_flag(trap_exit, true),
    logger:info("started ~s on ~s:~p", [Label, rabbit_misc:ntoab(IPAddress), Port]),
    apply(M, F, A ++ [IPAddress, Port]),
    {ok, #state{on_startup = OnStartup, on_shutdown = OnShutdown,
                label = Label, ip=IPAddress, port=Port}}.

handle_call(_Request, _From, State) ->
    {noreply, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info(_Info, State) ->
    {noreply, State}.

terminate(_Reason, #state{on_shutdown = {M,F,A}, label=Label, ip=IPAddress, port=Port}) ->
    logger:info("stopped ~s on ~s:~p", [Label, rabbit_misc:ntoab(IPAddress), Port]),
    apply(M, F, A ++ [IPAddress, Port]).

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.