From 08418bd3c3eaf8b3c9095780e7efb02f11d72d70 Mon Sep 17 00:00:00 2001 From: Björn Geiger Date: Tue, 13 Sep 2011 11:33:26 +0200 Subject: jobs und times werden nun im Client gespeichert --- .../controllerWorker/ControllerWorker/Boot.java | 146 ++++++++------------- .../controllerWorker/ControllerWorker/Client.java | 44 ++++++- .../ControllerWorker/ClientJob.java | 2 - .../ControllerWorker/Shutdown.java | 83 ++++-------- 4 files changed, 127 insertions(+), 148 deletions(-) diff --git a/gearman/controllerWorker/ControllerWorker/Boot.java b/gearman/controllerWorker/ControllerWorker/Boot.java index bbed8ae..6bc5c74 100755 --- a/gearman/controllerWorker/ControllerWorker/Boot.java +++ b/gearman/controllerWorker/ControllerWorker/Boot.java @@ -3,7 +3,6 @@ package ControllerWorker; import java.io.IOException; import java.lang.Thread; import java.util.Date; -import java.util.HashMap; import java.util.StringTokenizer; import java.util.Vector; @@ -24,7 +23,6 @@ import org.json.simple.JSONValue; import org.apache.log4j.Logger; public class Boot extends Thread { - static final Logger logger = Logger.getLogger(Boot.class); private String eventName; @@ -37,10 +35,6 @@ public class Boot extends Thread { private final GearmanJobServerConnection gearmanConnection; private GearmanClient gearmanClient; - private Vector jobs; - private HashMap pingWolTime; - private HashMap pingRestartShutdownTime; - private HashMap pingRestartBootTime; private Boolean finished; private Boolean error; @@ -59,10 +53,6 @@ public class Boot extends Thread { gearmanServerAddress, gearmanServerPort); gearmanClient = new GearmanClientImpl(); gearmanClient.addJobServer(this.gearmanConnection); - jobs = new Vector(); - pingWolTime = new HashMap(); - pingRestartShutdownTime = new HashMap(); - pingRestartBootTime = new HashMap(); finished = false; error = false; } @@ -137,7 +127,7 @@ public class Boot extends Thread { case MAGIC_PACKET_SENT: date = new Date(); timestamp = date.getTime(); - pingWolTime.put(client.getId(), timestamp); + client.addPingTime(ClientPingTime.WOL, timestamp); pingWakeOnLan(client); break; @@ -190,7 +180,7 @@ public class Boot extends Thread { case RESTART_COMMAND_SENT: date = new Date(); timestamp = date.getTime(); - pingRestartShutdownTime.put(client.getId(), timestamp); + client.addPingTime(ClientPingTime.RESTARTSHUTDOWN, timestamp); pingRestartShutdown(client); break; @@ -215,7 +205,7 @@ public class Boot extends Thread { case CLIENT_IS_DOWN: date = new Date(); timestamp = date.getTime(); - pingRestartBootTime.put(client.getId(), timestamp); + client.addPingTime(ClientPingTime.RESTARTBOOT, timestamp); pingRestartBoot(client); break; @@ -275,13 +265,12 @@ public class Boot extends Thread { .getBytes(), "ping" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_PING_PROGRESS); - addClientJob(client.getId(), ClientJob.PINGJOB, job); + client.addJob(ClientJob.PINGJOB, job); logger.info("ping " + client.getIp()); } private void checkPingProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.PINGJOB); - GearmanJob pingJob = job.getGearmanJob(); + GearmanJob pingJob = client.getJob(ClientJob.PINGJOB); if (pingJob != null) { GearmanJobStatus jobStatus = gearmanClient.getJobStatus(pingJob); @@ -296,11 +285,11 @@ public class Boot extends Thread { if (alive.equals("true")) { logger.info(client.getIp() + " alive"); client.setState(ClientState.CLIENT_IS_ALIVE); - removeClientJob(job); + client.removeJob(pingJob); } else if (alive.equals("false")) { logger.info(client.getIp() + " not alive"); client.setState(ClientState.CLIENT_NOT_ALIVE); - removeClientJob(job); + client.removeJob(pingJob); } } else { logger.error(client.getIp() @@ -308,7 +297,7 @@ public class Boot extends Thread { client .setError("Sending the ping message has been failed."); client.setState(ClientState.BOOT_ERROR); - removeClientJob(job); + client.removeJob(pingJob); } } } @@ -320,20 +309,20 @@ public class Boot extends Thread { .getBytes(), "ping" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_PING_WOL_PROGRESS); - addClientJob(client.getId(), ClientJob.PINGJOB, job); + client.addJob(ClientJob.PINGJOB, job); logger.info("ping " + client.getIp()); } private void checkPingWolProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.PINGJOB); - GearmanJob pingJobWoL = job.getGearmanJob(); + GearmanJob pingJobWoL = client.getJob(ClientJob.PINGJOB); if (pingJobWoL != null) { Date currentDate = new Date(); Long currentTimestamp = currentDate.getTime(); // wait 2 min until WoL - Failed - Long expectedTimestamp = pingWolTime.get(client.getId()) + waitTime; + Long expectedTimestamp = client.getPingTime(ClientPingTime.WOL) + + waitTime; if (expectedTimestamp >= currentTimestamp) { GearmanJobStatus jobStatus = gearmanClient .getJobStatus(pingJobWoL); @@ -351,11 +340,11 @@ public class Boot extends Thread { + " is alive after WoL"); // alive, go in successState client.setState(ClientState.BOOT_SUCCESS); - removeClientJob(job); + client.removeJob(pingJobWoL); } else if (alive.equals("false")) { logger.info("ping again " + client.getIp()); client.setState(ClientState.PING_WOL_AGAIN); - removeClientJob(job); + client.removeJob(pingJobWoL); } } else { logger @@ -368,7 +357,7 @@ public class Boot extends Thread { * been failed, go to errorState */ client.setState(ClientState.BOOT_ERROR); - removeClientJob(job); + client.removeJob(pingJobWoL); } } } @@ -377,7 +366,7 @@ public class Boot extends Thread { client.setError("The wake on LAN has been failed."); // not alive, go in errorState client.setState(ClientState.BOOT_ERROR); - removeClientJob(job); + client.removeJob(pingJobWoL); } } } @@ -387,20 +376,19 @@ public class Boot extends Thread { .getBytes(), "ping" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_PING_RESTART_SHUTDOWN_PROGRESS); - addClientJob(client.getId(), ClientJob.PINGJOB, job); + client.addJob(ClientJob.PINGJOB, job); logger.info("ping " + client.getIp()); } private void checkPingRestartShutdownProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.PINGJOB); - GearmanJob pingJobRestartShutdown = job.getGearmanJob(); + GearmanJob pingJobRestartShutdown = client.getJob(ClientJob.PINGJOB); if (pingJobRestartShutdown != null) { Date currentDate = new Date(); Long currentTimestamp = currentDate.getTime(); // wait 2 min until Restart - Failed - Long expectedTimestamp = pingRestartShutdownTime - .get(client.getId()) + Long expectedTimestamp = client + .getPingTime(ClientPingTime.RESTARTSHUTDOWN) + waitTime; if (expectedTimestamp >= currentTimestamp) { GearmanJobStatus jobStatus = gearmanClient @@ -420,12 +408,12 @@ public class Boot extends Thread { // still alive, ping again client .setState(ClientState.PING_RESTART_SHUTDOWN_AGAIN); - removeClientJob(job); + client.removeJob(pingJobRestartShutdown); } else if (alive.equals("false")) { logger.info(client.getIp() + " is down"); // not alive, ping again client.setState(ClientState.CLIENT_IS_DOWN); - removeClientJob(job); + client.removeJob(pingJobRestartShutdown); } } else { logger @@ -438,7 +426,7 @@ public class Boot extends Thread { * failed */ client.setState(ClientState.BOOT_ERROR); - removeClientJob(job); + client.removeJob(pingJobRestartShutdown); } } } @@ -447,7 +435,7 @@ public class Boot extends Thread { client.setError("The shutdown has been failed."); // still alive, go in errorState client.setState(ClientState.BOOT_ERROR); - removeClientJob(job); + client.removeJob(pingJobRestartShutdown); } } } @@ -457,20 +445,20 @@ public class Boot extends Thread { .getBytes(), "ping" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_PING_RESTART_BOOT_PROGRESS); - addClientJob(client.getId(), ClientJob.PINGJOB, job); + client.addJob(ClientJob.PINGJOB, job); logger.info("ping " + client.getIp()); } private void checkPingRestartBootProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.PINGJOB); - GearmanJob pingJobRestartBoot = job.getGearmanJob(); + GearmanJob pingJobRestartBoot = client.getJob(ClientJob.PINGJOB); if (pingJobRestartBoot != null) { Date currentDate = new Date(); Long currentTimestamp = currentDate.getTime(); // wait 2 min until Restart - Failed - Long expectedTimestamp = pingRestartBootTime.get(client.getId()) + Long expectedTimestamp = client + .getPingTime(ClientPingTime.RESTARTBOOT) + waitTime; if (expectedTimestamp >= currentTimestamp) { GearmanJobStatus jobStatus = gearmanClient @@ -490,13 +478,13 @@ public class Boot extends Thread { + " is alive after restart"); // alive, go to success state client.setState(ClientState.BOOT_SUCCESS); - removeClientJob(job); + client.removeJob(pingJobRestartBoot); } else if (alive.equals("false")) { logger.info("ping again " + client.getIp()); // not alive, ping again client .setState(ClientState.PING_RESTART_BOOT_AGAIN); - removeClientJob(job); + client.removeJob(pingJobRestartBoot); } } else { logger @@ -517,7 +505,7 @@ public class Boot extends Thread { client.setError("The reboot has been failed."); // not alive, go in errorState client.setState(ClientState.BOOT_ERROR); - removeClientJob(job); + client.removeJob(pingJobRestartBoot); } } } @@ -527,14 +515,13 @@ public class Boot extends Thread { .getBytes(), "wol" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_WAKE_ON_LAN_PROGRESS); - addClientJob(client.getId(), ClientJob.WOLJOB, job); + client.addJob(ClientJob.WOLJOB, job); logger.info("wake on lan " + client.getMac()); } private void checkWakeOnLanProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.WOLJOB); - GearmanJob wolJob = job.getGearmanJob(); + GearmanJob wolJob = client.getJob(ClientJob.WOLJOB); if (wolJob != null) { GearmanJobStatus jobStatus = gearmanClient.getJobStatus(wolJob); @@ -545,14 +532,14 @@ public class Boot extends Thread { if (result.equals("Magic packet send.")) { logger.info(client.getMac() + " Magic packet sent."); client.setState(ClientState.MAGIC_PACKET_SENT); - removeClientJob(job); + client.removeJob(wolJob); } else { logger.error(client.getIp() + " Cannot send magic packet."); client .setError("Sending the magic packet has been failed."); // cannot send magic packet, go in errorState client.setState(ClientState.BOOT_ERROR); - removeClientJob(job); + client.removeJob(wolJob); } } } @@ -563,14 +550,13 @@ public class Boot extends Thread { .getBytes(), "os" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_CHECKOS_PROGRESS); - addClientJob(client.getId(), ClientJob.OSJOB, job); + client.addJob(ClientJob.OSJOB, job); logger.info("check OS " + client.getIp()); } private void checkCheckosProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.OSJOB); - GearmanJob osJob = job.getGearmanJob(); + GearmanJob osJob = client.getJob(ClientJob.OSJOB); if (osJob != null) { GearmanJobStatus jobStatus = gearmanClient.getJobStatus(osJob); @@ -594,11 +580,11 @@ public class Boot extends Thread { logger.info(client.getIp() + " right OS"); // right os, go to successState client.setState(ClientState.BOOT_SUCCESS); - removeClientJob(job); + client.removeJob(osJob); } else { logger.info(client.getIp() + " wrong OS"); client.setState(ClientState.WRONG_OS); - removeClientJob(job); + client.removeJob(osJob); } } else { logger.error(client.getIp() + " Cannot check os"); @@ -606,7 +592,7 @@ public class Boot extends Thread { .setError("The check for correct operating system has been failed."); // cannot check os, go in errorState client.setState(ClientState.BOOT_ERROR); - removeClientJob(job); + client.removeJob(osJob); } } } @@ -618,13 +604,12 @@ public class Boot extends Thread { .getBytes(), "who" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_WHO_PROGRESS); - addClientJob(client.getId(), ClientJob.WHOJOB, job); + client.addJob(ClientJob.WHOJOB, job); logger.info("who " + client.getIp()); } private void checkWhoProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.WHOJOB); - GearmanJob whoJob = job.getGearmanJob(); + GearmanJob whoJob = client.getJob(ClientJob.WHOJOB); if (whoJob != null) { GearmanJobStatus jobStatus = gearmanClient.getJobStatus(whoJob); @@ -652,13 +637,13 @@ public class Boot extends Thread { // ----- // didn´t work in test-pool, check ps client.setState(ClientState.A_USER_IS_LOGGED_IN); - removeClientJob(job); + client.removeJob(whoJob); } else { logger .info(client.getIp() + " a user is logged in"); client.setState(ClientState.A_USER_IS_LOGGED_IN); - removeClientJob(job); + client.removeJob(whoJob); } } else { logger.error(client.getIp() @@ -670,7 +655,7 @@ public class Boot extends Thread { */ // client client.setState(ClientState.BOOT_ERROR); - removeClientJob(job); + client.removeJob(whoJob); } } } @@ -682,13 +667,12 @@ public class Boot extends Thread { .getBytes(), "ps" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_PS_PROGRESS); - addClientJob(client.getId(), ClientJob.PSJOB, job); + client.addJob(ClientJob.PSJOB, job); logger.info("ps " + client.getIp()); } private void checkPsProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.PSJOB); - GearmanJob psJob = job.getGearmanJob(); + GearmanJob psJob = client.getJob(ClientJob.PSJOB); if (psJob != null) { GearmanJobStatus jobStatus = gearmanClient.getJobStatus(psJob); @@ -721,16 +705,16 @@ public class Boot extends Thread { * if (whitelistFound) { logger.info(client.getIp() * + " is not working"); // is not working * status.put(client.getId(), 13); - * removeClientJob(job); } else { + * client.removeClientJob(job); } else { */ logger.info(client.getIp() + " is working"); client.setState(ClientState.USER_IS_WORKING); - removeClientJob(job); + client.removeJob(psJob); // } } else { // user is not working, doing restart client.setState(ClientState.RESTART_CLIENT); - removeClientJob(job); + client.removeJob(psJob); } } else { logger.error(client.getIp() @@ -741,7 +725,7 @@ public class Boot extends Thread { * cannot check if user is working, go in errorState */ client.setState(ClientState.BOOT_ERROR); - removeClientJob(job); + client.removeJob(psJob); } } } @@ -753,14 +737,13 @@ public class Boot extends Thread { .getBytes(), "restart" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_RESTART_PROGRESS); - addClientJob(client.getId(), ClientJob.RESTARTJOB, job); + client.addJob(ClientJob.RESTARTJOB, job); logger.info("restart " + client.getIp()); } private void checkRestartProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.RESTARTJOB); - GearmanJob restartJob = job.getGearmanJob(); + GearmanJob restartJob = client.getJob(ClientJob.RESTARTJOB); if (restartJob != null) { GearmanJobStatus jobStatus = gearmanClient.getJobStatus(restartJob); @@ -773,7 +756,7 @@ public class Boot extends Thread { if (!resultObj.containsKey("err")) { logger.info(client.getIp() + " Restart command sent"); client.setState(ClientState.RESTART_COMMAND_SENT); - removeClientJob(job); + client.removeJob(restartJob); } else { logger.error(client.getIp() + " Cannot send restart command"); @@ -781,30 +764,13 @@ public class Boot extends Thread { .setError("Sending the restart command has been failed."); // cannot send restart command, go in errorState client.setState(ClientState.BOOT_ERROR); - removeClientJob(job); + client.removeJob(restartJob); } } } } } - private ClientJob getClientJob(int clientID, int jobType) { - for (ClientJob job : jobs) { - if (job.getJobType() == jobType && job.getClientID() == clientID) { - return job; - } - } - return null; - } - - private void addClientJob(int clientID, int jobType, GearmanJob gearmanJob) { - jobs.add(new ClientJob(clientID, jobType, gearmanJob)); - } - - private void removeClientJob(ClientJob clientJob) { - jobs.remove(clientJob); - } - public Boolean isFinished() { return finished && !error; } @@ -820,4 +786,4 @@ public class Boot extends Thread { public String getEventName() { return eventName; } -} +} \ No newline at end of file diff --git a/gearman/controllerWorker/ControllerWorker/Client.java b/gearman/controllerWorker/ControllerWorker/Client.java index 114c6ef..de252df 100755 --- a/gearman/controllerWorker/ControllerWorker/Client.java +++ b/gearman/controllerWorker/ControllerWorker/Client.java @@ -1,5 +1,8 @@ package ControllerWorker; +import java.util.Vector; + +import org.gearman.client.GearmanJob; import org.json.simple.JSONObject; public class Client { @@ -7,6 +10,8 @@ public class Client { private String ip; private String mac; private ClientState state; + private Vector times; + private Vector jobs; private String error; private Boolean finished; @@ -17,6 +22,43 @@ public class Client { finished = false; error = ""; setState(ClientState.CLIENT_UNKNOWN); + times = new Vector(); + jobs = new Vector(); + } + + public GearmanJob getJob(int jobType) { + for (ClientJob job : jobs) { + if (job.getJobType() == jobType) { + return job.getGearmanJob(); + } + } + return null; + } + + public void addJob(int jobType, GearmanJob gearmanJob) { + jobs.add(new ClientJob(id, jobType, gearmanJob)); + } + + public void removeJob(GearmanJob gearmanJob) { + for (ClientJob job : jobs) { + if (job.getGearmanJob().equals(gearmanJob)) { + jobs.remove(job); + break; + } + } + } + + public long getPingTime(int timeType) { + for (ClientPingTime time : times) { + if (time.getType() == timeType) { + return time.getTime(); + } + } + return 0; + } + + public void addPingTime(int timeType, long time) { + times.add(new ClientPingTime(id, timeType, time)); } public int getId() { @@ -46,7 +88,7 @@ public class Client { public void setState(ClientState state_) { state = state_; } - + public void setError(String error) { this.error = error; } diff --git a/gearman/controllerWorker/ControllerWorker/ClientJob.java b/gearman/controllerWorker/ControllerWorker/ClientJob.java index f9e4bbd..c80cb78 100755 --- a/gearman/controllerWorker/ControllerWorker/ClientJob.java +++ b/gearman/controllerWorker/ControllerWorker/ClientJob.java @@ -1,7 +1,5 @@ package ControllerWorker; -import java.util.HashMap; - import org.gearman.client.GearmanJob; public class ClientJob { diff --git a/gearman/controllerWorker/ControllerWorker/Shutdown.java b/gearman/controllerWorker/ControllerWorker/Shutdown.java index 69fe5b0..0395cfb 100755 --- a/gearman/controllerWorker/ControllerWorker/Shutdown.java +++ b/gearman/controllerWorker/ControllerWorker/Shutdown.java @@ -3,7 +3,6 @@ package ControllerWorker; import java.io.IOException; import java.lang.Thread; import java.util.Date; -import java.util.HashMap; import java.util.StringTokenizer; import java.util.Vector; @@ -24,7 +23,6 @@ import org.json.simple.JSONValue; import org.apache.log4j.Logger; public class Shutdown extends Thread { - static final Logger logger = Logger.getLogger(Shutdown.class); private String eventName; @@ -36,8 +34,6 @@ public class Shutdown extends Thread { private final GearmanJobServerConnection gearmanConnection; private GearmanClient gearmanClient; - private Vector jobs; - private HashMap pingShutdownTime; private Boolean finished; private Boolean error; @@ -55,8 +51,6 @@ public class Shutdown extends Thread { gearmanServerAddress, gearmanServerPort); gearmanClient = new GearmanClientImpl(); gearmanClient.addJobServer(gearmanConnection); - jobs = new Vector(); - pingShutdownTime = new HashMap(); finished = false; error = false; } @@ -133,7 +127,7 @@ public class Shutdown extends Thread { case SHUTDOWN_COMMAND_SENT: Date date = new Date(); Long timestamp = date.getTime(); - pingShutdownTime.put(client.getId(), timestamp); + client.addPingTime(ClientPingTime.SHUTDOWN, timestamp); pingShutdown(client); break; @@ -210,13 +204,12 @@ public class Shutdown extends Thread { .getBytes(), "ping" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_PING_PROGRESS); - addClientJob(client.getId(), ClientJob.PINGJOB, job); + client.addJob(ClientJob.PINGJOB, job); logger.info("ping " + client.getIp()); } private void checkPingProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.PINGJOB); - GearmanJob pingJob = job.getGearmanJob(); + GearmanJob pingJob = client.getJob(ClientJob.PINGJOB); if (pingJob != null) { GearmanJobStatus jobStatus = gearmanClient.getJobStatus(pingJob); @@ -236,12 +229,12 @@ public class Shutdown extends Thread { client.setState(ClientState.CLIENT_IS_ALIVE); // check // Users - removeClientJob(job); + client.removeJob(pingJob); } else if (alive.equals("false")) { logger.info(client.getIp() + " not alive"); // not alive, go in successState client.setState(ClientState.SHUTDOWN_SUCCESS); - removeClientJob(job); + client.removeJob(pingJob); } } else { logger.error(client.getIp() @@ -250,7 +243,7 @@ public class Shutdown extends Thread { .setError("Sending the ping message has been failed."); // sending the ping message has been failed client.setState(ClientState.SHUTDOWN_ERROR); - removeClientJob(job); + client.removeJob(pingJob); } } } @@ -262,13 +255,12 @@ public class Shutdown extends Thread { .getBytes(), "who" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_WHO_PROGRESS); - addClientJob(client.getId(), ClientJob.WHOJOB, job); + client.addJob(ClientJob.WHOJOB, job); logger.info("who " + client.getIp()); } private void checkWhoProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.WHOJOB); - GearmanJob whoJob = job.getGearmanJob(); + GearmanJob whoJob = client.getJob(ClientJob.WHOJOB); if (whoJob != null) { GearmanJobStatus jobStatus = gearmanClient.getJobStatus(whoJob); @@ -296,14 +288,14 @@ public class Shutdown extends Thread { // ----- // didn´t work in test-pool, check ps client.setState(ClientState.USER_IS_LOGGED_IN); - removeClientJob(job); + client.removeJob(whoJob); } else { logger .info(client.getIp() + " a user is logged in"); // a user is logged in client.setState(ClientState.USER_IS_LOGGED_IN); - removeClientJob(job); + client.removeJob(whoJob); } } else { logger.error(client.getIp() @@ -314,7 +306,7 @@ public class Shutdown extends Thread { * cannot check if a user is logged in, go in errorState */ client.setState(ClientState.SHUTDOWN_ERROR); - removeClientJob(job); + client.removeJob(whoJob); } } } @@ -327,14 +319,13 @@ public class Shutdown extends Thread { .getBytes(), "doShutdown" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_SHUTDOWN_PROGRESS); - addClientJob(client.getId(), ClientJob.SHUTDOWNJOB, job); + client.addJob(ClientJob.SHUTDOWNJOB, job); logger.info("doShutdown " + client.getIp()); } private void checkShutdownProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.SHUTDOWNJOB); - GearmanJob doShutdownJob = job.getGearmanJob(); + GearmanJob doShutdownJob = client.getJob(ClientJob.SHUTDOWNJOB); if (doShutdownJob != null) { GearmanJobStatus jobStatus = gearmanClient @@ -348,7 +339,7 @@ public class Shutdown extends Thread { if (!resultObj.containsKey("err")) { logger.info(client.getIp() + " Shutdown command sent"); client.setState(ClientState.SHUTDOWN_COMMAND_SENT); - removeClientJob(job); + client.removeJob(doShutdownJob); } else { logger.error(client.getIp() + " Cannot send shutdown command"); @@ -356,7 +347,7 @@ public class Shutdown extends Thread { .setError("Sending the shutdown command has been failed."); // cannot send shutdown command, go in / errorState client.setState(ClientState.SHUTDOWN_ERROR); - removeClientJob(job); + client.removeJob(doShutdownJob); } } } @@ -368,20 +359,20 @@ public class Shutdown extends Thread { .getBytes(), "ping" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_PING_SHUTDOWN_PROGRESS); - addClientJob(client.getId(), ClientJob.PINGJOB, job); + client.addJob(ClientJob.PINGJOB, job); logger.info("ping " + client.getIp()); } private void checkPingShutdwonProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.PINGJOB); - GearmanJob pingJobShutdown = job.getGearmanJob(); + GearmanJob pingJobShutdown = client.getJob(ClientJob.PINGJOB); if (pingJobShutdown != null) { Date currentDate = new Date(); Long currentTimestamp = currentDate.getTime(); // wait 2 min until shutdown - Long expectedTimestamp = pingShutdownTime.get(client.getId()) + Long expectedTimestamp = client + .getPingTime(ClientPingTime.SHUTDOWN) + waitTime; if (expectedTimestamp >= currentTimestamp) { GearmanJobStatus jobStatus = gearmanClient @@ -399,7 +390,7 @@ public class Shutdown extends Thread { logger.info(client.getIp() + " is not alive anymore"); client.setState(ClientState.SHUTDOWN_SUCCESS); - removeClientJob(job); + client.removeJob(pingJobShutdown); } else if (alive.equals("true")) { logger .info(client.getIp() @@ -418,7 +409,7 @@ public class Shutdown extends Thread { * failed */ client.setState(ClientState.SHUTDOWN_ERROR); - removeClientJob(job); + client.removeJob(pingJobShutdown); } } } @@ -427,7 +418,7 @@ public class Shutdown extends Thread { client.setError("Client is still alive after shutdown."); // still alive, go in errorState client.setState(ClientState.SHUTDOWN_ERROR); - removeClientJob(job); + client.removeJob(pingJobShutdown); } } } @@ -437,13 +428,12 @@ public class Shutdown extends Thread { .getBytes(), "ps" + client.getId()); gearmanClient.submit(job); client.setState(ClientState.CHECK_PS_PROGRESS); - addClientJob(client.getId(), ClientJob.PSJOB, job); + client.addJob(ClientJob.PSJOB, job); logger.info("ps " + client.getIp()); } private void checkPsProgress(Client client) throws Exception, IOException { - ClientJob job = getClientJob(client.getId(), ClientJob.PSJOB); - GearmanJob psJob = job.getGearmanJob(); + GearmanJob psJob = client.getJob(ClientJob.PSJOB); if (psJob != null) { GearmanJobStatus jobStatus = gearmanClient.getJobStatus(psJob); @@ -476,18 +466,18 @@ public class Shutdown extends Thread { * if (whitelistFound) { logger.info(client.getIp() * + " is not working"); // is not working * status.put(client.getId(), 4); - * removeClientJob(job); + * client.removeClientJob(job); * * } else { */ client.setState(ClientState.USER_IS_WORKING); - removeClientJob(job); + client.removeJob(psJob); // } } else { logger.info(client.getIp() + " is not working"); // is not working client.setState(ClientState.SHUTDOWN_CLIENT); - removeClientJob(job); + client.removeJob(psJob); } } else { logger.error(client.getIp() @@ -498,30 +488,13 @@ public class Shutdown extends Thread { * cannot check if user is working, go in errorState */ client.setState(ClientState.SHUTDOWN_ERROR); - removeClientJob(job); + client.removeJob(psJob); } } } } } - private ClientJob getClientJob(int clientID, int jobType) { - for (ClientJob job : jobs) { - if (job.getJobType() == jobType && job.getClientID() == clientID) { - return job; - } - } - return null; - } - - private void addClientJob(int clientID, int jobType, GearmanJob gearmanJob) { - jobs.add(new ClientJob(clientID, jobType, gearmanJob)); - } - - private void removeClientJob(ClientJob clientJob) { - jobs.remove(clientJob); - } - public Boolean isFinished() { return finished && !error; } -- cgit v1.2.3-55-g7522