/***************************************************************************************/
/* cfw: simple J2ME framework for HTTP connections                                     */
/*-------------------------------------------------------------------------------------*/
/* class files:                                                                        */
/*  c_main.java - main class for setting up application midlet                         */
/*  c_run.java  - thread class for operating the HTTP connection                       */
/*-------------------------------------------------------------------------------------*/
/* This source code is free for any kind of personal or educational use,               */
/* which is absolutely non-profitable.                                                 */
/*                                                                                     */
/* This source code as all derivatives of it may not be used for any                   */
/* commercial purpose. This especially excludes any rights for claiming any            */
/* kind of patents, warranty, applicability for a certain purpose, as                  */
/* finally for earning money on base of the before described inclusion range.          */
/*-------------------------------------------------------------------------------------*/
/* (c) 2004 H. Weghorn, BA-University of Cooperative Education, Germany                */
/***************************************************************************************/

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.lang.Thread.*;

// thread for HTTP connection
public class C_run extends Thread {
  Display disp = null; // will all
  Form fo = null;      // be filled out 
  String url = null;   // by MIDlet
  boolean debug;       //

  StreamConnection c1 = null; // local references
  InputStream ins = null;     // for connection, stream
  StringBuffer inp;           // and input response
 
  // contruction means intialization of above references
  public C_run ( Display di, Form f, String u, boolean de ) { 
    disp = di; url = u; fo = f; debug = de;
  }

  // running process:
  // 1. set up display screen
  // 2. set up connection and input stream
  // 3. poll input bytes till done
  // 4. display received string
  public void run () {
    int err = 0; // remember error indicator
    if ( debug ) System.out.println ( "runnning thread" );

    disp.setCurrent(fo); wait ( 500 ); // phase 1: prepare display

    try { 
      err = 1; // phase 2a
      c1 = (StreamConnection)Connector.open(url);
      fo.setTitle ( "Con created" ); wait ( 444 );

      err = 2; // phase 2b
      String ti = "Reading ";
      ins = c1.openInputStream();
      fo.setTitle ( ti );  wait ( 444 );

      err = 3; // phase 3
      int nextc, count = 0;
      byte[] nextb = new byte[1];
      inp = new StringBuffer ();

      while ( (nextc=ins.read()) != -1 ) {
        //fo.setTitle ( ti + ++count );
        nextb[0] = (byte) nextc;
        inp.append ( new String(nextb) );
      }

    }
    catch (Exception e) { 
      if ( debug ) System.out.println ( "error #" + err + " occured" );
      fo.setTitle ( "I/O error" );
      fo.delete (0);
      String em = "unknown";
      if ( err == 1 ) em = "in open";
      if ( err == 2 ) em = "in stream creation";
      if ( err == 3 ) em = "in stream reading";
      fo.append(new StringItem("", em ));
    }
    finally {
      if ( debug ) System.out.println ( "trying to close input stream and connection" );
      try { if ( ins != null ) ins.close(); } catch (Exception e) {;}
      try { if ( c1  != null ) c1.close();  } catch (Exception e) {;}
    }

    if ( inp != null ) { // phase 4
      if ( debug ) System.out.println ( "received " + inp.length() + " bytes" );  
      fo.setTitle ( "Received" );
      fo.delete (0);
      fo.append(new StringItem("", new String (inp) ));
    }

  }

  // actions for destruction
  public void destroy () { 
    try { if ( ins != null ) ins.close(); } catch (Exception e) {;}
    try { if ( c1  != null ) c1.close();  } catch (Exception eee) {;}
  }

  // utility function
  public static void wait ( int millisecs ) {
    try { java.lang.Thread.sleep ( millisecs ); }
    catch ( Exception e ) { ; /*ignored */ }
  }
}