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.java227
1 files changed, 89 insertions, 138 deletions
diff --git a/gearman/controllerWorker/ControllerWorker/Shutdown.java b/gearman/controllerWorker/ControllerWorker/Shutdown.java
index 723e592..0973491 100644
--- a/gearman/controllerWorker/ControllerWorker/Shutdown.java
+++ b/gearman/controllerWorker/ControllerWorker/Shutdown.java
@@ -22,6 +22,8 @@ import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
+import org.apache.log4j.Logger;
+
public class Shutdown extends Thread {
private static final int CLIENT_INITIALIZED = 0;
private static final int PING_INITIALIZED = 1;
@@ -38,6 +40,8 @@ public class Shutdown extends Thread {
private static final int ERROR = 12;
private static final int SUCCESS = 13;
+ static final Logger logger = Logger.getLogger(Shutdown.class);
+
private String eventName;
private Vector<Client> clients;
private final int updateRate;
@@ -57,17 +61,16 @@ public class Shutdown extends Thread {
private Boolean error;
public Shutdown(String eventName, Vector<Client> clients, int updateRate,
- long waitTime, Vector<String> psWhitelist,
- Vector<String> psBlacklist, String gearmanServerAddress,
- int gearmanServerPort) {
+ long waitTime, Vector<String> psWhitelist, Vector<String> psBlacklist,
+ String gearmanServerAddress, int gearmanServerPort) {
this.eventName = eventName;
this.clients = clients;
this.updateRate = updateRate; // updates per second
this.waitTime = waitTime * 1000;
this.psWhitelist = psWhitelist;
this.psBlacklist = psBlacklist;
- gearmanConnection = new GearmanNIOJobServerConnection(
- gearmanServerAddress, gearmanServerPort);
+ gearmanConnection = new GearmanNIOJobServerConnection(gearmanServerAddress,
+ gearmanServerPort);
gearmanClient = new GearmanClientImpl();
gearmanClient.addJobServer(gearmanConnection);
pingJobs = new HashMap<Integer, GearmanJob>();
@@ -105,9 +108,9 @@ public class Shutdown extends Thread {
}
finished = true;
if (error) {
- System.out.println("Shutdown of " + eventName + " failed");
+ logger.info("Shutdown of " + eventName + " failed");
} else {
- System.out.println("Shutdown of " + eventName + " finished");
+ logger.info("Shutdown of " + eventName + " finished");
}
}
@@ -125,44 +128,35 @@ public class Shutdown extends Thread {
GearmanJob pingJob = pingJobs.get(client.getId());
if (pingJob != null) {
- GearmanJobStatus jobStatus = gearmanClient
- .getJobStatus(pingJob);
+ GearmanJobStatus jobStatus = gearmanClient.getJobStatus(pingJob);
if (!jobStatus.isKnown() && pingJob.isDone()) {
GearmanJobResult pingJobRes = pingJob.get();
- String result = ByteUtils.fromUTF8Bytes(pingJobRes
- .getResults());
+ String result = ByteUtils.fromUTF8Bytes(pingJobRes.getResults());
if (!result.isEmpty()) {
- JSONObject resultObj = (JSONObject) JSONValue
- .parse(result);
+ JSONObject resultObj = (JSONObject) JSONValue.parse(result);
if (!resultObj.containsKey("err")) {
- String alive = resultObj.get("alive")
- .toString();
+ String alive = resultObj.get("alive").toString();
if (alive.equals("true")) {
- System.out.println(client.getIp()
- + " alive");
- client.changeState(CLIENT_IS_ALIVE,
- "CLIENT_IS_ALIVE",
- "The client is alive.");
+ logger.info(client.getIp() + " alive");
+ //client.changeState(CLIENT_IS_ALIVE, "CLIENT_IS_ALIVE",
+ // "The client is alive.");
+ client.setState(States.FOO);
// check
// Users
pingJobs.remove(client.getId());
} else if (alive.equals("false")) {
- System.out.println(client.getIp()
- + " not alive");
+ logger.info(client.getIp() + " not alive");
// not alive, go in successState
- client
- .changeState(SUCCESS, "SUCCESS",
- "Shutdown of the client has been finished.");
+ client.changeState(SUCCESS, "SUCCESS",
+ "Shutdown of the client has been finished.");
pingJobs.remove(client.getId());
}
} else {
- System.out.println(client.getIp()
- + " Cannot send the ping message.");
- client
- .setError("Sending the ping message has been failed.");
+ logger.error(client.getIp() + " Cannot send the ping message.");
+ client.setError("Sending the ping message has been failed.");
// sending the ping message has been failed
client
.changeState(ERROR, "ERROR",
@@ -184,53 +178,42 @@ public class Shutdown extends Thread {
GearmanJob whoJob = whoJobs.get(client.getId());
if (whoJob != null) {
- GearmanJobStatus jobStatus = gearmanClient
- .getJobStatus(whoJob);
+ GearmanJobStatus jobStatus = gearmanClient.getJobStatus(whoJob);
if (!jobStatus.isKnown() && whoJob.isDone()) {
GearmanJobResult whoJobRes = whoJob.get();
- String result = ByteUtils.fromUTF8Bytes(whoJobRes
- .getResults());
+ String result = ByteUtils.fromUTF8Bytes(whoJobRes.getResults());
if (!result.isEmpty()) {
- JSONObject resultObj = (JSONObject) JSONValue
- .parse(result);
+ JSONObject resultObj = (JSONObject) JSONValue.parse(result);
if (!resultObj.containsKey("err")) {
- String rawoutput = resultObj.get("rawoutput")
- .toString();
- StringTokenizer str = new StringTokenizer(
- rawoutput, " ");
+ String rawoutput = resultObj.get("rawoutput").toString();
+ StringTokenizer str = new StringTokenizer(rawoutput, " ");
String user = "";
if (str.hasMoreTokens()) {
user = str.nextToken();
}
if (user.isEmpty()) {
- System.out.println(client.getIp()
- + " no user is logged in");
+ logger.info(client.getIp() + " no user is logged in");
// no user is logged in
- client.changeState(SHUTDOWN_CLIENT,
- "SHUTDOWN_CLIENT",
+ client.changeState(SHUTDOWN_CLIENT, "SHUTDOWN_CLIENT",
"No user is logged in.");
whoJobs.remove(client.getId());
} else {
- System.out.println(client.getIp()
- + " a user is logged in");
+ logger.info(client.getIp() + " a user is logged in");
// a user is logged in
- client.changeState(USER_IS_LOGGED_IN,
- "USER_IS_LOGGED_IN",
+ client.changeState(USER_IS_LOGGED_IN, "USER_IS_LOGGED_IN",
"A user is logged in.");
whoJobs.remove(client.getId());
}
} else {
- System.out
- .println(client.getIp()
- + " Cannot check if a user is logged in.");
+ logger.error(client.getIp()
+ + " Cannot check if a user is logged in.");
client
.setError("The check if a user is logged in has been failed.");
/*
- * cannot check if a user is logged in, go in
- * errorState
+ * cannot check if a user is logged in, go in errorState
*/
client
.changeState(ERROR, "ERROR",
@@ -256,26 +239,21 @@ public class Shutdown extends Thread {
if (!jobStatus.isKnown() && doShutdownJob.isDone()) {
GearmanJobResult wolJobRes = doShutdownJob.get();
- String result = ByteUtils.fromUTF8Bytes(wolJobRes
- .getResults());
+ String result = ByteUtils.fromUTF8Bytes(wolJobRes.getResults());
if (!result.isEmpty()) {
- JSONObject resultObj = (JSONObject) JSONValue
- .parse(result);
+ JSONObject resultObj = (JSONObject) JSONValue.parse(result);
if (!resultObj.containsKey("err")) {
- System.out.println(client.getIp()
- + " Shutdown command sent");
+ logger.info(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(client.getIp()
- + " Cannot send shutdown command");
+ logger.error(client.getIp() + " Cannot send shutdown command");
client
.setError("Sending the shutdown command has been failed.");
/*
- * cannot send shutdown command, go in /
- * errorState
+ * cannot send shutdown command, go in / errorState
*/
client
.changeState(ERROR, "ERROR",
@@ -302,59 +280,44 @@ public class Shutdown extends Thread {
break;
case PING_SHUTDOWN_INITIALIZED:
- GearmanJob pingJobShutdown = pingShutdownJobs.get(client
- .getId());
+ 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(client
- .getId())
+ Long expectedTimestamp = pingShutdownTime.get(client.getId())
+ waitTime;
if (expectedTimestamp >= currentTimestamp) {
GearmanJobStatus jobStatus = gearmanClient
.getJobStatus(pingJobShutdown);
if (!jobStatus.isKnown() && pingJobShutdown.isDone()) {
GearmanJobResult pingJobRes = pingJobShutdown.get();
- String result = ByteUtils.fromUTF8Bytes(pingJobRes
- .getResults());
+ String result = ByteUtils.fromUTF8Bytes(pingJobRes.getResults());
if (!result.isEmpty()) {
- JSONObject resultObj = (JSONObject) JSONValue
- .parse(result);
+ JSONObject resultObj = (JSONObject) JSONValue.parse(result);
if (!resultObj.containsKey("err")) {
- String alive = resultObj.get("alive")
- .toString();
+ String alive = resultObj.get("alive").toString();
if (alive.equals("false")) {
- System.out.println(client.getIp()
- + " is not alive anymore");
+ logger.info(client.getIp() + " is not alive anymore");
// not alive, go in successState
- client
- .changeState(SUCCESS,
- "SUCCESS",
- "Shutdown of the client has been finished.");
+ client.changeState(SUCCESS, "SUCCESS",
+ "Shutdown of the client has been finished.");
pingShutdownJobs.remove(client.getId());
} else if (alive.equals("true")) {
- System.out
- .println(client.getIp()
- + " is still alive after shutdown command");
- 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");
+ logger.info(client.getIp()
+ + " is still alive after shutdown command");
+ 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(client.getIp()
- + " Cannot send the ping after shutdown message.");
+ logger.error(client.getIp()
+ + " Cannot send the ping after shutdown message.");
client
.setError("Sending the ping after shutdown message has been failed.");
/*
- * sending the ping after shutdown message
- * has been failed
+ * sending the ping after shutdown message has been failed
*/
client
.changeState(ERROR, "ERROR",
@@ -364,10 +327,8 @@ public class Shutdown extends Thread {
}
}
} else {
- System.out.println(client.getIp()
- + " is alive after shutdown");
- client
- .setError("Client is still alive after shutdown.");
+ logger.error(client.getIp() + " is alive after shutdown");
+ client.setError("Client is still alive after shutdown.");
// still alive, go in errorState
client
.changeState(ERROR, "ERROR",
@@ -387,17 +348,14 @@ public class Shutdown extends Thread {
GearmanJob psJob = psJobs.get(client.getId());
if (psJob != null) {
- GearmanJobStatus jobStatus = gearmanClient
- .getJobStatus(psJob);
+ GearmanJobStatus jobStatus = gearmanClient.getJobStatus(psJob);
if (!jobStatus.isKnown() && psJob.isDone()) {
GearmanJobResult whoJobRes = psJob.get();
- String result = ByteUtils.fromUTF8Bytes(whoJobRes
- .getResults());
+ String result = ByteUtils.fromUTF8Bytes(whoJobRes.getResults());
if (!result.isEmpty()) {
- JSONObject resultObj = (JSONObject) JSONValue
- .parse(result);
+ JSONObject resultObj = (JSONObject) JSONValue.parse(result);
if (!resultObj.containsKey("err")) {
JSONArray ps = (JSONArray) resultObj.get("ps");
// boolean whitelistFound = false;
@@ -411,42 +369,37 @@ public class Shutdown extends Thread {
/*
* for (String whiteEntry : psWhitelist) { if
- * (ps.toString().contains(whiteEntry)) {
- * whitelistFound = true; } }
+ * (ps.toString().contains(whiteEntry)) { whitelistFound = true;
+ * } }
*/
if (blacklistFound) {
/*
- * if (whitelistFound) {
- * System.out.println(client.getIp() +
+ * if (whitelistFound) { logger.info(client.getIp() +
* " is not working"); // is not working
* status.put(client.getId(), 4);
* psJobs.remove(client.getId());
*
* } else {
*/
- client.changeState(USER_IS_WORKING,
- "USER_IS_WORKING",
+ client.changeState(USER_IS_WORKING, "USER_IS_WORKING",
"The user is working.");
psJobs.remove(client.getId());
// }
} else {
- System.out.println(client.getIp()
- + " is not working");
+ logger.info(client.getIp() + " is not working");
// is not working
- client.changeState(SHUTDOWN_CLIENT,
- "SHUTDOWN_CLIENT",
+ client.changeState(SHUTDOWN_CLIENT, "SHUTDOWN_CLIENT",
"The User is not working.");
psJobs.remove(client.getId());
}
} else {
- System.out.println(client.getIp()
+ logger.error(client.getIp()
+ " Cannot check if user is working.");
client
.setError("The check if a user is working has been failed.");
/*
- * cannot check if user is working, go in
- * errorState
+ * cannot check if user is working, go in errorState
*/
client
.changeState(ERROR, "ERROR",
@@ -460,18 +413,17 @@ public class Shutdown extends Thread {
break;
case USER_IS_WORKING:
- System.out.println(client.getIp() + " User has been working");
+ logger.error(client.getIp() + " User has been working");
client.setError("The user has been working.");
// user has been working, go in errorState
- client
- .changeState(ERROR, "ERROR",
- "Shutdown of the client has not been finished, due to an error.");
+ client.changeState(ERROR, "ERROR",
+ "Shutdown of the client has not been finished, due to an error.");
break;
case ERROR:
if (!client.isFinished()) {
- System.out.println(client.getIp() + " Shutdown failed"); // errorState
+ logger.info(client.getIp() + " Shutdown failed"); // errorState
client.finish();
error = true;
}
@@ -480,7 +432,7 @@ public class Shutdown extends Thread {
case SUCCESS:
if (!client.isFinished()) {
- System.out.println(client.getIp() + " Shutdown finished"); // successState
+ logger.info(client.getIp() + " Shutdown finished"); // successState
client.finish();
}
@@ -507,23 +459,23 @@ public class Shutdown extends Thread {
}
private void ping(Client client) {
- GearmanJob job = GearmanJobImpl.createJob("ping", client.getIp()
- .getBytes(), "ping" + client.getId());
+ GearmanJob job = GearmanJobImpl.createJob("ping",
+ client.getIp().getBytes(), "ping" + client.getId());
gearmanClient.submit(job);
client.changeState(PING_INITIALIZED, "PING_INITIALIZED",
"The ping has been intialized.");
pingJobs.put(client.getId(), job);
- System.out.println("ping " + client.getIp());
+ logger.info("ping " + client.getIp());
}
private void who(Client client) {
- GearmanJob job = GearmanJobImpl.createJob("who", client.getIp()
- .getBytes(), "who" + client.getId());
+ GearmanJob job = GearmanJobImpl.createJob("who", client.getIp().getBytes(),
+ "who" + client.getId());
gearmanClient.submit(job);
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());
+ logger.info("who " + client.getIp());
}
private void doShutdown(Client client) {
@@ -533,28 +485,27 @@ public class Shutdown extends Thread {
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());
+ logger.info("doShutdown " + client.getIp());
}
private void pingShutdown(Client client) {
- GearmanJob job = GearmanJobImpl.createJob("ping", client.getIp()
- .getBytes(), "ping" + client.getId());
+ GearmanJob job = GearmanJobImpl.createJob("ping",
+ client.getIp().getBytes(), "ping" + client.getId());
gearmanClient.submit(job);
- client.changeState(PING_SHUTDOWN_INITIALIZED,
- "PING_SHUTDOWN_INITIALIZED",
+ 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());
+ logger.info("ping " + client.getIp());
}
private void ps(Client client) {
- GearmanJob job = GearmanJobImpl.createJob("ps", client.getIp()
- .getBytes(), "ps" + client.getId());
+ GearmanJob job = GearmanJobImpl.createJob("ps", client.getIp().getBytes(),
+ "ps" + client.getId());
gearmanClient.submit(job);
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());
+ logger.info("ps " + client.getIp());
}
public Boolean isFinished() {