comparison src/com/five_ten_sg/connectbot/transport/AbsTransport.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children f3b3bbd227b8
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
1 /*
2 * ConnectBot: simple, powerful, open-source SSH client for Android
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package com.five_ten_sg.connectbot.transport;
19
20 import java.io.IOException;
21 import java.util.List;
22 import java.util.Map;
23
24 import com.five_ten_sg.connectbot.bean.HostBean;
25 import com.five_ten_sg.connectbot.bean.PortForwardBean;
26 import com.five_ten_sg.connectbot.service.TerminalBridge;
27 import com.five_ten_sg.connectbot.service.TerminalManager;
28 import android.content.Context;
29 import android.net.Uri;
30
31 /**
32 * @author Kenny Root
33 *
34 */
35 public abstract class AbsTransport {
36 HostBean host;
37 TerminalBridge bridge;
38 TerminalManager manager;
39
40 String emulation;
41
42 public AbsTransport() {}
43
44 public AbsTransport(HostBean host, TerminalBridge bridge, TerminalManager manager) {
45 this.host = host;
46 this.bridge = bridge;
47 this.manager = manager;
48 }
49
50 /**
51 * @return protocol part of the URI
52 */
53 public static String getProtocolName() {
54 return "unknown";
55 }
56
57 /**
58 * Encode the current transport into a URI that can be passed via intent calls.
59 * @return URI to host
60 */
61 public static Uri getUri(String input) {
62 return null;
63 }
64
65 /**
66 * Causes transport to connect to the target host. After connecting but before a
67 * session is started, must call back to {@link TerminalBridge#onConnected()}.
68 * After that call a session may be opened.
69 */
70 public abstract void connect();
71
72 /**
73 * Checks if read() will block. If there are no bytes remaining in
74 * the underlying transport, return true.
75 */
76 public abstract boolean willBlock();
77
78 /**
79 * Reads from the transport. Transport must support reading into a byte array
80 * <code>buffer</code> at the start of <code>offset</code> and a maximum of
81 * <code>length</code> bytes. If the remote host disconnects, throw an
82 * {@link IOException}.
83 * @param buffer byte buffer to store read bytes into
84 * @param offset where to start writing in the buffer
85 * @param length maximum number of bytes to read
86 * @return number of bytes read
87 * @throws IOException when remote host disconnects
88 */
89 public abstract int read(byte[] buffer, int offset, int length) throws IOException;
90
91 /**
92 * Writes to the transport. If the host is not yet connected, simply return without
93 * doing anything. An {@link IOException} should be thrown if there is an error after
94 * connection.
95 * @param buffer bytes to write to transport
96 * @throws IOException when there is a problem writing after connection
97 */
98 public abstract void write(byte[] buffer) throws IOException;
99
100 /**
101 * Writes to the transport. See {@link #write(byte[])} for behavior details.
102 * @param c character to write to the transport
103 * @throws IOException when there is a problem writing after connection
104 */
105 public abstract void write(int c) throws IOException;
106
107 /**
108 * Flushes the write commands to the transport.
109 * @throws IOException when there is a problem writing after connection
110 */
111 public abstract void flush() throws IOException;
112
113 /**
114 * Closes the connection to the terminal. Note that the resulting failure to read
115 * should call {@link TerminalBridge#dispatchDisconnect(boolean)}.
116 */
117 public abstract void close();
118
119 /**
120 * Tells the transport what dimensions the display is currently
121 * @param columns columns of text
122 * @param rows rows of text
123 * @param width width in pixels
124 * @param height height in pixels
125 */
126 public abstract void setDimensions(int columns, int rows, int width, int height);
127
128 public void setOptions(Map<String, String> options) {
129 // do nothing
130 }
131
132 public Map<String, String> getOptions() {
133 return null;
134 }
135
136 public void setCompression(boolean compression) {
137 // do nothing
138 }
139
140 public void setHttpproxy(String httpproxy) {
141 // do nothing
142 }
143
144 public void setUseAuthAgent(String useAuthAgent) {
145 // do nothing
146 }
147
148 public void setEmulation(String emulation) {
149 this.emulation = emulation;
150 }
151
152 public String getEmulation() {
153 return emulation;
154 }
155
156 public void setHost(HostBean host) {
157 this.host = host;
158 }
159
160 public void setBridge(TerminalBridge bridge) {
161 this.bridge = bridge;
162 }
163
164 public void setManager(TerminalManager manager) {
165 this.manager = manager;
166 }
167
168 /**
169 * Whether or not this transport type can forward ports.
170 * @return true on ability to forward ports
171 */
172 public boolean canForwardPorts() {
173 return false;
174 }
175
176 /**
177 * Adds the {@link PortForwardBean} to the list.
178 * @param portForward the port forward bean to add
179 * @return true on successful addition
180 */
181 public boolean addPortForward(PortForwardBean portForward) {
182 return false;
183 }
184
185 /**
186 * Enables a port forward member. After calling this method, the port forward should
187 * be operational iff it could be enabled by the transport.
188 * @param portForward member of our current port forwards list to enable
189 * @return true on successful port forward setup
190 */
191 public boolean enablePortForward(PortForwardBean portForward) {
192 return false;
193 }
194
195 /**
196 * Disables a port forward member. After calling this method, the port forward should
197 * be non-functioning iff it could be disabled by the transport.
198 * @param portForward member of our current port forwards list to enable
199 * @return true on successful port forward tear-down
200 */
201 public boolean disablePortForward(PortForwardBean portForward) {
202 return false;
203 }
204
205 /**
206 * Removes the {@link PortForwardBean} from the available port forwards.
207 * @param portForward the port forward bean to remove
208 * @return true on successful removal
209 */
210 public boolean removePortForward(PortForwardBean portForward) {
211 return false;
212 }
213
214 /**
215 * Gets a list of the {@link PortForwardBean} currently used by this transport.
216 * @return the list of port forwards
217 */
218 public List<PortForwardBean> getPortForwards() {
219 return null;
220 }
221
222 /**
223 * Whether or not this transport type can transfer files.
224 * @return true on ability to transfer files
225 */
226 public boolean canTransferFiles() {
227 return false;
228 }
229
230 /**
231 * Downloads the specified remote file to a local folder.
232 * @param remoteFile The path to the remote file to be downloaded. Must be non-null.
233 * @param localFolder The path to local folder. Null = default external storage folder.
234 * @return true on success, false on failure
235 */
236 public boolean downloadFile(String remoteFile, String localFolder) {
237 return false;
238 }
239
240 /**
241 * Uploads the specified local file to the remote host.
242 * @param localFile The path to the local file to be uploaded. Must be non-null.
243 * @param remoteFolder The path to the remote directory. Null == default remote directory.
244 * @return true on success, false on failure
245 */
246 public boolean uploadFile(String localFile, String remoteFile,
247 String remoteFolder, String mode) {
248 return false;
249 }
250
251 public abstract boolean isConnected();
252 public abstract boolean isSessionOpen();
253 public abstract boolean isAuthenticated();
254
255 /**
256 * @return int default port for protocol
257 */
258 public abstract int getDefaultPort();
259
260 /**
261 * @param username
262 * @param hostname
263 * @param port
264 * @return
265 */
266 public abstract String getDefaultNickname(String username, String hostname, int port);
267
268 /**
269 * @param uri
270 * @param selectionKeys
271 * @param selectionValues
272 */
273 public abstract void getSelectionArgs(Uri uri, Map<String, String> selection);
274
275 /**
276 * @param uri
277 * @return
278 */
279 public abstract HostBean createHost(Uri uri);
280
281 /**
282 * @param context context containing the correct resources
283 * @return string that hints at the format for connection
284 */
285 public static String getFormatHint(Context context) {
286 return "???";
287 }
288
289 /**
290 * @return
291 */
292 public abstract boolean usesNetwork();
293 }