package hu.u_szeged.nbo.client.solver;
import hu.u_szeged.nbo.client.model.Model;
import hu.u_szeged.nbo.client.model.ResourceAllocation;
import hu.u_szeged.nbo.client.ui.components.perspective.SettingsPerspective;
import hu.u_szeged.nbo.client.util.log.ClientLogManager;
import hu.u_szeged.nbo.common.Result;
import hu.u_szeged.nbo.server.NBOServerIF;
import java.io.Serializable;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.HashMap;
import javax.rmi.ssl.SslRMIClientSocketFactory;
public class ResourceAllocationSolverThread extends Thread implements Serializable {
private static final long serialVersionUID = -4556948701419130120L;
private ResourceAllocation raProblem;
private String user;
private HashMap<String, String> inputFiles;
private HashMap<String, String> settings;
private boolean stop;
public ResourceAllocationSolverThread(ResourceAllocation raProblem) {
super();
this.settings = SettingsPerspective.getSettings();
this.raProblem = raProblem;
this.user = settings.get("username");
this.setName("ResourceAllocationSolverThread of " + this.raProblem.getName() + " for user " + this.user );
this.stop = false;
String parameters = raProblem.getAlgParams().toString().replaceAll("\\{", "")
.replaceAll("=", ": ")
.replaceAll(", ", "\n")
.replaceAll("\\}", "");
String properties = "algorithm: " + raProblem.getAlgorithm() + "\n" +
parameters + "\n" +
"hashcode: " + raProblem.getUniqueID() + "\n";
this.inputFiles = new HashMap<String, String>();
this.inputFiles = raProblem.getSourceMap();
this.inputFiles.put("properties.txt", properties);
}
public synchronized void run() {
Registry remoteRegistry;
NBOServerIF server;
String address = settings.get("server_address");
int port = Integer.parseInt(settings.get("server_port"));
long solutionSleep = Long.parseLong(this.settings.get("solution_sleep"));
HashMap<String, String> resultMap = null;
HashMap<Integer, String> log = null;
HashMap<Integer, Result> resultObject = null;
String resultString = null;
try {
remoteRegistry = LocateRegistry.getRegistry(address, port, new SslRMIClientSocketFactory());
ClientLogManager.addClientLog(remoteRegistry.toString(), false);
server = (NBOServerIF) remoteRegistry.lookup("NBOServer");
ClientLogManager.addClientLog(server.toString(), false);
if(!server.login(this.user)) {
this.stop = true;
this.raProblem.setState(Model.FAILED);
ClientLogManager.addClientLog("Access denied for user " + this.user + "!", true);
return;
}
if (!this.raProblem.isSentToServer()) {
server.compute(this.user, this.raProblem.getName(), inputFiles, Model.RES_ALLOC);
this.raProblem.setState(Model.SENT_TO_SERVER);
}
while (resultObject == null) {
if (this.stop) {
return;
}
resultObject = server.isReady(this.user, this.raProblem.getName(), this.raProblem.getUniqueID());
sleep(solutionSleep * 1000);
}
resultMap = resultObject.get(this.raProblem.getUniqueID()).getResultMap();
log = server.getLog(this.user, this.raProblem.getName(), this.raProblem.getUniqueID());
resultString = resultMap.get(this.raProblem.getName());
if ((resultString.equals("")) || (resultString == null)) {
throw new Exception("Result from server is empty!");
}
else {
HashMap<String, String> tempMap = new HashMap<String, String>();
tempMap.putAll(resultMap);
raProblem.setSolution(tempMap);
raProblem.setState(Model.SOLVED);
}
String tempString = log.get(raProblem.getUniqueID());
raProblem.setLog(tempString);
}
catch (Exception e) {
this.stop = true;
this.raProblem.setState(Model.FAILED);
ClientLogManager.addClientLog("Exception in " + this.getName() + ":\n\t" + e.toString(), true);
return;
}
}
public void stopSolving() {
this.stop = true;
}
}