cleaning up stop/dispose events

This commit is contained in:
benfry
2012-11-25 17:41:33 +00:00
parent 03b5baa8b7
commit 8b771c0d98
3 changed files with 51 additions and 38 deletions

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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;
}