2
%% <year>2008-2008</year>
3
%% <holder>Ericsson AB, All Rights Reserved</holder>
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
17
%% The Initial Developer of the Original Code is Ericsson AB.
20
%%----------------------------------------------------------------------
21
%% Purpose: Ssh connection supervisor.
22
%%----------------------------------------------------------------------
24
-module(ssh_connection_sup).
26
-behaviour(supervisor).
28
-export([start_link/1, start_handler_child/2, start_manager_child/2,
29
connection_manager/1]).
31
%% Supervisor callback
34
%%%=========================================================================
36
%%%=========================================================================
38
supervisor:start_link(?MODULE, [Args]).
40
%% Will be called from the manager child process
41
start_handler_child(Sup, Args) ->
42
[Spec] = child_specs(handler, Args),
43
supervisor:start_child(Sup, Spec).
45
%% Will be called from the acceptor process
46
start_manager_child(Sup, Args) ->
47
[Spec] = child_specs(manager, Args),
48
supervisor:start_child(Sup, Spec).
50
connection_manager(SupPid) ->
51
Children = supervisor:which_children(SupPid),
52
{ok, ssh_connection_manager(Children)}.
54
%%%=========================================================================
55
%%% Supervisor callback
56
%%%=========================================================================
58
RestartStrategy = one_for_all,
61
Children = child_specs(Args),
62
{ok, {{RestartStrategy, MaxR, MaxT}, Children}}.
64
%%%=========================================================================
65
%%% Internal functions
66
%%%=========================================================================
68
case proplists:get_value(role, Opts) of
70
child_specs(manager, [client | Opts]);
72
%% Children started by acceptor process
76
% The manager process starts the handler process
77
child_specs(manager, Opts) ->
79
child_specs(handler, Opts) ->
82
manager_spec([server = Role, Socket, Opts]) ->
83
Address = proplists:get_value(address, Opts),
84
Port = proplists:get_value(port, Opts),
86
StartFunc = {ssh_connection_manager, start_link, [[Role, Socket, Opts]]},
89
Modules = [ssh_connection_manager],
91
{Name, StartFunc, Restart, Shutdown, Type, Modules};
93
manager_spec([client = Role | Opts]) ->
94
Address = proplists:get_value(address, Opts),
95
Port = proplists:get_value(port, Opts),
97
StartFunc = {ssh_connection_manager, start_link, [[Role, Opts]]},
101
Modules = [ssh_connection_manager],
103
{Name, StartFunc, Restart, Shutdown, Type, Modules}.
105
handler_spec([Role, Socket, Opts]) ->
106
Address = proplists:get_value(address, Opts),
107
Port = proplists:get_value(port, Opts),
109
StartFunc = {ssh_connection_handler,
110
start_link, [Role, self(), Socket, Opts]},
113
Modules = [ssh_connection_handler],
115
{Name, StartFunc, Restart, Shutdown, Type, Modules}.
117
ssh_connection_manager([{_, Child, _, [ssh_connection_manager]} | _]) ->
119
ssh_connection_manager([_ | Rest]) ->
120
ssh_connection_manager(Rest).