From ceaa8457f82107df480e04397aa1f636f55429cb Mon Sep 17 00:00:00 2001 From: Björn Geiger Date: Wed, 27 Jul 2011 12:37:37 +0200 Subject: verschiedene Korrekturen --- .../controllerWorker/ControllerWorker/Boot.java | 109 +++++++++++++++------ 1 file changed, 77 insertions(+), 32 deletions(-) (limited to 'gearman/controllerWorker/ControllerWorker/Boot.java') diff --git a/gearman/controllerWorker/ControllerWorker/Boot.java b/gearman/controllerWorker/ControllerWorker/Boot.java index 8481612..74aa828 100644 --- a/gearman/controllerWorker/ControllerWorker/Boot.java +++ b/gearman/controllerWorker/ControllerWorker/Boot.java @@ -20,24 +20,27 @@ import org.json.simple.JSONObject; import org.json.simple.JSONValue; public class Boot extends Thread { - private Vector> clients; - private GearmanClient gearmanClient; - private final GearmanJobServerConnection gearmanConnection; private long beginTime; private long timeTaken; private long timeLeft; private Boolean finished; - private Boolean finishedWithErrors; + private Boolean error; + private final int updateRate; + private final long updatePeriod; - static HashMap PING_JOBS = new HashMap(); - static HashMap WOL_JOBS = new HashMap(); - static HashMap SSH_JOBS = new HashMap(); - static HashMap STATUS = new HashMap(); - static final int UPDATE_RATE = 1; // updates per second - static final long UPDATE_PERIOD = 1000000000L / UPDATE_RATE; // nanoseconds + private HashMap pingJobs; + private HashMap wolJobs; + private HashMap sshJobs; + private HashMap status; + private HashMap errors; + + private Vector> clients; + + private final GearmanJobServerConnection gearmanConnection; + private GearmanClient gearmanClient; public Boot(String serverAddress, int port, - Vector> clients) { + Vector> clients, int updateRate) { this.gearmanConnection = new GearmanNIOJobServerConnection( serverAddress, port); this.gearmanClient = new GearmanClientImpl(); @@ -45,10 +48,17 @@ public class Boot extends Thread { this.clients = clients; for (HashMap client : clients) { int clientID = Integer.parseInt(client.get("id")); - STATUS.put(clientID, 0); // no work + status.put(clientID, 0); // no work } this.finished = false; - this.finishedWithErrors = false; + this.error = false; + this.pingJobs = new HashMap(); + this.wolJobs = new HashMap(); + this.sshJobs = new HashMap(); + this.status = new HashMap(); + this.errors = new HashMap(); + this.updateRate = updateRate; // updates per second + this.updatePeriod = 1000000000L / this.updateRate; // nanoseconds } public void run() { @@ -62,7 +72,7 @@ public class Boot extends Thread { beginTime = System.nanoTime(); run = update(); timeTaken = System.nanoTime() - beginTime; - timeLeft = (UPDATE_PERIOD - timeTaken) / 1000000; + timeLeft = (updatePeriod - timeTaken) / 1000000; if (timeLeft < 10) timeLeft = 10; Thread.sleep(timeLeft); @@ -82,9 +92,9 @@ public class Boot extends Thread { String ipAddress = client.get("ip"); String macAddress = client.get("mac"); int clientID = Integer.parseInt(client.get("id")); - int status = STATUS.get(clientID); + int clientStatus = status.get(clientID); - switch (status) { + switch (clientStatus) { case 0: ping(client); @@ -92,7 +102,7 @@ public class Boot extends Thread { break; case 1: - GearmanJob pingJob = PING_JOBS.get(clientID); + GearmanJob pingJob = pingJobs.get(clientID); if (pingJob != null) { GearmanJobStatus jobStatus = gearmanClient @@ -110,12 +120,12 @@ public class Boot extends Thread { if (alive.equals("true")) { System.out.println(ipAddress + " alive"); - STATUS.put(clientID, 2); // alive - PING_JOBS.remove(clientID); + status.put(clientID, 2); // alive + pingJobs.remove(clientID); } else { System.out.println(ipAddress + " not alive"); - STATUS.put(clientID, 3); // not alive - PING_JOBS.remove(clientID); + status.put(clientID, 3); // not alive + pingJobs.remove(clientID); } } } @@ -134,7 +144,7 @@ public class Boot extends Thread { break; case 4: - GearmanJob wolJob = WOL_JOBS.get(clientID); + GearmanJob wolJob = wolJobs.get(clientID); if (wolJob != null) { GearmanJobStatus jobStatus = gearmanClient .getJobStatus(wolJob); @@ -147,8 +157,8 @@ public class Boot extends Thread { if (result.equals("Magic packet send.")) { System.out .println(macAddress + "Magic packet send"); - STATUS.put(clientID, 5); // magic packet send - WOL_JOBS.remove(clientID); + status.put(clientID, 5); // magic packet send + wolJobs.remove(clientID); } } } @@ -164,8 +174,11 @@ public class Boot extends Thread { break; } - if (status == 20) { + if (clientStatus == 20) { + allFinished = true; + } else if (clientStatus == 19) { allFinished = true; + this.error = true; } else { allFinished = false; } @@ -186,8 +199,8 @@ public class Boot extends Thread { "ping" + clientID); gearmanClient.submit(job); - STATUS.put(clientID, 1); // ping started - PING_JOBS.put(clientID, job); + status.put(clientID, 1); // ping started + pingJobs.put(clientID, job); System.out.println("ping " + ipAddress); } @@ -200,8 +213,8 @@ public class Boot extends Thread { "wol" + clientID); gearmanClient.submit(job); - STATUS.put(clientID, 4); // wake on lan started - WOL_JOBS.put(clientID, job); + status.put(clientID, 4); // wake on lan started + wolJobs.put(clientID, job); System.out.println("wake on lan " + macAddress); } @@ -214,17 +227,49 @@ public class Boot extends Thread { "ssh" + clientID); gearmanClient.submit(job); - STATUS.put(clientID, 6); // ssh started - SSH_JOBS.put(clientID, job); + status.put(clientID, 6); // ssh started + sshJobs.put(clientID, job); System.out.println("ssh " + ipAddress); } + public Vector> getClients() { + return clients; + } + + public GearmanClient getGearmanClient() { + return gearmanClient; + } + + public GearmanJobServerConnection getGearmanConnection() { + return gearmanConnection; + } + + public HashMap getPingJobs() { + return pingJobs; + } + + public HashMap getWolJobs() { + return wolJobs; + } + + public HashMap getSshJobs() { + return sshJobs; + } + + public HashMap getStatus() { + return status; + } + + public HashMap getErrors() { + return errors; + } + public Boolean isFinished() { return finished; } public Boolean isFinishedWithErrors() { - return finishedWithErrors; + return finished && error; } } -- cgit v1.2.3-55-g7522