Cleanup: use generic list instead of low level array

This commit is contained in:
Laurent Clouet
2015-01-25 19:07:32 +00:00
parent 741601712d
commit f444bd5526
6 changed files with 31 additions and 47 deletions

View File

@@ -32,10 +32,10 @@ import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
@@ -529,19 +529,16 @@ public class Client {
size_command += 2; size_command += 2;
} }
String[] command = new String[size_command]; List<String> command = new ArrayList<String>(size_command);
Map<String, String> new_env = new HashMap<String, String>(); Map<String, String> new_env = new HashMap<String, String>();
new_env.put("BLENDER_USER_CONFIG", this.config.workingDirectory.getAbsolutePath().replace("\\", "\\\\")); new_env.put("BLENDER_USER_CONFIG", this.config.workingDirectory.getAbsolutePath().replace("\\", "\\\\"));
int index = 0;
for (String arg : command1) { for (String arg : command1) {
if (arg.equals(".c")) { if (arg.equals(".c")) {
command[index] = ajob.getScenePath(); command.add(ajob.getScenePath());
index += 1; command.add("-P");
command[index] = "-P";
index += 1;
try { try {
script_file = File.createTempFile("script_", "", this.config.workingDirectory); script_file = File.createTempFile("script_", "", this.config.workingDirectory);
@@ -556,7 +553,7 @@ public class Client {
out.write("\n"); // GPU part out.write("\n"); // GPU part
out.close(); out.close();
command[index] = script_file.getAbsolutePath(); command.add(script_file.getAbsolutePath());
} }
catch (IOException e) { catch (IOException e) {
return Error.Type.UNKNOWN; return Error.Type.UNKNOWN;
@@ -564,26 +561,22 @@ public class Client {
script_file.deleteOnExit(); script_file.deleteOnExit();
} }
else if (arg.equals(".e")) { else if (arg.equals(".e")) {
command[index] = ajob.getRendererPath(); command.add(ajob.getRendererPath());
// the number of cores has to be put after the binary and before the scene arg // the number of cores has to be put after the binary and before the scene arg
if (this.config.getNbCores() > 0) { if (this.config.getNbCores() > 0) {
index += 1; command.add("-t");
command[index] = "-t"; command.add(Integer.toString(this.config.getNbCores()));
index += 1;
command[index] = Integer.toString(this.config.getNbCores());
//index += 1; // do not do it, it will be done at the end of the loop
} }
} }
else if (arg.equals(".o")) { else if (arg.equals(".o")) {
command[index] = this.config.workingDirectory.getAbsolutePath() + File.separator + ajob.getPrefixOutputImage(); command.add(this.config.workingDirectory.getAbsolutePath() + File.separator + ajob.getPrefixOutputImage());
} }
else if (arg.equals(".f")) { else if (arg.equals(".f")) {
command[index] = ajob.getFrameNumber(); command.add(ajob.getFrameNumber());
} }
else { else {
command[index] = arg; command.add(arg);
} }
index += 1;
} }
long rending_start = new Date().getTime(); long rending_start = new Date().getTime();
@@ -591,7 +584,7 @@ public class Client {
int nb_lines = 0; int nb_lines = 0;
try { try {
String line; String line;
this.log.debug(Arrays.toString(command)); this.log.debug(command.toString());
OS os = OS.getOS(); OS os = OS.getOS();
ajob.setProcess(os.exec(command, new_env)); ajob.setProcess(os.exec(command, new_env));
BufferedReader input = new BufferedReader(new InputStreamReader(ajob.getProcess().getInputStream())); BufferedReader input = new BufferedReader(new InputStreamReader(ajob.getProcess().getInputStream()));

View File

@@ -28,7 +28,6 @@ import java.io.InputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
@@ -233,18 +232,4 @@ public class Utils {
} }
return Math.round(Double.parseDouble(m.group(1)) * scale); return Math.round(Double.parseDouble(m.group(1)) * scale);
} }
public static <T> T[] concatAll(T[] first, T[]... rest) {
int totalLength = first.length;
for (T[] array : rest) {
totalLength += array.length;
}
T[] result = Arrays.copyOf(first, totalLength);
int offset = first.length;
for (T[] array : rest) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}
return result;
}
} }

View File

@@ -21,11 +21,11 @@ package com.sheepit.client.os;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import com.sheepit.client.Log; import com.sheepit.client.Log;
import com.sheepit.client.Utils;
import com.sheepit.client.hardware.cpu.CPU; import com.sheepit.client.hardware.cpu.CPU;
public class Linux extends OS { public class Linux extends OS {
@@ -122,7 +122,7 @@ public class Linux extends OS {
} }
@Override @Override
public Process exec(String[] command, Map<String, String> env_overight) throws IOException { public Process exec(List<String> command, Map<String, String> env_overight) throws IOException {
// the renderer have a lib directory so add to the LD_LIBRARY_PATH // the renderer have a lib directory so add to the LD_LIBRARY_PATH
// (even if we are not sure that it is the renderer who is launch // (even if we are not sure that it is the renderer who is launch
@@ -130,7 +130,7 @@ public class Linux extends OS {
new_env.putAll(java.lang.System.getenv()); // clone the env new_env.putAll(java.lang.System.getenv()); // clone the env
Boolean has_ld_library_path = new_env.containsKey("LD_LIBRARY_PATH"); Boolean has_ld_library_path = new_env.containsKey("LD_LIBRARY_PATH");
String lib_dir = (new File(command[0])).getParent() + File.separator + "lib"; String lib_dir = (new File(command.get(0))).getParent() + File.separator + "lib";
String new_ld_library_path = "/lib:/lib64:/usr/lib:/usr/lib64:/lib/i386-linux-gnu:/lib/x86_64-linux-gnu:/usr/share/local" + ":" + lib_dir; String new_ld_library_path = "/lib:/lib64:/usr/lib:/usr/lib64:/lib/i386-linux-gnu:/lib/x86_64-linux-gnu:/usr/share/local" + ":" + lib_dir;
if (has_ld_library_path == false) { if (has_ld_library_path == false) {
new_env.put("LD_LIBRARY_PATH", new_ld_library_path); new_env.put("LD_LIBRARY_PATH", new_ld_library_path);
@@ -139,13 +139,15 @@ public class Linux extends OS {
new_env.put("LD_LIBRARY_PATH", new_env.get("LD_LIBRARY_PATH") + ":" + new_ld_library_path); new_env.put("LD_LIBRARY_PATH", new_env.get("LD_LIBRARY_PATH") + ":" + new_ld_library_path);
} }
String[] actual_command = command; List<String> actual_command = command;
if (this.hasNiceBinary == null) { if (this.hasNiceBinary == null) {
this.checkNiceAvailability(); this.checkNiceAvailability();
} }
if (this.hasNiceBinary.booleanValue()) { if (this.hasNiceBinary.booleanValue()) {
String[] low = { NICE_BINARY_PATH, "-n", "19" }; // launch the process in lowest priority // launch the process in lowest priority
actual_command = Utils.concatAll(low, command); actual_command.add(0, "19");
actual_command.add(0, "-n");
actual_command.add(0, NICE_BINARY_PATH);
} }
else { else {
Log.getInstance(null).error("No low priority binary, will not launch renderer in normal priority"); Log.getInstance(null).error("No low priority binary, will not launch renderer in normal priority");

View File

@@ -22,10 +22,10 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.List;
import java.util.Map; import java.util.Map;
import com.sheepit.client.Log; import com.sheepit.client.Log;
import com.sheepit.client.Utils;
import com.sheepit.client.hardware.cpu.CPU; import com.sheepit.client.hardware.cpu.CPU;
public class Mac extends OS { public class Mac extends OS {
@@ -141,14 +141,16 @@ public class Mac extends OS {
} }
@Override @Override
public Process exec(String[] command, Map<String, String> env) throws IOException { public Process exec(List<String> command, Map<String, String> env) throws IOException {
String[] actual_command = command; List<String> actual_command = command;
if (this.hasNiceBinary == null) { if (this.hasNiceBinary == null) {
this.checkNiceAvailability(); this.checkNiceAvailability();
} }
if (this.hasNiceBinary.booleanValue()) { if (this.hasNiceBinary.booleanValue()) {
String[] low = { NICE_BINARY_PATH, "-n", "19" }; // launch the process in lowest priority // launch the process in lowest priority
actual_command = Utils.concatAll(low, command); actual_command.add(0, "19");
actual_command.add(0, "-n");
actual_command.add(0, NICE_BINARY_PATH);
} }
else { else {
Log.getInstance(null).error("No low priority binary, will not launch renderer in normal priority"); Log.getInstance(null).error("No low priority binary, will not launch renderer in normal priority");

View File

@@ -19,6 +19,7 @@
package com.sheepit.client.os; package com.sheepit.client.os;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Map; import java.util.Map;
import com.sheepit.client.hardware.cpu.CPU; import com.sheepit.client.hardware.cpu.CPU;
@@ -38,7 +39,7 @@ public abstract class OS {
return null; return null;
} }
public Process exec(String[] command, Map<String, String> env) throws IOException { public Process exec(List<String> command, Map<String, String> env) throws IOException {
ProcessBuilder builder = new ProcessBuilder(command); ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true); builder.redirectErrorStream(true);
if (env != null) { if (env != null) {

View File

@@ -19,6 +19,7 @@
package com.sheepit.client.os; package com.sheepit.client.os;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Map; import java.util.Map;
import com.sheepit.client.hardware.cpu.CPU; import com.sheepit.client.hardware.cpu.CPU;
@@ -107,7 +108,7 @@ public class Windows extends OS {
} }
@Override @Override
public Process exec(String[] command, Map<String, String> env) throws IOException { public Process exec(List<String> command, Map<String, String> env) throws IOException {
// disable a popup because the renderer might crash (seg fault) // disable a popup because the renderer might crash (seg fault)
Kernel32Lib kernel32lib = null; Kernel32Lib kernel32lib = null;
try { try {