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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2009-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%
%%
%%--------------------------------------------------------------------
%% File : ssh_connection_controler.erl
%% Description :
%%
%%--------------------------------------------------------------------
-module(ssh_connection_controler).
-behaviour(gen_server).
%%-----------------------------------------------------------------
%% External exports
%%-----------------------------------------------------------------
-export([start_link/1, start_handler_child/2, start_manager_child/2,
connection_manager/1]).
%%-----------------------------------------------------------------
%% Internal exports
%%-----------------------------------------------------------------
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
code_change/3, terminate/2, stop/1]).
-record(state, {role, manager, handler, timeout}).
%%-----------------------------------------------------------------
%% External interface functions
%%-----------------------------------------------------------------
%%-----------------------------------------------------------------
%% Func: start/0
%%-----------------------------------------------------------------
start_link(Args) ->
gen_server:start_link(?MODULE, [Args], []).
%% Will be called from the manager child process
start_handler_child(ServerRef, Args) ->
gen_server:call(ServerRef, {handler, self(), Args}, infinity).
%% Will be called from the acceptor process
start_manager_child(ServerRef, Args) ->
gen_server:call(ServerRef, {manager, Args}, infinity).
connection_manager(ServerRef) ->
{ok, gen_server:call(ServerRef, manager, infinity)}.
%%-----------------------------------------------------------------
%% Internal interface functions
%%-----------------------------------------------------------------
%%-----------------------------------------------------------------
%% Func: stop/1
%%-----------------------------------------------------------------
stop(Pid) ->
gen_server:cast(Pid, stop).
%%-----------------------------------------------------------------
%% Server functions
%%-----------------------------------------------------------------
%%-----------------------------------------------------------------
%% Func: init/1
%%-----------------------------------------------------------------
init([Opts]) ->
process_flag(trap_exit, true),
case proplists:get_value(role, Opts) of
client ->
{ok, Manager} = ssh_connection_manager:start_link([client, Opts]),
{ok, #state{role = client, manager = Manager}};
_server ->
%% Children started by acceptor process
{ok, #state{role = server}}
end.
%%-----------------------------------------------------------------
%% Func: terminate/2
%%-----------------------------------------------------------------
terminate(_Reason, #state{}) ->
ok.
%%-----------------------------------------------------------------
%% Func: handle_call/3
%%-----------------------------------------------------------------
handle_call({handler, Pid, [Role, Socket, Opts]}, _From, State) ->
{ok, Handler} = ssh_connection_handler:start_link(Role, Pid, Socket, Opts),
{reply, {ok, Handler}, State#state{handler = Handler}};
handle_call({manager, [server = Role, Socket, Opts, SubSysSup]}, _From, State) ->
{ok, Manager} = ssh_connection_manager:start_link([Role, Socket, Opts, SubSysSup]),
{reply, {ok, Manager}, State#state{manager = Manager}};
handle_call({manager, [client = Role | Opts]}, _From, State) ->
{ok, Manager} = ssh_connection_manager:start_link([Role, Opts]),
{reply, {ok, Manager}, State#state{manager = Manager}};
handle_call(manager, _From, State) ->
{reply, State#state.manager, State};
handle_call(stop, _From, State) ->
{stop, normal, ok, State};
handle_call(_, _, State) ->
{noreply, State, State#state.timeout}.
%%-----------------------------------------------------------------
%% Func: handle_cast/2
%%-----------------------------------------------------------------
handle_cast(stop, State) ->
{stop, normal, State};
handle_cast(_, State) ->
{noreply, State, State#state.timeout}.
%%-----------------------------------------------------------------
%% Func: handle_info/2
%%-----------------------------------------------------------------
%% handle_info(ssh_connected, State) ->
%% {stop, normal, State};
%% Servant termination.
handle_info({'EXIT', _Pid, normal}, State) ->
{stop, normal, State}.
%%-----------------------------------------------------------------
%% Func: code_change/3
%%-----------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
|