From 7d2209a0f4c6acd040314d56187c010834e717b6 Mon Sep 17 00:00:00 2001 From: Rolf Aretz Lap Date: Sun, 19 Feb 2017 14:19:48 +0100 Subject: [PATCH] Add commands to text user interface --- README.md | 11 ++ src/com/sheepit/client/Job.java | 9 ++ .../sheepit/client/standalone/GuiText.java | 8 ++ .../client/standalone/GuiTextOneLine.java | 8 ++ .../standalone/swing/activity/Working.java | 7 +- .../text/CLIInputActionHandler.java | 104 ++++++++++++++++++ .../standalone/text/CLIInputListener.java | 26 +++++ .../standalone/text/CLIInputObserver.java | 65 +++++++++++ 8 files changed, 232 insertions(+), 6 deletions(-) create mode 100644 src/com/sheepit/client/standalone/text/CLIInputActionHandler.java create mode 100644 src/com/sheepit/client/standalone/text/CLIInputListener.java create mode 100644 src/com/sheepit/client/standalone/text/CLIInputObserver.java diff --git a/README.md b/README.md index e0a1c73..d24732b 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,14 @@ Once you have a jar file, you can view the usage by running: java -jar bin/sheepit-client.jar --help When you are doing development work, you can use a mirror of the main site specially made for demo/dev. The mirror is located at **http://sandbox.sheepit-renderfarm.com**, and you can use it by passing `-server http://sandbox.sheepit-renderfarm.com` to your invocation of the client. + +At the command line ui (-ui text / -ui oneLine) you could type in the following commands and press enter to controll the client: + +* status: to get the current status of the client (paused, stoped, ...) +* priority : to set the renderer process priority +* block: to block the current project +* pause: pause the client to request new jobs after the current frame has finished to render +* resume: resume the client after it was paused +* stop: stop the client after the current frame has finished +* cancel: cancel the stop request +* quit: stops the client directly without finishing the current frame diff --git a/src/com/sheepit/client/Job.java b/src/com/sheepit/client/Job.java index 225a64b..6005eb3 100644 --- a/src/com/sheepit/client/Job.java +++ b/src/com/sheepit/client/Job.java @@ -92,6 +92,15 @@ public class Job { render = new RenderProcess(); } + public void block() { + setAskForRendererKill(true); + setUserBlockJob(true); + RenderProcess process = getProcessRender(); + if (process != null) { + OS.getOS().kill(process.getProcess()); + } + } + public RenderProcess getProcessRender() { return render; } diff --git a/src/com/sheepit/client/standalone/GuiText.java b/src/com/sheepit/client/standalone/GuiText.java index 3c6f1c8..d4d3c25 100644 --- a/src/com/sheepit/client/standalone/GuiText.java +++ b/src/com/sheepit/client/standalone/GuiText.java @@ -21,8 +21,11 @@ package com.sheepit.client.standalone; import com.sheepit.client.Client; import com.sheepit.client.Gui; +import com.sheepit.client.Job; import com.sheepit.client.Log; import com.sheepit.client.Stats; +import com.sheepit.client.standalone.text.CLIInputActionHandler; +import com.sheepit.client.standalone.text.CLIInputObserver; import sun.misc.Signal; import sun.misc.SignalHandler; @@ -47,6 +50,11 @@ public class GuiText implements Gui { public void start() { if (client != null) { + CLIInputObserver cli_input_observer = new CLIInputObserver(client); + cli_input_observer.addListener(new CLIInputActionHandler()); + Thread cli_input_observer_thread = new Thread(cli_input_observer); + cli_input_observer_thread.start(); + Signal.handle(new Signal("INT"), new SignalHandler() { @Override public void handle(Signal signal) { diff --git a/src/com/sheepit/client/standalone/GuiTextOneLine.java b/src/com/sheepit/client/standalone/GuiTextOneLine.java index a770e2b..31a160a 100644 --- a/src/com/sheepit/client/standalone/GuiTextOneLine.java +++ b/src/com/sheepit/client/standalone/GuiTextOneLine.java @@ -21,7 +21,10 @@ package com.sheepit.client.standalone; import com.sheepit.client.Client; import com.sheepit.client.Gui; +import com.sheepit.client.Job; import com.sheepit.client.Stats; +import com.sheepit.client.standalone.text.CLIInputActionHandler; +import com.sheepit.client.standalone.text.CLIInputObserver; import sun.misc.Signal; import sun.misc.SignalHandler; @@ -55,6 +58,11 @@ public class GuiTextOneLine implements Gui { public void start() { if (client != null) { + CLIInputObserver cli_input_observer = new CLIInputObserver(client); + cli_input_observer.addListener(new CLIInputActionHandler()); + Thread cli_input_observer_thread = new Thread(cli_input_observer); + cli_input_observer_thread.start(); + Signal.handle(new Signal("INT"), new SignalHandler() { @Override public void handle(Signal signal) { diff --git a/src/com/sheepit/client/standalone/swing/activity/Working.java b/src/com/sheepit/client/standalone/swing/activity/Working.java index 0a02d64..bcb442c 100644 --- a/src/com/sheepit/client/standalone/swing/activity/Working.java +++ b/src/com/sheepit/client/standalone/swing/activity/Working.java @@ -394,12 +394,7 @@ public class Working implements Activity { if (client != null) { Job job = client.getRenderingJob(); if (job != null) { - job.setAskForRendererKill(true); - job.setUserBlockJob(true); - RenderProcess process = job.getProcessRender(); - if (process != null) { - OS.getOS().kill(process.getProcess()); - } + job.block(); } } } diff --git a/src/com/sheepit/client/standalone/text/CLIInputActionHandler.java b/src/com/sheepit/client/standalone/text/CLIInputActionHandler.java new file mode 100644 index 0000000..fd95aad --- /dev/null +++ b/src/com/sheepit/client/standalone/text/CLIInputActionHandler.java @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2017 Laurent CLOUET + * Author Rolf Aretz Lap + * + * 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.standalone.text; + +import com.sheepit.client.Client; +import com.sheepit.client.Configuration; +import com.sheepit.client.Job; + +public class CLIInputActionHandler implements CLIInputListener { + + @Override + public void commandEntered(Client client, String command) { + int priorityLength = "priority".length(); + + //prevent Null Pointer at next step + if (command == null) { + return; + } + if (client == null) { + return; + } + if (command.equalsIgnoreCase("block")) { + Job job = client.getRenderingJob(); + if (job != null) { + job.block(); + } + } + else if (command.equalsIgnoreCase("resume")) { + client.resume(); + } + else if (command.equalsIgnoreCase("pause")) { + client.suspend(); + } + else if (command.equalsIgnoreCase("stop")) { + client.askForStop(); + } + else if (command.equalsIgnoreCase("status")) { + displayStatus(client); + } + else if (command.equalsIgnoreCase("cancel")) { + client.cancelStop(); + } + else if (command.equalsIgnoreCase("quit")) { + client.stop(); + System.exit(0); + } + else if ((command.length() > priorityLength) && (command.substring(0, priorityLength).equalsIgnoreCase("priority"))) { + changePriority(client, command.substring(priorityLength)); + } + else { + System.out.println("Unknown command: " + command); + System.out.println("status: display client status"); + System.out.println("priority : set the priority for the next renderjob"); + System.out.println("block: block project"); + System.out.println("pause: pause client requesting new jobs"); + System.out.println("resume: resume after client was paused"); + System.out.println("stop: exit after frame was finished"); + System.out.println("cancel: cancel exit"); + System.out.println("quit: exit now"); + } + } + + void changePriority(Client client, String newPriority) { + Configuration config = client.getConfiguration(); + if (config != null) { + try { + config.setUsePriority(Integer.parseInt(newPriority.trim())); + } + catch (NumberFormatException e) { + System.out.println("Invalid priority: " + newPriority); + } + } + + } + + void displayStatus(Client client) { + if (client.isSuspended()) { + System.out.println("Status: paused"); + } + else if (client.isRunning()) { + System.out.println("Status: will exit after the current frame"); + } + else { + System.out.println("Status: running"); + } + } +} diff --git a/src/com/sheepit/client/standalone/text/CLIInputListener.java b/src/com/sheepit/client/standalone/text/CLIInputListener.java new file mode 100644 index 0000000..89a67b8 --- /dev/null +++ b/src/com/sheepit/client/standalone/text/CLIInputListener.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2017 Laurent CLOUET + * Author Rolf Aretz Lap + * + * 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.standalone.text; + +import com.sheepit.client.Client; + +public interface CLIInputListener { + public void commandEntered(Client client, String command); +} diff --git a/src/com/sheepit/client/standalone/text/CLIInputObserver.java b/src/com/sheepit/client/standalone/text/CLIInputObserver.java new file mode 100644 index 0000000..ea70f5f --- /dev/null +++ b/src/com/sheepit/client/standalone/text/CLIInputObserver.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2017 Laurent CLOUET + * Author Rolf Aretz Lap + * + * 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.standalone.text; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +import com.sheepit.client.Client; + +public class CLIInputObserver implements Runnable { + private BufferedReader in; + private Client client; + + public CLIInputObserver(Client client) { + this.client = client; + } + + private List listeners = new ArrayList(); + + public void addListener(CLIInputListener toAdd) { + listeners.add(toAdd); + } + + public void run() { + in = new BufferedReader(new InputStreamReader(System.in)); + String line = ""; + + while ((line != null) && (line.equalsIgnoreCase("quit") == false)) { + try { + line = in.readLine(); + } + catch (Exception e) { + // TODO: handle exception + } + for (CLIInputListener cliil : listeners) + cliil.commandEntered(client, line); + } + try { + in.close(); + } + catch (Exception e) { + // TODO: handle exception + } + + } +}