From 35e0942b78542b896f01d0bde0d26410ac96d2d6 Mon Sep 17 00:00:00 2001 From: Björn Geiger Date: Wed, 31 Aug 2011 21:36:08 +0200 Subject: Shutdown nun auch vollständig umgestellt, noch nicht getestet --- .../controllerWorker/ControllerWorker/Boot.java | 268 +++++++++--------- .../controllerWorker/ControllerWorker/Client.java | 4 +- .../ControllerWorker/Shutdown.java | 312 +++++++++++---------- .../ControllerWorker/StatusWorker.java | 24 +- 4 files changed, 301 insertions(+), 307 deletions(-) (limited to 'gearman') diff --git a/gearman/controllerWorker/ControllerWorker/Boot.java b/gearman/controllerWorker/ControllerWorker/Boot.java index 23f3f39..7857e9b 100644 --- a/gearman/controllerWorker/ControllerWorker/Boot.java +++ b/gearman/controllerWorker/ControllerWorker/Boot.java @@ -23,7 +23,7 @@ import org.json.simple.JSONObject; import org.json.simple.JSONValue; public class Boot extends Thread { - private static final int INITIAL_STATE = 0; + private static final int CLIENT_INITIALIZED = 0; private static final int PING_INITIALIZED = 1; private static final int CLIENT_ALIVE = 2; private static final int CLIENT_NOT_ALIVE = 3; @@ -141,20 +141,15 @@ public class Boot extends Thread { long timestamp; for (Client client : clients) { - String ipAddress = client.getIp(); - String macAddress = client.getMac(); - int clientID = client.getId(); - int clientState = client.getState(); + switch (client.getState()) { - switch (clientState) { - - case INITIAL_STATE: + case CLIENT_INITIALIZED: ping(client); break; case PING_INITIALIZED: - GearmanJob pingJob = pingJobs.get(clientID); + GearmanJob pingJob = pingJobs.get(client.getId()); if (pingJob != null) { GearmanJobStatus jobStatus = gearmanClient .getJobStatus(pingJob); @@ -169,21 +164,22 @@ public class Boot extends Thread { String alive = resultObj.get("alive") .toString(); if (alive.equals("true")) { - System.out.println(ipAddress + " alive"); + System.out.println(client.getIp() + + " alive"); client.changeState(CLIENT_ALIVE, "CLIENT_ALIVE", "The client is alive."); - 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"); client.changeState(CLIENT_NOT_ALIVE, "CLIENT_NOT_ALIVE", "The client is not alive."); - pingJobs.remove(clientID); + 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."); @@ -192,7 +188,7 @@ public class Boot extends Thread { client .changeState(ERROR, "ERROR", "Booting of the client has not been finished, due to an error."); - pingJobs.remove(clientID); + pingJobs.remove(client.getId()); } } } @@ -211,7 +207,7 @@ public class Boot extends Thread { break; case WAKE_ON_LAN_INITIALIZED: - GearmanJob wolJob = wolJobs.get(clientID); + GearmanJob wolJob = wolJobs.get(client.getId()); if (wolJob != null) { GearmanJobStatus jobStatus = gearmanClient .getJobStatus(wolJob); @@ -221,14 +217,14 @@ public class Boot extends Thread { String result = ByteUtils.fromUTF8Bytes(wolJobRes .getResults()); if (result.equals("Magic packet send.")) { - System.out.println(macAddress + System.out.println(client.getMac() + " Magic packet sent."); client.changeState(MAGIC_PACKET_SENT, "MAGIC_PACKET_SENT", "The Magic packet has been sent."); - wolJobs.remove(clientID); + wolJobs.remove(client.getId()); } else { - System.out.println(ipAddress + System.out.println(client.getIp() + " Cannot send magic packet."); client .setError("Sending the magic packet has been failed."); @@ -236,7 +232,7 @@ public class Boot extends Thread { client .changeState(ERROR, "ERROR", "Booting of the client has not been finished, due to an error."); - wolJobs.remove(clientID); + wolJobs.remove(client.getId()); } } } @@ -246,7 +242,7 @@ public class Boot extends Thread { case MAGIC_PACKET_SENT: date = new Date(); timestamp = date.getTime(); - pingWolTime.put(clientID, timestamp); + pingWolTime.put(client.getId(), timestamp); pingWakeOnLan(client); break; @@ -257,12 +253,12 @@ public class Boot extends Thread { break; case PING_WOL_INITIALIZED: - GearmanJob pingJobWoL = pingWoLJobs.get(clientID); + GearmanJob pingJobWoL = pingWoLJobs.get(client.getId()); if (pingJobWoL != null) { Date currentDate = new Date(); Long currentTimestamp = currentDate.getTime(); // wait 2 min until WoL - Failed - Long expectedTimestamp = pingWolTime.get(clientID) + Long expectedTimestamp = pingWolTime.get(client.getId()) + waitTime; if (expectedTimestamp >= currentTimestamp) { GearmanJobStatus jobStatus = gearmanClient @@ -278,17 +274,17 @@ public class Boot extends Thread { String alive = resultObj.get("alive") .toString(); if (alive.equals("true")) { - System.out.println(ipAddress + System.out.println(client.getIp() + " is alive after WoL"); // alive, go in successState client .changeState(SUCCESS, "SUCCESS", "Booting of the client has been finished."); - pingWoLJobs.remove(clientID); + pingWoLJobs.remove(client.getId()); } else if (alive.equals("false")) { System.out.println("ping again " - + ipAddress); + + client.getIp()); client .changeState( PING_WOL_AGAIN, @@ -297,11 +293,11 @@ public class Boot extends Thread { + waitTime / 60 + " minutes has been elapsed"); - pingWoLJobs.remove(clientID); + pingWoLJobs.remove(client.getId()); } } else { System.out - .println(ipAddress + .println(client.getIp() + " Cannot send the ping after wake on LAN message."); client .setError("Sending the ping after wake on LAN message has been failed."); @@ -312,26 +308,26 @@ public class Boot extends Thread { client .changeState(ERROR, "ERROR", "Booting of the client has not been finished, due to an error."); - pingWoLJobs.remove(clientID); + pingWoLJobs.remove(client.getId()); } } } } else { - System.out.println(ipAddress + System.out.println(client.getIp() + " is not alive after WoL"); client.setError("The wake on LAN has been failed."); // not alive, go in errorState client .changeState(ERROR, "ERROR", "Booting of the client has not been finished, due to an error."); - pingWoLJobs.remove(clientID); + pingWoLJobs.remove(client.getId()); } } break; case CHECKOS_INITIALIZED: - GearmanJob osJob = osJobs.get(clientID); + GearmanJob osJob = osJobs.get(client.getId()); if (osJob != null) { GearmanJobStatus jobStatus = gearmanClient @@ -356,21 +352,23 @@ public class Boot extends Thread { "Description").toString(); if (description.equals(bootOS)) { - System.out.println(ipAddress + " right OS"); + System.out.println(client.getIp() + + " right OS"); // right os, go to successState client .changeState(SUCCESS, "SUCCESS", "Booting of the client has been finished."); - osJobs.remove(clientID); + osJobs.remove(client.getId()); } else { - System.out.println(ipAddress + " wrong OS"); + System.out.println(client.getIp() + + " wrong OS"); client .changeState(WRONG_OS, "WRONG_OS", "The wrong operating system is running."); - osJobs.remove(clientID); + osJobs.remove(client.getId()); } } else { - System.out.println(ipAddress + System.out.println(client.getIp() + " Cannot check os"); client .setError("The check for correct operating system has been failed."); @@ -378,7 +376,7 @@ public class Boot extends Thread { client .changeState(ERROR, "ERROR", "Booting of the client has not been finished, due to an error."); - osJobs.remove(clientID); + osJobs.remove(client.getId()); } } } @@ -392,7 +390,7 @@ public class Boot extends Thread { break; case WHO_INITIALIZED: - GearmanJob whoJob = whoJobs.get(clientID); + GearmanJob whoJob = whoJobs.get(client.getId()); if (whoJob != null) { GearmanJobStatus jobStatus = gearmanClient @@ -417,24 +415,24 @@ public class Boot extends Thread { } if (user.isEmpty()) { - System.out.println(ipAddress + System.out.println(client.getIp() + " no user is logged in"); // no user is logged in, doing restart client.changeState(RESTART_CLIENT, "RESTART_CLIENT", "No user is logged in."); - whoJobs.remove(clientID); + whoJobs.remove(client.getId()); } else { - System.out.println(ipAddress + System.out.println(client.getIp() + " a user is logged in"); client.changeState(A_USER_IS_LOGGED_IN, "A_USER_IS_LOGGED_IN", "A user is logged in."); - whoJobs.remove(clientID); + 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."); @@ -445,7 +443,7 @@ public class Boot extends Thread { client .changeState(ERROR, "ERROR", "Booting of the client has not been finished, due to an error."); - whoJobs.remove(clientID); + whoJobs.remove(client.getId()); } } } @@ -459,7 +457,7 @@ public class Boot extends Thread { break; case PS_INITIALIZED: - GearmanJob psJob = psJobs.get(clientID); + GearmanJob psJob = psJobs.get(client.getId()); if (psJob != null) { GearmanJobStatus jobStatus = gearmanClient @@ -493,27 +491,27 @@ public class Boot extends Thread { if (blacklistFound) { /* * if (whitelistFound) { - * System.out.println(ipAddress + + * System.out.println(client.getIp() + * " is not working"); // is not working - * status.put(clientID, 13); - * psJobs.remove(clientID); } else { + * status.put(client.getId(), 13); + * psJobs.remove(client.getId()); } else { */ - System.out.println(ipAddress + System.out.println(client.getIp() + " is working"); client.changeState(USER_IS_WORKING, "USER_IS_WORKING", "The user is working."); - psJobs.remove(clientID); + psJobs.remove(client.getId()); // } } else { // user is not working, doing restart client.changeState(RESTART_CLIENT, "RESTART_CLIENT", "The user is'nt working."); - psJobs.remove(clientID); + 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."); @@ -524,7 +522,7 @@ public class Boot extends Thread { client .changeState(ERROR, "ERROR", "Booting of the client has not been finished, due to an error."); - psJobs.remove(clientID); + psJobs.remove(client.getId()); } } } @@ -538,7 +536,7 @@ public class Boot extends Thread { break; case RESTART_INITIALIZED: - GearmanJob restartJob = restartJobs.get(clientID); + GearmanJob restartJob = restartJobs.get(client.getId()); if (restartJob != null) { GearmanJobStatus jobStatus = gearmanClient .getJobStatus(restartJob); @@ -551,14 +549,14 @@ public class Boot extends Thread { JSONObject resultObj = (JSONObject) JSONValue .parse(result); if (!resultObj.containsKey("err")) { - System.out.println(ipAddress + System.out.println(client.getIp() + " Restart command sent"); client.changeState(RESTART_COMMAND_SENT, "RESTART_COMMAND_SENT", "The restart command has been sent."); - restartJobs.remove(clientID); + restartJobs.remove(client.getId()); } else { - System.out.println(ipAddress + System.out.println(client.getIp() + " Cannot send restart command"); client .setError("Sending the restart command has been failed."); @@ -566,7 +564,7 @@ public class Boot extends Thread { client .changeState(ERROR, "ERROR", "Booting of the client has not been finished, due to an error."); - restartJobs.remove(clientID); + restartJobs.remove(client.getId()); } } } @@ -577,7 +575,7 @@ public class Boot extends Thread { case RESTART_COMMAND_SENT: date = new Date(); timestamp = date.getTime(); - pingRestartShutdownTime.put(clientID, timestamp); + pingRestartShutdownTime.put(client.getId(), timestamp); pingRestartShutdown(client); break; @@ -589,13 +587,13 @@ public class Boot extends Thread { case PING_RESTART_SHUTDOWN_INITIALIZED: GearmanJob pingJobRestartShutdown = pingRestartShutdownJobs - .get(clientID); + .get(client.getId()); if (pingJobRestartShutdown != null) { Date currentDate = new Date(); Long currentTimestamp = currentDate.getTime(); // wait 2 min until Restart - Failed - Long expectedTimestamp = pingRestartShutdownTime - .get(clientID) + Long expectedTimestamp = pingRestartShutdownTime.get(client + .getId()) + waitTime; if (expectedTimestamp >= currentTimestamp) { GearmanJobStatus jobStatus = gearmanClient @@ -614,7 +612,7 @@ public class Boot extends Thread { String alive = resultObj.get("alive") .toString(); if (alive.equals("true")) { - System.out.println(ipAddress + System.out.println(client.getIp() + " is still alive"); // still alive, ping again client @@ -625,22 +623,22 @@ public class Boot extends Thread { + waitTime / 60 + " minutes has been elapsed"); - pingRestartShutdownJobs - .remove(clientID); + pingRestartShutdownJobs.remove(client + .getId()); } else if (alive.equals("false")) { - System.out.println(ipAddress + System.out.println(client.getIp() + " is down"); // not alive, ping again client .changeState(CLIENT_IS_DOWN, "CLIENT_IS_DOWN", "The client has been shutted down."); - pingRestartShutdownJobs - .remove(clientID); + pingRestartShutdownJobs.remove(client + .getId()); } } else { System.out - .println(ipAddress + .println(client.getIp() + " Cannot send the ping after restart message."); client .setError("Sending the ping after restart message has been failed."); @@ -651,25 +649,26 @@ public class Boot extends Thread { client .changeState(ERROR, "ERROR", "Booting of the client has not been finished, due to an error."); - pingRestartShutdownJobs.remove(clientID); + pingRestartShutdownJobs.remove(client + .getId()); } } } } else { - System.out.println(ipAddress + " shutdown failed"); + System.out.println(client.getIp() + " shutdown failed"); client.setError("The shutdown has been failed."); // still alive, go in errorState client .changeState(ERROR, "ERROR", "Booting of the client has not been finished, due to an error."); - pingRestartShutdownJobs.remove(clientID); + pingRestartShutdownJobs.remove(client.getId()); } } break; case USER_IS_WORKING: - System.out.println(ipAddress + " User has been working"); + System.out.println(client.getIp() + " User has been working"); client.setError("The user has been working."); // user has been working, go in errorState client @@ -681,7 +680,7 @@ public class Boot extends Thread { case CLIENT_IS_DOWN: date = new Date(); timestamp = date.getTime(); - pingRestartBootTime.put(clientID, timestamp); + pingRestartBootTime.put(client.getId(), timestamp); pingRestartBoot(client); break; @@ -692,13 +691,14 @@ public class Boot extends Thread { break; case PING_RESTART_BOOT_INITIALIZED: - GearmanJob pingJobRestartBoot = pingRestartBootJobs - .get(clientID); + GearmanJob pingJobRestartBoot = pingRestartBootJobs.get(client + .getId()); if (pingJobRestartBoot != null) { Date currentDate = new Date(); Long currentTimestamp = currentDate.getTime(); // wait 2 min until Restart - Failed - Long expectedTimestamp = pingRestartBootTime.get(clientID) + Long expectedTimestamp = pingRestartBootTime.get(client + .getId()) + waitTime; if (expectedTimestamp >= currentTimestamp) { GearmanJobStatus jobStatus = gearmanClient @@ -716,17 +716,18 @@ public class Boot extends Thread { String alive = resultObj.get("alive") .toString(); if (alive.equals("true")) { - System.out.println(ipAddress + System.out.println(client.getIp() + " is alive after restart"); // alive, go to success state client .changeState(SUCCESS, "SUCCESS", "Booting of the client has been finished."); - pingRestartBootJobs.remove(clientID); + pingRestartBootJobs.remove(client + .getId()); } else if (alive.equals("false")) { System.out.println("ping again " - + ipAddress); + + client.getIp()); // not alive, ping again client .changeState( @@ -736,11 +737,12 @@ public class Boot extends Thread { + waitTime / 60 + " minutes has been elapsed"); - pingRestartBootJobs.remove(clientID); + pingRestartBootJobs.remove(client + .getId()); } } 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."); @@ -755,14 +757,14 @@ public class Boot extends Thread { } } } else { - System.out.println(ipAddress + System.out.println(client.getIp() + " is not alive after reboot"); client.setError("The reboot has been failed."); // not alive, go in errorState client .changeState(0, "ERROR", "Booting of the client has not been finished, due to an error."); - pingRestartBootJobs.remove(clientID); + pingRestartBootJobs.remove(client.getId()); } } @@ -770,7 +772,7 @@ public class Boot extends Thread { case ERROR: if (!client.isFinished()) { - System.out.println(ipAddress + " Booting failed"); // errorState + System.out.println(client.getIp() + " Booting failed"); // errorState client.finish(); error = true; } @@ -779,7 +781,7 @@ public class Boot extends Thread { case SUCCESS: if (!client.isFinished()) { - System.out.println(ipAddress + " Booting finished"); // successState + System.out.println(client.getIp() + " Booting finished"); // successState client.finish(); } @@ -805,114 +807,96 @@ public class Boot 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); client.changeState(PING_INITIALIZED, "PING_INITIALIZED", "The ping has been initialized."); - pingJobs.put(clientID, job); - System.out.println("ping " + ipAddress); + pingJobs.put(client.getId(), job); + System.out.println("ping " + client.getIp()); } private void pingWakeOnLan(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); client.changeState(PING_WOL_INITIALIZED, "PING_WOL_INITIALIZED", "The ping after wake on LAN has been initialized."); - pingWoLJobs.put(clientID, job); - System.out.println("ping " + ipAddress); + pingWoLJobs.put(client.getId(), job); + System.out.println("ping " + client.getIp()); } private void pingRestartShutdown(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); client.changeState(PING_RESTART_SHUTDOWN_INITIALIZED, "PING_RESTART_SHUTDOWN_INITIALIZED", "The ping after shutdown has been initialized."); - pingRestartShutdownJobs.put(clientID, job); - System.out.println("ping " + ipAddress); + pingRestartShutdownJobs.put(client.getId(), job); + System.out.println("ping " + client.getIp()); } private void pingRestartBoot(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); client.changeState(PING_RESTART_BOOT_INITIALIZED, "PING_RESTART_BOOT_INITIALIZED", "The ping after reboot has been initialized."); - pingRestartBootJobs.put(clientID, job); - System.out.println("ping " + ipAddress); + pingRestartBootJobs.put(client.getId(), job); + System.out.println("ping " + client.getIp()); } private void wakeOnLan(Client client) { - String macAddress = client.getMac(); - int clientID = client.getId(); - GearmanJob job = GearmanJobImpl.createJob("wol", macAddress.getBytes(), - "wol" + clientID); + GearmanJob job = GearmanJobImpl.createJob("wol", client.getMac() + .getBytes(), "wol" + client.getId()); gearmanClient.submit(job); client.changeState(WAKE_ON_LAN_INITIALIZED, "WAKE_ON_LAN_INITIALIZED", "The wake on LAN has been initialized."); - wolJobs.put(clientID, job); - System.out.println("wake on lan"); + wolJobs.put(client.getId(), job); + System.out.println("wake on lan" + client.getMac()); } private void checkOS(Client client) { - String ipAddress = client.getIp(); - int clientID = client.getId(); - GearmanJob job = GearmanJobImpl.createJob("os", ipAddress.getBytes(), - "os" + clientID); + GearmanJob job = GearmanJobImpl.createJob("os", client.getIp() + .getBytes(), "os" + client.getId()); gearmanClient.submit(job); client .changeState(CHECKOS_INITIALIZED, "CHECKOS_INITIALIZED", "The check for the correct operating system has been initialized."); - osJobs.put(clientID, job); - System.out.println("check OS " + ipAddress); + osJobs.put(client.getId(), job); + System.out.println("check OS " + 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); client.changeState(WHO_INITIALIZED, "WHO_INITIALIZED", "The check if a user is logged in has been initialized."); - whoJobs.put(clientID, job); - System.out.println("who " + ipAddress); + whoJobs.put(client.getId(), job); + System.out.println("who " + 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); client.changeState(PS_INITIALIZED, "PS_INITIALIZED", "The check if the user is working has been initialized."); - psJobs.put(clientID, job); - System.out.println("ps " + ipAddress); + psJobs.put(client.getId(), job); + System.out.println("ps " + client.getIp()); } private void restart(Client client) { - String ipAddress = client.getIp(); - int clientID = client.getId(); - GearmanJob job = GearmanJobImpl.createJob("restart", ipAddress - .getBytes(), "restart" + clientID); + GearmanJob job = GearmanJobImpl.createJob("restart", client.getIp() + .getBytes(), "restart" + client.getId()); gearmanClient.submit(job); client.changeState(RESTART_INITIALIZED, "RESTART_INITIALIZED", "A restart of the client has been initialized."); - restartJobs.put(clientID, job); - System.out.println("restart " + ipAddress); + restartJobs.put(client.getId(), job); + System.out.println("restart " + client.getIp()); } public Boolean isFinished() { diff --git a/gearman/controllerWorker/ControllerWorker/Client.java b/gearman/controllerWorker/ControllerWorker/Client.java index 5fffb16..e891d02 100644 --- a/gearman/controllerWorker/ControllerWorker/Client.java +++ b/gearman/controllerWorker/ControllerWorker/Client.java @@ -47,8 +47,8 @@ public class Client { state = new ClientState(stateID, stateName, stateText); } - public void initializeState(String stateText) { - state = new ClientState(0, "INITIAL_STATE", stateText); + private void initializeState(String stateText) { + state = new ClientState(0, "CLIENT_INITIALIZED", stateText); } public void setError(String error) { 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 clients; private final int updateRate; @@ -38,10 +53,8 @@ public class Shutdown extends Thread { private HashMap pingShutdownJobs; private HashMap psJobs; private HashMap pingShutdownTime; - private HashMap status; private Boolean finished; private Boolean error; - private String[] statusText; public Shutdown(String eventName, Vector clients, int updateRate, long waitTime, Vector psWhitelist, @@ -63,24 +76,8 @@ public class Shutdown extends Thread { pingShutdownJobs = new HashMap(); psJobs = new HashMap(); pingShutdownTime = new HashMap(); - status = new HashMap(); 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() { diff --git a/gearman/controllerWorker/ControllerWorker/StatusWorker.java b/gearman/controllerWorker/ControllerWorker/StatusWorker.java index 880b6aa..1698129 100644 --- a/gearman/controllerWorker/ControllerWorker/StatusWorker.java +++ b/gearman/controllerWorker/ControllerWorker/StatusWorker.java @@ -52,10 +52,9 @@ public class StatusWorker extends AbstractGearmanFunction { + " has not been finished, due to errors!"); LinkedHashMap clientErrors = new LinkedHashMap(); for (Client client : clients) { - String clientID = Integer.toString(client.getId()); - String errorText = client.getError(); - if (!errorText.isEmpty()) { - clientErrors.put(clientID, errorText); + if (!client.getError().isEmpty()) { + clientErrors.put(Integer.toString(client + .getId()), client.getError()); } } content.put("clients errors", clientErrors); @@ -68,9 +67,8 @@ public class StatusWorker extends AbstractGearmanFunction { + " has not yet been finished!"); LinkedHashMap clientStatus = new LinkedHashMap(); for (Client client : clients) { - String clientID = Integer.toString(client.getId()); - String stateText = client.getStateText(); - clientStatus.put(clientID, stateText); + clientStatus.put(Integer.toString(client.getId()), + client.getStateText()); } content.put("clients states", clientStatus); res.put(event, content); @@ -101,10 +99,9 @@ public class StatusWorker extends AbstractGearmanFunction { + " has not been finished, due to errors!"); LinkedHashMap clientErrors = new LinkedHashMap(); for (Client client : clients) { - String clientID = Integer.toString(client.getId()); - String errorText = client.getError(); - if (!errorText.isEmpty()) { - clientErrors.put(clientID, errorText); + if (!client.getError().isEmpty()) { + clientErrors.put(Integer.toString(client + .getId()), client.getError()); } } content.put("clients errors", clientErrors); @@ -117,9 +114,8 @@ public class StatusWorker extends AbstractGearmanFunction { + " has not yet been finished!"); LinkedHashMap clientStatus = new LinkedHashMap(); for (Client client : clients) { - String clientID = Integer.toString(client.getId()); - String statusText = client.getStateText(); - clientStatus.put(clientID, statusText); + clientStatus.put(Integer.toString(client.getId()), + client.getStateText()); } content.put("clients states", clientStatus); res.put(event, content); -- cgit v1.2.3-55-g7522