+
+ tracer.trace(name + ": client terminated on port " + port);
+ }
+ }
+
+ /**
+ * Will stop the server, synchronously and without a timeout.
+ */
+ public void stop() {
+ tracer.trace(name + ": stopping server");
+ stop(0, true);
+ }
+
+ /**
+ * Stop the server.
+ *
+ * @param timeout
+ * the maximum timeout to wait for existing actions to complete,
+ * or 0 for "no timeout"
+ * @param wait
+ * wait for the server to be stopped before returning
+ * (synchronous) or not (asynchronous)
+ */
+ public void stop(final long timeout, final boolean wait) {
+ if (wait) {
+ stop(timeout);
+ } else {
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ stop(timeout);
+ }
+ }).start();
+ }
+ }
+
+ /**
+ * Stop the server (synchronous).
+ *
+ * @param timeout
+ * the maximum timeout to wait for existing actions to complete,
+ * or 0 for "no timeout"
+ */
+ private void stop(long timeout) {
+ tracer.trace(name + ": server stopping on port " + port);
+ synchronized (lock) {
+ if (started && !exiting) {
+ exiting = true;
+
+ try {
+ new ConnectActionClient(createSocket(null, port, ssl))
+ .connect();
+ long time = 0;
+ while (ss != null && timeout > 0 && timeout > time) {
+ Thread.sleep(10);
+ time += 10;
+ }
+ } catch (Exception e) {
+ if (ss != null) {
+ counter = 0; // will stop the main thread
+ onError(e);
+ }
+ }
+ }
+
+ // only return when stopped
+ while (started || exiting) {
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException e) {
+ }
+ }