| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,7 +45,6 @@ | |||
| 45 | 45 | * @see_external LIB_net/clientEvent | |
| 46 | 46 | */ | |
| 47 | 47 | public class Client implements Runnable { | |
| 48 | - | ||
| 49 | 48 | PApplet parent; | |
| 50 | 49 | Method clientEventMethod; | |
| 51 | 50 | Method disconnectEventMethod; | |
@@ -63,6 +62,7 @@ public class Client implements Runnable { | |||
| 63 | 62 | int bufferIndex; | |
| 64 | 63 | int bufferLast; | |
| 65 | 64 | ||
| 65 | + | ||
| 66 | 66 | /** | |
| 67 | 67 | * @param parent typically use "this" | |
| 68 | 68 | * @param host address of the server | |
@@ -83,7 +83,7 @@ public Client(PApplet parent, String host, int port) { | |||
| 83 | 83 | ||
| 84 | 84 | parent.registerMethod("dispose", this); | |
| 85 | 85 | ||
| 86 | - // reflection to check whether host applet has a call for | ||
| 86 | + // reflection to check whether host sketch has a call for | ||
| 87 | 87 | // public void clientEvent(processing.net.Client) | |
| 88 | 88 | // which would be called each time an event comes in | |
| 89 | 89 | try { | |
@@ -139,8 +139,7 @@ public Client(PApplet parent, Socket socket) throws IOException { | |||
| 139 | 139 | * @brief Disconnects from the server | |
| 140 | 140 | * @usage application | |
| 141 | 141 | */ | |
| 142 | - public void stop() { | ||
| 143 | - dispose(); | ||
| 142 | + public void stop() { | ||
| 144 | 143 | if (disconnectEventMethod != null) { | |
| 145 | 144 | try { | |
| 146 | 145 | disconnectEventMethod.invoke(parent, new Object[] { this }); | |
@@ -149,6 +148,7 @@ public void stop() { | |||
| 149 | 148 | disconnectEventMethod = null; | |
| 150 | 149 | } | |
| 151 | 150 | } | |
| 151 | + dispose(); | ||
| 152 | 152 | } | |
| 153 | 153 | ||
| 154 | 154 | ||
@@ -161,23 +161,31 @@ public void stop() { | |||
| 161 | 161 | public void dispose() { | |
| 162 | 162 | thread = null; | |
| 163 | 163 | 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 | + } | ||
| 168 | 168 | } catch (Exception e) { | |
| 169 | 169 | e.printStackTrace(); | |
| 170 | 170 | } | |
| 171 | - input = null; | ||
| 172 | - output = null; | ||
| 173 | 171 | ||
| 174 | 172 | 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 | + } | ||
| 177 | 186 | } catch (Exception e) { | |
| 178 | 187 | e.printStackTrace(); | |
| 179 | 188 | } | |
| 180 | - socket = null; | ||
| 181 | 189 | } | |
| 182 | 190 | ||
| 183 | 191 | ||
@@ -615,7 +623,7 @@ protected void disconnect(Exception e) { | |||
| 615 | 623 | ||
| 616 | 624 | ||
| 617 | 625 | /** | |
| 618 | - * General error reporting, all corraled here just in case | ||
| 626 | + * General error reporting, all corralled here just in case | ||
| 619 | 627 | * I think of something slightly more intelligent to do. | |
| 620 | 628 | */ | |
| 621 | 629 | //public void errorMessage(String where, Exception e) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,7 +47,6 @@ | |||
| 47 | 47 | * @instanceName server any variable of type Server | |
| 48 | 48 | */ | |
| 49 | 49 | public class Server implements Runnable { | |
| 50 | - | ||
| 51 | 50 | PApplet parent; | |
| 52 | 51 | Method serverEventMethod; | |
| 53 | 52 | ||
@@ -60,6 +59,7 @@ public class Server implements Runnable { | |||
| 60 | 59 | /** Array of client objects, useful length is determined by clientCount. */ | |
| 61 | 60 | public Client[] clients; | |
| 62 | 61 | ||
| 62 | + | ||
| 63 | 63 | /** | |
| 64 | 64 | * @param parent typically use "this" | |
| 65 | 65 | * @param port port used to transfer data | |
@@ -155,9 +155,6 @@ static public String ip() { | |||
| 155 | 155 | e.printStackTrace(); | |
| 156 | 156 | } | |
| 157 | 157 | return null; | |
| 158 | - // InetAddress thisIp = InetAddress.getLocalHost(); | ||
| 159 | - // thisIpAddress = thisIp.getHostAddress() | ||
| 160 | - // return server.getInetAddress().getHostAddress(); | ||
| 161 | 158 | } | |
| 162 | 159 | ||
| 163 | 160 | ||
@@ -219,25 +216,23 @@ public void stop() { | |||
| 219 | 216 | * Disconnect all clients and stop the server: internal use only. | |
| 220 | 217 | */ | |
| 221 | 218 | public void dispose() { | |
| 222 | - try { | ||
| 223 | - thread = null; | ||
| 219 | + thread = null; | ||
| 224 | 220 | ||
| 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]); | ||
| 231 | 224 | } | |
| 225 | + clientCount = 0; | ||
| 226 | + clients = null; | ||
| 227 | + } | ||
| 232 | 228 | ||
| 229 | + try { | ||
| 233 | 230 | if (server != null) { | |
| 234 | 231 | server.close(); | |
| 235 | 232 | server = null; | |
| 236 | 233 | } | |
| 237 | - | ||
| 238 | 234 | } catch (IOException e) { | |
| 239 | 235 | e.printStackTrace(); | |
| 240 | - //errorMessage("stop", e); | ||
| 241 | 236 | } | |
| 242 | 237 | } | |
| 243 | 238 | ||
@@ -294,6 +289,7 @@ public void write(int data) { // will also cover char | |||
| 294 | 289 | } | |
| 295 | 290 | } | |
| 296 | 291 | } | |
| 292 | + | ||
| 297 | 293 | ||
| 298 | 294 | public void write(byte data[]) { | |
| 299 | 295 | int index = 0; | |
@@ -306,6 +302,7 @@ public void write(byte data[]) { | |||
| 306 | 302 | } | |
| 307 | 303 | } | |
| 308 | 304 | } | |
| 305 | + | ||
| 309 | 306 | ||
| 310 | 307 | public void write(String data) { | |
| 311 | 308 | int index = 0; | |
@@ -321,7 +318,7 @@ public void write(String data) { | |||
| 321 | 318 | ||
| 322 | 319 | ||
| 323 | 320 | /** | |
| 324 | - * General error reporting, all corraled here just in case | ||
| 321 | + * General error reporting, all corralled here just in case | ||
| 325 | 322 | * I think of something slightly more intelligent to do. | |
| 326 | 323 | */ | |
| 327 | 324 | // public void errorMessage(String where, Exception e) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -225,23 +225,31 @@ public void stop() { | |||
| 225 | 225 | */ | |
| 226 | 226 | public void dispose() { | |
| 227 | 227 | 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 | + } | ||
| 232 | 232 | } catch (Exception e) { | |
| 233 | 233 | e.printStackTrace(); | |
| 234 | 234 | } | |
| 235 | - input = null; | ||
| 236 | - output = null; | ||
| 237 | 235 | ||
| 238 | 236 | 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 | + } | ||
| 240 | 244 | ||
| 245 | + try { | ||
| 246 | + if (port != null) { | ||
| 247 | + port.close(); | ||
| 248 | + port = null; | ||
| 249 | + } | ||
| 241 | 250 | } catch (Exception e) { | |
| 242 | 251 | e.printStackTrace(); | |
| 243 | 252 | } | |
| 244 | - port = null; | ||
| 245 | 253 | } | |
| 246 | 254 | ||
| 247 | 255 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments