~rdoering/ubuntu/karmic/erlang/fix-535090

« back to all changes in this revision

Viewing changes to lib/ssh/src/ssh_connection_sup.erl

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-02-15 16:42:52 UTC
  • mfrom: (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090215164252-q5x4rcf8a5pbesb1
Tags: 1:12.b.5-dfsg-2
Upload to unstable after lenny is released.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%%<copyright>
 
2
%% <year>2008-2008</year>
 
3
%% <holder>Ericsson AB, All Rights Reserved</holder>
 
4
%%</copyright>
 
5
%%<legalnotice>
 
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/.
 
11
%%
 
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
 
15
%% under the License.
 
16
%%
 
17
%% The Initial Developer of the Original Code is Ericsson AB.
 
18
%%</legalnotice>
 
19
%%
 
20
%%----------------------------------------------------------------------
 
21
%% Purpose: Ssh connection supervisor.
 
22
%%----------------------------------------------------------------------
 
23
 
 
24
-module(ssh_connection_sup).
 
25
 
 
26
-behaviour(supervisor).
 
27
 
 
28
-export([start_link/1, start_handler_child/2, start_manager_child/2,
 
29
         connection_manager/1]).
 
30
 
 
31
%% Supervisor callback
 
32
-export([init/1]).
 
33
 
 
34
%%%=========================================================================
 
35
%%%  API
 
36
%%%=========================================================================
 
37
start_link(Args) ->
 
38
    supervisor:start_link(?MODULE, [Args]).
 
39
 
 
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).
 
44
 
 
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).
 
49
 
 
50
connection_manager(SupPid) -> 
 
51
    Children = supervisor:which_children(SupPid),
 
52
    {ok, ssh_connection_manager(Children)}.
 
53
 
 
54
%%%=========================================================================
 
55
%%%  Supervisor callback
 
56
%%%=========================================================================
 
57
init([Args]) ->  
 
58
    RestartStrategy = one_for_all,
 
59
    MaxR = 0,
 
60
    MaxT = 3600,
 
61
    Children = child_specs(Args),
 
62
    {ok, {{RestartStrategy, MaxR, MaxT}, Children}}.
 
63
 
 
64
%%%=========================================================================
 
65
%%%  Internal functions
 
66
%%%=========================================================================
 
67
child_specs(Opts) ->
 
68
    case proplists:get_value(role, Opts) of
 
69
        client ->               
 
70
            child_specs(manager, [client | Opts]);
 
71
        server ->
 
72
            %% Children started by acceptor process
 
73
            []
 
74
    end.
 
75
 
 
76
% The manager process starts the handler process
 
77
child_specs(manager, Opts) ->
 
78
    [manager_spec(Opts)];
 
79
child_specs(handler, Opts) ->
 
80
    [handler_spec(Opts)].
 
81
 
 
82
manager_spec([server = Role, Socket, Opts]) ->
 
83
    Address =  proplists:get_value(address, Opts),
 
84
    Port = proplists:get_value(port, Opts),
 
85
    Name = make_ref(), 
 
86
    StartFunc = {ssh_connection_manager, start_link, [[Role, Socket, Opts]]},
 
87
    Restart = temporary, 
 
88
    Shutdown = 3600,
 
89
    Modules = [ssh_connection_manager],
 
90
    Type = worker,
 
91
    {Name, StartFunc, Restart, Shutdown, Type, Modules};
 
92
 
 
93
manager_spec([client = Role | Opts]) ->
 
94
    Address =  proplists:get_value(address, Opts),
 
95
    Port = proplists:get_value(port, Opts),
 
96
    Name = make_ref(), 
 
97
    StartFunc = {ssh_connection_manager, start_link, [[Role, Opts]]},
 
98
    %%TODO restarttype?
 
99
    Restart = temporary, 
 
100
    Shutdown = 3600,
 
101
    Modules = [ssh_connection_manager],
 
102
    Type = worker,
 
103
    {Name, StartFunc, Restart, Shutdown, Type, Modules}.
 
104
 
 
105
handler_spec([Role, Socket, Opts]) ->
 
106
    Address =  proplists:get_value(address, Opts),
 
107
    Port = proplists:get_value(port, Opts),
 
108
    Name = make_ref(), 
 
109
    StartFunc = {ssh_connection_handler, 
 
110
                 start_link, [Role, self(), Socket, Opts]},
 
111
    Restart = temporary, 
 
112
    Shutdown = 3600,
 
113
    Modules = [ssh_connection_handler],
 
114
    Type = worker,
 
115
    {Name, StartFunc, Restart, Shutdown, Type, Modules}.
 
116
 
 
117
ssh_connection_manager([{_, Child, _, [ssh_connection_manager]} | _]) ->
 
118
    Child;
 
119
ssh_connection_manager([_ | Rest]) ->
 
120
    ssh_connection_manager(Rest).