commit
c8e8daabd2
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/BesenBoincServer"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
@ -0,0 +1 @@
|
||||
/bin/
|
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>BesenBoincClient</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -0,0 +1,70 @@
|
||||
package Core;
|
||||
|
||||
import Comunication.Client;
|
||||
import Job.Worker;
|
||||
|
||||
public class Starter {
|
||||
|
||||
public static void main(String args[]) {
|
||||
System.out.println("Starting BesenBoincClient...");
|
||||
|
||||
String host = BB.host;
|
||||
int port = BB.port;
|
||||
if(args.length >= 1) {
|
||||
String[] split = args[0].split(":");
|
||||
if(split.length >= 1) {
|
||||
host = split[0];
|
||||
if(split.length>=2) {
|
||||
port = Integer.parseInt(split[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
boolean run = true;
|
||||
int lost_counter = 0;//ho often loast?
|
||||
long lasttest = -1;
|
||||
while(run) {
|
||||
boolean tryagain = false;
|
||||
while(!tryagain) {
|
||||
if(lost_counter < 4)
|
||||
tryagain = true;
|
||||
if(lost_counter >= 4 & lost_counter < 10 & ((System.currentTimeMillis()-lasttest)/1000) > 10) {
|
||||
tryagain = true;
|
||||
}
|
||||
if(lost_counter >= 10 & lost_counter < 25 & ((System.currentTimeMillis()-lasttest)/1000) > 30) {
|
||||
tryagain = true;
|
||||
}
|
||||
if(lost_counter >= 25 & ((System.currentTimeMillis()-lasttest)/1000) > 120) {
|
||||
tryagain = true;
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.yield();
|
||||
Thread.sleep(500);
|
||||
} catch(Exception e) {}
|
||||
}
|
||||
|
||||
System.out.println("Connecting to " + host + " on port: " + port);
|
||||
lasttest = System.currentTimeMillis();
|
||||
try {
|
||||
Client c = new Client(host, port, new Worker());
|
||||
Thread client = new Thread(c, "Connection");
|
||||
client.start();
|
||||
|
||||
while(client.isAlive())
|
||||
;
|
||||
|
||||
if(lost_counter > 0) {
|
||||
int minus = (int) ((System.currentTimeMillis()-lasttest)/10000);//für alle 10 sekunden verbinung ein lost count weniger
|
||||
if(minus > lost_counter)
|
||||
lost_counter = 0;
|
||||
else
|
||||
lost_counter -= minus;
|
||||
}
|
||||
} catch(Exception e) {
|
||||
//failed
|
||||
lost_counter++;
|
||||
}
|
||||
System.out.println("Connection LOST!");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
@ -0,0 +1 @@
|
||||
/bin/
|
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>BesenBoincServer</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -0,0 +1 @@
|
||||
/org.eclipse.jdt.core.prefs
|
@ -0,0 +1,92 @@
|
||||
package Comunication;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class Client implements Runnable{
|
||||
private Socket soc;
|
||||
private ObjectOutputStream out;
|
||||
private ObjectInputStream in;
|
||||
|
||||
private PacketHandler handler;
|
||||
private boolean hold_connection;
|
||||
|
||||
public Client(Socket soc, PacketHandler hand) {//server side constructor
|
||||
hold_connection = true;
|
||||
handler = hand;
|
||||
this.soc = soc;
|
||||
getStreams();
|
||||
}
|
||||
|
||||
public Client(String addr, int port, PacketHandler hand) throws UnknownHostException, IOException {//client side constructor
|
||||
hold_connection = true;
|
||||
handler = hand;
|
||||
soc = new Socket(InetAddress.getByName(addr), port);//connect
|
||||
getStreams();
|
||||
System.out.println("Connection established.");
|
||||
}
|
||||
|
||||
public void getStreams() {
|
||||
try {
|
||||
out = new ObjectOutputStream(soc.getOutputStream());
|
||||
in = new ObjectInputStream(soc.getInputStream());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void send(Data data) {
|
||||
if(hold_connection & hasConnection()) {
|
||||
try {
|
||||
out.writeObject(data);
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
hold_connection = false;
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
soc.close();
|
||||
System.out.println("Disconnected!");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(!soc.isClosed() & hold_connection) {
|
||||
//handle data
|
||||
try {
|
||||
handler.HandleData((Data) in.readObject(), this);
|
||||
// System.out.println("Recived data!");
|
||||
} catch(NullPointerException|SocketException|EOFException e) {
|
||||
e.printStackTrace();
|
||||
disconnect();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
disconnect();
|
||||
}
|
||||
|
||||
public boolean hasConnection() {
|
||||
return soc.isConnected();
|
||||
}
|
||||
|
||||
public interface PacketHandler {
|
||||
public void HandleData(Data data, Client c);
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package Comunication;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import Job.Job;
|
||||
import Job.Result;
|
||||
|
||||
public class Data implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = 8845895533697133L;
|
||||
|
||||
public Object content;
|
||||
public ContentType type;
|
||||
|
||||
public Data(Object o) {
|
||||
if(o instanceof String) {
|
||||
type = ContentType.Info;
|
||||
} else if( o instanceof Job) {
|
||||
type = ContentType.Job;
|
||||
} else if( o instanceof Result) {
|
||||
type = ContentType.Result;
|
||||
} else {
|
||||
throw new ClassCastException("The Object is not a String, Job or Result!");
|
||||
}
|
||||
content = o;
|
||||
}
|
||||
|
||||
public Data(Object o, ContentType t) {
|
||||
content = o;
|
||||
type = t;
|
||||
}
|
||||
|
||||
public enum ContentType {
|
||||
Job,
|
||||
Result,
|
||||
Info
|
||||
}
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
package Comunication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import Comunication.Client.PacketHandler;
|
||||
import Comunication.Data.ContentType;
|
||||
import Core.BB;
|
||||
import Core.Program;
|
||||
import Job.Result;
|
||||
|
||||
public class Server implements PacketHandler {
|
||||
|
||||
ServerSocket ssoc;
|
||||
List<Client> connections = new ArrayList<Client>();
|
||||
boolean accept_new_connections = false;
|
||||
Thread serverthread;
|
||||
Thread programthread;
|
||||
private static Server server;
|
||||
private ExecutorService execution = Executors.newCachedThreadPool();
|
||||
private Program prog;
|
||||
|
||||
public Server() {
|
||||
server = this;
|
||||
}
|
||||
|
||||
public static Server getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
public Server open() {
|
||||
try {
|
||||
accept_new_connections = true;
|
||||
ssoc = new ServerSocket(BB.port);
|
||||
serverthread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (accept_new_connections) {
|
||||
try {
|
||||
Socket soc = ssoc.accept();
|
||||
System.out.println("Connection established with: " + soc.getInetAddress());
|
||||
Client c = new Client(soc, Server.getServer());
|
||||
connections.add(c);
|
||||
execution.submit(c);
|
||||
c.send(new Data("welcomme"));
|
||||
prog.jobmanager.update();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}, "Serverthread");
|
||||
serverthread.start();
|
||||
System.out.println("Server is Listening on port " + BB.port);
|
||||
|
||||
programthread = new Thread(prog, "Programm Thread");
|
||||
programthread.start();
|
||||
System.out.println("Programm execution started");
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Server setProgram(Program prog) {
|
||||
this.prog = prog;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Program getProgram() {
|
||||
return prog;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public Server cancelProgram() {
|
||||
programthread.stop();
|
||||
prog = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void close() {
|
||||
accept_new_connections = false;
|
||||
serverthread.stop();
|
||||
//disconect all
|
||||
for(Client c : connections)
|
||||
c.disconnect();
|
||||
|
||||
execution.shutdownNow();
|
||||
|
||||
try {
|
||||
ssoc.close();//close server socket
|
||||
ssoc = null;
|
||||
Runtime.getRuntime().gc();//delete ssoc
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void HandleData(Data data, Client c) {
|
||||
// System.out.println("Data Recived: " + data);
|
||||
if(data.type == ContentType.Info) {
|
||||
String info = (String) data.content;
|
||||
if(info.equalsIgnoreCase("ping")) {
|
||||
c.send(new Data("pingback"));
|
||||
// System.out.println("Pinged, pining back...");
|
||||
} else if(info.equalsIgnoreCase("nextplease")) {
|
||||
if(prog.jobmanager.hasNext()) {
|
||||
c.send(new Data(prog.jobmanager.next(),ContentType.Job));
|
||||
} else {
|
||||
System.out.println("Out of Tasks");
|
||||
}
|
||||
}
|
||||
} else if(data.type == ContentType.Result) {
|
||||
prog.HandleResult((Result) data.content);
|
||||
}else
|
||||
System.out.println("unhandled Data!");
|
||||
}
|
||||
|
||||
public int getConnectionCount() {
|
||||
return connections.size();
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package Core;
|
||||
|
||||
public class BB {
|
||||
|
||||
public static int port = 5454;
|
||||
public static String host = "127.0.0.1";
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package Core;
|
||||
|
||||
import Job.JobManager;
|
||||
import Job.Result;
|
||||
import Job.Result.PartialResult;
|
||||
|
||||
public abstract class Program implements Runnable{
|
||||
|
||||
public JobManager jobmanager = new JobManager();
|
||||
|
||||
public void HandleResult(Result r) {
|
||||
while(r.hasnext()) {
|
||||
PartialResult pres = r.next();
|
||||
switch(pres.type) {
|
||||
case Console: System.out.println((String) pres.obj);
|
||||
case Value: HandlePartialResult(pres);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void HandlePartialResult(PartialResult pres);
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package Core;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import Comunication.Server;
|
||||
import Job.JobManager;
|
||||
|
||||
public class Starter {
|
||||
|
||||
public static void main(String[] args) throws ClassNotFoundException {
|
||||
System.out.println("Starting BesenBoincServer...");
|
||||
|
||||
String classname = "Test";//load programm
|
||||
if(args.length >= 1) {
|
||||
classname = args[0];
|
||||
}
|
||||
Program prog = null;
|
||||
prog = loadProgram(classname);
|
||||
|
||||
Server server = (new Server()).setProgram(prog).open();//start server
|
||||
boolean run = true;
|
||||
Scanner s = new Scanner(System.in);
|
||||
while(run) {
|
||||
String in = s.nextLine();
|
||||
if(in.equalsIgnoreCase("stop")) {
|
||||
System.out.println("Stopping Server...");
|
||||
Server.getServer().close();
|
||||
run = false;
|
||||
} else if(in.equalsIgnoreCase("stats")) {
|
||||
JobManager jmanager = server.getProgram().jobmanager;
|
||||
System.out.println("\nStats:\nTasks done : " + jmanager.jobs_done() + "\nTasks compiled: " + jmanager.jobs_compiled() + "/" + jmanager.jobs_compiledtarget() + "\nTasks enqued : " + jmanager.jobs_enqued() + "\n--------------------\ntotal : " + jmanager.jobs_total()+ "\n");
|
||||
}else {
|
||||
System.out.println("unknown Command.");
|
||||
}
|
||||
}
|
||||
s.close();
|
||||
}
|
||||
|
||||
public static Program loadProgram(String classname) throws ClassNotFoundException{
|
||||
System.out.println("Try to load class: " + classname);
|
||||
Program prog = null;
|
||||
try {
|
||||
prog = (Program) Class.forName(classname).newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(prog == null)
|
||||
throw new ClassNotFoundException("Could not load Programm called: " + classname);
|
||||
return prog;
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package Job;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Job implements Serializable{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 54437263428201383L;
|
||||
|
||||
private int id;
|
||||
public String code;//a class code extends Jobsrc
|
||||
public byte[] classfile;
|
||||
public boolean compiled = false;
|
||||
|
||||
public String classname = "";
|
||||
|
||||
public Job(String src) {
|
||||
code = src;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
// code.replace("[[Jobid]]",""+ id);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
@ -0,0 +1,169 @@
|
||||
package Job;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import Comunication.Server;
|
||||
import Utils.StringUtils;
|
||||
|
||||
public class JobManager implements Iterator<Job>{
|
||||
|
||||
private List<Job> enqued = new ArrayList<Job>();//enqued jobs
|
||||
private List<Job> todo = new ArrayList<Job>();//compiled jobs
|
||||
private List<Job> done = new ArrayList<Job>();//done jobs
|
||||
|
||||
int jobcount = 0;
|
||||
|
||||
private boolean isCompiling = false;
|
||||
|
||||
public void enque(Job newjob) {
|
||||
newjob.setId(jobcount);
|
||||
enqued.add(newjob);
|
||||
jobcount++;
|
||||
update();
|
||||
}
|
||||
|
||||
public Thread getnewThread() {
|
||||
return new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
isCompiling = true;//doppelt hält besser
|
||||
// System.out.println("Compilingque started!");
|
||||
while(enqued.size() > 0 & todo.size() < 15) {
|
||||
Job j = enqued.get(0);
|
||||
//compile script
|
||||
try {
|
||||
// System.out.println(j.code);
|
||||
String classname = StringUtils.cutout(j.code, "class ", " extends");//class [name] extends
|
||||
|
||||
File file = new File(classname + ".java");//delete old & file creation
|
||||
if (file.exists())
|
||||
file.delete();
|
||||
file.createNewFile();
|
||||
|
||||
File classfile = new File(classname+".class");//remove class file
|
||||
if(classfile.exists())
|
||||
classfile.delete();
|
||||
|
||||
FileWriter fw = new FileWriter(file);//file writing
|
||||
fw.write(j.code);
|
||||
fw.close();
|
||||
|
||||
Process process = Runtime.getRuntime().exec("javac " + file.getAbsolutePath());//compile
|
||||
Scanner scan = new Scanner(process.getErrorStream());
|
||||
while(scan.hasNextLine()) {
|
||||
System.out.println("Compile Err: " + scan.nextLine());
|
||||
}
|
||||
scan.close();
|
||||
process.waitFor();
|
||||
|
||||
/*BufferedReader br = new BufferedReader(new FileReader(classfile));//save the file to the String
|
||||
String line = null;
|
||||
StringBuilder strb = new StringBuilder();
|
||||
String linesep = System.getProperty("line.seperator");
|
||||
String out = "";
|
||||
|
||||
while((line= br.readLine()) != null) {
|
||||
out += (line + linesep);
|
||||
}
|
||||
|
||||
j.code = out;*/
|
||||
/*
|
||||
Scanner scanner = new Scanner(classfile);
|
||||
j.code = "";
|
||||
String out = "";
|
||||
while(scanner.hasNext()) {
|
||||
out = out + scanner.next();
|
||||
}
|
||||
scanner.close();
|
||||
j.code = out;
|
||||
System.out.println("File Readed size: " + j.code.length() + " " + out.length());
|
||||
*/
|
||||
|
||||
RandomAccessFile f = new RandomAccessFile(classfile, "r");
|
||||
j.classfile = new byte[(int)f.length()];
|
||||
f.read(j.classfile);
|
||||
f.close();
|
||||
|
||||
j.compiled = true;
|
||||
j.classname = classname;
|
||||
|
||||
enqued.remove(0);//manage ques
|
||||
todo.add(j);
|
||||
|
||||
//br.close();
|
||||
|
||||
if(classfile.exists())//delete all files
|
||||
classfile.delete();
|
||||
if (file.exists())
|
||||
file.delete();
|
||||
|
||||
// System.out.println("Compile done.");
|
||||
} catch(IOException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
isCompiling = false;
|
||||
// System.out.println("Compiling que Stopped!");
|
||||
}
|
||||
}, "Compiler");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return todo.size() != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Job next() {
|
||||
done.add(todo.get(0));
|
||||
todo.remove(0);
|
||||
update();
|
||||
return done.get(done.size()-1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {//unused
|
||||
|
||||
}
|
||||
|
||||
public void update() {//called from Server on new Client Connection
|
||||
if(enqued.size() > 0 & !isCompiling & todo.size() < jobs_compiledtarget()) {//7 für jede connection vorrätig
|
||||
startCompile();
|
||||
}
|
||||
}
|
||||
|
||||
public int jobs_done() {
|
||||
return done.size();
|
||||
}
|
||||
|
||||
public int jobs_compiled() {
|
||||
return todo.size();
|
||||
}
|
||||
public int jobs_enqued() {
|
||||
return enqued.size();
|
||||
}
|
||||
public int jobs_total() {
|
||||
return jobcount;
|
||||
}
|
||||
|
||||
public int jobs_compiledtarget() {
|
||||
int w = 7 * Server.getServer().getConnectionCount();
|
||||
if(w < 10)
|
||||
w = 10;
|
||||
return w;
|
||||
}
|
||||
|
||||
private void startCompile() {
|
||||
if(!isCompiling) {
|
||||
isCompiling = true;
|
||||
getnewThread().start();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package Job;
|
||||
|
||||
public abstract class Jobsrc {
|
||||
|
||||
public abstract Result run();
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package Job;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Result implements Serializable{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -4086039173401754665L;
|
||||
|
||||
private List<PartialResult> list = new ArrayList<PartialResult>();
|
||||
private int jobid;
|
||||
|
||||
public void OutputConsole(String out) {
|
||||
list.add(new PartialResult(ResultType.Console, out));
|
||||
}
|
||||
|
||||
public Result add(PartialResult partres) {
|
||||
list.add(partres);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PartialResult next() {
|
||||
PartialResult r = list.get(0);
|
||||
list.remove(0);
|
||||
return r;
|
||||
}
|
||||
|
||||
public boolean hasnext() {
|
||||
return !list.isEmpty();
|
||||
}
|
||||
|
||||
public int getJobId() {
|
||||
return jobid;
|
||||
}
|
||||
|
||||
public enum ResultType{
|
||||
Console,//something to output in the console
|
||||
Value//something to compute with
|
||||
}
|
||||
|
||||
public class PartialResult implements Serializable{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 8517252929857625435L;
|
||||
|
||||
public ResultType type;
|
||||
public Object obj;
|
||||
public PartialResult(ResultType t, Object o) {
|
||||
type = t; obj = o;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
package Job;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import Comunication.Client;
|
||||
import Comunication.Client.PacketHandler;
|
||||
import Comunication.Data;
|
||||
import Comunication.Data.ContentType;
|
||||
|
||||
public class Worker implements PacketHandler, Runnable{
|
||||
|
||||
//private Job current = null;
|
||||
//private Job next;
|
||||
private List<Job> jobs = new ArrayList<Job>();
|
||||
private Client client;
|
||||
private long lastasked = System.currentTimeMillis();
|
||||
private boolean run = true;
|
||||
|
||||
public Worker() {
|
||||
Thread workerthread = new Thread(this, "Worker");
|
||||
workerthread.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
run = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(1500);//just waiting for final initalisation
|
||||
while(!client.hasConnection()) {//waiting for connection
|
||||
Thread.sleep(500);
|
||||
}
|
||||
} catch(Exception e) {}
|
||||
while (run) {
|
||||
if(jobs.size()>0) {
|
||||
Result r = justrun(jobs.get(0));
|
||||
if(r != null) {
|
||||
client.send(new Data(r));
|
||||
jobs.remove(0);
|
||||
requestnewjob();//new job
|
||||
} else {
|
||||
System.err.println("Result ist null!");
|
||||
}
|
||||
} else {//derzeit kein job in petto
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
requestnewjob();
|
||||
} catch(InterruptedException e) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Result justrun(Job j) {
|
||||
// System.out.println("recived code:" + srccode);
|
||||
Result r = null;
|
||||
try {
|
||||
File classfile = new File(j.classname+".class");//remove class file
|
||||
if(classfile.exists())
|
||||
classfile.delete();
|
||||
|
||||
/* System.out.println("Writing file size:" + j.code.length());
|
||||
FileWriter fw = new FileWriter(classfile);//file writing
|
||||
fw.write(j.code);
|
||||
fw.close();
|
||||
*/
|
||||
FileOutputStream fos = new FileOutputStream(classfile);
|
||||
fos.write(j.classfile);
|
||||
fos.close();
|
||||
|
||||
r = ((Jobsrc) Class.forName(j.classname).newInstance()).run();//load & run
|
||||
//unload?
|
||||
|
||||
if(classfile.exists())
|
||||
classfile.delete();
|
||||
|
||||
} catch (IOException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void HandleData(Data data, Client c) {
|
||||
client = c;
|
||||
// if(data.type == ContentType.Info)
|
||||
// System.out.println("Data Recived: " + (String) data.content);
|
||||
if(data.type == ContentType.Job) {
|
||||
lastasked = 1; //den wait resetten, da er was bekommen hat.
|
||||
jobs.add((Job) data.content);
|
||||
// System.out.println("Recived job id: " + jobs.get(jobs.size()-1).getId());
|
||||
// System.out.println("new Job recived");
|
||||
if(jobs.size() < 4)
|
||||
requestnewjob();//wenn buffer leer -> direct nächsten requesten.
|
||||
} //else
|
||||
// System.err.println("unhandled Data!");
|
||||
}
|
||||
|
||||
private void requestnewjob() {
|
||||
if((System.currentTimeMillis()-lastasked) > 1500 & run & client.hasConnection()) {//request wenn letster unerfolg reicher lange genug her ist (Server nicht nerven)
|
||||
client.send(new Data("nextplease"));
|
||||
// System.out.println("asked for next job");
|
||||
lastasked = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package Utils;
|
||||
|
||||
public class StringUtils {
|
||||
|
||||
public static String cutout(String source, String bevore, String after) {
|
||||
source = source.substring(source.indexOf(bevore) + bevore.length());
|
||||
source = source.substring(0,source.indexOf(after));
|
||||
|
||||
return source;
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
/bin/
|
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>BBTestprogram</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -0,0 +1,17 @@
|
||||
import Core.Program;
|
||||
import Job.Job;
|
||||
import Job.Result.PartialResult;
|
||||
|
||||
public class Test extends Program {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for(int c = 5; c < 1000; c++) {
|
||||
String code = "import Job.Job;\nimport Job.Result;\nimport Job.Result.PartialResult;\nimport Job.Jobsrc;\npublic class A" + c + " extends Jobsrc {\n@Override\npublic Result run() {\ndouble i ="+c+";\nfor(int n = 2; n < " + c + ";n++){\n i *= n;\n}\n Result r = new Result();\nr.OutputConsole(\"!"+c+"= \"+i);\n return r;\n }}";
|
||||
jobmanager.enque(new Job(code));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void HandlePartialResult(PartialResult pres) {}//unused
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/BesenBoincServer"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
@ -0,0 +1 @@
|
||||
/bin/
|
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>BBPrimeFinder</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -0,0 +1,18 @@
|
||||
import Job.Jobsrc;
|
||||
import Job.Result;
|
||||
import Job.Result.ResultType;
|
||||
public class A extends Jobsrc{
|
||||
double num = 2;
|
||||
@Override
|
||||
public Result run() {
|
||||
Result r = new Result();
|
||||
for(double test = num; test < num + 200; test = test +2) {
|
||||
for(double i = 2; i * i <= test+2; i++) {
|
||||
if(test%i == 0)
|
||||
r.add(r.new PartialResult(ResultType.Value, test + "|" + false));
|
||||
}
|
||||
r.add(r.new PartialResult(ResultType.Value, test + "|" + true));
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
package mrbesen;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import Core.Program;
|
||||
import Job.Job;
|
||||
import Job.Result.PartialResult;
|
||||
|
||||
public class PrimeFinder extends Program {
|
||||
|
||||
List<Double> primes = new ArrayList<Double>();
|
||||
double count = -1;
|
||||
File file = new File("primes");
|
||||
|
||||
int save_count;//to save every 20 times
|
||||
int save_intervall = 100;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
//load primes
|
||||
try {
|
||||
if(file.exists()) {
|
||||
FileReader fr = new FileReader(file);
|
||||
Scanner frs = new Scanner(fr);
|
||||
|
||||
int i = 0;
|
||||
while(frs.hasNextLine()) {
|
||||
primes.add(Double.parseDouble(frs.nextLine()));
|
||||
i ++;
|
||||
}
|
||||
frs.close();
|
||||
System.out.println(i + " primes loaded. last prime: " + primes.get(primes.size()-1));
|
||||
} else {
|
||||
System.out.println("No Primes-file found, starting with 2");
|
||||
primes.add(2D);
|
||||
}
|
||||
for(int i = 0; i < 50; i++) {
|
||||
checkforprime();
|
||||
}
|
||||
} catch(IOException e) {}
|
||||
}
|
||||
|
||||
public void checkforprime() {
|
||||
if(count == -1)
|
||||
count = primes.get(primes.size()-1)+1;
|
||||
|
||||
String insert = PrimetoString(count);
|
||||
jobmanager.enque(new Job("import Job.Jobsrc;\nimport Job.Result;\nimport Job.Result.ResultType;\npublic class A" + insert + " extends Jobsrc{\n double num = " + insert + ";\n @Override\n public Result run() {\n Result r = new Result();\n for(double test = num; test < num + 200; test = test +2) {\n for(double i = 2; i * i <= test+2; i++) {\n if(test%i == 0)\n r.add(r.new PartialResult(ResultType.Value, test + \"|\" + false));\n }\n r.add(r.new PartialResult(ResultType.Value, test + \"|\" + true));\n }\n return r;\n }\n}") );
|
||||
count+= 200;
|
||||
}
|
||||
|
||||
public void save() throws Exception{
|
||||
save_count = 0;
|
||||
if(!file.exists())
|
||||
file.createNewFile();
|
||||
|
||||
FileWriter fw = new FileWriter(file);
|
||||
for(Double doub : primes) {
|
||||
fw.write(PrimetoString(doub)+"\n");
|
||||
}
|
||||
fw.close();
|
||||
System.out.println("Primes saved");
|
||||
}
|
||||
|
||||
private String PrimetoString(double prime) {
|
||||
String out = prime + "";
|
||||
out = out.substring(0,out.length()-2);
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void HandlePartialResult(PartialResult pres) {
|
||||
checkforprime();//enque next
|
||||
|
||||
if(pres.obj instanceof String) {
|
||||
String return_ = (String) pres.obj;
|
||||
System.out.println("Recived Result: " + return_);
|
||||
String[] split = return_.split("\\|", 2);
|
||||
if(split[1].equalsIgnoreCase("true")) {
|
||||
System.out.println("Prime Found: " + split[0]);
|
||||
primes.add(Double.parseDouble(split[0]));
|
||||
}
|
||||
}
|
||||
|
||||
save_count++;
|
||||
if(save_count >=save_intervall)
|
||||
try {
|
||||
save();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue