summaryrefslogtreecommitdiffstats
path: root/gearman
diff options
context:
space:
mode:
Diffstat (limited to 'gearman')
-rw-r--r--gearman/controllerWorker/ControllerWorker/Boot.java80
1 files changed, 76 insertions, 4 deletions
diff --git a/gearman/controllerWorker/ControllerWorker/Boot.java b/gearman/controllerWorker/ControllerWorker/Boot.java
index e730874..5259ed1 100644
--- a/gearman/controllerWorker/ControllerWorker/Boot.java
+++ b/gearman/controllerWorker/ControllerWorker/Boot.java
@@ -2,6 +2,7 @@ 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;
@@ -30,9 +31,11 @@ public class Boot extends Thread {
private HashMap<Integer, GearmanJob> pingJobs;
private HashMap<Integer, GearmanJob> wolJobs;
+ private HashMap<Integer, GearmanJob> pingWoLJobs;
private HashMap<Integer, GearmanJob> sshJobs;
private HashMap<Integer, Integer> status;
private HashMap<Integer, Integer> errors;
+ private HashMap<Integer, Long> pingWolTime;
private Vector<HashMap<String, String>> clients;
@@ -42,10 +45,12 @@ public class Boot extends Thread {
public Boot(String serverAddress, int port,
Vector<HashMap<String, String>> clients, int updateRate) {
this.pingJobs = new HashMap<Integer, GearmanJob>();
+ this.pingWoLJobs = new HashMap<Integer, GearmanJob>();
this.wolJobs = new HashMap<Integer, GearmanJob>();
this.sshJobs = new HashMap<Integer, GearmanJob>();
this.status = new HashMap<Integer, Integer>();
this.errors = new HashMap<Integer, Integer>();
+ this.pingWolTime = new HashMap<Integer, Long>();
this.updateRate = updateRate; // updates per second
this.updatePeriod = 1000000000L / this.updateRate; // nanoseconds
this.gearmanConnection = new GearmanNIOJobServerConnection(
@@ -153,10 +158,9 @@ public class Boot extends Thread {
GearmanJobResult wolJobRes = wolJob.get();
String result = ByteUtils.fromUTF8Bytes(wolJobRes
.getResults());
-
- if (result.equals("Magic packet send")) {
- System.out
- .println(macAddress + "Magic packet send");
+ 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);
}
@@ -166,12 +170,66 @@ public class Boot extends Thread {
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
+ 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); // go in errorState
+ pingWoLJobs.remove(clientID);
+ }
+ }
+ break;
+ case 8:
+ System.out.println("WoL Error"); // errorState
break;
+
+ case 9:
+ System.out.println("WoL Success"); // successState
+ break;
+
}
if (clientStatus == 20) {
@@ -205,6 +263,20 @@ public class Boot extends Thread {
System.out.println("ping " + ipAddress);
}
+ private void pingWoL(HashMap<String, String> 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<String, String> client) {
String macAddress = client.get("mac");
int clientID = Integer.parseInt(client.get("id"));