2016-10-04 06:33:25 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2015 Laurent CLOUET
|
|
|
|
|
* Author Laurent CLOUET <laurent.clouet@nopnop.net>
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-04-08 20:45:54 +01:00
|
|
|
package com.sheepit.client;
|
2015-03-31 00:29:58 +01:00
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
2015-04-22 19:41:50 +01:00
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.nio.file.attribute.PosixFilePermission;
|
|
|
|
|
import java.util.HashSet;
|
2015-03-31 00:29:58 +01:00
|
|
|
import java.util.Properties;
|
2015-04-22 19:41:50 +01:00
|
|
|
import java.util.Set;
|
2015-03-31 00:29:58 +01:00
|
|
|
|
|
|
|
|
import com.sheepit.client.Configuration;
|
|
|
|
|
import com.sheepit.client.Configuration.ComputeType;
|
|
|
|
|
import com.sheepit.client.hardware.gpu.GPU;
|
|
|
|
|
import com.sheepit.client.hardware.gpu.GPUDevice;
|
|
|
|
|
|
|
|
|
|
public class SettingsLoader {
|
|
|
|
|
private String path;
|
|
|
|
|
|
|
|
|
|
private String login;
|
|
|
|
|
private String password;
|
2015-07-06 18:41:28 +01:00
|
|
|
private String proxy;
|
2017-05-08 01:35:08 +02:00
|
|
|
private String hostname;
|
2015-03-31 00:29:58 +01:00
|
|
|
private String computeMethod;
|
|
|
|
|
private String gpu;
|
2015-04-20 21:26:07 +01:00
|
|
|
private String cores;
|
2017-03-29 22:13:04 +02:00
|
|
|
private String ram;
|
2017-04-25 13:06:23 +02:00
|
|
|
private String renderTime;
|
2015-03-31 00:29:58 +01:00
|
|
|
private String cacheDir;
|
2015-04-03 19:50:58 +01:00
|
|
|
private String autoSignIn;
|
2015-04-08 20:37:53 +01:00
|
|
|
private String ui;
|
2016-09-21 23:26:44 +02:00
|
|
|
private String tileSize;
|
2017-03-19 17:38:22 +01:00
|
|
|
private int priority;
|
2015-03-31 00:29:58 +01:00
|
|
|
|
|
|
|
|
public SettingsLoader() {
|
2015-04-07 20:07:53 +01:00
|
|
|
path = getDefaultFilePath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SettingsLoader(String path_) {
|
|
|
|
|
path = path_;
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
|
2017-05-08 01:35:08 +02:00
|
|
|
public SettingsLoader(String login_, String password_, String proxy_, String hostname_, ComputeType computeMethod_, GPUDevice gpu_, int cores_, int maxRam_, int maxRenderTime_, String cacheDir_, boolean autoSignIn_, String ui_, String tileSize_, int priority_) {
|
2015-04-07 20:07:53 +01:00
|
|
|
path = getDefaultFilePath();
|
2015-03-31 00:29:58 +01:00
|
|
|
login = login_;
|
|
|
|
|
password = password_;
|
2015-07-06 18:41:28 +01:00
|
|
|
proxy = proxy_;
|
2017-05-08 01:35:08 +02:00
|
|
|
hostname = hostname_;
|
2015-03-31 00:29:58 +01:00
|
|
|
cacheDir = cacheDir_;
|
2015-04-03 19:50:58 +01:00
|
|
|
autoSignIn = String.valueOf(autoSignIn_);
|
2015-04-08 20:37:53 +01:00
|
|
|
ui = ui_;
|
2016-09-21 23:26:44 +02:00
|
|
|
tileSize = tileSize_;
|
2017-03-19 17:38:22 +01:00
|
|
|
priority = priority_;
|
2015-04-20 21:26:07 +01:00
|
|
|
if (cores_ > 0) {
|
|
|
|
|
cores = String.valueOf(cores_);
|
|
|
|
|
}
|
2017-03-29 22:13:04 +02:00
|
|
|
if (maxRam_ > 0) {
|
|
|
|
|
ram = String.valueOf(maxRam_);
|
|
|
|
|
}
|
2017-04-25 13:06:23 +02:00
|
|
|
if (maxRenderTime_ > 0) {
|
|
|
|
|
renderTime = String.valueOf(maxRenderTime_);
|
|
|
|
|
}
|
2015-03-31 00:29:58 +01:00
|
|
|
if (computeMethod_ != null) {
|
|
|
|
|
try {
|
|
|
|
|
computeMethod = computeMethod_.name();
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalArgumentException e) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (gpu_ != null) {
|
2018-08-24 19:46:03 +02:00
|
|
|
gpu = gpu_.getId();
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-07 20:07:53 +01:00
|
|
|
public static String getDefaultFilePath() {
|
|
|
|
|
return System.getProperty("user.home") + File.separator + ".sheepit.conf";
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
|
2015-04-02 20:31:16 +01:00
|
|
|
public String getFilePath() {
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 00:29:58 +01:00
|
|
|
public void saveFile() {
|
|
|
|
|
Properties prop = new Properties();
|
|
|
|
|
OutputStream output = null;
|
|
|
|
|
try {
|
|
|
|
|
output = new FileOutputStream(path);
|
2017-03-19 17:38:22 +01:00
|
|
|
prop.setProperty("priority", new Integer(priority).toString());
|
2015-03-31 00:29:58 +01:00
|
|
|
|
|
|
|
|
if (cacheDir != null) {
|
|
|
|
|
prop.setProperty("cache-dir", cacheDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (computeMethod != null) {
|
|
|
|
|
prop.setProperty("compute-method", computeMethod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (gpu != null) {
|
|
|
|
|
prop.setProperty("compute-gpu", gpu);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-20 21:26:07 +01:00
|
|
|
if (cores != null) {
|
2018-12-11 07:25:04 +01:00
|
|
|
prop.setProperty("cores", cores);
|
2015-04-20 21:26:07 +01:00
|
|
|
}
|
|
|
|
|
|
2017-03-29 22:13:04 +02:00
|
|
|
if (ram != null) {
|
|
|
|
|
prop.setProperty("ram", ram);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 13:06:23 +02:00
|
|
|
if (renderTime != null) {
|
|
|
|
|
prop.setProperty("rendertime", renderTime);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 00:29:58 +01:00
|
|
|
if (login != null) {
|
|
|
|
|
prop.setProperty("login", login);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (password != null) {
|
|
|
|
|
prop.setProperty("password", password);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-06 18:41:28 +01:00
|
|
|
if (proxy != null) {
|
|
|
|
|
prop.setProperty("proxy", proxy);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 01:35:08 +02:00
|
|
|
if (hostname != null) {
|
|
|
|
|
prop.setProperty("hostname", hostname);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-03 19:50:58 +01:00
|
|
|
if (autoSignIn != null) {
|
|
|
|
|
prop.setProperty("auto-signin", autoSignIn);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 20:37:53 +01:00
|
|
|
if (ui != null) {
|
|
|
|
|
prop.setProperty("ui", ui);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 23:26:44 +02:00
|
|
|
if (tileSize != null) {
|
|
|
|
|
prop.setProperty("tile-size", tileSize);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 00:29:58 +01:00
|
|
|
prop.store(output, null);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException io) {
|
|
|
|
|
io.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
if (output != null) {
|
|
|
|
|
try {
|
|
|
|
|
output.close();
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-22 19:41:50 +01:00
|
|
|
|
|
|
|
|
// Set Owner read/write
|
|
|
|
|
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
|
|
|
|
|
perms.add(PosixFilePermission.OWNER_READ);
|
|
|
|
|
perms.add(PosixFilePermission.OWNER_WRITE);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Files.setPosixFilePermissions(Paths.get(path), perms);
|
|
|
|
|
}
|
|
|
|
|
catch (UnsupportedOperationException e) {
|
|
|
|
|
// most likely because it's MS Windows
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void loadFile() {
|
|
|
|
|
this.login = null;
|
|
|
|
|
this.password = null;
|
2015-07-06 18:41:28 +01:00
|
|
|
this.proxy = null;
|
2017-05-08 01:35:08 +02:00
|
|
|
this.hostname = null;
|
2015-03-31 00:29:58 +01:00
|
|
|
this.computeMethod = null;
|
|
|
|
|
this.gpu = null;
|
|
|
|
|
this.cacheDir = null;
|
2015-04-03 19:50:58 +01:00
|
|
|
this.autoSignIn = null;
|
2015-04-08 20:37:53 +01:00
|
|
|
this.ui = null;
|
2016-09-21 23:26:44 +02:00
|
|
|
this.tileSize = null;
|
2017-03-19 17:38:22 +01:00
|
|
|
this.priority = 19; // must be the same default as Configuration
|
2017-03-29 22:13:04 +02:00
|
|
|
this.ram = null;
|
2017-04-25 13:06:23 +02:00
|
|
|
this.renderTime = null;
|
2015-03-31 00:29:58 +01:00
|
|
|
|
|
|
|
|
if (new File(path).exists() == false) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Properties prop = new Properties();
|
|
|
|
|
InputStream input = null;
|
|
|
|
|
try {
|
|
|
|
|
input = new FileInputStream(path);
|
|
|
|
|
prop.load(input);
|
|
|
|
|
|
|
|
|
|
if (prop.containsKey("cache-dir")) {
|
|
|
|
|
this.cacheDir = prop.getProperty("cache-dir");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prop.containsKey("compute-method")) {
|
|
|
|
|
this.computeMethod = prop.getProperty("compute-method");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prop.containsKey("compute-gpu")) {
|
|
|
|
|
this.gpu = prop.getProperty("compute-gpu");
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 07:25:04 +01:00
|
|
|
if (prop.containsKey("cpu-cores")) { // backward compatibility
|
2015-04-20 21:26:07 +01:00
|
|
|
this.cores = prop.getProperty("cpu-cores");
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 07:25:04 +01:00
|
|
|
if (prop.containsKey("cores")) {
|
|
|
|
|
this.cores = prop.getProperty("cores");
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-29 22:13:04 +02:00
|
|
|
if (prop.containsKey("ram")) {
|
|
|
|
|
this.ram = prop.getProperty("ram");
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 13:06:23 +02:00
|
|
|
if (prop.containsKey("rendertime")) {
|
|
|
|
|
this.renderTime = prop.getProperty("rendertime");
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 00:29:58 +01:00
|
|
|
if (prop.containsKey("login")) {
|
|
|
|
|
this.login = prop.getProperty("login");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prop.containsKey("password")) {
|
|
|
|
|
this.password = prop.getProperty("password");
|
|
|
|
|
}
|
2015-04-03 19:50:58 +01:00
|
|
|
|
2015-07-06 18:41:28 +01:00
|
|
|
if (prop.containsKey("proxy")) {
|
|
|
|
|
this.proxy = prop.getProperty("proxy");
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 01:35:08 +02:00
|
|
|
if (prop.containsKey("hostname")) {
|
|
|
|
|
this.hostname = prop.getProperty("hostname");
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-03 19:50:58 +01:00
|
|
|
if (prop.containsKey("auto-signin")) {
|
|
|
|
|
this.autoSignIn = prop.getProperty("auto-signin");
|
|
|
|
|
}
|
2015-04-08 20:37:53 +01:00
|
|
|
|
|
|
|
|
if (prop.containsKey("ui")) {
|
|
|
|
|
this.ui = prop.getProperty("ui");
|
|
|
|
|
}
|
2016-09-21 23:26:44 +02:00
|
|
|
|
|
|
|
|
if (prop.containsKey("tile-size")) {
|
|
|
|
|
this.tileSize = prop.getProperty("tile-size");
|
|
|
|
|
}
|
2017-03-19 17:38:22 +01:00
|
|
|
|
|
|
|
|
if (prop.containsKey("priority")) {
|
|
|
|
|
this.priority = Integer.parseInt(prop.getProperty("priority"));
|
|
|
|
|
}
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
catch (IOException io) {
|
|
|
|
|
io.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
if (input != null) {
|
|
|
|
|
try {
|
|
|
|
|
input.close();
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 23:06:21 +01:00
|
|
|
/**
|
|
|
|
|
* Merge the Settings file with the Configuration.
|
|
|
|
|
* The Configuration will have high priority.
|
|
|
|
|
*/
|
2015-03-31 00:29:58 +01:00
|
|
|
public void merge(Configuration config) {
|
|
|
|
|
if (config == null) {
|
|
|
|
|
System.out.println("SettingsLoader::merge config is null");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadFile();
|
|
|
|
|
|
2015-03-31 23:06:21 +01:00
|
|
|
if (config.login().isEmpty() && login != null) {
|
2015-03-31 00:29:58 +01:00
|
|
|
config.setLogin(login);
|
|
|
|
|
}
|
2015-03-31 23:06:21 +01:00
|
|
|
if (config.password().isEmpty() && password != null) {
|
2015-03-31 00:29:58 +01:00
|
|
|
config.setPassword(password);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-06 18:41:28 +01:00
|
|
|
if ((config.getProxy() == null || config.getProxy().isEmpty()) && proxy != null) {
|
|
|
|
|
config.setProxy(proxy);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 01:35:08 +02:00
|
|
|
if ((config.getHostname() == null || config.getHostname().isEmpty() || config.getHostname().equals(config.getDefaultHostname())) && hostname != null) {
|
|
|
|
|
config.setHostname(hostname);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-19 17:38:22 +01:00
|
|
|
if (config.getPriority() == 19) { // 19 is default value
|
|
|
|
|
config.setUsePriority(priority);
|
|
|
|
|
}
|
2015-04-07 20:37:47 +01:00
|
|
|
try {
|
|
|
|
|
if ((config.getComputeMethod() == null && computeMethod != null) || (computeMethod != null && config.getComputeMethod() != ComputeType.valueOf(computeMethod))) {
|
|
|
|
|
config.setComputeMethod(ComputeType.valueOf(computeMethod));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalArgumentException e) {
|
|
|
|
|
System.err.println("SettingsLoader::merge failed to handle compute method (raw value: '" + computeMethod + "')");
|
|
|
|
|
computeMethod = null;
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
2015-03-31 23:06:21 +01:00
|
|
|
if (config.getGPUDevice() == null && gpu != null) {
|
2015-03-31 00:29:58 +01:00
|
|
|
GPUDevice device = GPU.getGPUDevice(gpu);
|
|
|
|
|
if (device != null) {
|
|
|
|
|
config.setUseGPU(device);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-05 09:35:59 +01:00
|
|
|
if (config.getNbCores() == -1 && cores != null) {
|
2015-04-20 21:26:07 +01:00
|
|
|
config.setUseNbCores(Integer.valueOf(cores));
|
|
|
|
|
}
|
2017-03-29 22:13:04 +02:00
|
|
|
|
|
|
|
|
if (config.getMaxMemory() == -1 && ram != null) {
|
|
|
|
|
config.setMaxMemory(Integer.valueOf(ram));
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 13:06:23 +02:00
|
|
|
if (config.getMaxRenderTime() == -1 && renderTime != null) {
|
|
|
|
|
config.setMaxRenderTime(Integer.valueOf(renderTime));
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-18 22:48:23 +01:00
|
|
|
if (config.getUserHasSpecifiedACacheDir() == false && cacheDir != null) {
|
2015-03-31 00:29:58 +01:00
|
|
|
config.setCacheDir(new File(cacheDir));
|
|
|
|
|
}
|
2015-04-03 19:50:58 +01:00
|
|
|
|
2015-04-08 20:37:53 +01:00
|
|
|
if (config.getUIType() == null && ui != null) {
|
|
|
|
|
config.setUIType(ui);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 23:26:44 +02:00
|
|
|
if (config.getTileSize() == -1 && tileSize != null) {
|
|
|
|
|
config.setTileSize(Integer.valueOf(tileSize));
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-03 19:50:58 +01:00
|
|
|
config.setAutoSignIn(Boolean.valueOf(autoSignIn));
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
2017-03-19 17:38:22 +01:00
|
|
|
return "SettingsLoader [path=" + path + ", login=" + login + ", password=" + password + ", computeMethod=" + computeMethod + ", gpu=" + gpu + ", cacheDir=" + cacheDir + "priority="+priority+"]";
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
}
|