package ControllerWorker; import java.io.IOException; import java.lang.Thread; import java.util.Date; import java.util.concurrent.ExecutionException; import java.util.HashMap; import java.util.Vector; import org.gearman.client.GearmanClient; import org.gearman.client.GearmanClientImpl; import org.gearman.client.GearmanJob; import org.gearman.client.GearmanJobImpl; import org.gearman.client.GearmanJobResult; import org.gearman.client.GearmanJobStatus; import org.gearman.common.GearmanJobServerConnection; import org.gearman.common.GearmanNIOJobServerConnection; import org.gearman.util.ByteUtils; import org.json.simple.JSONObject; import org.json.simple.JSONValue; public class Boot extends Thread { private long beginTime; private long timeTaken; private long timeLeft; private Boolean finished; private Boolean error; private final int updateRate; private final long updatePeriod; private HashMap pingJobs; private HashMap wolJobs; private HashMap pingWoLJobs; private HashMap sshJobs; private HashMap status; private HashMap errors; private HashMap pingWolTime; private Vector> clients; private final GearmanJobServerConnection gearmanConnection; private GearmanClient gearmanClient; public Boot(String serverAddress, int port, Vector> clients, int updateRate) { this.pingJobs = new HashMap(); this.pingWoLJobs = new HashMap(); this.wolJobs = new HashMap(); this.sshJobs = new HashMap(); this.status = new HashMap(); this.errors = new HashMap(); this.pingWolTime = new HashMap(); this.updateRate = updateRate; // updates per second this.updatePeriod = 1000000000L / this.updateRate; // nanoseconds this.gearmanConnection = new GearmanNIOJobServerConnection( serverAddress, port); this.gearmanClient = new GearmanClientImpl(); gearmanClient.addJobServer(this.gearmanConnection); this.clients = clients; for (HashMap client : clients) { int clientID = Integer.parseInt(client.get("id")); status.put(clientID, 0); // no work } this.finished = false; this.error = false; } public void run() { workerLoop(); } private void workerLoop() { Boolean run = true; while (run) { try { beginTime = System.nanoTime(); run = update(); timeTaken = System.nanoTime() - beginTime; timeLeft = (updatePeriod - timeTaken) / 1000000; if (timeLeft < 10) timeLeft = 10; Thread.sleep(timeLeft); } catch (Exception e) { e.printStackTrace(); } } this.finished = true; System.out.println("Booting finished"); } private Boolean update() throws IllegalStateException, IOException, InterruptedException, ExecutionException { Boolean allFinished = false; for (HashMap client : clients) { String ipAddress = client.get("ip"); String macAddress = client.get("mac"); int clientID = Integer.parseInt(client.get("id")); int clientStatus = status.get(clientID); switch (clientStatus) { case 0: ping(client); break; case 1: GearmanJob pingJob = pingJobs.get(clientID); if (pingJob != null) { GearmanJobStatus jobStatus = gearmanClient .getJobStatus(pingJob); if (!jobStatus.isKnown() && pingJob.isDone()) { GearmanJobResult pingJobRes = pingJob.get(); String result = ByteUtils.fromUTF8Bytes(pingJobRes .getResults()); if (result != "") { JSONObject resultObj = (JSONObject) JSONValue .parse(result); String alive = resultObj.get("alive").toString(); if (alive.equals("true")) { System.out.println(ipAddress + " alive"); status.put(clientID, 2); // alive pingJobs.remove(clientID); } else { System.out.println(ipAddress + " not alive"); status.put(clientID, 3); // not alive pingJobs.remove(clientID); } } } } break; case 2: ssh(client); break; case 3: wakeOnLan(client); break; case 4: GearmanJob wolJob = wolJobs.get(clientID); if (wolJob != null) { GearmanJobStatus jobStatus = gearmanClient .getJobStatus(wolJob); if (!jobStatus.isKnown() && wolJob.isDone()) { GearmanJobResult wolJobRes = wolJob.get(); String result = ByteUtils.fromUTF8Bytes(wolJobRes .getResults()); System.out.println(result.toString()); if (result.equals("Magic packet send.\n")) { System.out.println(macAddress + " Magic packet send."); status.put(clientID, 5); // magic packet send wolJobs.remove(clientID); } } } break; case 5: Date date = new Date(); Long timestamp = date.getTime(); pingWolTime.put(clientID, timestamp); status.put(clientID, 6); // ping after wake on LAN break; case 6: pingWoL(client); break; case 7: GearmanJob pingJobWoL = pingWoLJobs.get(clientID); if (pingJobWoL != null) { Date currentDate = new Date(); Long currentTimestamp = currentDate.getTime(); Long expectedTimestamp = this.pingWolTime.get(clientID) + 120000L; //wait 2 min until WoL-Failed if (expectedTimestamp >= currentTimestamp) { GearmanJobStatus jobStatus = gearmanClient .getJobStatus(pingJobWoL); if (!jobStatus.isKnown() && pingJobWoL.isDone()) { GearmanJobResult pingJobRes = pingJobWoL.get(); String result = ByteUtils.fromUTF8Bytes(pingJobRes .getResults()); if (result != "") { JSONObject resultObj = (JSONObject) JSONValue .parse(result); String alive = resultObj.get("alive") .toString(); if (alive.equals("true")) { System.out.println(ipAddress + " is alive after WoL"); status.put(clientID, 9); // alive, go in successState pingWoLJobs.remove(clientID); } else { System.out.println("ping again " +ipAddress); status.put(clientID, 6); // again ping } } } } else { System.out.println(ipAddress + " is not alive after WoL"); status.put(clientID, 8); // not alive, go in errorState pingWoLJobs.remove(clientID); } } break; case 8: System.out.println("WoL failed after 2min"); // errorState break; case 9: System.out.println("WoL successful"); // successState break; } if (clientStatus == 20) { allFinished = true; } else if (clientStatus == 19) { allFinished = true; this.error = true; } else { allFinished = false; } } if (allFinished) { return false; } else { return true; } } private void ping(HashMap client) { String ipAddress = client.get("ip"); int clientID = Integer.parseInt(client.get("id")); GearmanJob job = GearmanJobImpl.createJob("ping", ipAddress.getBytes(), "ping" + clientID); gearmanClient.submit(job); status.put(clientID, 1); // ping started pingJobs.put(clientID, job); System.out.println("ping " + ipAddress); } private void pingWoL(HashMap client) { String ipAddress = client.get("ip"); int clientID = Integer.parseInt(client.get("id")); GearmanJob job = GearmanJobImpl.createJob("ping", ipAddress.getBytes(), "ping" + clientID); gearmanClient.submit(job); status.put(clientID, 7); // ping started pingWoLJobs.put(clientID, job); System.out.println("ping " + ipAddress); } private void wakeOnLan(HashMap client) { String macAddress = client.get("mac"); int clientID = Integer.parseInt(client.get("id")); GearmanJob job = GearmanJobImpl.createJob("wol", macAddress.getBytes(), "wol" + clientID); gearmanClient.submit(job); status.put(clientID, 4); // wake on lan started wolJobs.put(clientID, job); System.out.println("wake on lan"); } private void ssh(HashMap client) { String ipAddress = client.get("ip"); int clientID = Integer.parseInt(client.get("id")); GearmanJob job = GearmanJobImpl.createJob("ssh", ipAddress.getBytes(), "ssh" + clientID); gearmanClient.submit(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 finished && error; } }