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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2001-2010. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%
%%----------------------------------------------------------------------
%% Purpose: The top supervisor for an instance of the http server. (You may
%% have several instances running on the same machine.) Hangs under
%% httpd_sup.
%%----------------------------------------------------------------------
-module(httpd_instance_sup).
-behaviour(supervisor).
%% Internal application API
-export([start_link/3, start_link/4]).
%% Supervisor callbacks
-export([init/1]).
%%%=========================================================================
%%% Internal Application API
%%%=========================================================================
start_link([{_, _}| _] = Config, AcceptTimeout, Debug) ->
case (catch httpd_conf:validate_properties(Config)) of
{ok, Config2} ->
Address = proplists:get_value(bind_address, Config2),
Port = proplists:get_value(port, Config2),
Name = make_name(Address, Port),
SupName = {local, Name},
supervisor:start_link(SupName, ?MODULE,
[undefined, Config2, AcceptTimeout,
Debug, Address, Port]);
{error, Reason} ->
error_logger:error_report(Reason),
{stop, Reason}
end;
start_link(ConfigFile, AcceptTimeout, Debug) ->
case file_2_config(ConfigFile) of
{ok, ConfigList, Address, Port} ->
Name = make_name(Address, Port),
SupName = {local, Name},
supervisor:start_link(SupName, ?MODULE,
[ConfigFile, ConfigList, AcceptTimeout,
Debug, Address, Port]);
{error, Reason} ->
error_logger:error_report(Reason),
{stop, Reason}
end.
start_link([{_, _}| _] = Config, AcceptTimeout, ListenInfo, Debug) ->
case (catch httpd_conf:validate_properties(Config)) of
{ok, Config2} ->
Address = proplists:get_value(bind_address, Config2),
Port = proplists:get_value(port, Config2),
Name = make_name(Address, Port),
SupName = {local, Name},
supervisor:start_link(SupName, ?MODULE,
[undefined, Config2, AcceptTimeout,
Debug, Address, Port, ListenInfo]);
{error, Reason} ->
error_logger:error_report(Reason),
{stop, Reason}
end;
start_link(ConfigFile, AcceptTimeout, ListenInfo, Debug) ->
case file_2_config(ConfigFile) of
{ok, ConfigList, Address, Port} ->
Name = make_name(Address, Port),
SupName = {local, Name},
supervisor:start_link(SupName, ?MODULE,
[ConfigFile, ConfigList, AcceptTimeout,
Debug, Address, Port, ListenInfo]);
{error, Reason} ->
error_logger:error_report(Reason),
{stop, Reason}
end.
%%%=========================================================================
%%% Supervisor callback
%%%=========================================================================
init([ConfigFile, ConfigList, AcceptTimeout, Debug, Address, Port]) ->
httpd_util:enable_debug(Debug),
Flags = {one_for_one, 0, 1},
Children = [sup_spec(httpd_acceptor_sup, Address, Port),
sup_spec(httpd_misc_sup, Address, Port),
worker_spec(httpd_manager, Address, Port,
ConfigFile, ConfigList,AcceptTimeout)],
{ok, {Flags, Children}};
init([ConfigFile, ConfigList, AcceptTimeout, Debug, Address, Port, ListenInfo]) ->
httpd_util:enable_debug(Debug),
Flags = {one_for_one, 0, 1},
Children = [sup_spec(httpd_acceptor_sup, Address, Port),
sup_spec(httpd_misc_sup, Address, Port),
worker_spec(httpd_manager, Address, Port, ListenInfo,
ConfigFile, ConfigList, AcceptTimeout)],
{ok, {Flags, Children}}.
%%%=========================================================================
%%% Internal functions
%%%=========================================================================
sup_spec(SupModule, Address, Port) ->
Name = {SupModule, Address, Port},
StartFunc = {SupModule, start_link, [Address, Port]},
Restart = permanent,
Shutdown = infinity,
Modules = [SupModule],
Type = supervisor,
{Name, StartFunc, Restart, Shutdown, Type, Modules}.
worker_spec(WorkerModule, Address, Port, ConfigFile,
ConfigList, AcceptTimeout) ->
Name = {WorkerModule, Address, Port},
StartFunc = {WorkerModule, start_link,
[ConfigFile, ConfigList, AcceptTimeout]},
Restart = permanent,
Shutdown = 4000,
Modules = [WorkerModule],
Type = worker,
{Name, StartFunc, Restart, Shutdown, Type, Modules}.
worker_spec(WorkerModule, Address, Port, ListenInfo, ConfigFile,
ConfigList, AcceptTimeout) ->
Name = {WorkerModule, Address, Port},
StartFunc = {WorkerModule, start_link,
[ConfigFile, ConfigList, AcceptTimeout, ListenInfo]},
Restart = permanent,
Shutdown = 4000,
Modules = [WorkerModule],
Type = worker,
{Name, StartFunc, Restart, Shutdown, Type, Modules}.
make_name(Address,Port) ->
httpd_util:make_name("httpd_instance_sup", Address, Port).
file_2_config(ConfigFile) ->
case httpd_conf:load(ConfigFile) of
{ok, ConfigList} ->
case (catch httpd_conf:validate_properties(ConfigList)) of
{ok, Config} ->
Address = proplists:get_value(bind_address, ConfigList),
Port = proplists:get_value(port, ConfigList),
{ok, Config, Address, Port};
Error ->
Error
end;
Error ->
Error
end.
|