From 5203997a05774de8374a501e9eef4b77b55f8497 Mon Sep 17 00:00:00 2001 From: felix Date: Wed, 14 Feb 2007 21:16:38 +0000 Subject: [PATCH] - das grosse warten wurde wieder rausgenommen - das wichtigste: in der Methode send(int[]) werden nun die ints zuerst in ein byte array gewandelt und das das dann verschickt. sonst wurde (warum auch immer) zuerst das START_CMD einzeln geschickt und dann erst der rest. git-svn-id: https://www.internetallee.de/svn/bytewurf@8 a944a559-bf0e-0410-8ddc-85264b264b6c --- projekte/netzschalter/src/Communication.java | 687 ++++++++++++++------------- 1 file changed, 344 insertions(+), 343 deletions(-) diff --git a/projekte/netzschalter/src/Communication.java b/projekte/netzschalter/src/Communication.java index e162902..29e1daa 100644 --- a/projekte/netzschalter/src/Communication.java +++ b/projekte/netzschalter/src/Communication.java @@ -3,13 +3,11 @@ */ import java.io.BufferedReader; -//import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.InterruptedIOException; -//import java.io.OutputStreamWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; @@ -20,362 +18,365 @@ import java.net.UnknownHostException; * This class does the TCP/IP work to operate the XPort Most of the get and set * methods herein follow the same pattern: Establish a connection, prepare a * command, send this command and disconnect. - * + * */ public class Communication { - /** connection to XPort */ - protected Socket socket = null; + /** connection to XPort */ + protected Socket socket = null; + + /** dis contains data provided by XPort */ + protected BufferedReader dis = null; + + protected DataInputStream dis2 = null; + + /** dos contains data sent to XPort */ + protected DataOutputStream dos = null; + + /** + * This method converts the given String ipa_str and the int port into a + * useable TCP/IP address and then calls a helper method tcpip_connect() to + * establish a connection to the XPort + * + * @param ipa_str + * @param port + * @throws IOException + * @throws Exception + */ + public void connect(String ipa_str, int port) throws IOException, Exception { + InetAddress ipa = null; + try { + ipa = InetAddress.getByName(ipa_str); + } catch (UnknownHostException UHEx) { + throw UHEx; + } catch (Exception Ex) { + throw Ex; + } + try { + tcpip_connect(ipa, port); + } catch (IOException IOEx) { + throw IOEx; + } catch (Exception Ex) { + throw Ex; + } + } + + /** + * this method will end the current connection to the XPort + * + * @throws IOException + * @throws Exception + */ + public synchronized void disconnect() throws IOException, Exception { + try { + tcpip_disconnect(); + } catch (IOException IOEx) { + throw IOEx; + } catch (Exception Ex) { + throw Ex; + } + } + + /** + * uses a TCP/IP connection to send a String to the XPort + * + * @param sendString + * @throws IOException + * @throws Exception + */ + public synchronized void send(String sendString) throws IOException, + Exception { + System.out.println(sendString); + try { + dos.writeBytes(sendString);// , 0, sendString.length()); + // write data to dos as String starting at String[0] + // and ending at String[String.length()] + } catch (IOException IOEx) { + throw IOEx; + } catch (Exception Ex) { + throw Ex; + } + try { + dos.flush(); // write dos to XPort + } catch (IOException IOEx) { + throw IOEx; + } catch (Exception Ex) { + throw Ex; + } + return; + } + + /** + * uses a TCP/IP connection to send a String to the XPort + * + * @param sendByte + * @throws IOException + * @throws Exception + */ + public synchronized void send(int sendByte[]) throws IOException, Exception { + dos.flush(); + byte[] b = new byte[sendByte.length]; + for (int i = 0; i < sendByte.length; i++) { + b[i] = (byte)(sendByte[i] & 255); + } + try { + dos.write(b); // bytewise write data to dos + } catch (IOException IOEx) { + throw IOEx; + } catch (Exception Ex) { + throw Ex; + } + try { + dos.flush(); // write dos to XPort + } catch (IOException IOEx) { + throw IOEx; + } catch (Exception Ex) { + throw Ex; + } + + return; + } + + /** + * closes the TCP/IP connection + * + * @throws IOException + * @throws Exception + */ + private void tcpip_disconnect() throws IOException, Exception { + if (socket.isConnected()) { + try { + dis.close(); // close input and output stream + dos.close(); // as well as the socket + socket.close(); + } catch (IOException IOEx) { + throw IOEx; + } catch (Exception Ex) { + throw Ex; + } + } + } + + /** + * reads data supplied by the XPort and returns a String + * + * @return + * @throws InterruptedIOException + * @throws IOException + * @throws Exception + */ + public String receiveString() throws InterruptedIOException, IOException, + Exception { + String antwort = ""; + int length = 255; + byte[] in = new byte[length]; + + try { + dis.ready(); // check if data is available (actually redundant + // since + // the calling method from the GUI class already made + // this sure but better safe than sorry... :-) + } catch (IOException IOEx) { + throw IOEx; + } catch (Exception Ex) { + throw Ex; + } + + try { + + length = readBuffer(in, length); // call helper method to read + // from dis + antwort = new String(in, 0, length); + } catch (IOException IOEx) { + throw IOEx; + } catch (Exception Ex) { + throw Ex; + } + return antwort; + } - /** dis contains data provided by XPort */ - protected BufferedReader dis = null; - - protected DataInputStream dis2 =null; - - /** dos contains data sent to XPort */ - protected DataOutputStream dos=null; - - /** - * This method converts the given String ipa_str and the int port into a - * useable TCP/IP address and then calls a helper method tcpip_connect() to - * establish a connection to the XPort - * - * @param ipa_str - * @param port - * @throws IOException - * @throws Exception - */ - public void connect(String ipa_str, int port) throws IOException, Exception { - InetAddress ipa = null; - try { - ipa = InetAddress.getByName(ipa_str); - } catch (UnknownHostException UHEx) { - throw UHEx; - } catch (Exception Ex) { - throw Ex; - } - try { - tcpip_connect(ipa, port); - } catch (IOException IOEx) { - throw IOEx; - } catch (Exception Ex) { - throw Ex; - } - } + /** + * this method will read from DataInputStream dis, convert the chars read + * into ASCII and return its answer as a String + * + * @return + * @throws IOException + * @throws Exception + */ + public synchronized int readBuffer(int[] gelesen, int length) + throws IOException, Exception { + int i = 0; // init helper variables + // byte [] gelesen = new byte[length.intValue()]; + // while (!dis.ready() && i++<20) { + // Thread.sleep(100); + // } + // i=0; + while ((dis.ready()) & (i < length)) { // loop while data available + if (gelesen != null) + gelesen[i++] = dis2.readUnsignedByte();// read from dis + else + dis2.readUnsignedByte();// read from dis + } - /** - * this method will end the current connection to the XPort - * - * @throws IOException - * @throws Exception - */ - public synchronized void disconnect() throws IOException, Exception { - try { - tcpip_disconnect(); - } catch (IOException IOEx) { - throw IOEx; - } catch (Exception Ex) { - throw Ex; - } - } + return i; + } - /** - * uses a TCP/IP connection to send a String to the XPort - * - * @param sendString - * @throws IOException - * @throws Exception - */ - public synchronized void send(String sendString) throws IOException, - Exception { - System.out.println(sendString); - try { - dos.writeBytes(sendString);//, 0, sendString.length()); - // write data to dos as String starting at String[0] - // and ending at String[String.length()] - } catch (IOException IOEx) { - throw IOEx; - } catch (Exception Ex) { - throw Ex; - } - try { - dos.flush(); // write dos to XPort - } catch (IOException IOEx) { - throw IOEx; - } catch (Exception Ex) { - throw Ex; - } - return; - } + /** + * this method will read from DataInputStream dis, convert the chars read + * into ASCII and return its answer as a String + * + * @return + * @throws IOException + * @throws Exception + */ + public synchronized int readBuffer(byte[] gelesen, int length) + throws IOException, Exception { + int i = 0; // init helper variables + // byte [] gelesen = new byte[length.intValue()]; + // while (!dis.ready() && i++<20) { + // Thread.sleep(100); + // } + // i=0; + while ((dis.ready()) & (i < length)) { // loop while data available + gelesen[i++] = (byte) dis2.readUnsignedByte();// read from dis - /** - * uses a TCP/IP connection to send a String to the XPort - * - * @param sendByte - * @throws IOException - * @throws Exception - */ - public synchronized void send(int sendByte[]) throws IOException, - Exception { - int dummy; - try { - for (int i = 0; i < sendByte.length; i++) { - dummy= sendByte[i] & 255; - dos.write(dummy); // bytewise write data to dos - } - } catch (IOException IOEx) { - throw IOEx; - } catch (Exception Ex) { - throw Ex; - } - try { - dos.flush(); // write dos to XPort - } catch (IOException IOEx) { - throw IOEx; - } catch (Exception Ex) { - throw Ex; - } + } - return; - } + return i; + } - /** - * closes the TCP/IP connection - * - * @throws IOException - * @throws Exception - */ - private void tcpip_disconnect() throws IOException, Exception { - if (socket.isConnected()) { - try { - dis.close(); // close input and output stream - dos.close(); // as well as the socket - socket.close(); - } catch (IOException IOEx) { - throw IOEx; - } catch (Exception Ex) { - throw Ex; - } - } - } + /** + * reads data supplied by the XPort and returns an array of bytes + * + * @param length + * @return + * @throws InterruptedIOException + * @throws IOException + * @throws Exception + */ + public byte[] receiveByteArray(int length) throws InterruptedIOException, + IOException, Exception { + byte[] antwort = { 0, // this method hardly differs from the string + 0, 0, 0, 0 }; // reading one, so same comments apply + try { + dis.ready(); + } catch (IOException IOEx) { + throw IOEx; + } catch (Exception Ex) { + throw Ex; + } + try { + antwort = readByteArrayBuffer(length); + } catch (IOException IOEx) { + throw IOEx; + } catch (Exception Ex) { + throw Ex; + } + return antwort; + } - /** - * reads data supplied by the XPort and returns a String - * - * @return - * @throws InterruptedIOException - * @throws IOException - * @throws Exception - */ - public String receiveString() throws InterruptedIOException, IOException, - Exception { - String antwort = ""; - int length=255; - byte[] in = new byte[length]; - - try { - dis.ready(); // check if data is available (actually redundant since - // the calling method from the GUI class already made - // this sure but better safe than sorry... :-) - } catch (IOException IOEx) { - throw IOEx; - } catch (Exception Ex) { - throw Ex; - } - - try { - - length = readBuffer(in,length); // call helper method to read from dis - antwort= new String(in,0,length); - } catch (IOException IOEx) { - throw IOEx; - } catch (Exception Ex) { - throw Ex; - } - return antwort; - } + /** + * this method will read from DataInputStream dis and return its answer as + * an array of bytes + * + * @param length + * @return + * @throws IOException + * @throws Exception + */ + public synchronized byte[] readByteArrayBuffer(int length) + throws IOException, Exception { + /* + * this method again hardly differs from the string reading one, so same + * comments apply here as well + */ - /** - * this method will read from DataInputStream dis, convert the chars read - * into ASCII and return its answer as a String - * - * @return - * @throws IOException - * @throws Exception - */ - public synchronized int readBuffer(int[] gelesen,int length) throws IOException, Exception { - int i=0; // init helper variables - //byte [] gelesen = new byte[length.intValue()]; - while (!dis.ready() && i++<200) { - Thread.sleep(100); - } - i=0; - while ((dis.ready())& (i