2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: RemoteAlgorithm.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.4 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2006/02/23 21:00:00 $ |
2 |
26 Feb 07 |
jari |
* $Author: caliente $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.remote; |
2 |
26 Feb 07 |
jari |
13 |
|
2 |
26 Feb 07 |
jari |
14 |
import org.tigr.microarray.mev.cluster.algorithm.AbstractAlgorithm; |
2 |
26 Feb 07 |
jari |
15 |
import org.tigr.microarray.mev.cluster.algorithm.AlgorithmData; |
2 |
26 Feb 07 |
jari |
16 |
import org.tigr.microarray.mev.cluster.algorithm.AlgorithmEvent; |
2 |
26 Feb 07 |
jari |
17 |
import org.tigr.microarray.mev.cluster.algorithm.AlgorithmException; |
2 |
26 Feb 07 |
jari |
18 |
import org.tigr.remote.communication.ClientCommunicator; |
2 |
26 Feb 07 |
jari |
19 |
import org.tigr.remote.communication.CommunicatorFactory; |
2 |
26 Feb 07 |
jari |
20 |
import org.tigr.remote.communication.JobControl; |
2 |
26 Feb 07 |
jari |
21 |
import org.tigr.remote.protocol.ExecutedJob; |
2 |
26 Feb 07 |
jari |
22 |
import org.tigr.remote.protocol.FailedJob; |
2 |
26 Feb 07 |
jari |
23 |
import org.tigr.remote.protocol.FinishedJob; |
2 |
26 Feb 07 |
jari |
24 |
import org.tigr.remote.protocol.JobData; |
2 |
26 Feb 07 |
jari |
25 |
import org.tigr.remote.protocol.JobVisitor; |
2 |
26 Feb 07 |
jari |
26 |
import org.tigr.remote.protocol.StartJob; |
2 |
26 Feb 07 |
jari |
27 |
import org.tigr.remote.protocol.SuccessfulJob; |
2 |
26 Feb 07 |
jari |
28 |
|
2 |
26 Feb 07 |
jari |
29 |
public class RemoteAlgorithm extends AbstractAlgorithm { |
2 |
26 Feb 07 |
jari |
30 |
|
2 |
26 Feb 07 |
jari |
31 |
private String name; |
2 |
26 Feb 07 |
jari |
32 |
private JobControl control; |
2 |
26 Feb 07 |
jari |
33 |
|
2 |
26 Feb 07 |
jari |
34 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>RemoteAlgorithm</code> with specified |
2 |
26 Feb 07 |
jari |
* algorithm name. |
2 |
26 Feb 07 |
jari |
37 |
* |
2 |
26 Feb 07 |
jari |
* @param name the name of an algorithm to be executed. |
2 |
26 Feb 07 |
jari |
39 |
*/ |
2 |
26 Feb 07 |
jari |
40 |
public RemoteAlgorithm(String name) { |
2 |
26 Feb 07 |
jari |
41 |
this.name = name; |
2 |
26 Feb 07 |
jari |
42 |
} |
2 |
26 Feb 07 |
jari |
43 |
|
2 |
26 Feb 07 |
jari |
44 |
/** |
2 |
26 Feb 07 |
jari |
* Executes the remote algorithm with a specified <code>AlgorithmData</code>. |
2 |
26 Feb 07 |
jari |
* @see AlgorithmData |
2 |
26 Feb 07 |
jari |
* @throws AlgorithmException |
2 |
26 Feb 07 |
jari |
48 |
*/ |
2 |
26 Feb 07 |
jari |
49 |
public AlgorithmData execute(AlgorithmData data) throws AlgorithmException { |
2 |
26 Feb 07 |
jari |
50 |
try { |
2 |
26 Feb 07 |
jari |
51 |
ClientCommunicator comm = CommunicatorFactory.getCommunicator(); |
2 |
26 Feb 07 |
jari |
52 |
StartJob startJob = new StartJob(comm.getNewJobId(), new JobData(data), name); |
2 |
26 Feb 07 |
jari |
53 |
this.control = comm.postJob(startJob); |
2 |
26 Feb 07 |
jari |
54 |
JobExecution exec = new JobExecution(this); |
2 |
26 Feb 07 |
jari |
55 |
FinishedJob finishedJob; |
2 |
26 Feb 07 |
jari |
56 |
while (true) { |
2 |
26 Feb 07 |
jari |
57 |
finishedJob = this.control.getResult(); |
2 |
26 Feb 07 |
jari |
58 |
finishedJob.accept(exec); |
2 |
26 Feb 07 |
jari |
59 |
if (exec.getResult() != null) // that means, that SuccessfullJob received, we can exit a loop execution |
2 |
26 Feb 07 |
jari |
60 |
return exec.getResult(); |
2 |
26 Feb 07 |
jari |
61 |
} |
2 |
26 Feb 07 |
jari |
62 |
} catch (Exception ex) { |
2 |
26 Feb 07 |
jari |
63 |
throw new AlgorithmException(ex); |
2 |
26 Feb 07 |
jari |
64 |
} |
2 |
26 Feb 07 |
jari |
65 |
} |
2 |
26 Feb 07 |
jari |
66 |
|
2 |
26 Feb 07 |
jari |
67 |
/** |
2 |
26 Feb 07 |
jari |
* Tried to interrupt remote calculation. |
2 |
26 Feb 07 |
jari |
69 |
*/ |
2 |
26 Feb 07 |
jari |
70 |
public void abort() { |
2 |
26 Feb 07 |
jari |
71 |
if (control == null) |
2 |
26 Feb 07 |
jari |
72 |
throw new RuntimeException("Not started yet"); |
2 |
26 Feb 07 |
jari |
73 |
else |
2 |
26 Feb 07 |
jari |
74 |
try { |
2 |
26 Feb 07 |
jari |
75 |
control.terminate(); |
2 |
26 Feb 07 |
jari |
76 |
} catch (RemoteException ex) { |
2 |
26 Feb 07 |
jari |
77 |
throw new RuntimeException("Abort error"); |
2 |
26 Feb 07 |
jari |
78 |
} |
2 |
26 Feb 07 |
jari |
79 |
} |
2 |
26 Feb 07 |
jari |
80 |
|
2 |
26 Feb 07 |
jari |
81 |
/** |
2 |
26 Feb 07 |
jari |
* The class to accept progress or result of the remote execution. |
2 |
26 Feb 07 |
jari |
83 |
*/ |
2 |
26 Feb 07 |
jari |
84 |
public class JobExecution implements JobVisitor { |
2 |
26 Feb 07 |
jari |
85 |
|
2 |
26 Feb 07 |
jari |
86 |
private RemoteAlgorithm ra; |
2 |
26 Feb 07 |
jari |
87 |
private AlgorithmData result; |
2 |
26 Feb 07 |
jari |
88 |
|
2 |
26 Feb 07 |
jari |
89 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>JobExecution</code> instance for |
2 |
26 Feb 07 |
jari |
* the specified remote algorithm. |
2 |
26 Feb 07 |
jari |
92 |
*/ |
2 |
26 Feb 07 |
jari |
93 |
public JobExecution( RemoteAlgorithm ra ) { |
2 |
26 Feb 07 |
jari |
94 |
this.ra = ra; |
2 |
26 Feb 07 |
jari |
95 |
} |
2 |
26 Feb 07 |
jari |
96 |
|
2 |
26 Feb 07 |
jari |
97 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the result of an algorithm execution. |
2 |
26 Feb 07 |
jari |
99 |
*/ |
2 |
26 Feb 07 |
jari |
100 |
public AlgorithmData getResult() { |
2 |
26 Feb 07 |
jari |
101 |
return result; |
2 |
26 Feb 07 |
jari |
102 |
} |
2 |
26 Feb 07 |
jari |
103 |
|
2 |
26 Feb 07 |
jari |
104 |
/** |
2 |
26 Feb 07 |
jari |
* Invoked when job is successfully executed. |
2 |
26 Feb 07 |
jari |
106 |
*/ |
2 |
26 Feb 07 |
jari |
107 |
public void visitSuccessfulJob( SuccessfulJob job ) { |
2 |
26 Feb 07 |
jari |
108 |
this.result = job.getData().getData(); |
2 |
26 Feb 07 |
jari |
109 |
} |
2 |
26 Feb 07 |
jari |
110 |
|
2 |
26 Feb 07 |
jari |
111 |
/** |
2 |
26 Feb 07 |
jari |
* Invoked if job is failed. |
2 |
26 Feb 07 |
jari |
113 |
*/ |
2 |
26 Feb 07 |
jari |
114 |
public void visitFailedJob( FailedJob job ) { |
2 |
26 Feb 07 |
jari |
115 |
throw new RuntimeException( "Server error: " + job.getFail().getDescription() ); |
2 |
26 Feb 07 |
jari |
116 |
} |
2 |
26 Feb 07 |
jari |
117 |
|
2 |
26 Feb 07 |
jari |
118 |
/** |
2 |
26 Feb 07 |
jari |
* Invoked for an executed job to update progress value. |
2 |
26 Feb 07 |
jari |
120 |
*/ |
2 |
26 Feb 07 |
jari |
121 |
public void visitExecutedJob( ExecutedJob job ) { |
2 |
26 Feb 07 |
jari |
122 |
AlgorithmEvent ev = job.getEvent(); |
2 |
26 Feb 07 |
jari |
123 |
ra.fireValueChanged(new AlgorithmEvent(this, ev.getId(), ev.getIntValue(), ev.getFloatValue(), ev.getDescription())); |
2 |
26 Feb 07 |
jari |
124 |
} |
2 |
26 Feb 07 |
jari |
125 |
} |
2 |
26 Feb 07 |
jari |
126 |
} |