comparison src/com/five_ten_sg/connectbot/transport/Telnet.java @ 31:139394237973 tn5250

start tn5250 integration
author Carl Byington <carl@five-ten-sg.com>
date Tue, 10 Jun 2014 12:00:07 -0700
parents b39bcf616a6f
children 77ac18bc1b2f
comparison
equal deleted inserted replaced
30:d738f6b876fe 31:139394237973
49 * 49 *
50 */ 50 */
51 public class Telnet extends AbsTransport { 51 public class Telnet extends AbsTransport {
52 private static final String TAG = "ConnectBot.Telnet"; 52 private static final String TAG = "ConnectBot.Telnet";
53 private static final String PROTOCOL = "telnet"; 53 private static final String PROTOCOL = "telnet";
54
55 private static final int DEFAULT_PORT = 23; 54 private static final int DEFAULT_PORT = 23;
56 55
57 private TelnetProtocolHandler handler; 56 private TelnetProtocolHandler handler;
58 private Socket socket; 57 private Socket socket;
59 58 private InputStream is;
60 private InputStream is; 59 private OutputStream os;
61 private OutputStream os; 60 private int width;
62 private int width; 61 private int height;
63 private int height; 62 private boolean connected = false;
64
65 private boolean connected = false;
66 63
67 static final Pattern hostmask; 64 static final Pattern hostmask;
68 static { 65 static {
69 hostmask = Pattern.compile("^([0-9a-z.-]+)(:(\\d+))?$", Pattern.CASE_INSENSITIVE); 66 hostmask = Pattern.compile("^([0-9a-z.-]+)(:(\\d+))?$", Pattern.CASE_INSENSITIVE);
70 } 67 }
106 return ""; 103 return "";
107 } 104 }
108 }; 105 };
109 } 106 }
110 107
111 /**
112 * @param host
113 * @param bridge
114 * @param manager
115 */
116 public Telnet(HostBean host, TerminalBridge bridge, TerminalManager manager) {
117 super(host, bridge, manager);
118 }
119 108
120 public static String getProtocolName() { 109 public static String getProtocolName() {
121 return PROTOCOL; 110 return PROTOCOL;
122 } 111 }
123 112
124 @Override
125 public void connect() {
126 try {
127 socket = new Socket(host.getHostname(), host.getPort());
128 connected = true;
129 is = socket.getInputStream();
130 os = socket.getOutputStream();
131 bridge.onConnected();
132 }
133 catch (UnknownHostException e) {
134 Log.d(TAG, "IO Exception connecting to host", e);
135 }
136 catch (IOException e) {
137 Log.d(TAG, "IO Exception connecting to host", e);
138 }
139 }
140
141 @Override
142 public void close() {
143 connected = false;
144
145 if (socket != null)
146 try {
147 socket.close();
148 socket = null;
149 }
150 catch (IOException e) {
151 Log.d(TAG, "Error closing telnet socket.", e);
152 }
153 }
154
155 @Override
156 public void flush() throws IOException {
157 os.flush();
158 }
159
160 @Override
161 public int getDefaultPort() {
162 return DEFAULT_PORT;
163 }
164
165 @Override
166 public boolean isConnected() {
167 return connected;
168 }
169
170 @Override
171 public boolean isSessionOpen() {
172 return connected;
173 }
174
175 @Override
176 public boolean isAuthenticated() {
177 return isConnected();
178 }
179
180 @Override
181 public boolean willBlock() {
182 if (is == null) return true;
183 try {
184 return is.available() == 0;
185 } catch (Exception e) {
186 return true;
187 }
188 }
189
190 @Override
191 public int read(byte[] buffer, int start, int len) throws IOException {
192 /* process all already read bytes */
193 int n = 0;
194
195 do {
196 n = handler.negotiate(buffer, start);
197
198 if (n > 0)
199 return n;
200 }
201 while (n == 0);
202
203 while (n <= 0) {
204 do {
205 n = handler.negotiate(buffer, start);
206
207 if (n > 0)
208 return n;
209 }
210 while (n == 0);
211
212 n = is.read(buffer, start, len);
213
214 if (n < 0) {
215 bridge.dispatchDisconnect(false);
216 throw new IOException("Remote end closed connection.");
217 }
218
219 handler.inputfeed(buffer, start, n);
220 n = handler.negotiate(buffer, start);
221 }
222
223 return n;
224 }
225
226 @Override
227 public void write(byte[] buffer) throws IOException {
228 try {
229 if (os != null)
230 os.write(buffer);
231 }
232 catch (SocketException e) {
233 bridge.dispatchDisconnect(false);
234 }
235 }
236
237 @Override
238 public void write(int c) throws IOException {
239 try {
240 if (os != null)
241 os.write(c);
242 }
243 catch (SocketException e) {
244 bridge.dispatchDisconnect(false);
245 }
246 }
247
248 @Override
249 public void setDimensions(int columns, int rows, int width, int height) {
250 try {
251 handler.setWindowSize(columns, rows);
252 }
253 catch (IOException e) {
254 Log.e(TAG, "Couldn't resize remote terminal", e);
255 }
256 }
257
258 @Override
259 public String getDefaultNickname(String username, String hostname, int port) {
260 if (port == DEFAULT_PORT) {
261 return String.format("%s", hostname);
262 }
263 else {
264 return String.format("%s:%d", hostname, port);
265 }
266 }
267 113
268 public Uri getUri(String input) { 114 public Uri getUri(String input) {
269 Matcher matcher = hostmask.matcher(input); 115 Matcher matcher = hostmask.matcher(input);
270 116
271 if (!matcher.matches()) 117 if (!matcher.matches())
300 .append(Uri.encode(input)); 146 .append(Uri.encode(input));
301 Uri uri = Uri.parse(sb.toString()); 147 Uri uri = Uri.parse(sb.toString());
302 return uri; 148 return uri;
303 } 149 }
304 150
151
152 @Override
153 public void connect() {
154 try {
155 socket = new Socket(host.getHostname(), host.getPort());
156 connected = true;
157 is = socket.getInputStream();
158 os = socket.getOutputStream();
159 bridge.onConnected();
160 }
161 catch (UnknownHostException e) {
162 Log.d(TAG, "IO Exception connecting to host", e);
163 }
164 catch (IOException e) {
165 Log.d(TAG, "IO Exception connecting to host", e);
166 }
167 }
168
169
170 @Override
171 public boolean willBlock() {
172 if (is == null) return true;
173 try {
174 return is.available() == 0;
175 } catch (Exception e) {
176 return true;
177 }
178 }
179
180
181 @Override
182 public int read(byte[] buffer, int start, int len) throws IOException {
183 /* process all already read bytes */
184 int n = 0;
185
186 do {
187 n = handler.negotiate(buffer, start);
188
189 if (n > 0)
190 return n;
191 }
192 while (n == 0);
193
194 while (n <= 0) {
195 do {
196 n = handler.negotiate(buffer, start);
197
198 if (n > 0)
199 return n;
200 }
201 while (n == 0);
202
203 n = is.read(buffer, start, len);
204
205 if (n < 0) {
206 bridge.dispatchDisconnect(false);
207 throw new IOException("Remote end closed connection.");
208 }
209
210 handler.inputfeed(buffer, start, n);
211 n = handler.negotiate(buffer, start);
212 }
213
214 return n;
215 }
216
217
218 @Override
219 public void write(byte[] buffer) throws IOException {
220 try {
221 if (os != null)
222 os.write(buffer);
223 }
224 catch (SocketException e) {
225 bridge.dispatchDisconnect(false);
226 }
227 }
228
229
230 @Override
231 public void write(int c) throws IOException {
232 try {
233 if (os != null)
234 os.write(c);
235 }
236 catch (SocketException e) {
237 bridge.dispatchDisconnect(false);
238 }
239 }
240
241
242 @Override
243 public void flush() throws IOException {
244 os.flush();
245 }
246
247
248 @Override
249 public void close() {
250 connected = false;
251
252 if (socket != null)
253 try {
254 socket.close();
255 socket = null;
256 }
257 catch (IOException e) {
258 Log.d(TAG, "Error closing telnet socket.", e);
259 }
260 }
261
262
263 @Override
264 public void setDimensions(int columns, int rows, int width, int height) {
265 try {
266 handler.setWindowSize(columns, rows);
267 }
268 catch (IOException e) {
269 Log.e(TAG, "Couldn't resize remote terminal", e);
270 }
271 }
272
273
274 @Override
275 public int getDefaultPort() {
276 return DEFAULT_PORT;
277 }
278
279 @Override
280 public boolean isConnected() {
281 return connected;
282 }
283
284 @Override
285 public boolean isSessionOpen() {
286 return isConnected();
287 }
288
289 @Override
290 public boolean isAuthenticated() {
291 return isConnected();
292 }
293
294
295 @Override
296 public String getDefaultNickname(String username, String hostname, int port) {
297 if (port == DEFAULT_PORT) {
298 return String.format("%s", hostname);
299 }
300 else {
301 return String.format("%s:%d", hostname, port);
302 }
303 }
304
305
306 @Override
307 public void getSelectionArgs(Uri uri, Map<String, String> selection) {
308 selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL);
309 selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment());
310 selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost());
311 int port = uri.getPort();
312
313 if (port < 0)
314 port = DEFAULT_PORT;
315
316 selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port));
317 }
318
319
305 @Override 320 @Override
306 public HostBean createHost(Uri uri) { 321 public HostBean createHost(Uri uri) {
307 HostBean host = new HostBean(); 322 HostBean host = new HostBean();
308 host.setProtocol(PROTOCOL); 323 host.setProtocol(PROTOCOL);
309 host.setHostname(uri.getHost()); 324 host.setHostname(uri.getHost());
324 } 339 }
325 340
326 return host; 341 return host;
327 } 342 }
328 343
329 @Override
330 public void getSelectionArgs(Uri uri, Map<String, String> selection) {
331 selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL);
332 selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment());
333 selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost());
334 int port = uri.getPort();
335
336 if (port < 0)
337 port = DEFAULT_PORT;
338
339 selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port));
340 }
341 344
342 public String getFormatHint(Context context) { 345 public String getFormatHint(Context context) {
343 return String.format("%s:%s", 346 return String.format("%s:%s",
344 context.getString(R.string.format_hostname), 347 context.getString(R.string.format_hostname),
345 context.getString(R.string.format_port)); 348 context.getString(R.string.format_port));