X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FWebLibraryServer.java;h=22f36e91f5729a7bfef72fe9c98d2a7fa157a6ec;hp=b3d897d04bbbb8bf5ecc433cd4dee280e415c7e6;hb=cab46763df87b1aacf51a9c064a3f5fc49642df9;hpb=e247c76b6ae8c21b9d4af979b972140e9283dfbe diff --git a/src/be/nikiroo/fanfix/library/WebLibraryServer.java b/src/be/nikiroo/fanfix/library/WebLibraryServer.java index b3d897d..22f36e9 100644 --- a/src/be/nikiroo/fanfix/library/WebLibraryServer.java +++ b/src/be/nikiroo/fanfix/library/WebLibraryServer.java @@ -28,7 +28,6 @@ import be.nikiroo.utils.NanoHTTPD; import be.nikiroo.utils.NanoHTTPD.Response; import be.nikiroo.utils.NanoHTTPD.Response.Status; import be.nikiroo.utils.Progress; -import be.nikiroo.utils.Progress.ProgressListener; public class WebLibraryServer extends WebLibraryServerHtml { class WLoginResult extends LoginResult { @@ -71,6 +70,8 @@ public class WebLibraryServer extends WebLibraryServerHtml { private Map imprts = new HashMap(); + private boolean exiting; + public WebLibraryServer(boolean secure) throws IOException { super(secure); @@ -98,6 +99,56 @@ public class WebLibraryServer extends WebLibraryServerHtml { new Thread(this).start(); } + @Override + protected Response stop(WLoginResult login) { + if (!login.isRw()) { + return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, + NanoHTTPD.MIME_PLAINTEXT, "Exit not allowed"); + } + + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is already exiting..."); + } + + exiting = true; + Instance.getInstance().getTraceHandler().trace("Exiting"); + + boolean ok; + do { + synchronized (imprts) { + ok = imprts.isEmpty(); + } + if (!ok) { + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + Instance.getInstance().getTraceHandler() + .trace("Waiting to exit..."); + } + } + } while (!ok); + + doStop(); + + new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(1500); + } catch (InterruptedException e) { + } + + Instance.getInstance().getTraceHandler() + .trace("Exit timeout: force-quit"); + System.exit(0); + } + }, "Exit program after timeout of 1500 ms").start(); + + return NanoHTTPD.newFixedLengthResponse(Status.OK, + NanoHTTPD.MIME_PLAINTEXT, "Exited"); + } + @Override protected WLoginResult login(boolean badLogin, boolean badCookie) { return new WLoginResult(false, false); @@ -305,6 +356,11 @@ public class WebLibraryServer extends WebLibraryServerHtml { NanoHTTPD.MIME_PLAINTEXT, "SET story part not allowed"); } + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); + } + String luid = uriParts[off + 0]; String type = uriParts[off + 1]; @@ -385,6 +441,11 @@ public class WebLibraryServer extends WebLibraryServerHtml { NanoHTTPD.MIME_PLAINTEXT, "Cover request not allowed"); } + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); + } + String type = uriParts[off + 0]; String id = uriParts[off + 1]; @@ -411,18 +472,15 @@ public class WebLibraryServer extends WebLibraryServerHtml { NanoHTTPD.MIME_PLAINTEXT, "Import not allowed"); } + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); + } + final URL url = new URL(urlStr); final Progress pg = new Progress(); final String luid = lib.getNextId(); - // Keep the latest name - pg.addProgressListener(new ProgressListener() { - @Override - public void progress(Progress progress, String name) { - pg.setName(name); - } - }); - synchronized (imprts) { imprts.put(luid, pg); } @@ -486,6 +544,11 @@ public class WebLibraryServer extends WebLibraryServerHtml { NanoHTTPD.MIME_PLAINTEXT, "Delete not allowed"); } + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); + } + String luid = uriParts[off + 0]; BasicLibrary lib = Instance.getInstance().getLibrary();