/
mock_apn_sup.erl
103 lines (82 loc) · 3.09 KB
/
mock_apn_sup.erl
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
%%%-------------------------------------------------------------------
%%% @author Paolo D'Incau <paolo.dincau@gmail.com>
%%% @copyright (C) 2013, Paolo D'Incau
%%% @doc
%%%
%%% @end
%%% Created : 21 Feb 2013 by Paolo D'Incau <paolo.dincau@gmail.com>
%%%-------------------------------------------------------------------
-module(mock_apn_sup).
-behaviour(supervisor).
%% API
-export([start_link/0, start_socket/0]).
%% Supervisor callbacks
-export([init/1]).
-define(SERVER, ?MODULE).
-define(CERTIFICATE, "/path/to/certificate.pem").
-define(KEY, "/path/to/key.pem").
-define(PASSWORD, "password").
-define(MOCK_APN_PORT, 2195).
-define(SSLOPTIONS, [{certfile, ?CERTIFICATE},
{keyfile, ?KEY},
{password, ?PASSWORD},
{mode, binary},
{packet, 0},
{backlog, 1000},
{active, true}]).
%%%===================================================================
%%% API functions
%%%===================================================================
%%--------------------------------------------------------------------
%% @doc
%% Starts the supervisor
%%
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
%%%===================================================================
%%% Supervisor callbacks
%%%===================================================================
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Whenever a supervisor is started using supervisor:start_link/[2,3],
%% this function is called by the new process to find out about
%% restart strategy, maximum restart frequency and child
%% specifications.
%%
%% @spec init(Args) -> {ok, {SupFlags, [ChildSpec]}} |
%% ignore |
%% {error, Reason}
%% @end
%%--------------------------------------------------------------------
init([]) ->
ssl:start(),
SSLOptions = lists:map(fun get_config_value/1, ?SSLOPTIONS),
{ok, LSocket} = ssl:listen(?MOCK_APN_PORT, SSLOptions),
spawn_link(fun empty_listeners/0),
RestartStrategy = simple_one_for_one,
MaxRestarts = 60,
MaxSecondsBetweenRestarts = 3600,
SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},
Restart = temporary,
Shutdown = 1000,
Type = worker,
AChild = {mock_apn_server, {mock_apn_server, start_link, [LSocket]},
Restart, Shutdown, Type, [mock_apn_server]},
{ok, {SupFlags, [AChild]}}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
start_socket() ->
supervisor:start_child(?SERVER, []).
empty_listeners() ->
[start_socket() || _ <- lists:seq(1, 2000)],
ok.
get_config_value({Key, CurrentValue}) ->
case application:get_env(mock_apn, Key) of
{ok, NewValue} -> {Key, NewValue};
undefined -> {Key, CurrentValue}
end.