?? myne64applet.java
字號:
/* * MyNE64Applet.java * * Created on March 17, 2005, 12:52 PM */package my.NE64.pkg;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URL;import java.net.*;/** * * @author rat579 */public class MyNE64Applet extends java.applet.Applet implements Runnable { private static final long serialVersionUID = 1; //just to get rid of warning private Thread NE64_App_Thread = null; final static String Version = "NE64 Application v0.1, (c) 2005 ."; private InputStream NE64_input_strm; private OutputStream NE64_output_strm; private int machine_type; private short machine_name_size; private String machine_name; private int machine_serial; private int machine_reorder_level; private int machine_Coolness; private int remote_temp_set; private short product1_name_size; private String product1_name; private float product1_cost; private short product1_units; private float product1_drop; private short product1_net_year; private short product2_name_size; private String product2_name; private float product2_cost; private short product2_units; private float product2_drop; private short product2_net_year; private short time_size; private String time; private float machine_total_drop; private int machine_sine; private short SW_ver_size; private String SW_ver; private short NotifyAddr_size; private String NotifyAddr; private int machine_DST; private int machine_Alarm; private short machine_message_size; private String machine_message; private short machine_SMTP_URL_size; private String machine_SMTP_URL; private short machine_TIME_URL_size; private String machine_TIME_URL; private short machine_SMTP_IP_size; private String machine_SMTP_IP; // Get applet information public String getAppletInfo() { System.out.println( "End getAppletInfo" ); return Version + "\r\n" + "Applet Information :\r\n" + "Name: NE64 Application\r\n" + "Author: rat579\r\n"; } // The init() method is called by the AWT when an applet is first loaded or // reloaded. Override this method to perform whatever initialization your // applet needs, such as initializing data structures, loading images or // fonts, creating frame windows, setting the layout manager, or adding UI // components. //-------------------------------------------------------------------------- /** Initializes the applet MyNE64Applet */ public void init() { try { System.out.println( "Start initComponents" ); initComponents(); } catch (Exception ex) { System.out.println( "Error initComponents" ); ex.printStackTrace(); } System.out.println( "End initComponents" ); } // THREAD SUPPORT // The run() method is called when the applet's thread is started. If // your applet performs any ongoing activities without waiting for user // input, the code for implementing that behavior typically goes here. For // example, for an applet that performs animation, the run() method controls // the display of images. //-------------------------------------------------------------------------- public void run() { System.out.println( "Start run" ); int nbytes; long MSDI_val; while (NE64_App_Thread != null && NE64_App_Thread.isAlive()) { //************************************************* if (NE64_input_strm != null ) { try { byte b[] = new byte[332]; //size of xmit buffer nbytes = NE64_input_strm.read(b,0,332); // read buffer into b[] if (nbytes == 332) // make sure got all { updateDisplay(b); } } catch (Exception e) { System.out.println("Error reading from socket: "+e); } } // if (NE64_output_strm != null) { // try { // byte a[] = new byte[184]; //size of xmit buffer // NE64_output_strm.write(a,0,184); // } catch (Exception e) { // System.out.println("Error writing to socket: "+e); // } // } } System.out.println( "End Run" ); } //------------------------------------------------------------------ // The start() method is called when the page containing the applet // first appears on the screen. //-------------------------------------------------------------------------- public void start() { String parm; System.out.println( "Start" ); if (NE64_App_Thread == null) { NE64_App_Thread = new Thread(this); NE64_App_Thread.start(); // NE64_App_Thread.setPriority(5); } //---------------------------------------------------------------------- // if there is no connection (shouldn't be) start one. //---------------------------------------------------------------------- if (NE64_input_strm == null) { try { parm = this.getParameter("src"); if (parm == null) { parm = "socket:localhost:2001"; System.out.println( "No Source IP specified" ); //return; } if (parm.startsWith("socket:")) { // eg. URL-style spec.: socket://[host:]port // host is optional, defaults to the host // the applet came from. // there must be a port number though. // NE64_input_strm = openSocketStreamInput(getDocumentBase(), parm.substring(7)); System.out.println( "openSocketStreamInput Socket ="+ parm.substring(7)); } //else { // NE64_input_strm = new URL(getDocumentBase(), parm).openStream(); // System.out.println( "URL Socket ="+ parm); //} } catch (Exception e) { System.out.println("Error opening Stream : "+e); return; } } } // The stop() method is called when the page containing the applet is // no longer on the screen. //-------------------------------------------------------------------------- public void stop() { System.out.println( "Stoping" ); //---------------------------------------------------------------------- // Close down the connection //---------------------------------------------------------------------- if (NE64_App_Thread != null) { // NE64_App_Thread.stop(); NE64_App_Thread = null; System.out.println( "NE64_App_Thread = null" ); } if (NE64_input_strm != null) { try { NE64_input_strm.close(); NE64_output_strm.close(); System.out.println( "NE64_input_strm.closed" ); } catch (Exception e) { System.out.println("Error closing Socket: "+e); } } NE64_input_strm = null; System.out.println( "Stopped" ); } private InputStream openSocketStreamInput(URL context, String hostnport) throws IOException { int colon = hostnport.lastIndexOf(':'); String host, portstr; int port; host = ""; portstr = ""; System.out.println( "openSocketStreamInput" ); if (colon <= 0) { host = context.getHost(); portstr = (colon == 0) ? hostnport.substring(1) : hostnport; } else { try { host = hostnport.substring(0, colon); portstr = hostnport.substring(colon+1); } catch (StringIndexOutOfBoundsException ex) { throw new IOException("invalid 'src' socket"); } } try { port = Integer.parseInt(portstr); } catch (Exception ex) { throw new IOException("port is not a number: "+portstr); } return ((new Socket(host,port)).getInputStream()); } private OutputStream openSocketStreamOutput(URL context, String hostnport) throws IOException { int colon = hostnport.lastIndexOf(':'); String host, portstr; int port; host = ""; portstr = ""; System.out.println( "openSocketStreamOutput" ); if (colon <= 0) { host = context.getHost(); portstr = (colon == 0) ? hostnport.substring(1) : hostnport; } else { try { host = hostnport.substring(0, colon); portstr = hostnport.substring(colon+1); } catch (StringIndexOutOfBoundsException ex) { throw new IOException("invalid 'src' socket"); } } try { port = Integer.parseInt(portstr); } catch (Exception ex) { throw new IOException("port is not a number: "+portstr); } return ((new Socket(host,port+1)).getOutputStream()); } /** * Converts a 4 byte array of unsigned bytes to Int * @param b an array of 4 unsigned bytes * @return a long representing the unsigned int */ public static final int unsignedIntToInt(byte[] b,int x) { int l = 0; l |= b[x] & 0xFF; l <<= 8; l |= b[x+1] & 0xFF; l <<= 8; l |= b[x+2] & 0xFF; l <<= 8; l |= b[x+3] & 0xFF; return l; } /** * Converts a 4 byte array of unsigned bytes to float * @param b an array of 4 unsigned bytes * @return a long representing the unsigned int */ public static final float unsignedIntToFloat(byte[] b,int x) { int l = 0; l |= b[x] & 0xFF; l <<= 8; l |= b[x+1] & 0xFF; l <<= 8; l |= b[x+2] & 0xFF; l <<= 8; l |= b[x+3] & 0xFF; return l; } /** * Converts a two byte array to an integer * @param b a byte array of length 2 * @return an int representing the unsigned short */ public static final int unsignedShortToInt(byte[] b,int x) { int i = 0; i |= b[x] & 0xFF; i <<= 8; i |= b[x+1] & 0xFF; return i; } /** * Converts a one byte array to an integer * @param b a byte array of length 1 * @return an short representing the unsigned byte */ public static final short unsignedByteToShort(byte[] b,int x) { short i = 0; i |= b[x] & 0xFF; return i; } /** * Update the JAVA display */ public final void updateDisplay(byte[] b) { machine_type = unsignedShortToInt(b,0); switch(machine_type) { case 1000 : jLabel_machine_type.setText("NE64 Demo");break; case 2000 : jLabel_machine_type.setText("NE64 EVB");break; default : jLabel_machine_type.setText(Integer.toString(machine_type)); } machine_name_size = unsignedByteToShort(b,2); machine_name = (new String(b,3,machine_name_size)); jTextField_machine_name.setText((machine_name)); machine_serial = unsignedShortToInt(b,23); jLabel_machine_serial.setText(Integer.toString(machine_serial)); machine_reorder_level = unsignedShortToInt(b,25); jTextField_machine_reorder_level.setText(Integer.toString(machine_reorder_level)); machine_Coolness = unsignedShortToInt(b,27); jProgressBar2.setValue(machine_Coolness); remote_temp_set = unsignedShortToInt(b,29); product1_cost = java.lang.Float.intBitsToFloat(unsignedIntToInt(b,52)); jTextField_product1_cost.setText("$ "+Float.toString(product1_cost)); product1_units = unsignedByteToShort(b,56); jLabel_product1_units.setText(Integer.toString(product1_units)); if (product1_units ==0){ jRadioButton1_Product1_empty.setSelected(true); jTextField_product1_name.setText("Out Of Service"); }else{ jRadioButton1_Product1_empty.setSelected(false); product1_name_size = unsignedByteToShort(b,31);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -