summaryrefslogtreecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/Shutdown.java
blob: 6cb8645b3f30be3e244a8cedb52d80c12d48c321 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package ControllerWorker;

import java.io.IOException;
import java.lang.Thread;
import java.util.concurrent.ExecutionException;
import java.util.HashMap;
import java.util.Vector;

import org.gearman.client.GearmanClient;
import org.gearman.client.GearmanClientImpl;
import org.gearman.client.GearmanJob;
import org.gearman.client.GearmanJobImpl;
import org.gearman.client.GearmanJobResult;
import org.gearman.client.GearmanJobStatus;
import org.gearman.common.GearmanJobServerConnection;
import org.gearman.common.GearmanNIOJobServerConnection;
import org.gearman.util.ByteUtils;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class Shutdown extends Thread {
	private long beginTime;
	private long timeTaken;
	private long timeLeft;
	private Boolean finished;
	private Boolean error;
	private final int updateRate;
	private final long updatePeriod;

	private HashMap<Integer, GearmanJob> pingJobs;
	private HashMap<Integer, GearmanJob> wolJobs;
	private HashMap<Integer, GearmanJob> sshJobs;
	private HashMap<Integer, Integer> status;
	private HashMap<Integer, Integer> errors;

	private Vector<HashMap<String, String>> clients;

	private final GearmanJobServerConnection gearmanConnection;
	private GearmanClient gearmanClient;

	public Shutdown(String serverAddress, int port,
			Vector<HashMap<String, String>> clients, int updateRate) {
		this.pingJobs = new HashMap<Integer, GearmanJob>();
		this.wolJobs = new HashMap<Integer, GearmanJob>();
		this.sshJobs = new HashMap<Integer, GearmanJob>();
		this.status = new HashMap<Integer, Integer>();
		this.errors = new HashMap<Integer, Integer>();
		this.updateRate = updateRate; // updates per second
		this.updatePeriod = 1000000000L / this.updateRate; // nanoseconds
		this.gearmanConnection = new GearmanNIOJobServerConnection(
				serverAddress, port);
		this.gearmanClient = new GearmanClientImpl();
		gearmanClient.addJobServer(this.gearmanConnection);
		this.clients = clients;
		for (HashMap<String, String> client : clients) {
			int clientID = Integer.parseInt(client.get("id"));
			status.put(clientID, 0); // no work
		}
		this.finished = false;
		this.error = false;
	}

	public void run() {
		workerLoop();
	}

	private void workerLoop() {
		Boolean run = true;
		while (run) {
			try {
				beginTime = System.nanoTime();
				run = update();
				timeTaken = System.nanoTime() - beginTime;
				timeLeft = (updatePeriod - timeTaken) / 1000000;
				if (timeLeft < 10)
					timeLeft = 10;
				Thread.sleep(timeLeft);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		this.finished = true;
		System.out.println("Booting finished");
	}

	private Boolean update() throws IllegalStateException, IOException,
			InterruptedException, ExecutionException {
		Boolean allFinished = false;

		for (HashMap<String, String> client : clients) {
			String ipAddress = client.get("ip");
			String macAddress = client.get("mac");
			int clientID = Integer.parseInt(client.get("id"));
			int clientStatus = status.get(clientID);

			switch (clientStatus) {
			}
			if (clientStatus == 20) {
				allFinished = true;
			} else if (clientStatus == 19) {
				allFinished = true;
				this.error = true;
			} else {
				allFinished = false;
			}
		}

		if (allFinished) {
			return false;
		} else {
			return true;
		}
	}

	private void ping(HashMap<String, String> client) {
		String ipAddress = client.get("ip");
		int clientID = Integer.parseInt(client.get("id"));

		GearmanJob job = GearmanJobImpl.createJob("ping", ipAddress.getBytes(),
				"ping" + clientID);
		gearmanClient.submit(job);

		status.put(clientID, 1); // ping started
		pingJobs.put(clientID, job);

		System.out.println("ping " + ipAddress);
	}

	private void ssh(HashMap<String, String> client) {
		String ipAddress = client.get("ip");
		int clientID = Integer.parseInt(client.get("id"));

		GearmanJob job = GearmanJobImpl.createJob("ssh", ipAddress.getBytes(),
				"ssh" + clientID);
		gearmanClient.submit(job);

		status.put(clientID, 6); // ssh started
		sshJobs.put(clientID, job);

		System.out.println("ssh " + ipAddress);
	}

	public Vector<HashMap<String, String>> getClients() {
		return clients;
	}

	public GearmanClient getGearmanClient() {
		return gearmanClient;
	}

	public GearmanJobServerConnection getGearmanConnection() {
		return gearmanConnection;
	}

	public HashMap<Integer, GearmanJob> getPingJobs() {
		return pingJobs;
	}

	public HashMap<Integer, GearmanJob> getWolJobs() {
		return wolJobs;
	}

	public HashMap<Integer, GearmanJob> getSshJobs() {
		return sshJobs;
	}

	public HashMap<Integer, Integer> getStatus() {
		return status;
	}

	public HashMap<Integer, Integer> getErrors() {
		return errors;
	}

	public Boolean isFinished() {
		return finished;
	}

	public Boolean isFinishedWithErrors() {
		return finished && error;
	}
}