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 org.crsh.term.CodeType;
026 import org.crsh.term.spi.TermIO;
027
028 import java.io.EOFException;
029 import java.io.IOException;
030 import java.net.SocketException;
031
032 /**
033 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
034 * @version $Revision$
035 */
036 public class TelnetIO implements TermIO {
037
038 /** . */
039 private final Connection conn;
040
041 /** . */
042 private final BasicTerminalIO termIO;
043
044 public TelnetIO(Connection conn) {
045 this.conn = conn;
046 this.termIO = conn.getTerminalIO();
047 }
048
049 public int read() throws IOException {
050 try {
051 return termIO.read();
052 }
053 catch (EOFException e) {
054 return TerminalIO.HANDLED;
055 }
056 catch (SocketException e) {
057 return TerminalIO.HANDLED;
058 }
059 }
060
061 public int getWidth() {
062 return termIO.getColumns();
063 }
064
065 public CodeType decode(int code) {
066 switch (code) {
067 case 3:
068 return CodeType.BREAK;
069 case TerminalIO.TABULATOR:
070 return CodeType.TAB;
071 case TerminalIO.DELETE:
072 case TerminalIO.BACKSPACE:
073 return CodeType.BACKSPACE;
074 case TerminalIO.UP:
075 return CodeType.UP;
076 case TerminalIO.DOWN:
077 return CodeType.DOWN;
078 case TerminalIO.RIGHT:
079 return CodeType.RIGHT;
080 case TerminalIO.LEFT:
081 return CodeType.LEFT;
082 case TerminalIO.HANDLED:
083 return CodeType.CLOSE;
084 default:
085 return CodeType.CHAR;
086 }
087 }
088
089 public void close() {
090 conn.close();
091 }
092
093 public void flush() throws IOException {
094 termIO.flush();
095 }
096
097 public void write(String s) throws IOException {
098 termIO.write(s);
099 }
100
101 public void write(char c) throws IOException {
102 termIO.write(c);
103 }
104
105 public void writeDel() throws IOException {
106 termIO.moveLeft(1);
107 termIO.write(' ');
108 termIO.moveLeft(1);
109 termIO.flush();
110 }
111
112 public void writeCRLF() throws IOException {
113 termIO.write("\r\n");
114 }
115
116 public boolean moveRight(char c) throws IOException {
117 termIO.moveRight(1);
118 return true;
119 }
120
121 public boolean moveLeft() throws IOException {
122 termIO.moveLeft(1);
123 return true;
124 }
125 }