From c8a9b89b1f316c259eaec68309f32fdd8289e375 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 21 Sep 2012 13:59:38 +0200 Subject: SingletonInstanceServerSocket: Add unit tests; Create new server Thread @ start, otherwise we may collide w/ a failed start. Misc: Cleanup / reuse strings. --- .../common/util/locks/SingletonInstance.java | 13 ++++++---- .../util/locks/SingletonInstanceFileLock.java | 6 ++--- .../util/locks/SingletonInstanceServerSocket.java | 30 +++++++++++----------- 3 files changed, 26 insertions(+), 23 deletions(-) (limited to 'src/java') diff --git a/src/java/com/jogamp/common/util/locks/SingletonInstance.java b/src/java/com/jogamp/common/util/locks/SingletonInstance.java index 5f2718b..476c269 100644 --- a/src/java/com/jogamp/common/util/locks/SingletonInstance.java +++ b/src/java/com/jogamp/common/util/locks/SingletonInstance.java @@ -95,19 +95,19 @@ public abstract class SingletonInstance implements Lock { locked = tryLockImpl(); if(locked) { if(DEBUG) { - System.err.println("SLOCK "+System.currentTimeMillis()+" +++ "+getName()+" - Locked "); + System.err.println(infoPrefix()+" +++ "+getName()+" - Locked "); } return true; } if(DEBUG && 0==i) { - System.err.println("SLOCK "+System.currentTimeMillis()+" ??? "+getName()+" - Wait for lock"); + System.err.println(infoPrefix()+" III "+getName()+" - Wait for lock"); } Thread.sleep(poll_ms); maxwait -= poll_ms; i++; } while ( 0 < maxwait ) ; } catch ( InterruptedException ie ) { - throw new RuntimeException("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - couldn't get lock", ie); + throw new RuntimeException(infoPrefix()+" EEE "+getName()+" - couldn't get lock", ie); } return false; } @@ -118,8 +118,7 @@ public abstract class SingletonInstance implements Lock { if(locked) { locked = !unlockImpl(); if(DEBUG) { - System.err.println("SLOCK "+System.currentTimeMillis()+" --- "+getName()+" - Unlock " - + ( locked ? "failed" : "ok" ) ); + System.err.println(infoPrefix()+" --- "+getName()+" - Unlock "+ ( locked ? "failed" : "ok" ) ); } } } @@ -130,6 +129,10 @@ public abstract class SingletonInstance implements Lock { return locked; } + protected String infoPrefix() { + return "SLOCK [T "+Thread.currentThread().getName()+" @ "+System.currentTimeMillis()+" ms"; + } + private final long poll_ms; private boolean locked = false; } diff --git a/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java b/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java index c8a05ed..a76f261 100644 --- a/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java +++ b/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java @@ -80,7 +80,7 @@ public class SingletonInstanceFileLock extends SingletonInstance { @Override public void run() { if(isLocked()) { - System.err.println("SLOCK "+System.currentTimeMillis()+" XXX "+getName()+" - Unlock @ JVM Shutdown"); + System.err.println(infoPrefix()+" XXX "+getName()+" - Unlock @ JVM Shutdown"); } unlock(); } @@ -97,7 +97,7 @@ public class SingletonInstanceFileLock extends SingletonInstance { return true; } } catch (Exception e) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Unable to create and/or lock file"); + System.err.println(infoPrefix()+" III "+getName()+" - Unable to create and/or lock file"); e.printStackTrace(); } return false; @@ -119,7 +119,7 @@ public class SingletonInstanceFileLock extends SingletonInstance { } return true; } catch (Exception e) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Unable to remove lock file"); + System.err.println(infoPrefix()+" EEE "+getName()+" - Unable to remove lock file"); e.printStackTrace(); } finally { fileLock = null; diff --git a/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java b/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java index eab687d..dac6e72 100644 --- a/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java +++ b/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java @@ -66,7 +66,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance { } catch (UnknownHostException e) { } } if(null == ilh) { - throw new RuntimeException("Could not determine local InetAddress"); + throw new RuntimeException(infoPrefix()+" EEE Could not determine local InetAddress"); } fullName = ilh.toString()+":"+portNumber; @@ -126,13 +126,11 @@ public class SingletonInstanceServerSocket extends SingletonInstance { private Object syncOnStartStop = new Object(); private ServerSocket serverSocket = null; - private Thread serverThread = null; + private Thread serverThread = null; // allowing kill() to force-stop last server-thread public Server(InetAddress localInetAddress, int portNumber) { this.localInetAddress = localInetAddress; this.portNumber = portNumber; - this.serverThread = new Thread(this); - this.serverThread.setDaemon(true); // be a daemon, don't keep the JVM running } public final InetAddress getLocalInetAddress() { return localInetAddress; } @@ -142,6 +140,8 @@ public class SingletonInstanceServerSocket extends SingletonInstance { if(alive) return true; synchronized (syncOnStartStop) { + serverThread = new Thread(this); + serverThread.setDaemon(true); // be a daemon, don't keep the JVM running serverThread.start(); try { syncOnStartStop.wait(); @@ -169,12 +169,10 @@ public class SingletonInstanceServerSocket extends SingletonInstance { } } if(alive) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Unable to remove lock: ServerThread still alive ?"); + System.err.println(infoPrefix()+" EEE "+getName()+" - Unable to remove lock: ServerThread still alive ?"); kill(); alive = false; } - serverThread = new Thread(this); - serverThread.setDaemon(true); // be a daemon, don't keep the JVM running return true; } @@ -185,11 +183,13 @@ public class SingletonInstanceServerSocket extends SingletonInstance { @SuppressWarnings("deprecation") public final void kill() { if(alive) { - System.err.println("SLOCK "+System.currentTimeMillis()+" XXX "+getName()+" - Kill @ JVM Shutdown"); + System.err.println(infoPrefix()+" XXX "+getName()+" - Kill @ JVM Shutdown"); + } + if(null != serverThread) { + try { + serverThread.stop(); + } catch(Throwable t) { } } - try { - serverThread.stop(); - } catch(Throwable t) { } if(null != serverSocket) { try { final ServerSocket ss = serverSocket; @@ -216,7 +216,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance { public void run() { { final Thread currentThread = Thread.currentThread(); - currentThread.setName(currentThread.getName() + " - SingletonInstanceServerSocket: "+getName()); + currentThread.setName(currentThread.getName() + " - SISock: "+getName()); if(DEBUG) { System.err.println(currentThread.getName()+" - started"); } @@ -227,7 +227,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance { serverSocket = new ServerSocket(portNumber, 1, localInetAddress); alive = true; } catch (IOException e) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Unable to install ServerSocket: "+e.getMessage()); + System.err.println(infoPrefix()+" III - Unable to install ServerSocket: "+e.getMessage()); shallQuit = true; } finally { syncOnStartStop.notifyAll(); @@ -239,7 +239,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance { final Socket clientSocket = serverSocket.accept(); clientSocket.close(); } catch (IOException ioe) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Exception during accept: " + ioe.getMessage()); + System.err.println(infoPrefix()+" EEE - Exception during accept: " + ioe.getMessage()); } } @@ -249,7 +249,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance { serverSocket.close(); } } catch (IOException e) { - System.err.println("SLOCK "+System.currentTimeMillis()+" EEE "+getName()+" - Exception during close: " + e.getMessage()); + System.err.println(infoPrefix()+" EEE - Exception during close: " + e.getMessage()); } finally { serverSocket = null; alive = false; -- cgit v1.2.3