From 5a7a66831bcea58ec6791a284026700582bd240e Mon Sep 17 00:00:00 2001 From: Björn Geiger Date: Thu, 21 Jul 2011 16:53:53 +0200 Subject: Controller Worker nun mit 'GameLoop' --- gearman/java/ControllerWorker/Boot.java | 172 ++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 gearman/java/ControllerWorker/Boot.java (limited to 'gearman/java/ControllerWorker/Boot.java') diff --git a/gearman/java/ControllerWorker/Boot.java b/gearman/java/ControllerWorker/Boot.java new file mode 100644 index 0000000..7a61279 --- /dev/null +++ b/gearman/java/ControllerWorker/Boot.java @@ -0,0 +1,172 @@ +package ControllerWorker; + +import java.io.IOException; +import java.lang.Thread; +import java.util.HashMap; +import java.util.Vector; +import java.util.concurrent.ExecutionException; + +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +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.common.GearmanJobServerConnection; +import org.gearman.common.GearmanNIOJobServerConnection; +import org.gearman.util.ByteUtils; + +public class Boot extends Thread { + private Vector> clients; + private GearmanClient gearmanClient; + private final GearmanJobServerConnection gearmanConnection; + private long beginTime; + private long timeTaken; + private long timeLeft; + + 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 + + public Boot(String serverAddress, int port, + Vector> clients) { + 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 + } + } + + public void run() { + workerLoop(); + } + + private void workerLoop() { + while (true) { + try { + beginTime = System.nanoTime(); + update(); + timeTaken = System.nanoTime() - beginTime; + timeLeft = (UPDATE_PERIOD - timeTaken) / 1000000; + if (timeLeft < 10) + timeLeft = 10; + Thread.sleep(timeLeft); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private void update() throws IllegalStateException, IOException, + InterruptedException, ExecutionException { + for (HashMap client : clients) { + String ipAddress = client.get("ip"); + String macAddress = client.get("mac"); + int clientID = Integer.parseInt(client.get("id")); + int status = STATUS.get(clientID); + switch (status) { + case 0: + ping(client); + break; + case 1: + GearmanJob pingJob = PING_JOBS.get(clientID); + if (!gearmanClient.getJobStatus(pingJob).isKnown()) { + if (!gearmanClient.getJobStatus(pingJob).isRunning()) { + 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 == "true") { + System.out.println(ipAddress + " alive"); + STATUS.put(clientID, 2); // alive + PING_JOBS.remove(clientID); + } else { + System.out.println(ipAddress + " not alive"); + STATUS.put(clientID, 3); // not alive + PING_JOBS.remove(clientID); + } + } + } + } else { + PING_JOBS.remove(clientID); + } + break; + case 2: + ssh(client); + break; + case 3: + wakeOnLan(client); + break; + case 4: + GearmanJob wolJob = WOL_JOBS.get(clientID); + if (!gearmanClient.getJobStatus(wolJob).isKnown()) { + if (!gearmanClient.getJobStatus(wolJob).isRunning()) { + GearmanJobResult wolJobRes = wolJob.get(); + String result = ByteUtils.fromUTF8Bytes(wolJobRes + .getResults()); + if (result == "Magic packet send.") { + System.out + .println(macAddress + "Magic packet send"); + STATUS.put(clientID, 5); // magic + // packet + // send + WOL_JOBS.remove(clientID); + } + } + } else { + WOL_JOBS.remove(clientID); + } + break; + case 5: + break; + case 6: + break; + } + } + } + + 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); + STATUS.put(clientID, 1); // ping started + gearmanClient.submit(job); + PING_JOBS.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); + STATUS.put(clientID, 4); // wake on lan started // + gearmanClient.submit(job); + WOL_JOBS.put(clientID, job); + System.out.println("wake on lan " + macAddress); + } + + 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); + STATUS.put(clientID, 6); // ssh started // + gearmanClient.submit(job); + SSH_JOBS.put(clientID, job); + System.out.println("ssh " + ipAddress); + } +} -- cgit v1.2.3-55-g7522