BB/Server/BesenBoincServer/src/Core/Program.java

59 lines
1.3 KiB
Java
Raw Normal View History

2017-05-20 11:03:05 +02:00
package Core;
import java.nio.ByteBuffer;
import java.util.Arrays;
2017-05-20 11:03:05 +02:00
import Job.JobManager;
import Job.Result;
import Job.Result.PartialResult;
public abstract class Program{
2017-05-20 11:03:05 +02:00
private JobManager jobmanager = new JobManager(this);
public JobManager getJobManager() {
return jobmanager;
}
/**
* Called by the JobManager or Server, to get the Program generating the next Job.
* The Program is allowed to sumbit more than one Program to the JobManager.
* returns false, when all jobs are submited
*/
public abstract boolean enquenextJob();
2017-05-20 11:03:05 +02:00
public void HandleResult(Result r) {
jobmanager.setdone(r.getJobId());
2017-05-20 11:03:05 +02:00
while(r.hasnext()) {
PartialResult pres = r.next();
switch(pres.type) {
case Console: System.out.println((String) pres.obj); break;
2017-05-20 11:03:05 +02:00
case Value: HandlePartialResult(pres);
}
}
}
protected byte[] inttoBytes(int i ) {
return ByteBuffer.allocate(4).putInt(i).array();
}
protected byte[] longtoBytes(long i ) {
return ByteBuffer.allocate(8).putLong(i).array();
}
/**
* removes the zero terminating byte
* @param s
* @return
*/
protected byte[] stringtoBytes(String s) {
byte[] tmp = s.getBytes();
if(tmp[tmp.length-1] == 0) {
tmp = Arrays.copyOf(tmp, tmp.length-1);
}
return tmp;
}
2017-05-20 11:03:05 +02:00
public abstract void HandlePartialResult(PartialResult pres);
}