001    /*
002     * Copyright (C) 2010 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.telnet.term;
021    
022    import net.wimpi.telnetd.io.BasicTerminalIO;
023    import net.wimpi.telnetd.io.TerminalIO;
024    import net.wimpi.telnetd.net.Connection;
025    import net.wimpi.telnetd.net.ConnectionData;
026    import org.crsh.term.CodeType;
027    import org.crsh.term.spi.TermIO;
028    
029    import java.io.EOFException;
030    import java.io.IOException;
031    import java.net.SocketException;
032    import java.util.HashMap;
033    
034    /**
035     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
036     * @version $Revision$
037     */
038    public class TelnetIO implements TermIO {
039    
040      /** . */
041      private final Connection conn;
042    
043      /** . */
044      private final BasicTerminalIO termIO;
045    
046      public TelnetIO(Connection conn) {
047        this.conn = conn;
048        this.termIO = conn.getTerminalIO();
049      }
050    
051      public int read() throws IOException {
052        try {
053          return termIO.read();
054        }
055        catch (EOFException e) {
056          return TerminalIO.HANDLED;
057        }
058        catch (SocketException e) {
059          return TerminalIO.HANDLED;
060        }
061      }
062    
063      public int getWidth() {
064        return termIO.getColumns();
065      }
066    
067      public String getProperty(String name) {
068        ConnectionData data = conn.getConnectionData();
069        if (data != null)
070        {
071          HashMap map = data.getEnvironment();
072          if (map != null) {
073            Object value = map.get(name);
074            if (value != null) {
075              return value.toString();
076            }
077          }
078        }
079        return null;
080      }
081    
082      public CodeType decode(int code) {
083        switch (code) {
084          case 3:
085            return CodeType.BREAK;
086          case TerminalIO.TABULATOR:
087            return CodeType.TAB;
088          case TerminalIO.DELETE:
089          case TerminalIO.BACKSPACE:
090            return CodeType.BACKSPACE;
091          case TerminalIO.UP:
092            return CodeType.UP;
093          case TerminalIO.DOWN:
094            return CodeType.DOWN;
095          case TerminalIO.RIGHT:
096            return CodeType.RIGHT;
097          case TerminalIO.LEFT:
098            return CodeType.LEFT;
099          case TerminalIO.HANDLED:
100            return CodeType.CLOSE;
101          default:
102            return CodeType.CHAR;
103        }
104      }
105    
106      public void close() {
107        conn.close();
108      }
109    
110      public void flush() throws IOException {
111        termIO.flush();
112      }
113    
114      public void write(String s) throws IOException {
115        termIO.write(s);
116      }
117    
118      public void write(char c) throws IOException {
119        termIO.write(c);
120      }
121    
122      public void writeDel() throws IOException {
123        termIO.moveLeft(1);
124        termIO.write(' ');
125        termIO.moveLeft(1);
126        termIO.flush();
127      }
128    
129      public void writeCRLF() throws IOException {
130        termIO.write("\r\n");
131      }
132    
133      public boolean moveRight(char c) throws IOException {
134        termIO.moveRight(1);
135        return true;
136      }
137    
138      public boolean moveLeft() throws IOException {
139        termIO.moveLeft(1);
140        return true;
141      }
142    }