00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.branchnbound.core;
00032
00033 import java.io.Serializable;
00034 import java.util.Vector;
00035
00036 import org.apache.log4j.Logger;
00037 import org.objectweb.proactive.ActiveObjectCreationException;
00038 import org.objectweb.proactive.ProActive;
00039 import org.objectweb.proactive.branchnbound.core.exception.NoResultsException;
00040 import org.objectweb.proactive.branchnbound.core.queue.TaskQueue;
00041 import org.objectweb.proactive.core.group.Group;
00042 import org.objectweb.proactive.core.group.ProActiveGroup;
00043 import org.objectweb.proactive.core.node.NodeException;
00044 import org.objectweb.proactive.core.util.log.Loggers;
00045 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00046 import org.objectweb.proactive.core.util.wrapper.BooleanWrapper;
00047
00048
00056 public class Worker implements Serializable {
00057 protected static Logger logger = ProActiveLogger.getLogger(Loggers.P2P_SKELETONS_WORKER);
00058 private Worker selfWorkerGroup = null;
00059 private Result bestCurrentResult = null;
00060 private TaskQueue taskProvider = null;
00061 private String workerNodeUrl = null;
00062 private Task currentTask = null;
00063
00067 public Worker() {
00068
00069 }
00070
00075 public Worker(TaskQueue taskProvider) {
00076 this.taskProvider = taskProvider;
00077 }
00078
00084 public Result execute(Task task) {
00085 if (this.bestCurrentResult == null) {
00086 this.bestCurrentResult = this.taskProvider.getBestCurrentResult();
00087 }
00088 Exception exception = null;
00089 Task activedTask = null;
00090 try {
00091
00092 if (this.workerNodeUrl == null) {
00093 this.workerNodeUrl = ProActive.getBodyOnThis().getNodeURL();
00094 }
00095 activedTask = (Task) ProActive.turnActive(ProActive.getFutureValue(
00096 task), workerNodeUrl);
00097 activedTask.setWorker((Worker) ProActive.getStubOnThis());
00098 this.currentTask = activedTask;
00099 ProActive.setImmediateService(this.currentTask, "setBestKnownResult");
00100 ProActive.setImmediateService(this.currentTask, "immediateTerminate");
00101 } catch (ActiveObjectCreationException e) {
00102 logger.fatal("Couldn't actived the task", e);
00103 exception = e;
00104 } catch (NodeException e) {
00105 logger.fatal("A problem with the task's node", e);
00106 exception = e;
00107 } catch (Exception e) {
00108 logger.fatal("Failed immediate service", e);
00109 exception = e;
00110 }
00111
00112 if (this.bestCurrentResult.getException() != null) {
00113 this.bestCurrentResult = null;
00114 }
00115 if (activedTask != null) {
00116 activedTask.setBestKnownSolution(this.bestCurrentResult.getSolution());
00117 activedTask.initLowerBound();
00118 activedTask.initUpperBound();
00119
00120 return activedTask.execute();
00121 }
00122 logger.fatal("The task was not actived");
00123 if (exception == null) {
00124 return new Result(new NoResultsException("The task was not actived"));
00125 }
00126 return new Result(exception);
00127 }
00128
00133 public void setWorkerGroup(Worker workerGroup) {
00134 Group group = ProActiveGroup.getGroup(workerGroup);
00135 group.remove(ProActive.getStubOnThis());
00136 this.selfWorkerGroup = workerGroup;
00137 }
00138
00144 public void setBestCurrentResult(Result newBest) {
00145 if ((this.bestCurrentResult == null) ||
00146 newBest.isBetterThan(this.bestCurrentResult)) {
00147 this.bestCurrentResult = newBest;
00148 if (this.selfWorkerGroup != null) {
00149 this.selfWorkerGroup.informNewBestResult(this.bestCurrentResult);
00150 this.taskProvider.informNewBestResult(this.bestCurrentResult);
00151 }
00152 if (this.currentTask != null) {
00153 this.currentTask.setBestKnownSolution(this.bestCurrentResult.getSolution());
00154 }
00155 logger.debug("A new best result was localy found: " +
00156 this.bestCurrentResult);
00157 }
00158 logger.debug("The new best result is NOT BETTER");
00159 }
00160
00164 public Result getBestCurrentResult() {
00165 if (this.bestCurrentResult == null) {
00166 return new Result(new NoResultsException());
00167 }
00168 return this.bestCurrentResult;
00169 }
00170
00176 public void informNewBestResult(Result newBest) {
00177 if ((this.bestCurrentResult == null) ||
00178 newBest.isBetterThan(this.bestCurrentResult)) {
00179 this.bestCurrentResult = newBest;
00180 if (this.currentTask != null) {
00181 this.currentTask.setBestKnownSolution(this.bestCurrentResult.getSolution());
00182 }
00183 if (logger.isInfoEnabled()) {
00184 logger.info("I was informed from a new remote best result: " +
00185 this.bestCurrentResult);
00186 }
00187 }
00188 }
00189
00194 public void sendSubTasksToTheManager(Vector subTaskList) {
00195 if (logger.isDebugEnabled()) {
00196 logger.debug("The task sends " + subTaskList.size() + " sub tasks");
00197 }
00198 this.taskProvider.addAll(subTaskList);
00199 }
00200
00204 public BooleanWrapper isHungry() {
00205 return this.taskProvider.isHungry();
00206 }
00207
00211 public void immediateStopComputation() {
00212 this.currentTask.immediateTerminate();
00213 this.currentTask = null;
00214 }
00215
00219 public Task getCurrentTask() {
00220 return this.currentTask;
00221 }
00222
00226 public void alive() {
00227
00228 }
00229
00233 public void reset() {
00234 this.bestCurrentResult = null;
00235 this.currentTask = null;
00236 }
00237 }