summaryrefslogtreecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/Shutdown.java
diff options
context:
space:
mode:
Diffstat (limited to 'gearman/controllerWorker/ControllerWorker/Shutdown.java')
-rw-r--r--gearman/controllerWorker/ControllerWorker/Shutdown.java312
1 files changed, 163 insertions, 149 deletions
diff --git a/gearman/controllerWorker/ControllerWorker/Shutdown.java b/gearman/controllerWorker/ControllerWorker/Shutdown.java
index 8f10e68..723e592 100644
--- a/gearman/controllerWorker/ControllerWorker/Shutdown.java
+++ b/gearman/controllerWorker/ControllerWorker/Shutdown.java
@@ -23,6 +23,21 @@ import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class Shutdown extends Thread {
+ private static final int CLIENT_INITIALIZED = 0;
+ private static final int PING_INITIALIZED = 1;
+ private static final int CLIENT_IS_ALIVE = 2;
+ private static final int WHO_INITIALIZED = 3;
+ private static final int SHUTDOWN_CLIENT = 4;
+ private static final int SHUTDOWN_INITIALIZED = 5;
+ private static final int SHUTDOWN_COMMAND_SENT = 6;
+ private static final int PING_SHUTDOWN_AGAIN = 7;
+ private static final int PING_SHUTDOWN_INITIALIZED = 8;
+ private static final int USER_IS_LOGGED_IN = 9;
+ private static final int PS_INITIALIZED = 10;
+ private static final int USER_IS_WORKING = 11;
+ private static final int ERROR = 12;
+ private static final int SUCCESS = 13;
+
private String eventName;
private Vector<Client> clients;
private final int updateRate;
@@ -38,10 +53,8 @@ public class Shutdown extends Thread {
private HashMap<Integer, GearmanJob> pingShutdownJobs;
private HashMap<Integer, GearmanJob> psJobs;
private HashMap<Integer, Long> pingShutdownTime;
- private HashMap<Integer, Integer> status;
private Boolean finished;
private Boolean error;
- private String[] statusText;
public Shutdown(String eventName, Vector<Client> clients, int updateRate,
long waitTime, Vector<String> psWhitelist,
@@ -63,24 +76,8 @@ public class Shutdown extends Thread {
pingShutdownJobs = new HashMap<Integer, GearmanJob>();
psJobs = new HashMap<Integer, GearmanJob>();
pingShutdownTime = new HashMap<Integer, Long>();
- status = new HashMap<Integer, Integer>();
finished = false;
error = false;
- statusText = new String[14];
- statusText[1] = "The ping has been started.";
- statusText[2] = "The client is alive.";
- statusText[3] = "The check if a user is logged in has been started.";
- statusText[4] = "No user is logged in.";
- statusText[5] = "A shutdown of the client has been triggered.";
- statusText[6] = "The shutdown command has been sent.";
- statusText[7] = "The ping after shutdown has been started.";
- statusText[8] = "Doing ping after shutdown again and again, until client is not alive or "
- + waitTime / 60 + " minutes has been elapsed";
- statusText[9] = "A user is logged in.";
- statusText[10] = "The check if the user is working has been started.";
- statusText[11] = "The user is working.";
- statusText[12] = "Shutdown of the client has not been finished, due to an error.";
- statusText[13] = "Shutdown of the client has been finished.";
}
public void run() {
@@ -117,19 +114,15 @@ public class Shutdown extends Thread {
private Boolean update() throws IllegalStateException, IOException,
InterruptedException, ExecutionException {
for (Client client : clients) {
- String ipAddress = client.getIp();
- int clientID = client.getId();
- int clientState = client.getState();
-
- switch (clientState) {
+ switch (client.getState()) {
- case 0:
+ case CLIENT_INITIALIZED:
ping(client);
break;
- case 1:
- GearmanJob pingJob = pingJobs.get(clientID);
+ case PING_INITIALIZED:
+ GearmanJob pingJob = pingJobs.get(client.getId());
if (pingJob != null) {
GearmanJobStatus jobStatus = gearmanClient
@@ -148,25 +141,33 @@ public class Shutdown extends Thread {
.toString();
if (alive.equals("true")) {
- System.out.println(ipAddress + " alive");
- status.put(clientID, 2); // alive, check
+ System.out.println(client.getIp()
+ + " alive");
+ client.changeState(CLIENT_IS_ALIVE,
+ "CLIENT_IS_ALIVE",
+ "The client is alive.");
+ // check
// Users
- pingJobs.remove(clientID);
+ pingJobs.remove(client.getId());
} else if (alive.equals("false")) {
- System.out
- .println(ipAddress + " not alive");
+ System.out.println(client.getIp()
+ + " not alive");
// not alive, go in successState
- status.put(clientID, 13);
- pingJobs.remove(clientID);
+ client
+ .changeState(SUCCESS, "SUCCESS",
+ "Shutdown of the client has been finished.");
+ pingJobs.remove(client.getId());
}
} else {
- System.out.println(ipAddress
+ System.out.println(client.getIp()
+ " Cannot send the ping message.");
client
.setError("Sending the ping message has been failed.");
// sending the ping message has been failed
- status.put(clientID, 12);
- pingJobs.remove(clientID);
+ client
+ .changeState(ERROR, "ERROR",
+ "Shutdown of the client has not been finished, due to an error.");
+ pingJobs.remove(client.getId());
}
}
}
@@ -174,13 +175,13 @@ public class Shutdown extends Thread {
break;
- case 2:
+ case CLIENT_IS_ALIVE:
who(client);
break;
- case 3:
- GearmanJob whoJob = whoJobs.get(clientID);
+ case WHO_INITIALIZED:
+ GearmanJob whoJob = whoJobs.get(client.getId());
if (whoJob != null) {
GearmanJobStatus jobStatus = gearmanClient
@@ -205,21 +206,25 @@ public class Shutdown extends Thread {
}
if (user.isEmpty()) {
- System.out.println(ipAddress
+ System.out.println(client.getIp()
+ " no user is logged in");
// no user is logged in
- status.put(clientID, 4);
- whoJobs.remove(clientID);
+ client.changeState(SHUTDOWN_CLIENT,
+ "SHUTDOWN_CLIENT",
+ "No user is logged in.");
+ whoJobs.remove(client.getId());
} else {
- System.out.println(ipAddress
+ System.out.println(client.getIp()
+ " a user is logged in");
// a user is logged in
- status.put(clientID, 9);
- whoJobs.remove(clientID);
+ client.changeState(USER_IS_LOGGED_IN,
+ "USER_IS_LOGGED_IN",
+ "A user is logged in.");
+ whoJobs.remove(client.getId());
}
} else {
System.out
- .println(ipAddress
+ .println(client.getIp()
+ " Cannot check if a user is logged in.");
client
.setError("The check if a user is logged in has been failed.");
@@ -227,8 +232,10 @@ public class Shutdown extends Thread {
* cannot check if a user is logged in, go in
* errorState
*/
- status.put(clientID, 12);
- whoJobs.remove(clientID);
+ client
+ .changeState(ERROR, "ERROR",
+ "Shutdown of the client has not been finished, due to an error.");
+ whoJobs.remove(client.getId());
}
}
}
@@ -236,13 +243,13 @@ public class Shutdown extends Thread {
break;
- case 4:
+ case SHUTDOWN_CLIENT:
doShutdown(client);
break;
- case 5:
- GearmanJob doShutdownJob = doShutdownJobs.get(clientID);
+ case SHUTDOWN_INITIALIZED:
+ GearmanJob doShutdownJob = doShutdownJobs.get(client.getId());
if (doShutdownJob != null) {
GearmanJobStatus jobStatus = gearmanClient
.getJobStatus(doShutdownJob);
@@ -255,13 +262,14 @@ public class Shutdown extends Thread {
JSONObject resultObj = (JSONObject) JSONValue
.parse(result);
if (!resultObj.containsKey("err")) {
- System.out.println(ipAddress
- + " Shutdown command send");
- status.put(clientID, 6); // shutdown command
- // send
- doShutdownJobs.remove(clientID);
+ System.out.println(client.getIp()
+ + " Shutdown command sent");
+ client.changeState(SHUTDOWN_COMMAND_SENT,
+ "SHUTDOWN_COMMAND_SENT",
+ "The shutdown command has been sent.");
+ doShutdownJobs.remove(client.getId());
} else {
- System.out.println(ipAddress
+ System.out.println(client.getIp()
+ " Cannot send shutdown command");
client
.setError("Sending the shutdown command has been failed.");
@@ -269,8 +277,10 @@ public class Shutdown extends Thread {
* cannot send shutdown command, go in /
* errorState
*/
- status.put(clientID, 12);
- doShutdownJobs.remove(clientID);
+ client
+ .changeState(ERROR, "ERROR",
+ "Shutdown of the client has not been finished, due to an error.");
+ doShutdownJobs.remove(client.getId());
}
}
}
@@ -278,26 +288,28 @@ public class Shutdown extends Thread {
break;
- case 6:
+ case SHUTDOWN_COMMAND_SENT:
Date date = new Date();
Long timestamp = date.getTime();
- pingShutdownTime.put(clientID, timestamp);
- status.put(clientID, 7); // ping after shutdown
+ pingShutdownTime.put(client.getId(), timestamp);
+ pingShutdown(client);
break;
- case 7:
+ case PING_SHUTDOWN_AGAIN:
pingShutdown(client);
break;
- case 8:
- GearmanJob pingJobShutdown = pingShutdownJobs.get(clientID);
+ case PING_SHUTDOWN_INITIALIZED:
+ GearmanJob pingJobShutdown = pingShutdownJobs.get(client
+ .getId());
if (pingJobShutdown != null) {
Date currentDate = new Date();
Long currentTimestamp = currentDate.getTime();
// wait 2 min until shutdown
- Long expectedTimestamp = pingShutdownTime.get(clientID)
+ Long expectedTimestamp = pingShutdownTime.get(client
+ .getId())
+ waitTime;
if (expectedTimestamp >= currentTimestamp) {
GearmanJobStatus jobStatus = gearmanClient
@@ -313,21 +325,30 @@ public class Shutdown extends Thread {
String alive = resultObj.get("alive")
.toString();
if (alive.equals("false")) {
- System.out.println(ipAddress
+ System.out.println(client.getIp()
+ " is not alive anymore");
// not alive, go in successState
- status.put(clientID, 13);
- pingShutdownJobs.remove(clientID);
+ client
+ .changeState(SUCCESS,
+ "SUCCESS",
+ "Shutdown of the client has been finished.");
+ pingShutdownJobs.remove(client.getId());
} else if (alive.equals("true")) {
System.out
- .println(ipAddress
+ .println(client.getIp()
+ " is still alive after shutdown command");
- // still alive, ping again
- status.put(clientID, 7);
+ client
+ .changeState(
+ PING_SHUTDOWN_AGAIN,
+ "PING_SHUTDOWN_AGAIN",
+ "Doing ping after shutdown again and again, until client is not alive or "
+ + waitTime
+ / 60
+ + " minutes has been elapsed");
}
} else {
System.out
- .println(ipAddress
+ .println(client.getIp()
+ " Cannot send the ping after shutdown message.");
client
.setError("Sending the ping after shutdown message has been failed.");
@@ -335,31 +356,35 @@ public class Shutdown extends Thread {
* sending the ping after shutdown message
* has been failed
*/
- status.put(clientID, 12);
- pingJobs.remove(clientID);
+ client
+ .changeState(ERROR, "ERROR",
+ "Shutdown of the client has not been finished, due to an error.");
+ pingJobs.remove(client.getId());
}
}
}
} else {
- System.out.println(ipAddress
+ System.out.println(client.getIp()
+ " is alive after shutdown");
client
.setError("Client is still alive after shutdown.");
// still alive, go in errorState
- status.put(clientID, 12);
- pingShutdownJobs.remove(clientID);
+ client
+ .changeState(ERROR, "ERROR",
+ "Shutdown of the client has not been finished, due to an error.");
+ pingShutdownJobs.remove(client.getId());
}
}
break;
- case 9:
+ case USER_IS_LOGGED_IN:
ps(client);
break;
- case 10:
- GearmanJob psJob = psJobs.get(clientID);
+ case PS_INITIALIZED:
+ GearmanJob psJob = psJobs.get(client.getId());
if (psJob != null) {
GearmanJobStatus jobStatus = gearmanClient
@@ -393,25 +418,29 @@ public class Shutdown extends Thread {
if (blacklistFound) {
/*
* if (whitelistFound) {
- * System.out.println(ipAddress +
+ * System.out.println(client.getIp() +
* " is not working"); // is not working
- * status.put(clientID, 4);
- * psJobs.remove(clientID);
+ * status.put(client.getId(), 4);
+ * psJobs.remove(client.getId());
*
* } else {
*/
- status.put(clientID, 11); // is working
- psJobs.remove(clientID);
+ client.changeState(USER_IS_WORKING,
+ "USER_IS_WORKING",
+ "The user is working.");
+ psJobs.remove(client.getId());
// }
} else {
- System.out.println(ipAddress
+ System.out.println(client.getIp()
+ " is not working");
// is not working
- status.put(clientID, 4);
- psJobs.remove(clientID);
+ client.changeState(SHUTDOWN_CLIENT,
+ "SHUTDOWN_CLIENT",
+ "The User is not working.");
+ psJobs.remove(client.getId());
}
} else {
- System.out.println(ipAddress
+ System.out.println(client.getIp()
+ " Cannot check if user is working.");
client
.setError("The check if a user is working has been failed.");
@@ -419,8 +448,10 @@ public class Shutdown extends Thread {
* cannot check if user is working, go in
* errorState
*/
- status.put(clientID, 12);
- psJobs.remove(clientID);
+ client
+ .changeState(ERROR, "ERROR",
+ "Shutdown of the client has not been finished, due to an error.");
+ psJobs.remove(client.getId());
}
}
}
@@ -428,26 +459,28 @@ public class Shutdown extends Thread {
break;
- case 11:
- System.out.println(ipAddress + " User has been working");
+ case USER_IS_WORKING:
+ System.out.println(client.getIp() + " User has been working");
client.setError("The user has been working.");
// user has been working, go in errorState
- status.put(clientID, 12);
+ client
+ .changeState(ERROR, "ERROR",
+ "Shutdown of the client has not been finished, due to an error.");
break;
- case 12:
+ case ERROR:
if (!client.isFinished()) {
- System.out.println(ipAddress + " Shutdown failed"); // errorState
+ System.out.println(client.getIp() + " Shutdown failed"); // errorState
client.finish();
error = true;
}
break;
- case 13:
+ case SUCCESS:
if (!client.isFinished()) {
- System.out.println(ipAddress + " Shutdown finished"); // successState
+ System.out.println(client.getIp() + " Shutdown finished"); // successState
client.finish();
}
@@ -474,73 +507,54 @@ public class Shutdown extends Thread {
}
private void ping(Client client) {
- String ipAddress = client.getIp();
- int clientID = client.getId();
-
- GearmanJob job = GearmanJobImpl.createJob("ping", ipAddress.getBytes(),
- "ping" + clientID);
+ GearmanJob job = GearmanJobImpl.createJob("ping", client.getIp()
+ .getBytes(), "ping" + client.getId());
gearmanClient.submit(job);
-
- status.put(clientID, 1); // ping started
- pingJobs.put(clientID, job);
-
- System.out.println("ping " + ipAddress);
+ client.changeState(PING_INITIALIZED, "PING_INITIALIZED",
+ "The ping has been intialized.");
+ pingJobs.put(client.getId(), job);
+ System.out.println("ping " + client.getIp());
}
private void who(Client client) {
- String ipAddress = client.getIp();
- int clientID = client.getId();
-
- GearmanJob job = GearmanJobImpl.createJob("who", ipAddress.getBytes(),
- "who" + clientID);
+ GearmanJob job = GearmanJobImpl.createJob("who", client.getIp()
+ .getBytes(), "who" + client.getId());
gearmanClient.submit(job);
-
- status.put(clientID, 3); // who started
- whoJobs.put(clientID, job);
-
- System.out.println("who " + ipAddress);
+ client.changeState(WHO_INITIALIZED, "WHO_INITIALIZED",
+ "The check if a user is logged in has been intialized.");
+ whoJobs.put(client.getId(), job);
+ System.out.println("who " + client.getIp());
}
private void doShutdown(Client client) {
- String ipAddress = client.getIp();
- int clientID = client.getId();
-
- GearmanJob job = GearmanJobImpl.createJob("doShutdown", ipAddress
- .getBytes(), "doShutdown" + clientID);
+ GearmanJob job = GearmanJobImpl.createJob("doShutdown", client.getIp()
+ .getBytes(), "doShutdown" + client.getId());
gearmanClient.submit(job);
-
- status.put(clientID, 5); // shutdown started
- doShutdownJobs.put(clientID, job);
-
- System.out.println("doShutdown " + ipAddress);
+ client.changeState(SHUTDOWN_INITIALIZED, "SHUTDOWN_INITIALIZED",
+ "A shutdown of the client has been triggered.");
+ doShutdownJobs.put(client.getId(), job);
+ System.out.println("doShutdown " + client.getIp());
}
private void pingShutdown(Client client) {
- String ipAddress = client.getIp();
- int clientID = client.getId();
-
- GearmanJob job = GearmanJobImpl.createJob("ping", ipAddress.getBytes(),
- "ping" + clientID);
+ GearmanJob job = GearmanJobImpl.createJob("ping", client.getIp()
+ .getBytes(), "ping" + client.getId());
gearmanClient.submit(job);
-
- status.put(clientID, 8); // pingShutdown started
- pingShutdownJobs.put(clientID, job);
-
- System.out.println("ping " + ipAddress);
+ client.changeState(PING_SHUTDOWN_INITIALIZED,
+ "PING_SHUTDOWN_INITIALIZED",
+ "The ping after shutdown has been intialized.");
+ pingShutdownJobs.put(client.getId(), job);
+ System.out.println("ping " + client.getIp());
}
private void ps(Client client) {
- String ipAddress = client.getIp();
- int clientID = client.getId();
-
- GearmanJob job = GearmanJobImpl.createJob("ps", ipAddress.getBytes(),
- "ps" + clientID);
+ GearmanJob job = GearmanJobImpl.createJob("ps", client.getIp()
+ .getBytes(), "ps" + client.getId());
gearmanClient.submit(job);
-
- status.put(clientID, 10); // ps started
- psJobs.put(clientID, job);
-
- System.out.println("ps " + ipAddress);
+ client.changeState(PS_INITIALIZED, "PS_INITIALIZED",
+ "The check if the user is working has been intialized.");
+ psJobs.put(client.getId(), job);
+ System.out.println("ps " + client.getIp());
}
public Boolean isFinished() {