1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2008-2018. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%% 20 21%% 22%%---------------------------------------------------------------------- 23%% Purpose: The ssh client subsystem supervisor 24%%---------------------------------------------------------------------- 25 26-module(sshc_sup). 27 28-behaviour(supervisor). 29 30-export([start_link/0, 31 start_child/4, 32 start_system_subsystem/4, 33 stop_child/1, 34 stop_system/1 35 ]). 36 37%% Supervisor callback 38-export([init/1]). 39 40-include("ssh.hrl"). 41 42-define(SSHC_SUP, ?MODULE). 43 44%%%========================================================================= 45%%% API 46%%%========================================================================= 47start_link() -> 48 supervisor:start_link({local,?MODULE}, ?MODULE, []). 49 50start_child(Address, Port, Profile, Options) -> 51 case ssh_system_sup:system_supervisor(Address, Port, Profile) of 52 undefined -> 53 %% Here we a new connction on a new Host/Port/Profile 54 Spec = child_spec(Address, Port, Profile, Options), 55 supervisor:start_child(?MODULE, Spec); 56 Pid -> 57 {ok,Pid} 58 end. 59 60 61start_system_subsystem(Host, Port, Profile, Options0) -> 62 Options = ?DELETE_OPT(password, Options0), 63 ssh_controller:start_system_subsystem(client_controller, ?MODULE, Host, Port, Profile, Options, 64 child_spec(Host, Port, Profile, Options) 65 ). 66 67stop_child(ChildId) when is_tuple(ChildId) -> 68 supervisor:terminate_child(?SSHC_SUP, ChildId); 69stop_child(ChildPid) when is_pid(ChildPid)-> 70 stop_child(system_name(ChildPid)). 71 72stop_system(SysSup) -> 73 ssh_controller:stop_system(client_controller, SysSup). 74 75 76%%%========================================================================= 77%%% Supervisor callback 78%%%========================================================================= 79init(_) -> 80 SupFlags = #{strategy => one_for_one, 81 intensity => 0, 82 period => 3600 83 }, 84 ChildSpecs = [#{id => client_controller, 85 start => {ssh_controller, start_link, [client, client_controller]}, 86 restart => permanent, 87 type => worker 88 }], 89 {ok, {SupFlags,ChildSpecs}}. 90 91%%%========================================================================= 92%%% Internal functions 93%%%========================================================================= 94child_spec(Address, Port, Profile, Options) -> 95 #{id => id(Address, Port, Profile), 96 start => {ssh_system_sup, start_link, [client, Address, Port, Profile, Options]}, 97 restart => temporary, 98 type => supervisor 99 }. 100 101id(Address, Port, Profile) -> 102 {client, ssh_system_sup, Address, Port, Profile}. 103 104system_name(SysSup) -> 105 case lists:keyfind(SysSup, 2, supervisor:which_children(?SSHC_SUP)) of 106 {Name, SysSup, _, _} -> Name; 107 false -> undefind 108 end. 109 110