From a48ca69d5dc7fd00b43c1678fc1d96c3274ab1f5 Mon Sep 17 00:00:00 2001 From: Björn Geiger Date: Wed, 31 Aug 2011 16:36:30 +0200 Subject: Client und State nun als eigenes Objekt, Shutdown.java noch nicht ganz angepasst --- .../ControllerWorker/Shutdown.java | 127 ++++++++------------- 1 file changed, 49 insertions(+), 78 deletions(-) (limited to 'gearman/controllerWorker/ControllerWorker/Shutdown.java') diff --git a/gearman/controllerWorker/ControllerWorker/Shutdown.java b/gearman/controllerWorker/ControllerWorker/Shutdown.java index 78ab144..8f10e68 100644 --- a/gearman/controllerWorker/ControllerWorker/Shutdown.java +++ b/gearman/controllerWorker/ControllerWorker/Shutdown.java @@ -24,7 +24,7 @@ import org.json.simple.JSONValue; public class Shutdown extends Thread { private String eventName; - private Vector> clients; + private Vector clients; private final int updateRate; private long waitTime; private Vector psWhitelist; @@ -39,14 +39,12 @@ public class Shutdown extends Thread { private HashMap psJobs; private HashMap pingShutdownTime; private HashMap status; - private HashMap errors; - private HashMap finishedClients; private Boolean finished; private Boolean error; private String[] statusText; - public Shutdown(String eventName, Vector> clients, - int updateRate, long waitTime, Vector psWhitelist, + public Shutdown(String eventName, Vector clients, int updateRate, + long waitTime, Vector psWhitelist, Vector psBlacklist, String gearmanServerAddress, int gearmanServerPort) { this.eventName = eventName; @@ -66,12 +64,9 @@ public class Shutdown extends Thread { psJobs = new HashMap(); pingShutdownTime = new HashMap(); status = new HashMap(); - errors = new HashMap(); - finishedClients = new HashMap(); finished = false; error = false; statusText = new String[14]; - statusText[0] = "The shutdown process of the client has been started."; 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."; @@ -86,11 +81,6 @@ public class Shutdown extends Thread { 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."; - for (HashMap client : clients) { - int clientID = Integer.parseInt(client.get("id")); - status.put(clientID, 0); // no work - finishedClients.put(clientID, false); - } } public void run() { @@ -126,12 +116,12 @@ public class Shutdown extends Thread { private Boolean update() throws IllegalStateException, IOException, InterruptedException, ExecutionException { - for (HashMap client : clients) { - String ipAddress = client.get("ip"); - int clientID = Integer.parseInt(client.get("id")); - int clientStatus = status.get(clientID); + for (Client client : clients) { + String ipAddress = client.getIp(); + int clientID = client.getId(); + int clientState = client.getState(); - switch (clientStatus) { + switch (clientState) { case 0: ping(client); @@ -172,9 +162,8 @@ public class Shutdown extends Thread { } else { System.out.println(ipAddress + " Cannot send the ping message."); - errors - .put(clientID, - "Sending the ping message has been failed."); + client + .setError("Sending the ping message has been failed."); // sending the ping message has been failed status.put(clientID, 12); pingJobs.remove(clientID); @@ -232,9 +221,8 @@ public class Shutdown extends Thread { System.out .println(ipAddress + " Cannot check if a user is logged in."); - errors - .put(clientID, - "The check if a user is logged in has been failed."); + client + .setError("The check if a user is logged in has been failed."); /* * cannot check if a user is logged in, go in * errorState @@ -275,9 +263,8 @@ public class Shutdown extends Thread { } else { System.out.println(ipAddress + " Cannot send shutdown command"); - errors - .put(clientID, - "Sending the shutdown command has been failed."); + client + .setError("Sending the shutdown command has been failed."); /* * cannot send shutdown command, go in / * errorState @@ -342,9 +329,8 @@ public class Shutdown extends Thread { System.out .println(ipAddress + " Cannot send the ping after shutdown message."); - errors - .put(clientID, - "Sending the ping after shutdown message has been failed."); + client + .setError("Sending the ping after shutdown message has been failed."); /* * sending the ping after shutdown message * has been failed @@ -357,8 +343,8 @@ public class Shutdown extends Thread { } else { System.out.println(ipAddress + " is alive after shutdown"); - errors.put(clientID, - "Client is still alive after shutdown."); + client + .setError("Client is still alive after shutdown."); // still alive, go in errorState status.put(clientID, 12); pingShutdownJobs.remove(clientID); @@ -389,7 +375,7 @@ public class Shutdown extends Thread { .parse(result); if (!resultObj.containsKey("err")) { JSONArray ps = (JSONArray) resultObj.get("ps"); - //boolean whitelistFound = false; + // boolean whitelistFound = false; boolean blacklistFound = false; for (String blackEntry : psBlacklist) { @@ -398,11 +384,11 @@ public class Shutdown extends Thread { } } - /*for (String whiteEntry : psWhitelist) { - if (ps.toString().contains(whiteEntry)) { - whitelistFound = true; - } - }*/ + /* + * for (String whiteEntry : psWhitelist) { if + * (ps.toString().contains(whiteEntry)) { + * whitelistFound = true; } } + */ if (blacklistFound) { /* @@ -427,9 +413,8 @@ public class Shutdown extends Thread { } else { System.out.println(ipAddress + " Cannot check if user is working."); - errors - .put(clientID, - "The check if a user is working has been failed."); + client + .setError("The check if a user is working has been failed."); /* * cannot check if user is working, go in * errorState @@ -445,25 +430,25 @@ public class Shutdown extends Thread { case 11: System.out.println(ipAddress + " User has been working"); - errors.put(clientID, "The user has been working."); + client.setError("The user has been working."); // user has been working, go in errorState status.put(clientID, 12); break; case 12: - if (!finishedClients.get(clientID)) { + if (!client.isFinished()) { System.out.println(ipAddress + " Shutdown failed"); // errorState - finishedClients.put(clientID, true); + client.finish(); error = true; } break; case 13: - if (!finishedClients.get(clientID)) { + if (!client.isFinished()) { System.out.println(ipAddress + " Shutdown finished"); // successState - finishedClients.put(clientID, true); + client.finish(); } break; @@ -472,10 +457,8 @@ public class Shutdown extends Thread { } boolean allFinished = false; - for (HashMap client : clients) { - int clientID = Integer.parseInt(client.get("id")); - boolean clientFinished = finishedClients.get(clientID); - if (clientFinished) { + for (Client client : clients) { + if (client.isFinished()) { allFinished = true; } else { allFinished = false; @@ -490,9 +473,9 @@ public class Shutdown extends Thread { } } - private void ping(HashMap client) { - String ipAddress = client.get("ip"); - int clientID = Integer.parseInt(client.get("id")); + private void ping(Client client) { + String ipAddress = client.getIp(); + int clientID = client.getId(); GearmanJob job = GearmanJobImpl.createJob("ping", ipAddress.getBytes(), "ping" + clientID); @@ -504,9 +487,9 @@ public class Shutdown extends Thread { System.out.println("ping " + ipAddress); } - private void who(HashMap client) { - String ipAddress = client.get("ip"); - int clientID = Integer.parseInt(client.get("id")); + private void who(Client client) { + String ipAddress = client.getIp(); + int clientID = client.getId(); GearmanJob job = GearmanJobImpl.createJob("who", ipAddress.getBytes(), "who" + clientID); @@ -518,9 +501,9 @@ public class Shutdown extends Thread { System.out.println("who " + ipAddress); } - private void doShutdown(HashMap client) { - String ipAddress = client.get("ip"); - int clientID = Integer.parseInt(client.get("id")); + private void doShutdown(Client client) { + String ipAddress = client.getIp(); + int clientID = client.getId(); GearmanJob job = GearmanJobImpl.createJob("doShutdown", ipAddress .getBytes(), "doShutdown" + clientID); @@ -532,9 +515,9 @@ public class Shutdown extends Thread { System.out.println("doShutdown " + ipAddress); } - private void pingShutdown(HashMap client) { - String ipAddress = client.get("ip"); - int clientID = Integer.parseInt(client.get("id")); + private void pingShutdown(Client client) { + String ipAddress = client.getIp(); + int clientID = client.getId(); GearmanJob job = GearmanJobImpl.createJob("ping", ipAddress.getBytes(), "ping" + clientID); @@ -546,9 +529,9 @@ public class Shutdown extends Thread { System.out.println("ping " + ipAddress); } - private void ps(HashMap client) { - String ipAddress = client.get("ip"); - int clientID = Integer.parseInt(client.get("id")); + private void ps(Client client) { + String ipAddress = client.getIp(); + int clientID = client.getId(); GearmanJob job = GearmanJobImpl.createJob("ps", ipAddress.getBytes(), "ps" + clientID); @@ -560,18 +543,6 @@ public class Shutdown extends Thread { System.out.println("ps " + ipAddress); } - public String getStatusText(HashMap client) { - int clientID = Integer.parseInt(client.get("id")); - int clientStatus = status.get(clientID); - return statusText[clientStatus]; - } - - public String getError(HashMap client) { - int clientID = Integer.parseInt(client.get("id")); - String clientError = errors.get(clientID); - return clientError; - } - public Boolean isFinished() { return finished && !error; } @@ -580,7 +551,7 @@ public class Shutdown extends Thread { return finished && error; } - public Vector> getClients() { + public Vector getClients() { return clients; } -- cgit v1.2.3-55-g7522