From 8b771c0d9841edfa7de644caf4e723c03bab4fd1 Mon Sep 17 00:00:00 2001 From: benfry Date: Sun, 25 Nov 2012 17:41:33 +0000 Subject: [PATCH] cleaning up stop/dispose events --- .../net/src/processing/net/Client.java | 36 +++++++++++-------- .../net/src/processing/net/Server.java | 27 +++++++------- .../serial/src/processing/serial/Serial.java | 26 +++++++++----- 3 files changed, 51 insertions(+), 38 deletions(-) diff --git a/java/libraries/net/src/processing/net/Client.java b/java/libraries/net/src/processing/net/Client.java index d6bccebcc..18867e5dc 100644 --- a/java/libraries/net/src/processing/net/Client.java +++ b/java/libraries/net/src/processing/net/Client.java @@ -45,7 +45,6 @@ import java.net.*; * @see_external LIB_net/clientEvent */ public class Client implements Runnable { - PApplet parent; Method clientEventMethod; Method disconnectEventMethod; @@ -63,6 +62,7 @@ public class Client implements Runnable { int bufferIndex; int bufferLast; + /** * @param parent typically use "this" * @param host address of the server @@ -83,7 +83,7 @@ public class Client implements Runnable { parent.registerMethod("dispose", this); - // reflection to check whether host applet has a call for + // reflection to check whether host sketch has a call for // public void clientEvent(processing.net.Client) // which would be called each time an event comes in try { @@ -139,8 +139,7 @@ public class Client implements Runnable { * @brief Disconnects from the server * @usage application */ - public void stop() { - dispose(); + public void stop() { if (disconnectEventMethod != null) { try { disconnectEventMethod.invoke(parent, new Object[] { this }); @@ -149,6 +148,7 @@ public class Client implements Runnable { disconnectEventMethod = null; } } + dispose(); } @@ -161,23 +161,31 @@ public class Client implements Runnable { public void dispose() { thread = null; try { - // do io streams need to be closed first? - if (input != null) input.close(); - if (output != null) output.close(); - + if (input != null) { + input.close(); + input = null; + } } catch (Exception e) { e.printStackTrace(); } - input = null; - output = null; try { - if (socket != null) socket.close(); - + if (output != null) { + output.close(); + output = null; + } + } catch (Exception e) { + e.printStackTrace(); + } + + try { + if (socket != null) { + socket.close(); + socket = null; + } } catch (Exception e) { e.printStackTrace(); } - socket = null; } @@ -615,7 +623,7 @@ public class Client implements Runnable { /** - * General error reporting, all corraled here just in case + * General error reporting, all corralled here just in case * I think of something slightly more intelligent to do. */ //public void errorMessage(String where, Exception e) { diff --git a/java/libraries/net/src/processing/net/Server.java b/java/libraries/net/src/processing/net/Server.java index d91333dfa..60fb5f160 100644 --- a/java/libraries/net/src/processing/net/Server.java +++ b/java/libraries/net/src/processing/net/Server.java @@ -47,7 +47,6 @@ import java.net.*; * @instanceName server any variable of type Server */ public class Server implements Runnable { - PApplet parent; Method serverEventMethod; @@ -60,6 +59,7 @@ public class Server implements Runnable { /** Array of client objects, useful length is determined by clientCount. */ public Client[] clients; + /** * @param parent typically use "this" * @param port port used to transfer data @@ -155,9 +155,6 @@ public class Server implements Runnable { e.printStackTrace(); } return null; -// InetAddress thisIp = InetAddress.getLocalHost(); -// thisIpAddress = thisIp.getHostAddress() -// return server.getInetAddress().getHostAddress(); } @@ -219,25 +216,23 @@ public class Server implements Runnable { * Disconnect all clients and stop the server: internal use only. */ public void dispose() { - try { - thread = null; + thread = null; - if (clients != null) { - for (int i = 0; i < clientCount; i++) { - disconnect(clients[i]); - } - clientCount = 0; - clients = null; + if (clients != null) { + for (int i = 0; i < clientCount; i++) { + disconnect(clients[i]); } + clientCount = 0; + clients = null; + } + try { if (server != null) { server.close(); server = null; } - } catch (IOException e) { e.printStackTrace(); - //errorMessage("stop", e); } } @@ -294,6 +289,7 @@ public class Server implements Runnable { } } } + public void write(byte data[]) { int index = 0; @@ -306,6 +302,7 @@ public class Server implements Runnable { } } } + public void write(String data) { int index = 0; @@ -321,7 +318,7 @@ public class Server implements Runnable { /** - * General error reporting, all corraled here just in case + * General error reporting, all corralled here just in case * I think of something slightly more intelligent to do. */ // public void errorMessage(String where, Exception e) { diff --git a/java/libraries/serial/src/processing/serial/Serial.java b/java/libraries/serial/src/processing/serial/Serial.java index 1d148455c..fd3595fc1 100644 --- a/java/libraries/serial/src/processing/serial/Serial.java +++ b/java/libraries/serial/src/processing/serial/Serial.java @@ -225,23 +225,31 @@ public class Serial implements SerialPortEventListener { */ public void dispose() { try { - // do io streams need to be closed first? - if (input != null) input.close(); - if (output != null) output.close(); - + if (input != null) { + input.close(); + input = null; + } } catch (Exception e) { e.printStackTrace(); } - input = null; - output = null; try { - if (port != null) port.close(); // close the port - + if (output != null) { + output.close(); + output = null; + } + } catch (Exception e) { + e.printStackTrace(); + } + + try { + if (port != null) { + port.close(); + port = null; + } } catch (Exception e) { e.printStackTrace(); } - port = null; }