annotate src/com/five_ten_sg/connectbot/transport/Telnet.java @ 130:2a0b10fd128d

host editor database values override summary text only if non-empty
author Carl Byington <carl@five-ten-sg.com>
date Thu, 19 Jun 2014 09:56:25 -0700
parents 77ac18bc1b2f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /*
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2 * ConnectBot: simple, powerful, open-source SSH client for Android
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 *
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 * Licensed under the Apache License, Version 2.0 (the "License");
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 * you may not use this file except in compliance with the License.
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 * You may obtain a copy of the License at
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 *
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 * http://www.apache.org/licenses/LICENSE-2.0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 *
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 * Unless required by applicable law or agreed to in writing, software
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 * distributed under the License is distributed on an "AS IS" BASIS,
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 * See the License for the specific language governing permissions and
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 * limitations under the License.
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 package com.five_ten_sg.connectbot.transport;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 import java.io.IOException;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 import java.io.InputStream;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22 import java.io.OutputStream;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 import java.net.Socket;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 import java.net.SocketException;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
25 import java.net.UnknownHostException;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 import java.nio.charset.Charset;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27 import java.util.Map;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28 import java.util.regex.Matcher;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 import java.util.regex.Pattern;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 import com.five_ten_sg.connectbot.R;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 import com.five_ten_sg.connectbot.bean.HostBean;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33 import com.five_ten_sg.connectbot.service.TerminalBridge;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34 import com.five_ten_sg.connectbot.service.TerminalManager;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 import com.five_ten_sg.connectbot.util.HostDatabase;
11
f3b3bbd227b8 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
36
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 import android.content.Context;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38 import android.net.Uri;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39 import android.util.Log;
13
b39bcf616a6f adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents: 12
diff changeset
40
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 import de.mud.telnet.TelnetProtocolHandler;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
42
13
b39bcf616a6f adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents: 12
diff changeset
43
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44 /**
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45 * Telnet transport implementation.<br/>
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46 * Original idea from the JTA telnet package (de.mud.telnet)
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47 *
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48 * @author Kenny Root
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49 *
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51 public class Telnet extends AbsTransport {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52 private static final String TAG = "ConnectBot.Telnet";
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 private static final String PROTOCOL = "telnet";
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 private static final int DEFAULT_PORT = 23;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56 private TelnetProtocolHandler handler;
31
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
57 private Socket socket;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
58 private InputStream is;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
59 private OutputStream os;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
60 private int width;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
61 private int height;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
62 private boolean connected = false;
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
64 static final Pattern hostmask;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65 static {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66 hostmask = Pattern.compile("^([0-9a-z.-]+)(:(\\d+))?$", Pattern.CASE_INSENSITIVE);
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69 public Telnet() {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
70 handler = new TelnetProtocolHandler() {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
71 /** get the current terminal type */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
72 @Override
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73 public String getTerminalType() {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74 return getEmulation();
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
76 /** get the current window size */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
77 @Override
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
78 public int[] getWindowSize() {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
79 return new int[] { width, height };
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
80 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
81 /** notify about local echo */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
82 @Override
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 public void setLocalEcho(boolean echo) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84 /* EMPTY */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 /** write data to our back end */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
87 @Override
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88 public void write(byte[] b) throws IOException {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89 if (os != null)
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
90 os.write(b);
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
91 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92 /** sent on IAC EOR (prompt terminator for remote access systems). */
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
93 @Override
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
94 public void notifyEndOfRecord() {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
96 @Override
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
97 protected String getCharsetName() {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
98 Charset charset = bridge.getCharset();
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
99
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
100 if (charset != null)
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
101 return charset.name();
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102 else
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
103 return "";
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
104 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
105 };
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
106 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
107
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
108
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
109 public static String getProtocolName() {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110 return PROTOCOL;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
111 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
112
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
113
12
6aaefb22d876 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents: 11
diff changeset
114 public Uri getUri(String input) {
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
115 Matcher matcher = hostmask.matcher(input);
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
116
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
117 if (!matcher.matches())
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
118 return null;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
119
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
120 StringBuilder sb = new StringBuilder();
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
121 sb.append(PROTOCOL)
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
122 .append("://")
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
123 .append(matcher.group(1));
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
124 String portString = matcher.group(3);
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
125 int port = DEFAULT_PORT;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
126
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
127 if (portString != null) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
128 try {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
129 port = Integer.parseInt(portString);
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
130
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
131 if (port < 1 || port > 65535) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
132 port = DEFAULT_PORT;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
133 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
134 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
135 catch (NumberFormatException nfe) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
136 // Keep the default port
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
137 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
138 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
139
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
140 if (port != DEFAULT_PORT) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
141 sb.append(':');
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
142 sb.append(port);
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
143 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
144
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
145 sb.append("/#")
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
146 .append(Uri.encode(input));
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
147 Uri uri = Uri.parse(sb.toString());
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
148 return uri;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
149 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
150
31
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
151
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
152 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
153 public void connect() {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
154 try {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
155 socket = new Socket(host.getHostname(), host.getPort());
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
156 connected = true;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
157 is = socket.getInputStream();
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
158 os = socket.getOutputStream();
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
159 bridge.onConnected();
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
160 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
161 catch (UnknownHostException e) {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
162 Log.d(TAG, "IO Exception connecting to host", e);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
163 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
164 catch (IOException e) {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
165 Log.d(TAG, "IO Exception connecting to host", e);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
166 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
167 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
168
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
169
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
170 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
171 public boolean willBlock() {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
172 if (is == null) return true;
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 31
diff changeset
173
31
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
174 try {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
175 return is.available() == 0;
112
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 31
diff changeset
176 }
77ac18bc1b2f cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 31
diff changeset
177 catch (Exception e) {
31
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
178 return true;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
179 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
180 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
181
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
182
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
183 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
184 public int read(byte[] buffer, int start, int len) throws IOException {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
185 /* process all already read bytes */
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
186 int n = 0;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
187
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
188 do {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
189 n = handler.negotiate(buffer, start);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
190
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
191 if (n > 0)
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
192 return n;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
193 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
194 while (n == 0);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
195
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
196 while (n <= 0) {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
197 do {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
198 n = handler.negotiate(buffer, start);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
199
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
200 if (n > 0)
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
201 return n;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
202 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
203 while (n == 0);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
204
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
205 n = is.read(buffer, start, len);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
206
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
207 if (n < 0) {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
208 bridge.dispatchDisconnect(false);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
209 throw new IOException("Remote end closed connection.");
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
210 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
211
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
212 handler.inputfeed(buffer, start, n);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
213 n = handler.negotiate(buffer, start);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
214 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
215
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
216 return n;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
217 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
218
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
219
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
220 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
221 public void write(byte[] buffer) throws IOException {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
222 try {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
223 if (os != null)
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
224 os.write(buffer);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
225 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
226 catch (SocketException e) {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
227 bridge.dispatchDisconnect(false);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
228 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
229 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
230
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
231
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
232 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
233 public void write(int c) throws IOException {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
234 try {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
235 if (os != null)
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
236 os.write(c);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
237 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
238 catch (SocketException e) {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
239 bridge.dispatchDisconnect(false);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
240 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
241 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
242
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
243
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
244 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
245 public void flush() throws IOException {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
246 os.flush();
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
247 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
248
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
249
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
250 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
251 public void close() {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
252 connected = false;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
253
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
254 if (socket != null)
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
255 try {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
256 socket.close();
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
257 socket = null;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
258 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
259 catch (IOException e) {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
260 Log.d(TAG, "Error closing telnet socket.", e);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
261 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
262 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
263
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
264
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
265 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
266 public void setDimensions(int columns, int rows, int width, int height) {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
267 try {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
268 handler.setWindowSize(columns, rows);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
269 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
270 catch (IOException e) {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
271 Log.e(TAG, "Couldn't resize remote terminal", e);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
272 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
273 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
274
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
275
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
276 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
277 public int getDefaultPort() {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
278 return DEFAULT_PORT;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
279 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
280
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
281 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
282 public boolean isConnected() {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
283 return connected;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
284 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
285
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
286 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
287 public boolean isSessionOpen() {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
288 return isConnected();
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
289 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
290
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
291 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
292 public boolean isAuthenticated() {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
293 return isConnected();
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
294 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
295
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
296
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
297 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
298 public String getDefaultNickname(String username, String hostname, int port) {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
299 if (port == DEFAULT_PORT) {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
300 return String.format("%s", hostname);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
301 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
302 else {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
303 return String.format("%s:%d", hostname, port);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
304 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
305 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
306
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
307
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
308 @Override
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
309 public void getSelectionArgs(Uri uri, Map<String, String> selection) {
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
310 selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL);
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
311 selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment());
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
312 selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost());
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
313 int port = uri.getPort();
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
314
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
315 if (port < 0)
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
316 port = DEFAULT_PORT;
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
317
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
318 selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port));
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
319 }
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
320
139394237973 start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents: 13
diff changeset
321
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
322 @Override
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
323 public HostBean createHost(Uri uri) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
324 HostBean host = new HostBean();
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
325 host.setProtocol(PROTOCOL);
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
326 host.setHostname(uri.getHost());
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
327 int port = uri.getPort();
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
328
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
329 if (port < 0)
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
330 port = DEFAULT_PORT;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
331
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
332 host.setPort(port);
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
333 String nickname = uri.getFragment();
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
334
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
335 if (nickname == null || nickname.length() == 0) {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
336 host.setNickname(getDefaultNickname(host.getUsername(),
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
337 host.getHostname(), host.getPort()));
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
338 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
339 else {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
340 host.setNickname(uri.getFragment());
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
341 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
342
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
343 return host;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
344 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
345
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
346
12
6aaefb22d876 adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents: 11
diff changeset
347 public String getFormatHint(Context context) {
0
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
348 return String.format("%s:%s",
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
349 context.getString(R.string.format_hostname),
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
350 context.getString(R.string.format_port));
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
351 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
352
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
353 @Override
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
354 public boolean usesNetwork() {
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
355 return true;
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
356 }
0ce5cc452d02 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
357 }