diff --git a/src/main/java/com/sheepit/client/Client.java b/src/main/java/com/sheepit/client/Client.java index 14d90f0..1c1994d 100644 --- a/src/main/java/com/sheepit/client/Client.java +++ b/src/main/java/com/sheepit/client/Client.java @@ -41,25 +41,21 @@ import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; -import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; import com.sheepit.client.Error.ServerCode; import com.sheepit.client.Error.Type; import com.sheepit.client.datamodel.Chunk; import com.sheepit.client.exception.SheepItException; -import com.sheepit.client.exception.SheepItExceptionBadResponseFromServer; import com.sheepit.client.exception.SheepItExceptionNoRendererAvailable; import com.sheepit.client.exception.SheepItExceptionNoRightToRender; import com.sheepit.client.exception.SheepItExceptionNoSession; import com.sheepit.client.exception.SheepItExceptionNoSpaceLeftOnDevice; import com.sheepit.client.exception.SheepItExceptionPathInvalid; import com.sheepit.client.exception.SheepItExceptionNoWritePermission; -import com.sheepit.client.exception.SheepItExceptionServerInMaintenance; -import com.sheepit.client.exception.SheepItExceptionServerOverloaded; import com.sheepit.client.exception.SheepItExceptionSessionDisabled; import com.sheepit.client.exception.SheepItExceptionSessionDisabledDenoisingNotSupported; -import com.sheepit.client.exception.SheepItServerDown; +import com.sheepit.client.exception.SheepItExceptionWithRequiredWait; import com.sheepit.client.hardware.cpu.CPU; import com.sheepit.client.os.OS; @@ -267,11 +263,15 @@ import okhttp3.HttpUrl; } } } - catch (SheepItServerDown e) { - int wait = ThreadLocalRandom.current().nextInt(10, 30 + 1); // max is exclusive - int time_sleep = 1000 * 60 * wait; - this.gui.status(String.format("Cannot connect to the server. Please check your connectivity. Will try again at %tR", - new Date(new Date().getTime() + time_sleep))); + catch (SheepItExceptionWithRequiredWait e) { + // exception example: + // SheepItExceptionServerInMaintenance + // SheepItExceptionServerOverloaded + // SheepItServerDown + // SheepItExceptionBadResponseFromServer + + int time_sleep = e.getWaitDuration(); + this.gui.status(String.format(e.getHumanText(), new Date(new Date().getTime() + time_sleep))); if (this.activeSleep(time_sleep) == false) { return -3; @@ -279,43 +279,6 @@ import okhttp3.HttpUrl; this.log.removeCheckPoint(step); continue; // go back to ask job } - catch (SheepItExceptionServerOverloaded e) { - int wait = ThreadLocalRandom.current().nextInt(10, 30 + 1); // max is exclusive - int time_sleep = 1000 * 60 * wait; - this.gui.status(String.format("The server is overloaded and cannot allocate a job. Will try again at %tR", - new Date(new Date().getTime() + time_sleep))); - - if (this.activeSleep(time_sleep) == false) { - return -3; - } - - this.log.removeCheckPoint(step); - continue; // go back to ask job - } - catch (SheepItExceptionServerInMaintenance e) { - int wait = ThreadLocalRandom.current().nextInt(20, 30 + 1); // max is exclusive - int time_sleep = 1000 * 60 * wait; - this.gui.status(String.format("The server is under maintenance and cannot allocate a job. Will try again at %tR", - new Date(new Date().getTime() + time_sleep))); - - if (this.activeSleep(time_sleep) == false) { - return -3; - } - this.log.removeCheckPoint(step); - continue; // go back to ask job - } - catch (SheepItExceptionBadResponseFromServer e) { - int wait = ThreadLocalRandom.current().nextInt(15, 30 + 1); // max is exclusive - int time_sleep = 1000 * 60 * wait; - this.gui.status(String.format("Bad answer from the server. Will try again at %tR", new Date(new Date().getTime() + time_sleep))); - - if (this.activeSleep(time_sleep) == false) { - return -3; - } - - this.log.removeCheckPoint(step); - continue; // go back to ask job - } catch (SheepItException e) { this.gui.error("Client::run exception requestJob (1) " + e.getMessage()); StringWriter sw = new StringWriter(); diff --git a/src/main/java/com/sheepit/client/exception/SheepItExceptionBadResponseFromServer.java b/src/main/java/com/sheepit/client/exception/SheepItExceptionBadResponseFromServer.java index d521a92..30a5793 100644 --- a/src/main/java/com/sheepit/client/exception/SheepItExceptionBadResponseFromServer.java +++ b/src/main/java/com/sheepit/client/exception/SheepItExceptionBadResponseFromServer.java @@ -19,7 +19,9 @@ package com.sheepit.client.exception; -public class SheepItExceptionBadResponseFromServer extends SheepItException { +import java.util.concurrent.ThreadLocalRandom; + +public class SheepItExceptionBadResponseFromServer extends SheepItExceptionWithRequiredWait { public SheepItExceptionBadResponseFromServer() { super(); } @@ -27,4 +29,12 @@ public class SheepItExceptionBadResponseFromServer extends SheepItException { public SheepItExceptionBadResponseFromServer(String message_) { super(message_); } + + public String getHumanText() { + return "Bad answer from the server. Will try again at %tR"; + } + + public int getWaitDuration() { + return 1000 * 60 * ThreadLocalRandom.current().nextInt(15, 30 + 1); + } } diff --git a/src/main/java/com/sheepit/client/exception/SheepItExceptionServerInMaintenance.java b/src/main/java/com/sheepit/client/exception/SheepItExceptionServerInMaintenance.java index 78e8bf2..faca6b6 100644 --- a/src/main/java/com/sheepit/client/exception/SheepItExceptionServerInMaintenance.java +++ b/src/main/java/com/sheepit/client/exception/SheepItExceptionServerInMaintenance.java @@ -19,7 +19,9 @@ package com.sheepit.client.exception; -public class SheepItExceptionServerInMaintenance extends SheepItException { +import java.util.concurrent.ThreadLocalRandom; + +public class SheepItExceptionServerInMaintenance extends SheepItExceptionWithRequiredWait { public SheepItExceptionServerInMaintenance() { super(); } @@ -27,4 +29,12 @@ public class SheepItExceptionServerInMaintenance extends SheepItException { public SheepItExceptionServerInMaintenance(String message_) { super(message_); } + + public String getHumanText() { + return "The server is under maintenance and cannot allocate a job. Will try again at %tR"; + } + + public int getWaitDuration() { + return 1000 * 60 * ThreadLocalRandom.current().nextInt(20, 30 + 1); + } } diff --git a/src/main/java/com/sheepit/client/exception/SheepItExceptionServerOverloaded.java b/src/main/java/com/sheepit/client/exception/SheepItExceptionServerOverloaded.java index e05c4fb..9260615 100644 --- a/src/main/java/com/sheepit/client/exception/SheepItExceptionServerOverloaded.java +++ b/src/main/java/com/sheepit/client/exception/SheepItExceptionServerOverloaded.java @@ -19,7 +19,9 @@ package com.sheepit.client.exception; -public class SheepItExceptionServerOverloaded extends SheepItException { +import java.util.concurrent.ThreadLocalRandom; + +public class SheepItExceptionServerOverloaded extends SheepItExceptionWithRequiredWait { public SheepItExceptionServerOverloaded() { super(); } @@ -27,4 +29,12 @@ public class SheepItExceptionServerOverloaded extends SheepItException { public SheepItExceptionServerOverloaded(String message_) { super(message_); } + + public String getHumanText() { + return "The server is overloaded and cannot allocate a job. Will try again at %tR"; + } + + public int getWaitDuration() { + return 1000 * 60 * ThreadLocalRandom.current().nextInt(10, 30 + 1); + } } diff --git a/src/main/java/com/sheepit/client/exception/SheepItExceptionWithRequiredWait.java b/src/main/java/com/sheepit/client/exception/SheepItExceptionWithRequiredWait.java new file mode 100644 index 0000000..22f8b3f --- /dev/null +++ b/src/main/java/com/sheepit/client/exception/SheepItExceptionWithRequiredWait.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2023 Laurent CLOUET + * Author Laurent CLOUET + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package com.sheepit.client.exception; + +/** + * When a request to the server generate this exception. + * The client must wait a bit before retrying. + */ +public class SheepItExceptionWithRequiredWait extends SheepItException { + public SheepItExceptionWithRequiredWait() { + super(); + } + + public SheepItExceptionWithRequiredWait(String message_) { + super(message_); + } + + public String getHumanText() { + return "Please wait a bit"; + } + + /** how much time, it needs to wait + * + * @return duration in ms + */ + public int getWaitDuration() { + return 0; + } +} diff --git a/src/main/java/com/sheepit/client/exception/SheepItServerDown.java b/src/main/java/com/sheepit/client/exception/SheepItServerDown.java index 4e55ec8..42db027 100644 --- a/src/main/java/com/sheepit/client/exception/SheepItServerDown.java +++ b/src/main/java/com/sheepit/client/exception/SheepItServerDown.java @@ -19,10 +19,12 @@ package com.sheepit.client.exception; +import java.util.concurrent.ThreadLocalRandom; + /** * Server down (server side error) or unreachable (client side error) */ -public class SheepItServerDown extends SheepItException { +public class SheepItServerDown extends SheepItExceptionWithRequiredWait { public SheepItServerDown() { super(); } @@ -30,4 +32,12 @@ public class SheepItServerDown extends SheepItException { public SheepItServerDown(String message_) { super(message_); } + + public String getHumanText() { + return "Cannot connect to the server. Please check your connectivity. Will try again at %tR"; + } + + public int getWaitDuration() { + return 1000 * 60 * ThreadLocalRandom.current().nextInt(10, 30 + 1); + } }