27package jau.test.util.parallel.locks.impl;
29import java.io.IOException;
30import java.net.InetAddress;
31import java.net.ServerSocket;
32import java.net.Socket;
33import java.net.UnknownHostException;
35import org.jau.lang.ExceptionUtils;
36import org.jau.lang.InterruptSource;
37import org.jau.lang.InterruptedRuntimeException;
38import org.jau.lang.SourcedInterruptedException;
40import jau.test.util.parallel.locks.SingletonInstance;
44 private static int serverInstanceCount = 0;
45 private final Server singletonServer;
46 private final String fullName;
52 InetAddress ilh =
null;
54 ilh = InetAddress.getByName(
null);
55 }
catch (
final UnknownHostException e1) { }
58 ilh = InetAddress.getByName(
"localhost");
59 if(
null!=ilh && !ilh.isLoopbackAddress()) { ilh =
null; }
60 }
catch (
final UnknownHostException e1) { }
64 ilh = InetAddress.getByAddress(
new byte[] { 127, 0, 0, 1 } );
65 if(
null!=ilh && !ilh.isLoopbackAddress()) { ilh =
null; }
66 }
catch (
final UnknownHostException e) { }
70 ilh = InetAddress.getLocalHost();
71 }
catch (
final UnknownHostException e) { }
74 throw new RuntimeException(
infoPrefix()+
" EEE Could not determine local InetAddress");
77 fullName = ilh.toString()+
":"+portNumber;
78 singletonServer =
new Server(ilh, portNumber);
82 singletonServer.kill();
88 return singletonServer.getLocalInetAddress();
92 return singletonServer.getPortNumber();
96 public final String
getName() {
return fullName; }
100 if( singletonServer.isRunning() ) {
105 final Socket clientSocket = singletonServer.connect();
106 if(
null != clientSocket) {
108 clientSocket.close();
109 }
catch (
final IOException e) { }
113 if( !singletonServer.start() ) {
122 return singletonServer.shutdown();
125 public class Server implements Runnable {
126 private final InetAddress localInetAddress;
127 private final int portNumber;
129 private volatile boolean shallQuit =
false;
130 private volatile boolean alive =
false;
132 private final Object syncOnStartStop =
new Object();
133 private ServerSocket serverSocket =
null;
134 private Thread serverThread =
null;
136 public Server(
final InetAddress localInetAddress,
final int portNumber) {
137 this.localInetAddress = localInetAddress;
138 this.portNumber = portNumber;
145 if(alive)
return true;
148 synchronized (
Server.class) {
149 serverInstanceCount++;
150 sname =
"SingletonServerSocket"+serverInstanceCount+
"-"+fullName;
152 synchronized (syncOnStartStop) {
155 serverThread.setDaemon(
true);
156 serverThread.start();
158 while( !alive && !shallQuit ) {
159 syncOnStartStop.wait();
161 }
catch (
final InterruptedException ie) {
167 final boolean ok = isBound();
175 return shutdown(
true);
177 private final boolean shutdown(
final boolean wait) {
178 if(!alive)
return true;
181 synchronized (syncOnStartStop) {
187 syncOnStartStop.wait();
189 }
catch (
final InterruptedException ie) {
190 throw new InterruptedRuntimeException(ie);
196 System.err.println(infoPrefix()+
" EEE "+getName()+
" - Unable to remove lock: ServerThread still alive ?");
207 @SuppressWarnings(
"deprecation")
208 public final
void kill() {
210 System.err.println(infoPrefix()+
" XXX "+getName()+
" - Kill @ JVM Shutdown");
214 if(
null != serverThread && serverThread.isAlive() ) {
217 }
catch(
final Throwable t) { }
219 if(
null != serverSocket) {
221 final ServerSocket ss = serverSocket;
224 }
catch (
final Throwable t) { }
231 return alive &&
null != serverSocket && serverSocket.isBound() ;
236 return new Socket(localInetAddress, portNumber);
237 }
catch (
final Exception e) { }
244 System.err.println(infoPrefix()+
" III - Start");
247 synchronized (syncOnStartStop) {
249 serverSocket =
new ServerSocket(portNumber, 1, localInetAddress);
250 serverSocket.setReuseAddress(
true);
252 }
catch (
final IOException e) {
253 System.err.println(infoPrefix()+
" III - Unable to install ServerSocket: "+e.getMessage());
256 syncOnStartStop.notifyAll();
262 final Socket clientSocket = serverSocket.accept();
263 clientSocket.close();
264 }
catch (
final IOException ioe) {
265 System.err.println(infoPrefix()+
" EEE - Exception during accept: " + ioe.getMessage());
268 }
catch(
final ThreadDeath td) {
273 synchronized (syncOnStartStop) {
275 System.err.println(infoPrefix()+
" III - Stopping: alive "+alive+
", shallQuit "+shallQuit+
", hasSocket "+(
null!=serverSocket));
277 if(
null != serverSocket) {
279 serverSocket.close();
280 }
catch (
final IOException e) {
281 System.err.println(infoPrefix()+
" EEE - Exception during close: " + e.getMessage());
286 syncOnStartStop.notifyAll();
final boolean isRunning()
final InetAddress getLocalInetAddress()
final int getPortNumber()
Server(final InetAddress localInetAddress, final int portNumber)
SingletonInstanceServerSocket(final long poll_ms, final int portNumber)
final int getPortNumber()
final InetAddress getLocalInetAddress()
static void dumpThrowable(final String additionalDescr, final Throwable t)
Dumps a Throwable to System.err in a decorating message including the current thread name,...
java.lang.Thread specialization implementing InterruptSource to track java.lang.Thread#interrupt() ca...
Unchecked exception propagating an InterruptedException where handling of the latter is not desired.
InterruptedException, which may include the source, see getInterruptSource().
static InterruptedException wrap(final InterruptedException ie)
Wraps the given InterruptedException into a SourcedInterruptedException if it is not yet of the desir...
Interface exposing java.lang.Thread#interrupt() source, intended for java.lang.Thread specializations...