2014-11-20 13:21:19 +00:00
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.DataInputStream;
|
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.io.OutputStreamWriter;
|
2015-04-30 20:40:03 +01:00
|
|
|
import java.io.PrintWriter;
|
2014-11-20 13:21:19 +00:00
|
|
|
import java.io.StringWriter;
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
import java.net.ConnectException;
|
2018-12-01 15:29:42 +01:00
|
|
|
import java.net.CookieHandler;
|
|
|
|
|
import java.net.CookieManager;
|
2014-11-20 13:21:19 +00:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
import java.net.NoRouteToHostException;
|
2019-07-12 17:46:57 +02:00
|
|
|
import java.net.UnknownHostException;
|
2014-11-20 13:21:19 +00:00
|
|
|
import java.net.URL;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
import java.security.cert.CertificateException;
|
|
|
|
|
import java.security.cert.X509Certificate;
|
2019-09-03 20:15:46 +02:00
|
|
|
import java.util.ArrayList;
|
2014-11-20 13:21:19 +00:00
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import javax.net.ssl.HostnameVerifier;
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
|
import javax.net.ssl.SSLSession;
|
|
|
|
|
import javax.net.ssl.SSLSocketFactory;
|
|
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
|
|
|
2019-09-03 20:15:46 +02:00
|
|
|
import com.sheepit.client.datamodel.CacheFileMD5;
|
|
|
|
|
import com.sheepit.client.datamodel.FileMD5;
|
|
|
|
|
import com.sheepit.client.datamodel.HeartBeatInfos;
|
|
|
|
|
import com.sheepit.client.datamodel.JobInfos;
|
|
|
|
|
import com.sheepit.client.datamodel.JobValidation;
|
|
|
|
|
import com.sheepit.client.datamodel.ServerConfig;
|
|
|
|
|
import org.simpleframework.xml.core.Persister;
|
2014-11-20 13:21:19 +00:00
|
|
|
|
|
|
|
|
import com.sheepit.client.Configuration.ComputeType;
|
|
|
|
|
import com.sheepit.client.Error.ServerCode;
|
|
|
|
|
import com.sheepit.client.exception.FermeException;
|
2016-09-11 13:46:12 +02:00
|
|
|
import com.sheepit.client.exception.FermeExceptionBadResponseFromServer;
|
2017-06-06 13:38:48 +02:00
|
|
|
import com.sheepit.client.exception.FermeExceptionNoRendererAvailable;
|
2014-11-20 13:21:19 +00:00
|
|
|
import com.sheepit.client.exception.FermeExceptionNoRightToRender;
|
|
|
|
|
import com.sheepit.client.exception.FermeExceptionNoSession;
|
2016-06-25 13:45:09 +02:00
|
|
|
import com.sheepit.client.exception.FermeExceptionNoSpaceLeftOnDevice;
|
2016-03-16 14:07:42 +01:00
|
|
|
import com.sheepit.client.exception.FermeExceptionServerInMaintenance;
|
|
|
|
|
import com.sheepit.client.exception.FermeExceptionServerOverloaded;
|
2014-11-20 13:21:19 +00:00
|
|
|
import com.sheepit.client.exception.FermeExceptionSessionDisabled;
|
2015-07-08 19:44:38 +01:00
|
|
|
import com.sheepit.client.exception.FermeServerDown;
|
2014-11-20 13:21:19 +00:00
|
|
|
import com.sheepit.client.os.OS;
|
|
|
|
|
|
|
|
|
|
public class Server extends Thread implements HostnameVerifier, X509TrustManager {
|
|
|
|
|
private String base_url;
|
2019-09-03 20:15:46 +02:00
|
|
|
private ServerConfig serverConfig;
|
2014-11-20 13:21:19 +00:00
|
|
|
private Configuration user_config;
|
|
|
|
|
private Client client;
|
|
|
|
|
private Log log;
|
|
|
|
|
private long lastRequestTime;
|
2014-12-11 00:06:55 +01:00
|
|
|
private int keepmealive_duration; // time in ms
|
2014-11-20 13:21:19 +00:00
|
|
|
|
|
|
|
|
public Server(String url_, Configuration user_config_, Client client_) {
|
|
|
|
|
super();
|
|
|
|
|
this.base_url = url_;
|
|
|
|
|
this.user_config = user_config_;
|
|
|
|
|
this.client = client_;
|
|
|
|
|
this.log = Log.getInstance(this.user_config);
|
|
|
|
|
this.lastRequestTime = 0;
|
|
|
|
|
this.keepmealive_duration = 15 * 60 * 1000; // default 15min
|
2018-12-01 15:29:42 +01:00
|
|
|
|
|
|
|
|
CookieManager cookies = new CookieManager();
|
|
|
|
|
CookieHandler.setDefault(cookies);
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
this.stayAlive();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void stayAlive() {
|
|
|
|
|
while (true) {
|
|
|
|
|
long current_time = new Date().getTime();
|
|
|
|
|
if ((current_time - this.lastRequestTime) > this.keepmealive_duration) {
|
|
|
|
|
try {
|
|
|
|
|
String args = "";
|
|
|
|
|
if (this.client != null && this.client.getRenderingJob() != null) {
|
|
|
|
|
args = "?frame=" + this.client.getRenderingJob().getFrameNumber() + "&job=" + this.client.getRenderingJob().getId();
|
2015-01-28 20:14:59 +00:00
|
|
|
if (this.client.getRenderingJob().getExtras() != null && this.client.getRenderingJob().getExtras().isEmpty() == false) {
|
2014-11-20 13:21:19 +00:00
|
|
|
args += "&extras=" + this.client.getRenderingJob().getExtras();
|
|
|
|
|
}
|
2015-04-27 20:35:44 +01:00
|
|
|
if (this.client.getRenderingJob().getProcessRender() != null) {
|
|
|
|
|
args += "&rendertime=" + this.client.getRenderingJob().getProcessRender().getDuration();
|
|
|
|
|
args += "&remainingtime=" + this.client.getRenderingJob().getProcessRender().getRemainingDuration();
|
|
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-01 22:12:59 +01:00
|
|
|
HttpURLConnection connection = this.HTTPRequest(this.getPage("keepmealive") + args);
|
2014-11-20 13:21:19 +00:00
|
|
|
|
|
|
|
|
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK && connection.getContentType().startsWith("text/xml")) {
|
|
|
|
|
DataInputStream in = new DataInputStream(connection.getInputStream());
|
|
|
|
|
try {
|
2019-09-03 20:15:46 +02:00
|
|
|
HeartBeatInfos heartBeartInfos = new Persister().read(HeartBeatInfos.class, in);
|
|
|
|
|
ServerCode serverCode = ServerCode.fromInt(heartBeartInfos.getStatus());
|
|
|
|
|
if (serverCode == ServerCode.KEEPMEALIVE_STOP_RENDERING) {
|
2017-03-05 22:39:40 +01:00
|
|
|
this.log.debug("Server::stayAlive server asked to kill local render process");
|
2014-11-20 13:21:19 +00:00
|
|
|
// kill the current process, it will generate an error but it's okay
|
2015-04-27 20:10:36 +01:00
|
|
|
if (this.client != null && this.client.getRenderingJob() != null && this.client.getRenderingJob().getProcessRender().getProcess() != null) {
|
2016-11-01 19:44:09 +01:00
|
|
|
this.client.getRenderingJob().setServerBlockJob(true);
|
2014-12-03 20:09:50 +00:00
|
|
|
this.client.getRenderingJob().setAskForRendererKill(true);
|
2017-05-01 20:48:02 +02:00
|
|
|
OS.getOS().kill(this.client.getRenderingJob().getProcessRender().getProcess());
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-03 20:15:46 +02:00
|
|
|
catch (Exception e) { // for the read
|
|
|
|
|
this.log.debug("Server::stayAlive Exception " + e);
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-08 19:44:38 +01:00
|
|
|
catch (NoRouteToHostException e) {
|
2017-03-05 22:39:40 +01:00
|
|
|
this.log.debug("Server::stayAlive can not connect to server");
|
2015-07-08 19:44:38 +01:00
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
catch (IOException e) {
|
2017-04-19 21:23:12 +02:00
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
this.log.debug("Server::stayAlive IOException " + e + " stacktrace: " + sw.toString());
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(60 * 1000); // 1min
|
|
|
|
|
}
|
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2017-04-19 21:23:12 +02:00
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
this.log.debug("Server::stayAlive Exception " + e + " stacktrace: " + sw.toString());
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String toString() {
|
2019-09-03 20:15:46 +02:00
|
|
|
return String.format("Server (base_url '%s', user_config %s", this.base_url, this.user_config);
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Error.Type getConfiguration() {
|
|
|
|
|
OS os = OS.getOS();
|
|
|
|
|
HttpURLConnection connection = null;
|
2019-08-22 21:35:26 +02:00
|
|
|
String publickey = null;
|
2014-11-20 13:21:19 +00:00
|
|
|
try {
|
2017-11-27 01:15:28 +01:00
|
|
|
String url_remote = this.base_url + "/server/config.php";
|
2018-12-20 17:35:06 +01:00
|
|
|
String parameters = String.format("login=%s&password=%s&cpu_family=%s&cpu_model=%s&cpu_model_name=%s&cpu_cores=%s&os=%s&ram=%s&bits=%s&version=%s&hostname=%s&ui=%s&extras=%s",
|
2019-08-07 22:17:59 +02:00
|
|
|
URLEncoder.encode(this.user_config.getLogin(), "UTF-8"),
|
|
|
|
|
URLEncoder.encode(this.user_config.getPassword(), "UTF-8"),
|
2016-09-25 20:08:47 +02:00
|
|
|
URLEncoder.encode(os.getCPU().family(), "UTF-8"),
|
|
|
|
|
URLEncoder.encode(os.getCPU().model(), "UTF-8"),
|
2014-11-20 13:21:19 +00:00
|
|
|
URLEncoder.encode(os.getCPU().name(), "UTF-8"),
|
|
|
|
|
((this.user_config.getNbCores() == -1) ? os.getCPU().cores() : this.user_config.getNbCores()),
|
2016-09-25 20:08:47 +02:00
|
|
|
URLEncoder.encode(os.name(), "UTF-8"),
|
2014-11-20 13:21:19 +00:00
|
|
|
os.getMemory(),
|
2016-09-25 20:08:47 +02:00
|
|
|
URLEncoder.encode(os.getCPU().arch(), "UTF-8"),
|
2014-11-20 13:21:19 +00:00
|
|
|
this.user_config.getJarVersion(),
|
2017-05-08 01:35:08 +02:00
|
|
|
URLEncoder.encode(this.user_config.getHostname(), "UTF-8"),
|
2018-12-20 17:35:06 +01:00
|
|
|
this.client.getGui().getClass().getSimpleName(),
|
2014-11-20 13:21:19 +00:00
|
|
|
this.user_config.getExtras());
|
2017-11-27 01:15:28 +01:00
|
|
|
this.log.debug("Server::getConfiguration url " + url_remote);
|
2014-11-20 13:21:19 +00:00
|
|
|
|
2017-11-27 01:15:28 +01:00
|
|
|
connection = this.HTTPRequest(url_remote, parameters);
|
2014-11-20 13:21:19 +00:00
|
|
|
int r = connection.getResponseCode();
|
|
|
|
|
String contentType = connection.getContentType();
|
|
|
|
|
|
|
|
|
|
if (r == HttpURLConnection.HTTP_OK && contentType.startsWith("text/xml")) {
|
|
|
|
|
DataInputStream in = new DataInputStream(connection.getInputStream());
|
2019-09-03 20:15:46 +02:00
|
|
|
serverConfig = new Persister().read(ServerConfig.class, in);
|
2014-11-20 13:21:19 +00:00
|
|
|
|
2019-09-03 20:15:46 +02:00
|
|
|
if (ServerCode.fromInt(serverConfig.getStatus()) != ServerCode.OK) {
|
|
|
|
|
return Error.ServerCodeToType(ServerCode.fromInt(serverConfig.getStatus()));
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-03 20:15:46 +02:00
|
|
|
publickey = serverConfig.getPublickey();
|
|
|
|
|
if (publickey.isEmpty()) {
|
|
|
|
|
publickey = null;
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
2019-09-03 20:15:46 +02:00
|
|
|
else {
|
|
|
|
|
this.user_config.setPassword(publickey);
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (ConnectException e) {
|
|
|
|
|
this.log.error("Server::getConfiguration error ConnectException " + e);
|
|
|
|
|
return Error.Type.NETWORK_ISSUE;
|
|
|
|
|
}
|
2019-07-12 17:46:57 +02:00
|
|
|
catch (UnknownHostException e) {
|
|
|
|
|
this.log.error("Server::getConfiguration: exception UnknownHostException " + e);
|
|
|
|
|
return Error.Type.NETWORK_ISSUE;
|
|
|
|
|
}
|
2016-02-18 18:30:05 +01:00
|
|
|
catch (UnsupportedEncodingException e) {
|
|
|
|
|
this.log.error("Server::getConfiguration: exception UnsupportedEncodingException " + e);
|
|
|
|
|
return Error.Type.UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
this.log.error("Server::getConfiguration: exception IOException " + e);
|
2014-11-20 13:21:19 +00:00
|
|
|
return Error.Type.UNKNOWN;
|
|
|
|
|
}
|
2019-09-03 20:15:46 +02:00
|
|
|
catch (Exception e) {
|
|
|
|
|
this.log.error("Server::getConfiguration: exception Exception " + e);
|
|
|
|
|
return Error.Type.UNKNOWN;
|
|
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
finally {
|
|
|
|
|
if (connection != null) {
|
|
|
|
|
connection.disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-22 21:35:26 +02:00
|
|
|
|
|
|
|
|
this.client.getGui().successfulAuthenticationEvent(publickey);
|
|
|
|
|
|
2014-11-20 13:21:19 +00:00
|
|
|
return Error.Type.OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-04 18:59:16 +00:00
|
|
|
public Job requestJob() throws FermeException {
|
2014-11-20 13:21:19 +00:00
|
|
|
this.log.debug("Server::requestJob");
|
|
|
|
|
String url_contents = "";
|
|
|
|
|
|
|
|
|
|
HttpURLConnection connection = null;
|
|
|
|
|
try {
|
2015-06-09 20:16:12 +01:00
|
|
|
OS os = OS.getOS();
|
2019-02-20 13:46:53 +01:00
|
|
|
long maxMemory = this.user_config.getMaxMemory();
|
|
|
|
|
long freeMemory = os.getFreeMemory();
|
2018-08-10 17:33:50 +02:00
|
|
|
if (maxMemory < 0) {
|
|
|
|
|
maxMemory = freeMemory;
|
|
|
|
|
}
|
|
|
|
|
else if (freeMemory > 0 && maxMemory > 0) {
|
|
|
|
|
maxMemory = Math.min(maxMemory, freeMemory);
|
|
|
|
|
}
|
|
|
|
|
String url = String.format("%s?computemethod=%s&cpu_cores=%s&ram_max=%s&rendertime_max=%s", this.getPage("request-job"), this.user_config.computeMethodToInt(), ((this.user_config.getNbCores() == -1) ? os.getCPU().cores() : this.user_config.getNbCores()), maxMemory, this.user_config.getMaxRenderTime());
|
2015-04-07 20:05:48 +01:00
|
|
|
if (this.user_config.getComputeMethod() != ComputeType.CPU && this.user_config.getGPUDevice() != null) {
|
2014-11-20 13:21:19 +00:00
|
|
|
String gpu_model = "";
|
|
|
|
|
try {
|
|
|
|
|
gpu_model = URLEncoder.encode(this.user_config.getGPUDevice().getModel(), "UTF-8");
|
|
|
|
|
}
|
|
|
|
|
catch (UnsupportedEncodingException e) {
|
|
|
|
|
}
|
2018-08-24 19:46:03 +02:00
|
|
|
url += "&gpu_model=" + gpu_model + "&gpu_ram=" + this.user_config.getGPUDevice().getMemory() + "&gpu_type=" + this.user_config.getGPUDevice().getType();
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connection = this.HTTPRequest(url, this.generateXMLForMD5cache());
|
|
|
|
|
|
|
|
|
|
int r = connection.getResponseCode();
|
|
|
|
|
String contentType = connection.getContentType();
|
|
|
|
|
|
|
|
|
|
if (r == HttpURLConnection.HTTP_OK && contentType.startsWith("text/xml")) {
|
|
|
|
|
DataInputStream in = new DataInputStream(connection.getInputStream());
|
2019-09-03 20:15:46 +02:00
|
|
|
|
|
|
|
|
JobInfos jobData = new Persister().read(JobInfos.class, in);
|
|
|
|
|
|
|
|
|
|
handleFileMD5DeleteDocument(jobData.getFileMD5s());
|
2019-09-09 10:25:12 +02:00
|
|
|
|
|
|
|
|
if (jobData.getSessionStats() != null) {
|
|
|
|
|
this.client.getGui().displayStats(new Stats(
|
|
|
|
|
jobData.getSessionStats().getRemainingFrames(),
|
|
|
|
|
jobData.getSessionStats().getPointsEarnedByUser(),
|
|
|
|
|
jobData.getSessionStats().getPointsEarnedOnSession(),
|
|
|
|
|
jobData.getSessionStats().getRenderableProjects(),
|
|
|
|
|
jobData.getSessionStats().getWaitingProjects(),
|
|
|
|
|
jobData.getSessionStats().getConnectedMachines()));
|
|
|
|
|
}
|
2019-09-03 20:15:46 +02:00
|
|
|
|
|
|
|
|
ServerCode serverCode = ServerCode.fromInt(jobData.getStatus());
|
|
|
|
|
if (serverCode != ServerCode.OK) {
|
|
|
|
|
switch (serverCode) {
|
|
|
|
|
case JOB_REQUEST_NOJOB:
|
|
|
|
|
return null;
|
|
|
|
|
case JOB_REQUEST_ERROR_NO_RENDERING_RIGHT:
|
|
|
|
|
throw new FermeExceptionNoRightToRender();
|
|
|
|
|
case JOB_REQUEST_ERROR_DEAD_SESSION:
|
|
|
|
|
throw new FermeExceptionNoSession();
|
|
|
|
|
case JOB_REQUEST_ERROR_SESSION_DISABLED:
|
|
|
|
|
throw new FermeExceptionSessionDisabled();
|
|
|
|
|
case JOB_REQUEST_ERROR_INTERNAL_ERROR:
|
|
|
|
|
throw new FermeExceptionBadResponseFromServer();
|
|
|
|
|
case JOB_REQUEST_ERROR_RENDERER_NOT_AVAILABLE:
|
|
|
|
|
throw new FermeExceptionNoRendererAvailable();
|
|
|
|
|
case JOB_REQUEST_SERVER_IN_MAINTENANCE:
|
|
|
|
|
throw new FermeExceptionServerInMaintenance();
|
|
|
|
|
case JOB_REQUEST_SERVER_OVERLOADED:
|
|
|
|
|
throw new FermeExceptionServerOverloaded();
|
|
|
|
|
default:
|
|
|
|
|
throw new FermeException("error requestJob: status is not ok (it's " + serverCode + ")");
|
2016-03-16 14:07:42 +01:00
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
2019-09-03 20:15:46 +02:00
|
|
|
|
2019-01-18 19:29:56 +01:00
|
|
|
String script = "import bpy\n";
|
|
|
|
|
// blender 2.7x
|
|
|
|
|
script += "try:\n";
|
2019-08-07 22:17:59 +02:00
|
|
|
script += "\tbpy.context.user_preferences.filepaths.temporary_directory = \"" + this.user_config.getWorkingDirectory().getAbsolutePath().replace("\\", "\\\\") + "\"\n";
|
2019-01-18 19:29:56 +01:00
|
|
|
script += "except AttributeError:\n";
|
|
|
|
|
script += "\tpass\n";
|
|
|
|
|
|
|
|
|
|
// blender 2.80
|
|
|
|
|
script += "try:\n";
|
2019-08-07 22:17:59 +02:00
|
|
|
script += "\tbpy.context.preferences.filepaths.temporary_directory = \"" + this.user_config.getWorkingDirectory().getAbsolutePath().replace("\\", "\\\\") + "\"\n";
|
2019-01-18 19:29:56 +01:00
|
|
|
script += "except AttributeError:\n";
|
|
|
|
|
script += "\tpass\n";
|
|
|
|
|
|
2019-09-03 20:15:46 +02:00
|
|
|
script += jobData.getRenderTask().getScript();
|
|
|
|
|
|
2014-11-20 13:21:19 +00:00
|
|
|
Job a_job = new Job(
|
|
|
|
|
this.user_config,
|
2015-05-06 20:34:59 +01:00
|
|
|
this.client.getGui(),
|
|
|
|
|
this.client.getLog(),
|
2019-09-03 20:15:46 +02:00
|
|
|
jobData.getRenderTask().getId(),
|
|
|
|
|
jobData.getRenderTask().getFrame(),
|
|
|
|
|
jobData.getRenderTask().getPath().replace("/", File.separator),
|
|
|
|
|
jobData.getRenderTask().getUseGpu() == 1,
|
|
|
|
|
jobData.getRenderTask().getRendererInfos().getCommandline(),
|
2014-11-20 13:21:19 +00:00
|
|
|
script,
|
2019-09-03 20:15:46 +02:00
|
|
|
jobData.getRenderTask().getArchive_md5(),
|
|
|
|
|
jobData.getRenderTask().getRendererInfos().getMd5(),
|
|
|
|
|
jobData.getRenderTask().getName(),
|
|
|
|
|
jobData.getRenderTask().getPassword(),
|
|
|
|
|
jobData.getRenderTask().getExtras(),
|
|
|
|
|
jobData.getRenderTask().getSynchronous_upload().equals("1"),
|
|
|
|
|
jobData.getRenderTask().getRendererInfos().getUpdate_method()
|
|
|
|
|
);
|
2014-11-20 13:21:19 +00:00
|
|
|
|
|
|
|
|
return a_job;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
System.out.println("Server::requestJob url " + url_contents + " r " + r + " contentType " + contentType);
|
2017-11-15 14:45:46 +01:00
|
|
|
if (r == HttpURLConnection.HTTP_UNAVAILABLE || r == HttpURLConnection. HTTP_CLIENT_TIMEOUT) {
|
2015-07-08 21:34:10 +01:00
|
|
|
// most likely varnish is up but apache down
|
|
|
|
|
throw new FermeServerDown();
|
|
|
|
|
}
|
2016-09-11 13:46:12 +02:00
|
|
|
else if (r == HttpURLConnection.HTTP_OK && contentType.startsWith("text/html")) {
|
|
|
|
|
throw new FermeExceptionBadResponseFromServer();
|
|
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
InputStream in = connection.getInputStream();
|
|
|
|
|
String line;
|
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
System.out.print(line);
|
|
|
|
|
}
|
|
|
|
|
System.out.println("");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FermeException e) {
|
2015-03-04 18:59:16 +00:00
|
|
|
throw e;
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
2015-07-08 19:44:38 +01:00
|
|
|
catch (NoRouteToHostException e) {
|
|
|
|
|
throw new FermeServerDown();
|
|
|
|
|
}
|
2019-07-12 17:46:57 +02:00
|
|
|
catch (UnknownHostException e) {
|
|
|
|
|
throw new FermeServerDown();
|
|
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
catch (Exception e) {
|
2015-06-10 19:24:42 +01:00
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
2015-07-20 17:45:38 +01:00
|
|
|
e.printStackTrace(pw);
|
2016-10-19 14:02:35 +02:00
|
|
|
throw new FermeException("error requestJob: unknown exception " + e + " stacktrace: " + sw.toString());
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
if (connection != null) {
|
|
|
|
|
connection.disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw new FermeException("error requestJob, end of function");
|
|
|
|
|
}
|
2019-09-03 20:15:46 +02:00
|
|
|
|
2014-11-20 13:21:19 +00:00
|
|
|
public HttpURLConnection HTTPRequest(String url_) throws IOException {
|
|
|
|
|
return this.HTTPRequest(url_, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HttpURLConnection HTTPRequest(String url_, String data_) throws IOException {
|
|
|
|
|
this.log.debug("Server::HTTPRequest url(" + url_ + ")");
|
|
|
|
|
HttpURLConnection connection = null;
|
|
|
|
|
URL url = new URL(url_);
|
|
|
|
|
|
|
|
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
connection.setDoInput(true);
|
|
|
|
|
connection.setDoOutput(true);
|
2017-05-10 00:06:43 +02:00
|
|
|
connection.setInstanceFollowRedirects(true);
|
2014-11-20 13:21:19 +00:00
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
|
|
|
|
|
|
if (url_.startsWith("https://")) {
|
|
|
|
|
try {
|
|
|
|
|
SSLContext sc;
|
|
|
|
|
sc = SSLContext.getInstance("SSL");
|
|
|
|
|
sc.init(null, new TrustManager[] { this }, null);
|
|
|
|
|
SSLSocketFactory factory = sc.getSocketFactory();
|
|
|
|
|
((HttpsURLConnection) connection).setSSLSocketFactory(factory);
|
|
|
|
|
((HttpsURLConnection) connection).setHostnameVerifier(this);
|
|
|
|
|
}
|
|
|
|
|
catch (NoSuchAlgorithmException e) {
|
2017-04-19 21:23:12 +02:00
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
this.log.debug("Server::HTTPRequest NoSuchAlgorithmException " + e + " stacktrace: " + sw.toString());
|
2014-11-20 13:21:19 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch (KeyManagementException e) {
|
2017-04-19 21:23:12 +02:00
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
this.log.debug("Server::HTTPRequest KeyManagementException " + e + " stacktrace: " + sw.toString());
|
2014-11-20 13:21:19 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data_ != null) {
|
|
|
|
|
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
|
|
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
|
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
|
|
|
|
|
out.write(data_);
|
|
|
|
|
out.flush();
|
|
|
|
|
out.close();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-01 00:30:48 +01:00
|
|
|
// actually use the connection to, in case of timeout, generate an exception
|
|
|
|
|
connection.getResponseCode();
|
|
|
|
|
|
2014-11-20 13:21:19 +00:00
|
|
|
this.lastRequestTime = new Date().getTime();
|
|
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-25 13:45:09 +02:00
|
|
|
public int HTTPGetFile(String url_, String destination_, Gui gui_, String status_) throws FermeExceptionNoSpaceLeftOnDevice {
|
2014-11-20 13:21:19 +00:00
|
|
|
// the destination_ parent directory must exist
|
|
|
|
|
try {
|
|
|
|
|
HttpURLConnection httpCon = this.HTTPRequest(url_);
|
|
|
|
|
|
|
|
|
|
InputStream inStrm = httpCon.getInputStream();
|
|
|
|
|
if (httpCon.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
2015-02-04 22:25:51 +00:00
|
|
|
this.log.error("Server::HTTPGetFile(" + url_ + ", ...) HTTP code is not " + HttpURLConnection.HTTP_OK + " it's " + httpCon.getResponseCode());
|
2014-11-20 13:21:19 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int size = httpCon.getContentLength();
|
|
|
|
|
long start = new Date().getTime();
|
|
|
|
|
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(destination_);
|
|
|
|
|
byte[] ch = new byte[512 * 1024];
|
|
|
|
|
int nb;
|
2015-01-18 16:50:28 +01:00
|
|
|
long written = 0;
|
2014-11-20 13:21:19 +00:00
|
|
|
long last_gui_update = 0; // size in byte
|
|
|
|
|
while ((nb = inStrm.read(ch)) != -1) {
|
|
|
|
|
fos.write(ch, 0, nb);
|
2015-01-18 16:50:28 +01:00
|
|
|
written += nb;
|
|
|
|
|
if ((written - last_gui_update) > 1000000) { // only update the gui every 1MB
|
|
|
|
|
gui_.status(String.format(status_, (int) (100.0 * written / size)));
|
|
|
|
|
last_gui_update = written;
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fos.close();
|
|
|
|
|
inStrm.close();
|
2019-09-05 13:16:28 +02:00
|
|
|
gui_.status(String.format(status_, 100));
|
2014-11-20 13:21:19 +00:00
|
|
|
long end = new Date().getTime();
|
2017-02-22 21:33:23 +01:00
|
|
|
this.log.debug(String.format("File downloaded at %.1f kB/s, written %d B", ((float) (size / 1000)) / ((float) (end - start) / 1000), written));
|
2014-11-20 13:21:19 +00:00
|
|
|
this.lastRequestTime = new Date().getTime();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2017-03-17 00:39:06 +01:00
|
|
|
if (Utils.noFreeSpaceOnDisk(new File(destination_).getParent())) {
|
2016-06-25 13:45:09 +02:00
|
|
|
throw new FermeExceptionNoSpaceLeftOnDevice();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-30 20:40:03 +01:00
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
e.printStackTrace(new PrintWriter(sw));
|
2017-04-19 21:23:12 +02:00
|
|
|
this.log.error("Server::HTTPGetFile Exception " + e + " stacktrace " + sw.toString());
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
2015-04-29 21:21:17 +01:00
|
|
|
this.log.debug("Server::HTTPGetFile(" + url_ + ", ...) will failed (end of function)");
|
2014-11-20 13:21:19 +00:00
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ServerCode HTTPSendFile(String surl, String file1) {
|
|
|
|
|
this.log.debug("Server::HTTPSendFile(" + surl + "," + file1 + ")");
|
|
|
|
|
|
|
|
|
|
HttpURLConnection conn = null;
|
|
|
|
|
DataOutputStream dos = null;
|
|
|
|
|
BufferedReader inStream = null;
|
|
|
|
|
|
|
|
|
|
String exsistingFileName = file1;
|
|
|
|
|
File fFile2Snd = new File(exsistingFileName);
|
|
|
|
|
|
|
|
|
|
String lineEnd = "\r\n";
|
|
|
|
|
String twoHyphens = "--";
|
|
|
|
|
String boundary = "***232404jkg4220957934FW**";
|
|
|
|
|
|
|
|
|
|
int bytesRead, bytesAvailable, bufferSize;
|
|
|
|
|
byte[] buffer;
|
|
|
|
|
int maxBufferSize = 1 * 1024 * 1024;
|
|
|
|
|
|
|
|
|
|
String urlString = surl;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName));
|
|
|
|
|
URL url = new URL(urlString);
|
|
|
|
|
|
|
|
|
|
conn = (HttpURLConnection) url.openConnection();
|
|
|
|
|
conn.setDoInput(true);
|
|
|
|
|
conn.setDoOutput(true);
|
2017-05-10 00:06:43 +02:00
|
|
|
conn.setInstanceFollowRedirects(true);
|
2014-11-20 13:21:19 +00:00
|
|
|
conn.setUseCaches(false);
|
|
|
|
|
|
|
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
|
conn.setRequestProperty("Connection", "Keep-Alive");
|
|
|
|
|
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
|
|
|
|
|
|
|
|
|
|
if (urlString.startsWith("https://")) {
|
|
|
|
|
try {
|
|
|
|
|
SSLContext sc;
|
|
|
|
|
sc = SSLContext.getInstance("SSL");
|
|
|
|
|
sc.init(null, new TrustManager[] { this }, null);
|
|
|
|
|
SSLSocketFactory factory = sc.getSocketFactory();
|
|
|
|
|
((HttpsURLConnection) conn).setSSLSocketFactory(factory);
|
|
|
|
|
((HttpsURLConnection) conn).setHostnameVerifier(this);
|
|
|
|
|
}
|
|
|
|
|
catch (NoSuchAlgorithmException e) {
|
|
|
|
|
this.log.error("Server::HTTPSendFile, exception NoSuchAlgorithmException " + e);
|
|
|
|
|
try {
|
|
|
|
|
fileInputStream.close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e1) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return ServerCode.UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
catch (KeyManagementException e) {
|
|
|
|
|
this.log.error("Server::HTTPSendFile, exception KeyManagementException " + e);
|
|
|
|
|
try {
|
|
|
|
|
fileInputStream.close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e1) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return ServerCode.UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dos = new DataOutputStream(conn.getOutputStream());
|
|
|
|
|
dos.writeBytes(twoHyphens + boundary + lineEnd);
|
|
|
|
|
dos.writeBytes("Content-Disposition: form-data; name=\"file\";" + " filename=\"" + fFile2Snd.getName() + "\"" + lineEnd);
|
|
|
|
|
dos.writeBytes(lineEnd);
|
|
|
|
|
|
|
|
|
|
bytesAvailable = fileInputStream.available();
|
|
|
|
|
bufferSize = Math.min(bytesAvailable, maxBufferSize);
|
|
|
|
|
buffer = new byte[bufferSize];
|
|
|
|
|
|
|
|
|
|
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
|
|
|
|
|
|
|
|
|
|
while (bytesRead > 0) {
|
|
|
|
|
dos.write(buffer, 0, bufferSize);
|
|
|
|
|
bytesAvailable = fileInputStream.available();
|
|
|
|
|
bufferSize = Math.min(bytesAvailable, maxBufferSize);
|
|
|
|
|
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dos.writeBytes(lineEnd);
|
|
|
|
|
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
|
|
|
|
fileInputStream.close();
|
|
|
|
|
dos.flush();
|
|
|
|
|
dos.close();
|
|
|
|
|
}
|
2017-04-19 21:23:12 +02:00
|
|
|
catch (MalformedURLException e) {
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
this.log.error("Server::HTTPSendFile, MalformedURLException " + e + " stacktrace " + sw.toString());
|
2014-11-20 13:21:19 +00:00
|
|
|
return ServerCode.UNKNOWN;
|
|
|
|
|
}
|
2017-04-19 21:23:12 +02:00
|
|
|
catch (IOException e) {
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
this.log.error("Server::HTTPSendFile, IOException " + e + " stacktrace " + sw.toString());
|
2014-11-20 13:21:19 +00:00
|
|
|
return ServerCode.UNKNOWN;
|
|
|
|
|
}
|
2017-04-19 21:23:12 +02:00
|
|
|
catch (OutOfMemoryError e) {
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
this.log.error("Server::HTTPSendFile, OutOfMemoryError " + e + " stacktrace " + sw.toString());
|
2017-03-18 17:05:54 +01:00
|
|
|
return ServerCode.JOB_VALIDATION_ERROR_UPLOAD_FAILED;
|
|
|
|
|
}
|
2017-04-19 21:23:12 +02:00
|
|
|
catch (Exception e) {
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
this.log.error("Server::HTTPSendFile, Exception " + e + " stacktrace " + sw.toString());
|
|
|
|
|
return ServerCode.UNKNOWN;
|
|
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
|
|
|
|
|
int r;
|
|
|
|
|
try {
|
|
|
|
|
r = conn.getResponseCode();
|
|
|
|
|
}
|
2017-04-19 21:23:12 +02:00
|
|
|
catch (IOException e) {
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
this.log.debug("Server::HTTPSendFile IOException " + e + " stacktrace: " + sw.toString());
|
2014-11-20 13:21:19 +00:00
|
|
|
return ServerCode.UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
String contentType = conn.getContentType();
|
|
|
|
|
|
|
|
|
|
if (r == HttpURLConnection.HTTP_OK && contentType.startsWith("text/xml")) {
|
|
|
|
|
DataInputStream in;
|
|
|
|
|
try {
|
|
|
|
|
in = new DataInputStream(conn.getInputStream());
|
|
|
|
|
}
|
2017-04-19 21:23:12 +02:00
|
|
|
catch (IOException e) {
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
this.log.debug("Server::HTTPSendFile IOException " + e + " stacktrace: " + sw.toString());
|
2014-11-20 13:21:19 +00:00
|
|
|
return ServerCode.UNKNOWN;
|
|
|
|
|
}
|
2019-09-03 20:15:46 +02:00
|
|
|
|
2014-11-20 13:21:19 +00:00
|
|
|
try {
|
2019-09-03 20:15:46 +02:00
|
|
|
JobValidation jobValidation = new Persister().read(JobValidation.class, in);
|
|
|
|
|
|
|
|
|
|
this.lastRequestTime = new Date().getTime();
|
|
|
|
|
|
|
|
|
|
ServerCode serverCode = ServerCode.fromInt(jobValidation.getStatus());
|
|
|
|
|
if (serverCode != ServerCode.OK) {
|
|
|
|
|
this.log.error("Server::HTTPSendFile wrong status (is " + serverCode + ")");
|
|
|
|
|
return serverCode;
|
|
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
2019-09-03 20:15:46 +02:00
|
|
|
catch (Exception e) { // for the .read
|
|
|
|
|
e.printStackTrace();
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ServerCode.OK;
|
|
|
|
|
}
|
2016-09-11 13:46:12 +02:00
|
|
|
else if (r == HttpURLConnection.HTTP_OK && contentType.startsWith("text/html")) {
|
|
|
|
|
return ServerCode.ERROR_BAD_RESPONSE;
|
|
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
else {
|
|
|
|
|
try {
|
|
|
|
|
inStream = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
|
|
|
|
|
|
|
String str;
|
|
|
|
|
while ((str = inStream.readLine()) != null) {
|
|
|
|
|
System.out.println(str);
|
|
|
|
|
System.out.println("");
|
|
|
|
|
}
|
|
|
|
|
inStream.close();
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ioex) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ServerCode.UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-15 23:20:17 +01:00
|
|
|
public byte[] getLastRender() {
|
|
|
|
|
try {
|
|
|
|
|
HttpURLConnection httpCon = this.HTTPRequest(this.getPage("last-render-frame"));
|
|
|
|
|
|
|
|
|
|
InputStream inStrm = httpCon.getInputStream();
|
|
|
|
|
if (httpCon.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
|
|
|
|
this.log.debug("Server::getLastRender code not ok " + httpCon.getResponseCode());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
int size = httpCon.getContentLength();
|
|
|
|
|
|
2017-02-12 20:23:00 +01:00
|
|
|
if (size <= 0) {
|
|
|
|
|
this.log.debug("Server::getLastRender size is negative (size: " + size + ")");
|
2015-01-15 23:20:17 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] ret = new byte[size];
|
|
|
|
|
byte[] ch = new byte[512 * 1024];
|
|
|
|
|
int n = 0;
|
|
|
|
|
int i = 0;
|
|
|
|
|
while ((n = inStrm.read(ch)) != -1) {
|
|
|
|
|
System.arraycopy(ch, 0, ret, i, n);
|
|
|
|
|
i += n;
|
|
|
|
|
}
|
|
|
|
|
inStrm.close();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2017-04-19 21:23:12 +02:00
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
this.log.debug("Server::getLastRender Exception " + e + " stacktrace: " + sw.toString());
|
2015-01-15 23:20:17 +01:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-20 13:21:19 +00:00
|
|
|
private String generateXMLForMD5cache() {
|
2019-09-03 20:15:46 +02:00
|
|
|
List<FileMD5> md5s = new ArrayList<>();
|
|
|
|
|
for (File local_file : this.user_config.getLocalCacheFiles()) {
|
|
|
|
|
try {
|
|
|
|
|
String extension = local_file.getName().substring(local_file.getName().lastIndexOf('.')).toLowerCase();
|
|
|
|
|
String name = local_file.getName().substring(0, local_file.getName().length() - 1 * extension.length());
|
|
|
|
|
if (extension.equals(".zip")) {
|
|
|
|
|
// node_file.setAttribute("md5", name);
|
|
|
|
|
FileMD5 fileMD5 = new FileMD5();
|
|
|
|
|
fileMD5.setMd5(name);
|
|
|
|
|
|
|
|
|
|
md5s.add(fileMD5);
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-09-03 20:15:46 +02:00
|
|
|
catch (StringIndexOutOfBoundsException e) { // because the file does not have an . its path
|
|
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
2019-09-03 20:15:46 +02:00
|
|
|
|
|
|
|
|
CacheFileMD5 cache = new CacheFileMD5();
|
|
|
|
|
cache.setMd5s(md5s);
|
|
|
|
|
|
|
|
|
|
final Persister persister = new Persister();
|
|
|
|
|
try (StringWriter writer = new StringWriter()) {
|
|
|
|
|
persister.write(cache, writer);
|
|
|
|
|
return writer.toString();
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
2019-09-03 20:15:46 +02:00
|
|
|
catch (final Exception e) {
|
|
|
|
|
log.debug("Failed to dump md5s " + e);
|
|
|
|
|
return "";
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-09-03 20:15:46 +02:00
|
|
|
|
|
|
|
|
private void handleFileMD5DeleteDocument(List<FileMD5> fileMD5s) {
|
|
|
|
|
if (fileMD5s != null && fileMD5s.isEmpty() == false) {
|
|
|
|
|
for(FileMD5 fileMD5 : fileMD5s) {
|
|
|
|
|
if ("delete".equals(fileMD5.getAction()) && fileMD5.getMd5() != null && fileMD5.getMd5().isEmpty() == false) {
|
|
|
|
|
String path = this.user_config.getWorkingDirectory().getAbsolutePath() + File.separatorChar + fileMD5.getMd5();
|
|
|
|
|
this.log.debug("Server::handleFileMD5DeleteDocument delete old file " + path);
|
|
|
|
|
File file_to_delete = new File(path + ".zip");
|
|
|
|
|
file_to_delete.delete();
|
|
|
|
|
Utils.delete(new File(path));
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getPage(String key) {
|
2019-09-03 20:15:46 +02:00
|
|
|
if (this.serverConfig != null) {
|
|
|
|
|
return this.base_url + this.serverConfig.getRequestEndPoint(key).getPath();
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean verify(String arg0, SSLSession arg1) {
|
|
|
|
|
return true; // trust every ssl certificate
|
|
|
|
|
}
|
|
|
|
|
}
|