comparison app/src/main/java/com/five_ten_sg/connectbot/transport/AbsTransport.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/com/five_ten_sg/connectbot/transport/AbsTransport.java@2a7199ad90be
children f698820bffdf
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
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.TerminalKeyListener;
28 import com.five_ten_sg.connectbot.service.TerminalManager;
29 import android.content.Context;
30 import android.net.Uri;
31 import android.util.Log;
32 import de.mud.terminal.vt320;
33
34 /**
35 * @author Kenny Root
36 *
37 */
38 public abstract class AbsTransport {
39 protected String TAG;
40 protected TerminalManager manager;
41 protected TerminalBridge bridge;
42 protected String homeDirectory;
43 protected HostBean host;
44 protected vt320 buffer = null;
45 protected String emulation;
46
47 class vt320Default extends vt320 {
48 @Override
49 public void debug(String s) {
50 Log.d(TAG, s);
51 }
52
53 // monitor injecting a field
54 //@Override
55 //public void setField(int l, int c, char [] data)
56 // implementation in the base vt320
57
58 // terminal key listener found special key, send notification to monitor
59 @Override
60 public void monitorKey(boolean down) {
61 if (bridge.monitor != null) bridge.monitor.keyState(down);
62 }
63
64 // terminal key listener sending to the host
65 @Override
66 public void write(byte[] b) {
67 try {
68 AbsTransport.this.write(b);
69 }
70 catch (IOException e) {
71 Log.e(TAG, "Problem writing outgoing data in vt320() thread", e);
72 }
73 }
74 @Override
75 public void write(int b) {
76 try {
77 AbsTransport.this.write(b);
78 }
79 catch (IOException e) {
80 Log.e(TAG, "Problem writing outgoing data in vt320() thread", e);
81 }
82 }
83
84 // We don't use telnet sequences.
85 @Override
86 public void sendTelnetCommand(byte cmd) {
87 }
88 // We don't want remote to resize our window.
89 @Override
90 public void setWindowSize(int c, int r) {
91 }
92 // play beep noise
93 @Override
94 public void beep() {
95 if ((bridge.parent != null) && (bridge.parent.isShown()))
96 manager.playBeep();
97 else
98 manager.sendActivityNotification(host);
99 }
100
101 // test for changed screen contents
102 @Override
103 public void testChanged() {
104 if (bridge.monitor != null) bridge.monitor.testChanged();
105 }
106 // relay socket writing to the screen
107 // bridge.monitor placement of new characters
108 @Override
109 public void putChar(int c, int l, char ch, int attributes) {
110 if (bridge.monitor != null) bridge.monitor.screenChanged(l, c);
111
112 super.putChar(c, l, ch, attributes);
113 }
114 @Override
115 public void insertChar(int c, int l, char ch, int attributes) {
116 if (bridge.monitor != null) bridge.monitor.screenChanged(l, l, c, width - 1);
117
118 super.insertChar(c, l, ch, attributes);
119 }
120 @Override
121 public void insertLine(int l, int n, boolean scrollDown) {
122 if (bridge.monitor != null) {
123 if (scrollDown) bridge.monitor.screenChanged(l, height - 1, 0, width - 1);
124 else bridge.monitor.screenChanged(0, l, 0, width - 1);
125 }
126
127 super.insertLine(l, n, scrollDown);
128 }
129 @Override
130 public void deleteLine(int l) {
131 if (bridge.monitor != null) bridge.monitor.screenChanged(l, height - 1, 0, width - 1);
132
133 super.deleteLine(l);
134 }
135 @Override
136 public void deleteChar(int c, int l) {
137 if (bridge.monitor != null) bridge.monitor.screenChanged(l, l, c, width - 1);
138
139 super.deleteChar(c, l);
140 }
141 @Override
142 public void setCursorPosition(int c, int l) {
143 if (bridge.monitor != null) bridge.monitor.cursorMove(l, c);
144
145 super.setCursorPosition(c, l);
146 }
147
148 };
149
150
151 public AbsTransport() {}
152
153 /**
154 * @return protocol part of the URI
155 */
156 public static String getProtocolName() {
157 return "unknown";
158 }
159
160 /**
161 * Encode the current transport into a URI that can be passed via intent calls.
162 * @return URI to host
163 */
164 public abstract Uri getUri(String input);
165
166
167 /**
168 * Causes transport to connect to the target host. After connecting but before a
169 * session is started, must call back to {@link TerminalBridge#onConnected()}.
170 * After that call a session may be opened.
171 */
172 public abstract void connect();
173
174 /**
175 * Checks if read() will block. If there are no bytes remaining in
176 * the underlying transport, return true.
177 */
178 public abstract boolean willBlock();
179
180 /**
181 * Reads from the transport. Transport must support reading into a byte array
182 * <code>buffer</code> at the start of <code>offset</code> and a maximum of
183 * <code>length</code> bytes. If the remote host disconnects, throw an
184 * {@link IOException}.
185 * @param buffer byte buffer to store read bytes into
186 * @param offset where to start writing in the buffer
187 * @param length maximum number of bytes to read
188 * @return number of bytes read
189 * @throws IOException when remote host disconnects
190 */
191 public abstract int read(byte[] buffer, int offset, int length) throws IOException;
192
193 /**
194 * Writes to the transport. If the host is not yet connected, simply return without
195 * doing anything. An {@link IOException} should be thrown if there is an error after
196 * connection.
197 * @param buffer bytes to write to transport
198 * @throws IOException when there is a problem writing after connection
199 */
200 public abstract void write(byte[] buffer) throws IOException;
201
202 /**
203 * Writes to the transport. See {@link #write(byte[])} for behavior details.
204 * @param c character to write to the transport
205 * @throws IOException when there is a problem writing after connection
206 */
207 public abstract void write(int c) throws IOException;
208
209 /**
210 * Flushes the write commands to the transport.
211 * @throws IOException when there is a problem writing after connection
212 */
213 public abstract void flush() throws IOException;
214
215 /**
216 * Closes the connection to the terminal. Note that the resulting failure to read
217 * should call {@link TerminalBridge#dispatchDisconnect(boolean)}.
218 */
219 public abstract void close();
220
221 /**
222 * Tells the transport what dimensions the display is currently
223 * @param columns columns of text
224 * @param rows rows of text
225 * @param width width in pixels
226 * @param height height in pixels
227 */
228 public abstract void setDimensions(int columns, int rows, int width, int height);
229
230 public void setOptions(Map<String, String> options) {
231 // do nothing
232 }
233
234 public Map<String, String> getOptions() {
235 return null;
236 }
237
238 public void setCompression(boolean compression) {
239 // do nothing
240 }
241
242 public void setHttpproxy(String httpproxy) {
243 // do nothing
244 }
245
246 public void setUseAuthAgent(String useAuthAgent) {
247 // do nothing
248 }
249
250 public String getEmulation() {
251 return emulation;
252 }
253
254 protected vt320 setupTransportBuffer() {
255 int scrollback = (host.getWantSession()) ? manager.getScrollback() : 0;
256 buffer.setBufferSize(scrollback);
257 buffer.setDisplay(bridge);
258 return buffer;
259 }
260
261 public vt320 getTransportBuffer() {
262 buffer = new vt320Default();
263 return setupTransportBuffer();
264 }
265
266 public void setLinks(TerminalManager manager, TerminalBridge bridge, String homeDirectory, HostBean host, String emulation) {
267 this.manager = manager;
268 this.bridge = bridge;
269 this.homeDirectory = homeDirectory;
270 this.host = host;
271 this.emulation = emulation;
272 }
273
274 /**
275 * Whether or not this transport type can forward ports.
276 * @return true on ability to forward ports
277 */
278 public boolean canForwardPorts() {
279 return false;
280 }
281
282 /**
283 * Adds the {@link PortForwardBean} to the list.
284 * @param portForward the port forward bean to add
285 * @return true on successful addition
286 */
287 public boolean addPortForward(PortForwardBean portForward) {
288 return false;
289 }
290
291 /**
292 * Enables a port forward member. After calling this method, the port forward should
293 * be operational iff it could be enabled by the transport.
294 * @param portForward member of our current port forwards list to enable
295 * @return true on successful port forward setup
296 */
297 public boolean enablePortForward(PortForwardBean portForward) {
298 return false;
299 }
300
301 /**
302 * Disables a port forward member. After calling this method, the port forward should
303 * be non-functioning iff it could be disabled by the transport.
304 * @param portForward member of our current port forwards list to enable
305 * @return true on successful port forward tear-down
306 */
307 public boolean disablePortForward(PortForwardBean portForward) {
308 return false;
309 }
310
311 /**
312 * Removes the {@link PortForwardBean} from the available port forwards.
313 * @param portForward the port forward bean to remove
314 * @return true on successful removal
315 */
316 public boolean removePortForward(PortForwardBean portForward) {
317 return false;
318 }
319
320 /**
321 * Gets a list of the {@link PortForwardBean} currently used by this transport.
322 * @return the list of port forwards
323 */
324 public List<PortForwardBean> getPortForwards() {
325 return null;
326 }
327
328 /**
329 * Whether or not this transport type can transfer files.
330 * @return true on ability to transfer files
331 */
332 public boolean canTransferFiles() {
333 return false;
334 }
335
336 /**
337 * Downloads the specified remote file to a local folder.
338 * @param remoteFile The path to the remote file to be downloaded. Must be non-null.
339 * @param localFolder The path to local folder. Null = default external storage folder.
340 * @return true on success, false on failure
341 */
342 public boolean downloadFile(String remoteFile, String localFolder) {
343 return false;
344 }
345
346 /**
347 * Uploads the specified local file to the remote host.
348 * @param localFile The path to the local file to be uploaded. Must be non-null.
349 * @param remoteFolder The path to the remote directory. Null == default remote directory.
350 * @return true on success, false on failure
351 */
352 public boolean uploadFile(String localFile, String remoteFile,
353 String remoteFolder, String mode) {
354 return false;
355 }
356
357
358 /**
359 * @return int default port for protocol
360 */
361 public abstract int getDefaultPort();
362 public abstract boolean isConnected();
363 public abstract boolean isSessionOpen();
364 public abstract boolean isAuthenticated();
365
366 /**
367 * @param username
368 * @param hostname
369 * @param port
370 * @return
371 */
372 public abstract String getDefaultNickname(String username, String hostname, int port);
373
374 /**
375 * @param uri
376 * @param selectionKeys
377 * @param selectionValues
378 */
379 public abstract void getSelectionArgs(Uri uri, Map<String, String> selection);
380
381 /**
382 * @param uri
383 * @return
384 */
385 public abstract HostBean createHost(Uri uri);
386
387 /**
388 * @param context context containing the correct resources
389 * @return string that hints at the format for connection
390 */
391 public abstract String getFormatHint(Context context);
392
393 /**
394 * @return do we use the network
395 */
396 public abstract boolean usesNetwork();
397
398 /**
399 * @return do we need a relay object to read from the transport
400 * and send the data into the vt320 buffer
401 */
402 public boolean needsRelay() {
403 return true;
404 }
405
406 /**
407 * @return a key listener
408 */
409 public TerminalKeyListener getTerminalKeyListener() {
410 return new TerminalKeyListener(manager, bridge, buffer, host.getEncoding());
411 }
412
413 }