Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010, 2011 SAP AG and others All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Lazar Kirchev, SAP AG - initial API and implementation IBM Corporation - ongoing development /
 
 package org.eclipse.equinox.console.telnet;
 
 
 import java.util.Set;

This class performs the processing of the telnet commands, and updates respectively what is displayed in the output. Also, it performs terminal type negotiation with the telnet client. This is important for some of the escape sequences, which are different for the different terminal types. Without such negotiation, some keys (such as backspace, del, insert, home, etc.) may not be correctly interpreted by the telnet server. Currently the supported terminal types are ANSI, VT100, VT220, VT320, XTERM and SCO. The support is limited to the following keys - BACKSPACE, DEL, INSERT, HOME, END, PAGEUP, PAGEDOWN, ARROWS.
 
 public class TelnetInputScanner extends Scanner {
 
     private boolean isCommand = false;
     private boolean isReadingTtype = false
     private boolean shouldFinish = false;
     private boolean tTypeNegotiationStarted = false;
     private int lastRead = -1;
     private ArrayList<IntegercurrentTerminalType = new ArrayList<Integer>();
     private ArrayList<IntegerlastTerminalType = null;
     private Set<StringsupportedTerminalTypes = new HashSet<String>();
     private Callback callback;
 
     public TelnetInputScanner(ConsoleInputStream toShellConsoleOutputStream toTelnetCallback callback) {
         super(toShelltoTelnet);
         TerminalTypeMappings currentMapping = .get();
     	 = currentMapping.getEscapesToKey();
     	 = currentMapping.getEscapes();
     	setBackspace(currentMapping.getBackspace());
     	setDel(currentMapping.getDel());
     	this. = callback;
     }
     
     private void initializeSupportedTerminalTypes() {
         .add("ANSI");
         .add("VT100");
         .add("VT220");
         .add("VT320");
         .add("XTERM");
         .add("SCO");     
     }
 
     public void scan(int bthrows IOException {
         b &= 0xFF;
 
         if () {
             scanEsc(b);
         } else if () {
             scanCommand(b);
         } else if (b == ) {
             startCommand();
         } else {
         	switch (b) {
                 case :
                     startEsc();
                     .add(new byte[]{(byteb});
                     break;
                 default:
                     if (b >=  && b < ) {
                         echo((byteb);
                         flush();
                     }
                     .add(new byte[]{(byteb});
             }
 
         }
          = b;
     }
 
     /* Telnet command codes are described in RFC 854, TELNET PROTOCOL SPECIFICATION
      * available at http://www.ietf.org/rfc/rfc854.txt
      * 
      * Telnet terminal type negotiation option is described in RFC 1091, Telnet Terminal-Type Option
      * available at http://www.ietf.org/rfc/rfc1091.txt
      */
    private static final int SE = 240;
    private static final int EC = 247;
    private static final int EL = 248;
    private static final int SB = 250;
    private static final int WILL = 251;
    private static final int WILL_NOT = 252;
    private static final int DO = 253;
    private static final int DO_NOT = 254;
    private static final int TTYPE = 24;
    private static final int SEND = 1;
    private static final int IAC = 255;
    private static final int IS = 0;
    private boolean isNegotiation;
    private boolean isWill;
    
    private byte[] tTypeRequest = {(byte), (byte), (byte), (byte), (byte), (byte)};
    private void scanCommand(final int bthrows IOException {
        if () {
            scanNegotiation(b);
        } else if () {
             = false;
             = false;
            if(b ==  &&  == false) {
        		sendRequest();
        	}
        } else {
            switch (b) {
                case :
                	 = true;
                	break;
                case :
                	break;
                case :
                	break;
                case :
                    break;
                case :
                     = true;
                    break;
                case :
                    eraseChar();
                     = false;
                    break;
                case :
                default:
                     = false;
                    break;
            }
        }
    }
    private void scanNegotiation(final int b) {
    	if ( ==  && b == ) {
    		 = true;
    	} else if (b == ) {
    		
    	} else if (b == ) {
    		
    	} else if (b == ) {
             = false;
             = false;
            if ( == true) {
				 = false;
				if ( == true) {
					 = false;
					return;
				}
				boolean isMatch = isTerminalTypeSupported();
				boolean isLast = isLast();
				if (isMatch == true) {
					return;
				}
				if (isLast == true && isMatch == false) {
					 = true;
else if (isLast == false && isMatch == false) {
				}
			}
        } else if ( == true){
        	.add(b);
        }
    }
    
    private boolean isTerminalTypeSupported() {
    	byte[] tmp = new byte[.size()];
    	int idx = 0;
    	for(Integer i : ) {
    		tmp[idx] = i.byteValue();
    		idx++;
    	}
    	String tType = new String(tmp);
    	
    	for(String terminal : ) {
    		if(tType.toUpperCase().contains(terminal)) {
    			return true;
    		}
    	}
    	
    	return false;
    }
    
    private boolean isLast() {
    		return true;
    	} else {
    		return false;
    	}
    }
    
    private void setCurrentTerminalType() {
    	byte[] tmp = new byte[.size()];
    	int idx = 0;
    	for(Integer i : ) {
    		tmp[idx] = i.byteValue();
    		idx++;
    	}
    	String tType = new String(tmp);
    	String term = null;
    	for(String terminal : ) {
    		if(tType.toUpperCase().contains(terminal)) {
    			term = terminal;
    		}
    	}
    	TerminalTypeMappings currentMapping = .get(term);
    	if(currentMapping == null) {
    		currentMapping = .get();
    	}
    	 = currentMapping.getEscapesToKey();
    	 = currentMapping.getEscapes();
    	setBackspace(currentMapping.getBackspace());
    	setDel(currentMapping.getDel());
    	if( != null) {
    		.finished();
    	}
    }
    
    private void sendRequest() {
    	try {
			if( == false) {
			}
catch (IOException e) {
		}
    }
    private void startCommand() {
         = true;
         = false;
         = false;
    }
    private void eraseChar() throws IOException {
        .add(new byte[]{});
    }
    protected void scanEsc(int bthrows IOException {
         += (charb;
        .add(new byte[]{(byteb});
        KEYS key = checkEscape();
        if (key == .) {
            return;
        }
        if (key == .) {
             = false;
            scan(b);
            return;
        }
         = false;
    }
New to GrepCode? Check out our FAQ X