From 018382ac6fb8a23f3696e4b987e480bccfe99910 Mon Sep 17 00:00:00 2001 From: Laurent Clouet Date: Sun, 11 Sep 2016 13:46:12 +0200 Subject: [PATCH] Handle when server doesn't send the expected response --- src/com/sheepit/client/Client.java | 13 +++++++++++++ src/com/sheepit/client/Error.java | 5 +++++ src/com/sheepit/client/Server.java | 10 ++++++++++ .../FermeExceptionBadResponseFromServer.java | 11 +++++++++++ 4 files changed, 39 insertions(+) create mode 100644 src/com/sheepit/client/exception/FermeExceptionBadResponseFromServer.java diff --git a/src/com/sheepit/client/Client.java b/src/com/sheepit/client/Client.java index 21c2c96..8963482 100644 --- a/src/com/sheepit/client/Client.java +++ b/src/com/sheepit/client/Client.java @@ -33,6 +33,7 @@ import java.util.concurrent.BlockingQueue; import com.sheepit.client.Error.ServerCode; import com.sheepit.client.Error.Type; import com.sheepit.client.exception.FermeException; +import com.sheepit.client.exception.FermeExceptionBadResponseFromServer; import com.sheepit.client.exception.FermeExceptionNoRightToRender; import com.sheepit.client.exception.FermeExceptionNoSession; import com.sheepit.client.exception.FermeExceptionNoSpaceLeftOnDevice; @@ -247,6 +248,18 @@ public class Client { } continue; // go back to ask job } + catch (FermeExceptionBadResponseFromServer e) { + int wait = 15; + int time_sleep = 1000 * 60 * wait; + this.gui.status(String.format("Bad answer from server. Will retry in %s minutes", wait)); + try { + Thread.sleep(time_sleep); + } + catch (InterruptedException e1) { + return -3; + } + continue; // go back to ask job + } catch (FermeException e) { this.gui.error("Client::renderingManagement exception requestJob (1) " + e.getMessage()); StringWriter sw = new StringWriter(); diff --git a/src/com/sheepit/client/Error.java b/src/com/sheepit/client/Error.java index 5f4adec..50f6272 100644 --- a/src/com/sheepit/client/Error.java +++ b/src/com/sheepit/client/Error.java @@ -46,7 +46,9 @@ public class Error { CPU_NOT_SUPPORTED(18), GPU_NOT_SUPPORTED(19), + // internal error handling NO_SPACE_LEFT_ON_DEVICE(100), + ERROR_BAD_RESPONSE(101), ; private final int id; @@ -89,6 +91,7 @@ public class Error { // internal error handling ERROR_NO_ROOT(2), + ERROR_BAD_RESPONSE(3), ERROR_REQUEST_FAILED(5); private final int id; @@ -141,6 +144,8 @@ public class Error { public static String humanString(Type in) { switch (in) { + case ERROR_BAD_RESPONSE: + return "Bad answer from server. It's a server side error, wait a bit an retry later."; case NETWORK_ISSUE: return "Could not connect to the server, please check if you have connectivity issue"; case TOO_OLD_CLIENT: diff --git a/src/com/sheepit/client/Server.java b/src/com/sheepit/client/Server.java index f5dabb0..cc5d3ad 100644 --- a/src/com/sheepit/client/Server.java +++ b/src/com/sheepit/client/Server.java @@ -75,6 +75,7 @@ import org.xml.sax.SAXException; import com.sheepit.client.Configuration.ComputeType; import com.sheepit.client.Error.ServerCode; import com.sheepit.client.exception.FermeException; +import com.sheepit.client.exception.FermeExceptionBadResponseFromServer; import com.sheepit.client.exception.FermeExceptionNoRightToRender; import com.sheepit.client.exception.FermeExceptionNoSession; import com.sheepit.client.exception.FermeExceptionNoSpaceLeftOnDevice; @@ -254,6 +255,9 @@ public class Server extends Thread implements HostnameVerifier, X509TrustManager } } } + else if (r == HttpURLConnection.HTTP_OK && contentType.startsWith("text/html")) { + return Error.Type.ERROR_BAD_RESPONSE; + } else { this.log.error("Server::getConfiguration: Invalid response " + contentType + " " + r); return Error.Type.WRONG_CONFIGURATION; @@ -443,6 +447,9 @@ public class Server extends Thread implements HostnameVerifier, X509TrustManager // most likely varnish is up but apache down throw new FermeServerDown(); } + else if (r == HttpURLConnection.HTTP_OK && contentType.startsWith("text/html")) { + throw new FermeExceptionBadResponseFromServer(); + } InputStream in = connection.getInputStream(); String line; BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); @@ -732,6 +739,9 @@ public class Server extends Thread implements HostnameVerifier, X509TrustManager } return ServerCode.OK; } + else if (r == HttpURLConnection.HTTP_OK && contentType.startsWith("text/html")) { + return ServerCode.ERROR_BAD_RESPONSE; + } else { try { inStream = new BufferedReader(new InputStreamReader(conn.getInputStream())); diff --git a/src/com/sheepit/client/exception/FermeExceptionBadResponseFromServer.java b/src/com/sheepit/client/exception/FermeExceptionBadResponseFromServer.java new file mode 100644 index 0000000..6cb13c7 --- /dev/null +++ b/src/com/sheepit/client/exception/FermeExceptionBadResponseFromServer.java @@ -0,0 +1,11 @@ +package com.sheepit.client.exception; + +public class FermeExceptionBadResponseFromServer extends FermeException { + public FermeExceptionBadResponseFromServer() { + super(); + } + + public FermeExceptionBadResponseFromServer(String message_) { + super(message_); + } +}