Skip to content

Commit 449b764

Browse files
committed
cleaning up stop/dispose events
1 parent 3634623 commit 449b764

File tree

3 files changed

+50
-37
lines changed

3 files changed

+50
-37
lines changed

java/libraries/net/src/processing/net/Client.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
* @see_external LIB_net/clientEvent
4646
*/
4747
public class Client implements Runnable {
48-
4948
PApplet parent;
5049
Method clientEventMethod;
5150
Method disconnectEventMethod;
@@ -63,6 +62,7 @@ public class Client implements Runnable {
6362
int bufferIndex;
6463
int bufferLast;
6564

65+
6666
/**
6767
* @param parent typically use "this"
6868
* @param host address of the server
@@ -83,7 +83,7 @@ public Client(PApplet parent, String host, int port) {
8383

8484
parent.registerMethod("dispose", this);
8585

86-
// reflection to check whether host applet has a call for
86+
// reflection to check whether host sketch has a call for
8787
// public void clientEvent(processing.net.Client)
8888
// which would be called each time an event comes in
8989
try {
@@ -139,8 +139,7 @@ public Client(PApplet parent, Socket socket) throws IOException {
139139
* @brief Disconnects from the server
140140
* @usage application
141141
*/
142-
public void stop() {
143-
dispose();
142+
public void stop() {
144143
if (disconnectEventMethod != null) {
145144
try {
146145
disconnectEventMethod.invoke(parent, new Object[] { this });
@@ -149,6 +148,7 @@ public void stop() {
149148
disconnectEventMethod = null;
150149
}
151150
}
151+
dispose();
152152
}
153153

154154

@@ -161,23 +161,31 @@ public void stop() {
161161
public void dispose() {
162162
thread = null;
163163
try {
164-
// do io streams need to be closed first?
165-
if (input != null) input.close();
166-
if (output != null) output.close();
167-
164+
if (input != null) {
165+
input.close();
166+
input = null;
167+
}
168168
} catch (Exception e) {
169169
e.printStackTrace();
170170
}
171-
input = null;
172-
output = null;
173171

174172
try {
175-
if (socket != null) socket.close();
176-
173+
if (output != null) {
174+
output.close();
175+
output = null;
176+
}
177+
} catch (Exception e) {
178+
e.printStackTrace();
179+
}
180+
181+
try {
182+
if (socket != null) {
183+
socket.close();
184+
socket = null;
185+
}
177186
} catch (Exception e) {
178187
e.printStackTrace();
179188
}
180-
socket = null;
181189
}
182190

183191

@@ -615,7 +623,7 @@ protected void disconnect(Exception e) {
615623

616624

617625
/**
618-
* General error reporting, all corraled here just in case
626+
* General error reporting, all corralled here just in case
619627
* I think of something slightly more intelligent to do.
620628
*/
621629
//public void errorMessage(String where, Exception e) {

java/libraries/net/src/processing/net/Server.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
* @instanceName server any variable of type Server
4848
*/
4949
public class Server implements Runnable {
50-
5150
PApplet parent;
5251
Method serverEventMethod;
5352

@@ -60,6 +59,7 @@ public class Server implements Runnable {
6059
/** Array of client objects, useful length is determined by clientCount. */
6160
public Client[] clients;
6261

62+
6363
/**
6464
* @param parent typically use "this"
6565
* @param port port used to transfer data
@@ -155,9 +155,6 @@ static public String ip() {
155155
e.printStackTrace();
156156
}
157157
return null;
158-
// InetAddress thisIp = InetAddress.getLocalHost();
159-
// thisIpAddress = thisIp.getHostAddress()
160-
// return server.getInetAddress().getHostAddress();
161158
}
162159

163160

@@ -219,25 +216,23 @@ public void stop() {
219216
* Disconnect all clients and stop the server: internal use only.
220217
*/
221218
public void dispose() {
222-
try {
223-
thread = null;
219+
thread = null;
224220

225-
if (clients != null) {
226-
for (int i = 0; i < clientCount; i++) {
227-
disconnect(clients[i]);
228-
}
229-
clientCount = 0;
230-
clients = null;
221+
if (clients != null) {
222+
for (int i = 0; i < clientCount; i++) {
223+
disconnect(clients[i]);
231224
}
225+
clientCount = 0;
226+
clients = null;
227+
}
232228

229+
try {
233230
if (server != null) {
234231
server.close();
235232
server = null;
236233
}
237-
238234
} catch (IOException e) {
239235
e.printStackTrace();
240-
//errorMessage("stop", e);
241236
}
242237
}
243238

@@ -294,6 +289,7 @@ public void write(int data) { // will also cover char
294289
}
295290
}
296291
}
292+
297293

298294
public void write(byte data[]) {
299295
int index = 0;
@@ -306,6 +302,7 @@ public void write(byte data[]) {
306302
}
307303
}
308304
}
305+
309306

310307
public void write(String data) {
311308
int index = 0;
@@ -321,7 +318,7 @@ public void write(String data) {
321318

322319

323320
/**
324-
* General error reporting, all corraled here just in case
321+
* General error reporting, all corralled here just in case
325322
* I think of something slightly more intelligent to do.
326323
*/
327324
// public void errorMessage(String where, Exception e) {

java/libraries/serial/src/processing/serial/Serial.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,31 @@ public void stop() {
225225
*/
226226
public void dispose() {
227227
try {
228-
// do io streams need to be closed first?
229-
if (input != null) input.close();
230-
if (output != null) output.close();
231-
228+
if (input != null) {
229+
input.close();
230+
input = null;
231+
}
232232
} catch (Exception e) {
233233
e.printStackTrace();
234234
}
235-
input = null;
236-
output = null;
237235

238236
try {
239-
if (port != null) port.close(); // close the port
237+
if (output != null) {
238+
output.close();
239+
output = null;
240+
}
241+
} catch (Exception e) {
242+
e.printStackTrace();
243+
}
240244

245+
try {
246+
if (port != null) {
247+
port.close();
248+
port = null;
249+
}
241250
} catch (Exception e) {
242251
e.printStackTrace();
243252
}
244-
port = null;
245253
}
246254

247255

0 commit comments

Comments
 (0)