5776804 [rkeene@sledge /home/rkeene/archive/floydssh/telnet]$ cat -n Telnet.java
  1 package telnet;

  2 
  3 import javax.microedition.midlet.*;

  4 import javax.microedition.lcdui.*;

  5 import javax.microedition.io.*;

  6 import java.io.*;

  7 import ssh.*;

  8 
  9 /* This file is part of "Telnet Floyd".
 10  *
 11  * (c) Radek Polak 2003-2004. All Rights Reserved.
 12  *
 13  * Please visit project homepage at http://phoenix.inf.upol.cz/~polakr
 14  *
 15  * --LICENSE NOTICE--
 16  * This program is free software; you can redistribute it and/or
 17  * modify it under the terms of the GNU General Public License
 18  * as published by the Free Software Foundation; either version 2
 19  * of the License, or (at your option) any later version.
 20  *
 21  * This program is distributed in the hope that it will be useful,
 22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 24  * GNU General Public License for more details.
 25  *
 26  * You should have received a copy of the GNU General Public License
 27  * along with this program; if not, write to the Free Software
 28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 29  * --LICENSE NOTICE--
 30  *
 31  */
 32 
 33 /**
 34  * Main class for whole application. Here are all important variables defined
 35  * as public static members, so that they can be easily accessed. See also
 36  * method run, that handles main telnet loop.
 37  */
 38 
 39 public class Telnet
 40     extends MIDlet
 41     implements Runnable {
 42 
 43   private static Telnet instance;
 44 
 45   /** handles with data from telnet and with data from user */
 46   public static vt320 emulation;
 47   /** user interface */
 48   public static MidletTerminal terminal;
 49 
 50 
 51   public static Console console=new Console();
 52 
 53   /**
 54    * We will collect here data for writing. Data will be sent when nothing is
 55    * in input stream, otherwise midlet hung up.
 56    * @see #run
 57    */
 58   public static byte[] output = new byte[16];  // this will grow if needed

 59 
 60   /**
 61    * Number of bytes to be written, from output array, because it has fixed
 62    * lenght.
 63    */
 64   public static int outputCount = 0;
 65 
 66   public static SshIO sshIO = null;
 67 
 68   public static int traffic = 0;
 69 
 70   /** Time to sleep between checks for new input from connection */
 71   public static int sleepTime = 1000;
 72 
 73   /** After this count of cycles without communication on connection we will send something just to keep connection
	alive */
 74   public static int keepAliveCycles = 300; // 5 minutes for sleepTime=1000

 75 
 76   /**
 77    * Holds the socket connetion object (from the Generic Connection
 78    * Framework) that is the basis of this connection.
 79    */
 80   public static StreamConnection socket;
 81 
 82   /**
 83    * Holds the InputStream associated with the socket.
 84    */
 85   private static DataInputStream in;
 86 
 87   /**
 88    * Holds the OutputStream associated with the socket.
 89    */
 90   public static DataOutputStream out;
 91 
 92   public static String host;
 93 
 94   public static Thread reader;
 95 
 96   public static boolean useColors;
 97 
 98   /** Constructor */
 99   public Telnet() {
100 
101 //    System.out.println( Action.EXIT_APP.hashCode() );

102     instance = this;
103     useColors = getDisplay().isColor();
104 
105     // we now create a new terminal that is used for the system

106     // if you want to configure it please refer to the api docs

107     emulation = new vt320();
108     terminal = new MidletTerminal(emulation);
109     sshIO = new SshIO();
110   }
111 
112   /** Main method */
113   public void startApp() {
114     setDisplay( (Displayable) terminal);
115     reader = new Thread( this );
116   }
117   /** Handle pausing the MIDlet */
118   public void pauseApp() {
119   }
120 
121   /** Handle destroying the MIDlet */
122   public void destroyApp(boolean unconditional) {
123   }
124 
125   /** Quit the MIDlet */
126   public static void quitApp() {
127     instance.destroyApp(true);
128     instance.notifyDestroyed();
129     instance = null;
130   }
131 
132   public static void setDisplay(Displayable display) {
133     instance.getDisplay().setCurrent(display);
134   }
135 
136   public Display getDisplay() {
137     return Display.getDisplay(this);
138   }
139 
140   /**
141    * Continuously read from remote host and display the data on screen.
142    */
143   public void run() {
144 
145     int n;
146     int noInputCycles = 0;
147 
148 
149     // Connect

150     try
151     {
152       emulation.putString("Connecting...");
153       terminal.redraw();
154       socket = (StreamConnection) Connector.open("socket://"+host+":22",
155                                                  Connector.READ_WRITE, false);
156       in = socket.openDataInputStream();
157       out = socket.openDataOutputStream();
158       emulation.putString("OK");
159     }
160     catch( Exception e )
161     {
162       emulation.putString("FAILED");
163     }
164     emulation.putString("\n\r");
165     terminal.redraw();
166 
167     // Main loop

168     try {
169       for(;;) {
170 
171         n = in.available();
172 
173         if( n <= 0 )
174         {
175           if( outputCount > 0 )  // Writing

176           {
177             traffic += outputCount;
178             out.write(output, 0, outputCount);
179             outputCount = 0;
180           }
181           else  // Sleeping

182           {
183             Thread.sleep(sleepTime);
184             if( noInputCycles++ > keepAliveCycles )
185             {
186               emulation.keyTyped( 0, 'a', 0 ); // BAD HACK - SHOULD SEND AYA...

187               emulation.keyPressed( 8, '\b', 0 );
188               noInputCycles=0;
189             }
190           }
191         }
192         else // Reading

193         {
194           byte[] b = new byte[n];
195           in.read( b, 0, n );
196           byte[] tmp = new byte[n];
197           System.arraycopy(b, 0, tmp, 0, n);
198           byte[] buffer = sshIO.handleSSH(tmp);
199           if(buffer != null && buffer.length > 0)
200           {
201             if (n > 0) {
202               try {
203                 emulation.putString(new String(buffer));
204                 terminal.redraw();
205               }
206               catch (Exception e) {}
207             }
208           }
209            noInputCycles = 0;
210         }
211       } // while( connected )

212     }
213     catch (Exception e) {
214       console.append( e.getMessage());
215     }
216   }
217 }
5776805 [rkeene@sledge /home/rkeene/archive/floydssh/telnet]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2004-03-06 22:47:32