Ref: cleanup, organise packages

This commit is contained in:
Sheepit Renderfarm
2024-12-14 13:54:56 +00:00
parent 2e10f7379d
commit 21059a4f19
67 changed files with 222 additions and 172 deletions

View File

@@ -0,0 +1,280 @@
/*
* Copyright (C) 2010-2014 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.
*/
package com.sheepit.client.config;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.List;
import com.sheepit.client.Client;
import com.sheepit.client.hardware.cpu.CPU;
import com.sheepit.client.hardware.gpu.GPUDevice;
import com.sheepit.client.os.OS;
import com.sheepit.client.utils.Pair;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* Provides facilities regarding the configuration of the SheepIt client
*/
@AllArgsConstructor
@Data public class Configuration {
public static final String WORKING_DIRNAME = "sheepit";
public static final String WOOL_CACHE_DIRNAME = "sheepit_wool_cache";
public static final String jarVersion = getJarVersion();
public enum ComputeType {
CPU_GPU, CPU, GPU
} // accept job for ...
private String configFilePath;
private String logDirectory;
private File workingDirectory;
private File sharedDownloadsDirectory;
private File woolCacheDirectory; // store all the chunks (project and blender)
private boolean userHasSpecifiedACacheDir;
private String login;
private String password;
private String proxy;
private int maxUploadingJob;
private int nbCores;
private long maxAllowedMemory; // in KiB, max memory allowed for render
private int maxRenderTime; // max render time per frame allowed
private int priority;
private ComputeType computeMethod;
private GPUDevice GPUDevice;
private boolean detectGPUs;
private boolean printLog;
private boolean debugLevel;
private List<Pair<Calendar, Calendar>> requestTime;
private long shutdownTime;
private String shutdownMode;
private String extras;
private boolean autoSignIn;
private boolean useSysTray;
private boolean headless;
private String UIType;
private String hostname;
private String theme;
private boolean disableLargeDownloads;
private String incompatibleProcess;
public Configuration(File cache_dir_, String login_, String password_) {
this.configFilePath = null;
this.logDirectory = null;
this.login = login_;
this.password = password_;
this.proxy = null;
this.hostname = this.getDefaultHostname();
this.maxUploadingJob = 1;
this.nbCores = -1; // ie not set
this.maxAllowedMemory = -1; // ie not set
this.maxRenderTime = -1; // ie not set
this.priority = 19; // default lowest
this.computeMethod = null;
this.GPUDevice = null;
this.userHasSpecifiedACacheDir = false;
this.detectGPUs = true;
this.workingDirectory = null;
this.sharedDownloadsDirectory = null;
this.woolCacheDirectory = null;
this.setCacheDir(cache_dir_);
this.printLog = false;
this.debugLevel = false;
this.requestTime = null;
this.shutdownTime = -1;
this.shutdownMode = "soft";
this.extras = "";
this.autoSignIn = false;
this.useSysTray = false;
this.headless = java.awt.GraphicsEnvironment.isHeadless();
this.UIType = null;
this.theme = null;
this.disableLargeDownloads = false;
this.incompatibleProcess = null;
}
/**
* @return string formatted with SheepIt Admin Log Viewer in mind
*/
@Override public String toString() {
String c = " CFG: ";
String n ="\n";
return
c + "version: " + getJarVersion() + n +
c + "configFilePath: " + configFilePath + n +
c + "logDir: " + (logDirectory == null ? "NULL" : logDirectory) + n +
c + "workingDirectory: " + workingDirectory + n +
c + "sharedDownloadsDirectory: " + sharedDownloadsDirectory + n +
c + "woolCacheDirectory: " + woolCacheDirectory + n +
c + "userHasSpecifiedACacheDir: " + userHasSpecifiedACacheDir + n +
c + "login: " + login + n +
c + "proxy: " + proxy + n +
c + "maxUploadingJob: " + maxUploadingJob + n +
c + "nbCores: " + nbCores + n +
c + "maxAllowedMemory: " + maxAllowedMemory + n +
c + "maxRenderTime: " + maxRenderTime + n +
c + "priority: " + priority + n +
c + "computeMethod: " + computeMethod + n +
c + "GPUDevice: " + GPUDevice + n +
c + "detectGPUs: " + detectGPUs + n +
c + "printLog: " + printLog + n +
c + "debugLog: " + debugLevel + n +
c + "requestTime: " + requestTime + n +
c + "shutdownTime: " + shutdownTime + n +
c + "shutdownMode: " + shutdownMode + n +
c + "extras: " + extras + n +
c + "autoSignIn: " + autoSignIn + n +
c + "useSysTray: " + useSysTray + n +
c + "headless: " + headless + n +
c + "UIType: " + UIType + n +
c + "hostname: " + hostname + n +
c + "theme: " + theme + n +
c + "incompatibleProcess: " + incompatibleProcess + n +
c + "disableLargeDownloads: " + disableLargeDownloads;
}
/**
* Sets the priority that Blender will be started with
* @param priority integer that will be clamped between 19 and -19,
* lowest to highest in terms of priority
*/
public void setPriority(int priority) {
if (priority > 19)
priority = 19;
else if (priority < -19)
priority = -19;
this.priority = priority;
}
/**
* Returns ordinal integer of the computeMethod enum
* @return ordinal integer of the computeMethod enum
*/
public int computeMethodToInt() {
return this.computeMethod.ordinal();
}
/**
* Sets and optionally creates cache directory
* @param cache_dir_ File representing cache directory
*/
public void setCacheDir(File cache_dir_) {
if (cache_dir_ == null) {
this.userHasSpecifiedACacheDir = false;
try {
this.workingDirectory = File.createTempFile("farm_", "");
this.workingDirectory.createNewFile(); // hoho...
this.workingDirectory.delete(); // hoho
}
catch (IOException e) {
e.printStackTrace();
}
// since there is no working directory and the client will be working in the system temp directory,
// we can also set up a 'permanent' directory for immutable files (like renderer binary)
this.woolCacheDirectory = new File(this.workingDirectory.getParent() + File.separator + WOOL_CACHE_DIRNAME);
}
else {
this.userHasSpecifiedACacheDir = true;
this.workingDirectory = new File(cache_dir_.getAbsolutePath() + File.separator + WORKING_DIRNAME);
this.woolCacheDirectory = new File(cache_dir_.getAbsolutePath() + File.separator + WOOL_CACHE_DIRNAME);
}
}
/**
* @return the user specified cache directory or null if it hasn't been specified
*/
public File getCacheDirForSettings() {
// when the user has a cache directory specified,
// a "sheepit" and "wool_cache" directory is to be automatically created
return this.userHasSpecifiedACacheDir == false ? null : this.workingDirectory.getParentFile();
}
/**
* @return string-representation of the hostname or an empty string if an error was encountered
*/
public String getDefaultHostname() {
try {
return InetAddress.getLocalHost().getHostName();
}
catch (UnknownHostException e) {
return "";
}
}
/**
* @return a string representing the version of the jar or
* {@code 6.0.0} if on error is encountered or the VERSION file doesn't exist
*/
private static String getJarVersion() {
String versionPath = "/VERSION";
String version = "6.0.0";
InputStream versionStream = Client.class.getResourceAsStream(versionPath);
if (versionStream != null) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(versionStream));
version = in.readLine();
}
catch (IOException ex) {
System.err.println("Configuration::getJarVersion error while reading manifest file (" + versionPath + "): " + ex.getMessage());
}
}
else {
System.err.println("Configuration::getJarVersion Failed to get version file");
}
return version;
}
/**
* @return true if operating system is in among the supported platforms
* and is running in a compatible configuration
* false otherwise
* @see OS#isSupported()
*/
public boolean checkOSisSupported() {
return OS.getOS() != null && OS.getOS().isSupported();
}
/**
* @return true if operating system is in among the supported platforms
* and if we have data on the cpu
* false otherwise
* @see CPU#haveData()
*/
public boolean checkCPUisSupported() {
OS os = OS.getOS();
if (os != null) {
CPU cpu = os.getCPU();
return cpu != null && cpu.haveData();
}
return false;
}
}

View File

@@ -0,0 +1,282 @@
/*
* Copyright (C) 2023 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.
*/
package com.sheepit.client.config;
import com.sheepit.client.logger.Log;
import com.sheepit.client.datamodel.server.Chunk;
import com.sheepit.client.utils.Utils;
import lombok.AllArgsConstructor;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
@AllArgsConstructor
public class DirectoryManager {
private Configuration configuration;
public String getActualStoragePathFor(Chunk chunk) {
return isSharedEnabled() ? getSharedPathFor(chunk) : getCachePathFor(chunk);
}
public String getCachePathFor(Chunk chunk) {
return configuration.getWoolCacheDirectory().getAbsolutePath() + File.separator + chunk.getId() + ".wool";
}
public String getSharedPathFor(Chunk chunk) {
return configuration.getSharedDownloadsDirectory().getAbsolutePath() + File.separator + chunk.getId() + ".wool";
}
public boolean isSharedEnabled() {
return configuration.getSharedDownloadsDirectory() != null && configuration.getSharedDownloadsDirectory().exists();
}
public boolean copyChunkFromSharedToCache(Chunk chunk) {
return copyFileFromSharedToCache(getSharedPathFor(chunk), getCachePathFor(chunk));
}
private boolean copyFileFromSharedToCache(String source, String target) {
Path existingArchivePath = Paths.get(source);
Path targetArchivePath = Paths.get(target);
if (existingArchivePath.equals(targetArchivePath)) {
// target are the same, do nothing
return true;
}
try {
Files.deleteIfExists(targetArchivePath); // createLink only works if the target does not exist
try {
Files.createLink(targetArchivePath, existingArchivePath);
Log.getInstance().debug("Created hardlink from " + targetArchivePath + " to " + existingArchivePath);
}
catch (UnsupportedOperationException // underlying file system does not support hard-linking
| FileSystemException // cache-dir and shared-zip are on separate file systems, even though hard-linking is supported
| SecurityException // user is not allowed to create hard-links
ignore) {
// Creating hardlinks might not be supported on some filesystems
Log.getInstance().debug("Failed to create hardlink, falling back to copying file to " + targetArchivePath);
Files.copy(existingArchivePath, targetArchivePath, StandardCopyOption.REPLACE_EXISTING);
}
}
catch (IOException e) {
Log.getInstance().error("Error while copying " + source + " from shared downloads directory to working dir");
return false;
}
return true;
}
/**
* Creates cache directory
*/
public void createCacheDir() {
this.removeWorkingDirectory();
this.configuration.getWorkingDirectory().mkdirs();
this.configuration.getWoolCacheDirectory().mkdirs();
if (this.configuration.getSharedDownloadsDirectory() != null) {
this.configuration.getSharedDownloadsDirectory().mkdirs();
if (this.configuration.getSharedDownloadsDirectory().exists() == false) {
System.err.println("DirectoryManager::createCacheDir Unable to create common directory " + this.configuration.getSharedDownloadsDirectory().getAbsolutePath());
}
}
}
/**
* Cleans working directory and also deletes it if the user hasn't specified a cache directory
*/
public void removeWorkingDirectory() {
if (this.configuration.isUserHasSpecifiedACacheDir()) {
this.cleanWorkingDirectory();
}
else {
Utils.delete(this.configuration.getWorkingDirectory());
}
}
/**
* Deletes the working and storage directories
*/
public void cleanWorkingDirectory() {
this.cleanDirectory(this.configuration.getWorkingDirectory());
this.cleanDirectory(this.configuration.getWoolCacheDirectory());
}
/**
* Cleans a directory and removes files in it from the md5 cache
* @param dir representing the directory to be cleaned
* @return false if the dir null, true otherwise
*/
private boolean cleanDirectory(File dir) {
if (dir == null) {
return false;
}
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
Utils.delete(file);
}
else {
try {
String extension = file.getName().substring(file.getName().lastIndexOf('.')).toLowerCase();
String name = file.getName().substring(0, file.getName().length() - 1 * extension.length());
if (".wool".equals(extension)) {
// check if the md5 of the file is ok
String md5_local = Utils.md5(file.getAbsolutePath());
if (md5_local.equals(name) == false) {
file.delete();
}
// TODO: remove old one
}
else {
file.delete();
}
}
catch (StringIndexOutOfBoundsException e) { // because the file does not have an . in his path
file.delete();
}
}
}
}
return true;
}
/**
* @return a list of archives (files with extensions .zip or .wool) in the
* working, storage, and shared downloads directories as long as they are not null
*/
public List<File> getLocalCacheFiles() {
List<File> files_local = new LinkedList<File>();
List<File> files = new LinkedList<File>();
if (this.configuration.getWorkingDirectory() != null) {
File[] filesInDirectory = this.configuration.getWorkingDirectory().listFiles();
if (filesInDirectory != null) {
files.addAll(Arrays.asList(filesInDirectory));
}
}
if (this.configuration.getWoolCacheDirectory() != null) {
File[] filesInDirectory = this.configuration.getWoolCacheDirectory().listFiles();
if (filesInDirectory != null) {
files.addAll(Arrays.asList(filesInDirectory));
}
}
if (this.configuration.getSharedDownloadsDirectory() != null) {
File[] filesInDirectory = this.configuration.getSharedDownloadsDirectory().listFiles();
if (filesInDirectory != null) {
files.addAll(Arrays.asList(filesInDirectory));
}
}
for (File file : files) {
if (file.isFile()) {
try {
String extension = file.getName().substring(file.getName().lastIndexOf('.')).toLowerCase();
String name = file.getName().substring(0, file.getName().length() - 1 * extension.length());
if (".wool".equals(extension)) {
// check if the md5 of the file is ok
String md5_local = Utils.md5(file.getAbsolutePath());
if (md5_local.equals(name)) {
files_local.add(file);
}
}
}
catch (StringIndexOutOfBoundsException e) { // because the file does not have an . his path
}
}
}
return files_local;
}
/**
* Runs through all SheepIt related directories and checks if files and folders are all readable, writeable
* and in case of directories, checks if the contents can be listed and if usable space is enough.
* Only logs instances where something was detected, otherwise is it will only print "FilesystemHealthCheck started"
* @return an ArrayList of Strings containing all logs of the FSHealth check
*/
public List<String> filesystemHealthCheck() {
List<String> logs = new ArrayList<>();
String f = "FSHealth: ";
logs.add(f + "FilesystemHealthCheck started");
List<File> dirsToCheck = new ArrayList<>();
List<File> dirsChecked = new ArrayList<>();
dirsToCheck.add(configuration.getWorkingDirectory().getAbsoluteFile());
if (configuration.getSharedDownloadsDirectory() != null && dirsToCheck.contains(configuration.getSharedDownloadsDirectory().getAbsoluteFile()) == false) {
dirsToCheck.add(configuration.getSharedDownloadsDirectory().getAbsoluteFile());
}
if (configuration.getWoolCacheDirectory() != null && dirsToCheck.contains(configuration.getWoolCacheDirectory().getAbsoluteFile()) == false) {
dirsToCheck.add(configuration.getWoolCacheDirectory().getAbsoluteFile());
}
ListIterator<File> dirs = dirsToCheck.listIterator();
while (dirs.hasNext()) {
File dir = dirs.next();
dirs.remove();
dirsChecked.add(dir);
File[] fileList = dir.listFiles();
if (fileList == null) {
logs.add(f + "File list of " + dir + " is null");
}
else {
for (File file : fileList) {
file = file.getAbsoluteFile();
//logs.add(f + file);
boolean canRead = file.canRead();
boolean canWrite = file.canWrite();
boolean isDir = file.isDirectory();
if (canRead == false) {
logs.add(f + "Can't read from " + file);
}
if (canWrite == false) {
logs.add(f + "Can't write to " + file);
}
if (canRead && canWrite && isDir) {
if (dirsChecked.contains(file)) {
logs.add(f + "Dir " + file + " already checked. Loop detected");
}
else {
dirs.add(file);
}
long usableSpace = file.getUsableSpace();
if (usableSpace < 512 * 1024) {
logs.add(f + "Usable space is " + usableSpace + " for " + file);
}
}
}
}
}
return logs;
}
}

View File

@@ -0,0 +1,600 @@
/*
* 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.
*/
package com.sheepit.client.config;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import com.sheepit.client.config.Configuration.ComputeType;
import com.sheepit.client.hardware.gpu.GPU;
import com.sheepit.client.hardware.gpu.GPUDevice;
import com.sheepit.client.main.Option;
import com.sheepit.client.ui.GuiText;
import com.sheepit.client.ui.GuiTextOneLine;
import com.sheepit.client.os.OS;
import com.sheepit.client.utils.Utils;
import lombok.Data;
@Data
public class SettingsLoader {
private enum PropertyNames {
PRIORITY("priority"),
CACHE_DIR("cache-dir"),
SHARED_ZIP("shared-zip"),
COMPUTE_METHOD("compute-method"),
GPU("compute-gpu"),
CORES("cores"),
CORES_BACKWARDS_COMPAT("cpu-cores"),
RAM("ram"),
RENDER_TIME("rendertime"),
LOGIN("login"),
PASSWORD("password"),
PROXY("proxy"),
HOSTNAME("hostname"),
AUTO_SIGNIN("auto-signin"),
USE_SYSTRAY("use-systray"),
HEADLESS("headless"),
UI("ui"),
THEME("theme"),
LOG_DIR("log-dir"),
DEBUG("debug"),
INCOMPATIBLE_PROCESS("incompatible-process"),
DISABLE_LARGE_DOWNLOADS("disable-large-downloads");
String propertyName;
PropertyNames(String prop) {
this.propertyName = prop;
}
@Override public String toString() {
return propertyName;
}
}
public static final String ARG_SERVER = "-server";
public static final String ARG_LOGIN = "-login";
public static final String ARG_PASSWORD = "-password";
public static final String ARG_CACHE_DIR = "-cache-dir";
public static final String ARG_SHARED_ZIP = "-shared-zip";
public static final String ARG_GPU = "-gpu";
public static final String ARG_NO_GPU = "--no-gpu";
public static final String ARG_COMPUTE_METHOD = "-compute-method";
public static final String ARG_CORES = "-cores";
public static final String ARG_MEMORY = "-memory";
public static final String ARG_RENDERTIME = "-rendertime";
public static final String ARG_VERBOSE = "--verbose";
public static final String ARG_LOG_STDOUT = "--log-stdout";
public static final String ARG_REQUEST_TIME = "-request-time";
public static final String ARG_SHUTDOWN = "-shutdown";
public static final String ARG_SHUTDOWN_MODE = "-shutdown-mode";
public static final String ARG_PROXY = "-proxy";
public static final String ARG_EXTRAS = "-extras";
public static final String ARG_UI = "-ui";
public static final String ARG_CONFIG = "-config";
public static final String ARG_LOG_DIRECTORY = "-logdir";
public static final String ARG_VERSION = "--version";
public static final String ARG_SHOW_GPU = "--show-gpu";
public static final String ARG_NO_SYSTRAY = "--no-systray";
public static final String ARG_PRIORITY = "-priority";
public static final String ARG_TITLE = "-title";
public static final String ARG_THEME = "-theme";
public static final String ARG_HOSTNAME = "-hostname";
public static final String ARG_HEADLESS = "--headless";
public static final String ARG_DISABLE_LARGE_DOWNLOADS = "--disable-large-downloads";
public static final String ARG_INCOMPATIBLE_PROCESS = "-incompatible-process";
private String path;
private Option<String> login;
private Option<String> password;
private Option<String> logDir;
private Option<String> debug;
private Option<String> proxy;
private Option<String> hostname;
private Option<String> computeMethod;
private Option<String> gpu;
private Option<String> cores;
private Option<String> ram;
private Option<String> renderTime;
private Option<String> cacheDir;
private Option<String> sharedZip;
private Option<String> autoSignIn;
private Option<String> useSysTray;
private Option<String> headless;
private Option<String> ui;
private Option<String> theme;
private Option<Integer> priority;
private Option<String> disableLargeDownloads;
private Option<String> incompatibleProcess;
public SettingsLoader(String path_) {
if (path_ == null) {
path = OS.getOS().getDefaultConfigFilePath();
}
else {
path = path_;
}
}
public void setSettings(String path_, String login_, String password_, String proxy_, String hostname_,
ComputeType computeMethod_, GPUDevice gpu_, Integer cores_, Long maxRam_,
Integer maxRenderTime_, String cacheDir_, String sharedZip_, Boolean autoSignIn_, Boolean useSysTray_,
Boolean isHeadless, String ui_, String theme_, Integer priority_, Boolean disableLargeDownloads_, Boolean debug_, String incompatibleProcess_) {
if (path_ == null) {
path = OS.getOS().getDefaultConfigFilePath();
}
else {
path = path_;
}
login = setValue(login_, login, ARG_LOGIN);
password = setValue(password_, password, ARG_PASSWORD);
proxy = setValue(proxy_, proxy, ARG_PROXY);
hostname = setValue(hostname_, hostname, ARG_HOSTNAME);
cacheDir = setValue(cacheDir_, cacheDir, ARG_CACHE_DIR);
sharedZip = setValue(sharedZip_, sharedZip, ARG_SHARED_ZIP);
autoSignIn = setValue(autoSignIn_.toString(), autoSignIn, "");
useSysTray = setValue(useSysTray_.toString(), useSysTray, ARG_NO_SYSTRAY);
headless = setValue(isHeadless.toString(), headless, ARG_HEADLESS);
ui = setValue(ui_, ui, ARG_UI);
priority = setValue(priority_, priority, ARG_PRIORITY);
theme = setValue(theme_, theme, ARG_THEME);
disableLargeDownloads = setValue(disableLargeDownloads_.toString(), disableLargeDownloads, ARG_DISABLE_LARGE_DOWNLOADS);
debug = setValue(debug_.toString(), debug, ARG_VERBOSE);
incompatibleProcess = setValue(incompatibleProcess_, incompatibleProcess, ARG_INCOMPATIBLE_PROCESS);
if (cores_ > 0) {
cores = setValue(cores_.toString(), cores, ARG_CORES);
}
if (maxRam_ > 0) {
ram = setValue(maxRam_+ "k", ram, ARG_MEMORY);
}
if (maxRenderTime_ > 0) {
renderTime = setValue(maxRenderTime_.toString(), renderTime, ARG_RENDERTIME);
}
if (computeMethod_ != null) {
try {
computeMethod = setValue(computeMethod_.name(), computeMethod, ARG_COMPUTE_METHOD);
}
catch (IllegalArgumentException e) {
}
}
if (gpu_ != null) {
gpu = setValue(gpu_.getId(), gpu, ARG_GPU);
}
}
/**
* sets an option to a given value. If the option being passed on is null it will be created with the given value and returned.
* @param value The value to be set
* @param option The {@link Option} object that the value is going to be stored in. Can be null
* @param launchFlag A flag indicating whether the option was set via a launch argument or not
* @param <T> The type of the value stored within the option
* @return The {@link Option} object that has been passed on as a parameter with the value being set, or a newly created object if option was null
*/
private <T> Option<T> setValue(T value, Option<T> option, String launchFlag) {
if (option == null && value != null) {
option = new Option<>(value, launchFlag);
}
else if (value != null){
option.setValue(value);
}
return option;
}
public String getFilePath() {
return path;
}
/**
* Takes the list of launch parameters and marks every config setting corresponding to one of the set values as launch command, ensuring that they wont overwrite
* the one in the config file
* @param argsList a list of the launch arguments
*/
public void markLaunchSettings(List<String> argsList) {
Option options[] = { login, password, proxy, hostname, computeMethod, gpu, cores, ram, renderTime, cacheDir, sharedZip,
autoSignIn, useSysTray, headless, ui, theme, priority, disableLargeDownloads };
for (Option option : options) {
if (option != null && argsList.contains(option.getLaunchFlag())) {
option.setLaunchCommand(true);
}
}
}
/**
* Selects the right setting to store to the config file between the value currently set in the config file and the option the client is working with
* currently, depending on whether the setting was set via launch argument or not
* @param saveTo the properties object representing the config file that is going to be written
* @param configFileProperties the properties object containing the current config file values
* @param property an enum representing the name of the setting
* @param option the option containing the currently used value
*/
private void setProperty(Properties saveTo, Properties configFileProperties, PropertyNames property, Option<String> option) {
if (option != null) {
if (option.isLaunchCommand()) {
String configValue = configFileProperties.getProperty(property.propertyName);
if (configValue != null) {
saveTo.setProperty(property.propertyName, configValue);
}
}
else {
saveTo.setProperty(property.propertyName, option.getValue());
}
}
}
@SuppressWarnings("PointlessBooleanExpression") public void saveFile() {
Properties configFileProp = new Properties();
if (new File(path).exists()) {
try {
InputStream input = new FileInputStream(path);
configFileProp.load(input);
} catch (IOException e) {
e.printStackTrace();
}
}
Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream(path);
setProperty(prop, configFileProp, PropertyNames.PRIORITY,
new Option<>(priority != null ? priority.getValue().toString() : null, priority.isLaunchCommand(), ARG_PRIORITY));
setProperty(prop, configFileProp, PropertyNames.CACHE_DIR, cacheDir);
setProperty(prop, configFileProp, PropertyNames.SHARED_ZIP, sharedZip);
setProperty(prop, configFileProp, PropertyNames.COMPUTE_METHOD, computeMethod);
setProperty(prop, configFileProp, PropertyNames.GPU, gpu);
setProperty(prop, configFileProp, PropertyNames.CORES, cores);
setProperty(prop, configFileProp, PropertyNames.RAM, ram);
setProperty(prop, configFileProp, PropertyNames.RENDER_TIME, renderTime);
setProperty(prop, configFileProp, PropertyNames.LOGIN, login);
setProperty(prop, configFileProp, PropertyNames.PASSWORD, password);
setProperty(prop, configFileProp, PropertyNames.PROXY, proxy);
setProperty(prop, configFileProp, PropertyNames.HOSTNAME, hostname);
setProperty(prop, configFileProp, PropertyNames.AUTO_SIGNIN, autoSignIn);
setProperty(prop, configFileProp, PropertyNames.USE_SYSTRAY, useSysTray);
setProperty(prop, configFileProp, PropertyNames.HEADLESS, headless);
setProperty(prop, configFileProp, PropertyNames.UI, ui);
setProperty(prop, configFileProp, PropertyNames.THEME, theme);
setProperty(prop, configFileProp, PropertyNames.DISABLE_LARGE_DOWNLOADS, disableLargeDownloads);
setProperty(prop, configFileProp, PropertyNames.LOG_DIR, logDir);
setProperty(prop, configFileProp, PropertyNames.DEBUG, debug);
setProperty(prop, configFileProp, PropertyNames.INCOMPATIBLE_PROCESS, incompatibleProcess);
prop.store(output, null);
}
catch (IOException io) {
io.printStackTrace();
}
finally {
if (output != null) {
try {
output.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
// 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();
}
}
/**
* Initializes or sets an option object to the corresponding value from the config file
* @param config the properties loaded from the config file
* @param property the name of the property
* @param option the option to store the property value
* @param launchFlag the launch argument corresponding to the respective option
*/
private Option<String> loadConfigOption(Properties config, PropertyNames property, Option<String> option, String launchFlag) {
String configValue;
if (config.containsKey(property.propertyName)) {
configValue = config.getProperty(property.propertyName);
if (option == null && configValue != null) {
option = new Option<>(configValue, launchFlag);
}
else if (configValue != null){
option.setValue(configValue);
}
}
return option;
}
public void loadFile(boolean initialize) throws Exception {
if (initialize)
initWithDefaults();
if (new File(path).exists() == false) {
return;
}
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(path);
prop.load(input);
cacheDir = loadConfigOption(prop, PropertyNames.CACHE_DIR, cacheDir, ARG_CACHE_DIR);
sharedZip = loadConfigOption(prop, PropertyNames.SHARED_ZIP, sharedZip, ARG_SHARED_ZIP);
computeMethod = loadConfigOption(prop, PropertyNames.COMPUTE_METHOD, computeMethod, ARG_COMPUTE_METHOD);
gpu = loadConfigOption(prop, PropertyNames.GPU, gpu, ARG_GPU);
cores = loadConfigOption(prop, PropertyNames.CORES_BACKWARDS_COMPAT, cores, ARG_CORES);
cores = loadConfigOption(prop, PropertyNames.CORES, cores, ARG_CORES);
ram = loadConfigOption(prop, PropertyNames.RAM, ram, ARG_MEMORY);
renderTime = loadConfigOption(prop, PropertyNames.RENDER_TIME, renderTime, ARG_RENDERTIME);
login = loadConfigOption(prop, PropertyNames.LOGIN, login, ARG_LOGIN);
password = loadConfigOption(prop, PropertyNames.PASSWORD, password, ARG_PASSWORD);
proxy = loadConfigOption(prop, PropertyNames.PROXY, proxy, ARG_PROXY);
hostname = loadConfigOption(prop, PropertyNames.HOSTNAME, hostname, ARG_HOSTNAME);
autoSignIn = loadConfigOption(prop, PropertyNames.AUTO_SIGNIN, autoSignIn, "");
useSysTray = loadConfigOption(prop, PropertyNames.USE_SYSTRAY, useSysTray, ARG_NO_SYSTRAY);
headless = loadConfigOption(prop, PropertyNames.HEADLESS, headless, ARG_HEADLESS);
ui = loadConfigOption(prop, PropertyNames.UI, ui, ARG_UI);
theme = loadConfigOption(prop, PropertyNames.THEME, theme, ARG_THEME);
disableLargeDownloads = loadConfigOption(prop, PropertyNames.DISABLE_LARGE_DOWNLOADS, disableLargeDownloads, ARG_DISABLE_LARGE_DOWNLOADS);
logDir = loadConfigOption(prop, PropertyNames.LOG_DIR, logDir, ARG_LOG_DIRECTORY);
incompatibleProcess = loadConfigOption(prop, PropertyNames.INCOMPATIBLE_PROCESS, incompatibleProcess, "");
debug = loadConfigOption(prop, PropertyNames.DEBUG, debug, ARG_VERBOSE);
if (prop.containsKey(PropertyNames.PRIORITY.propertyName)) {
int prio = Integer.parseInt(prop.getProperty(PropertyNames.PRIORITY.propertyName));
if (priority == null) {
priority = new Option<>(prio, ARG_PRIORITY);
}
else {
priority.setValue(prio);
}
}
}
catch (Exception e) { //We need the try-catch here to ensure that the input file will be closed though we'll deal with the exception in the calling method
throw e;
}
finally {
if (input != null) {
try {
input.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Merge the Settings file with the Configuration.
* The Configuration will have high priority.
* @param config the config file
* @param initialize whether to initialize all fields with default values, should only be true on first call
*/
public void merge(Configuration config, boolean initialize) {
if (config == null) {
System.out.println("SettingsLoader::merge config is null");
}
try {
loadFile(initialize);
applyConfigFileValues(config);
}
catch (Exception e) {
e.printStackTrace();
System.err.println("Exception while reading the config file. Falling back to defaults");
initWithDefaults();
applyConfigFileValues(config);
}
}
private void applyConfigFileValues(Configuration config) {
if (config.getLogin().isEmpty() && login != null) {
config.setLogin(login.getValue());
}
if (config.getPassword().isEmpty() && password != null) {
config.setPassword(password.getValue());
}
if ((config.getProxy() == null || config.getProxy().isEmpty()) && proxy != null) {
config.setProxy(proxy.getValue());
}
if ((config.getHostname() == null || config.getHostname().isEmpty() || config.getHostname().equals(config.getDefaultHostname())) && hostname != null) {
config.setHostname(hostname.getValue());
}
if (config.isHeadless() == false && headless != null) {
config.setHeadless(Boolean.parseBoolean(headless.getValue()));
}
if (config.getPriority() == 19) { // 19 is default value
config.setPriority(priority.getValue());
}
if (incompatibleProcess != null) {
config.setIncompatibleProcess(incompatibleProcess.getValue());
}
try {
if (config.getComputeMethod() == null && computeMethod == null) {
config.setComputeMethod(ComputeType.CPU);
}
else if ((config.getComputeMethod() == null && computeMethod != null) || (computeMethod != null && config.getComputeMethod() != ComputeType.valueOf(computeMethod.getValue()))) {
if (config.getComputeMethod() == null) {
config.setComputeMethod(ComputeType.valueOf(computeMethod.getValue()));
}
}
}
catch (IllegalArgumentException e) {
System.err.println("SettingsLoader::merge failed to handle compute method (raw value: '" + computeMethod + "')");
computeMethod = null;
}
if (config.getGPUDevice() == null && gpu != null) {
GPUDevice device = GPU.getGPUDevice(gpu.getValue());
if (device != null) {
config.setGPUDevice(device);
}
else if (config.getUIType() != null && (GuiText.type.equals(config.getUIType()) || GuiTextOneLine.type.equals(config.getUIType()))) {
System.err.println("SettingsLoader::merge could not find specified GPU");
System.exit(2);
}
}
if (config.getNbCores() == -1 && cores != null) {
config.setNbCores(Integer.parseInt(cores.getValue()));
}
if (config.getMaxAllowedMemory() == -1 && ram != null) {
config.setMaxAllowedMemory(Utils.parseNumber(ram.getValue()) / 1000); // internal ram value is in KiB
}
if (config.getMaxRenderTime() == -1 && renderTime != null) {
config.setMaxRenderTime(Integer.parseInt(renderTime.getValue()));
}
if (config.getSharedDownloadsDirectory() == null && sharedZip != null) {
config.setSharedDownloadsDirectory(new File(sharedZip.getValue()));
}
if (config.isUserHasSpecifiedACacheDir() == false && cacheDir != null) {
config.setCacheDir(new File(cacheDir.getValue()));
(new DirectoryManager(config)).createCacheDir();
}
if (config.getUIType() == null && ui != null) {
config.setUIType(ui.getValue());
}
if (config.getTheme() == null) {
if (this.theme != null && ("dark".equals(this.theme.getValue()) || "light".equals(this.theme.getValue()))) {
config.setTheme(this.theme.getValue());
}
else {
config.setTheme("light");
}
}
if (config.isDisableLargeDownloads() == false && disableLargeDownloads != null) {
config.setDisableLargeDownloads(Boolean.parseBoolean(disableLargeDownloads.getValue()));
}
// if the user has invoked the app with --no-systray, then we just overwrite the existing configuration with (boolean)false. If no parameter has been
// specified and the settings file contains use-systray=false, then deactivate as well.
if (!config.isUseSysTray() || (config.isUseSysTray() && useSysTray != null && "false".equals(useSysTray.getValue()))) {
config.setUseSysTray(false);
}
if (config.isAutoSignIn() == false && autoSignIn != null) {
config.setAutoSignIn(Boolean.parseBoolean(autoSignIn.getValue()));
}
if (config.getLogDirectory() != null && logDir != null) {
config.setLogDirectory(logDir.getValue());
}
if (config.isDebugLevel() == false && debug != null) {
config.setDebugLevel(Boolean.parseBoolean(debug.getValue()));
}
}
private void initWithDefaults() {
Configuration defaultConfigValues = new Configuration(null, null, null);
this.login = null;
this.password = null;
this.proxy = null;
this.hostname = null;
this.computeMethod = null;
this.gpu = null;
this.cacheDir = null;
this.sharedZip = null;
this.autoSignIn = null;
this.useSysTray = new Option<>(String.valueOf(defaultConfigValues.isUseSysTray()), ARG_NO_SYSTRAY);
this.headless = new Option<>(String.valueOf(defaultConfigValues.isHeadless()), ARG_HEADLESS);
this.ui = null;
this.priority = new Option<>(defaultConfigValues.getPriority(), ARG_PRIORITY); // must be the same default as Configuration
this.ram = null;
this.renderTime = null;
this.theme = null;
this.cores = new Option<>(String.valueOf(defaultConfigValues.getNbCores()), ARG_CORES);
this.disableLargeDownloads = new Option<>(String.valueOf(defaultConfigValues.isDisableLargeDownloads()), ARG_DISABLE_LARGE_DOWNLOADS);
this.logDir = null;
this.debug = null;
this.incompatibleProcess = null;
}
@Override public String toString() {
return String.format(
"SettingsLoader [path=%s, login=%s, password=%s, computeMethod=%s, gpu=%s, cacheDir=%s, sharedZip=%s, theme=%s, priority=%d, autosign=%s, usetray=%s, headless=%s, disableLargeDownloads=%s, logDir=%s, debug=%s]",
path, login, password, computeMethod, gpu, cacheDir, sharedZip, theme, priority, autoSignIn, useSysTray, headless, disableLargeDownloads, logDir, debug);
}
}