should fix #2

This commit is contained in:
mrbesen 2017-07-31 14:00:08 +02:00
parent 102f5c63a8
commit b8e779927c
4 changed files with 44 additions and 8 deletions

View File

@ -5,6 +5,11 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;
import Comunication.Data.ContentType;
import Job.Job;
public class Client implements Runnable{
private Socket soc;
@ -14,6 +19,8 @@ public class Client implements Runnable{
private PacketHandler handler;
private boolean hold_connection;
private List<Job> takenjobs = new LinkedList<Job>();
public Client(Socket soc, PacketHandler hand) {//server side constructor
hold_connection = true;
handler = hand;
@ -43,6 +50,9 @@ public class Client implements Runnable{
try {
out.writeObject(data);
out.flush();
if(data.type == ContentType.Job) {
takenjobs.add((Job) data.content);
}
} catch (IOException e) {
e.printStackTrace();
}
@ -56,6 +66,9 @@ public class Client implements Runnable{
out.close();
soc.close();
System.out.println("Disconnected!");
//re-enque all take jobs
Server.getServer().getProgram().jobmanager.reenque(takenjobs);
takenjobs.clear();
} catch (IOException e) {
e.printStackTrace();
}
@ -76,6 +89,15 @@ public class Client implements Runnable{
disconnect();
}
void removetakenjob(int jobid) {
for(int i = 0; i < takenjobs.size(); i++) {
if(takenjobs.get(i).getId() == jobid) {
takenjobs.remove(i);
break;
}
}
}
public boolean hasConnection() {
return soc.isConnected();
}

View File

@ -121,7 +121,9 @@ public class Server implements PacketHandler {
}
}
} else if(data.type == ContentType.Result) {
prog.HandleResult((Result) data.content);
Result r = (Result) data.content;
c.removetakenjob(r.getJobId());
prog.HandleResult(r);
}else
System.out.println("unhandled Data!");
}

View File

@ -23,6 +23,12 @@ public class JobManager implements Iterator<Job>{
private boolean isCompiling = false;
public void reenque(List<Job> jobs) {
todo.addAll(0, jobs);
send.removeAll(jobs);
update();
}
public void enque(Job newjob) {
jobcount++;
newjob.setId(jobcount);
@ -30,6 +36,10 @@ public class JobManager implements Iterator<Job>{
update();
}
/**
* Creates a new Compiling Thread
* @return
*/
private Thread getnewThread() {
return new Thread(new Runnable() {
@Override
@ -94,6 +104,9 @@ public class JobManager implements Iterator<Job>{
}, "Compiler");
}
/**
* is a new Compiled job in the list
*/
@Override
public boolean hasNext() {
return todo.size() != 0;
@ -108,11 +121,6 @@ public class JobManager implements Iterator<Job>{
return send.get(send.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();
@ -137,6 +145,10 @@ public class JobManager implements Iterator<Job>{
return jobcount;
}
/**
* How many Jobs should stored compiled?
* @return 7 Jobs for every Connection, and at least 10.
*/
public int jobs_compiledtarget() {
int w = 7 * Server.getServer().getConnectionCount();
return (w < 10) ? 10 : w;