summaryrefslogtreecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/Boot.java
blob: 444c55c93299a506601801489c339983fe9f76d1 (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
package ControllerWorker;

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

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

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;

public class Boot extends Thread {
	private Vector<HashMap<String, String>> clients;
	private GearmanClient gearmanClient;
	private final GearmanJobServerConnection gearmanConnection;
	private long beginTime;
	private long timeTaken;
	private long timeLeft;

	static HashMap<Integer, GearmanJob> PING_JOBS = new HashMap<Integer, GearmanJob>();
	static HashMap<Integer, GearmanJob> WOL_JOBS = new HashMap<Integer, GearmanJob>();
	static HashMap<Integer, GearmanJob> SSH_JOBS = new HashMap<Integer, GearmanJob>();
	static HashMap<Integer, Integer> STATUS = new HashMap<Integer, Integer>();
	static final int UPDATE_RATE = 1; // updates per second
	static final long UPDATE_PERIOD = 1000000000L / UPDATE_RATE; // nanoseconds

	public Boot(String serverAddress, int port,
			Vector<HashMap<String, String>> clients) {
		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
		}
	}

	public void run() {
		workerLoop();
	}

	private void workerLoop() {
		while (true) {
			try {
				beginTime = System.nanoTime();
				update();
				timeTaken = System.nanoTime() - beginTime;
				timeLeft = (UPDATE_PERIOD - timeTaken) / 1000000;
				if (timeLeft < 10)
					timeLeft = 10;
				Thread.sleep(timeLeft);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void update() throws IllegalStateException, IOException,
			InterruptedException, ExecutionException {
		for (HashMap<String, String> client : clients) {
			String ipAddress = client.get("ip");
			String macAddress = client.get("mac");
			int clientID = Integer.parseInt(client.get("id"));
			int status = STATUS.get(clientID);
			switch (status) {
			case 0:
				ping(client);
				break;
			case 1:
				GearmanJob pingJob = PING_JOBS.get(clientID);
				if (pingJob != null) {
					GearmanJobStatus jobStatus = gearmanClient
							.getJobStatus(pingJob);
					if (!jobStatus.isKnown() && pingJob.isDone()) {
						GearmanJobResult pingJobRes = pingJob.get();
						String result = ByteUtils.fromUTF8Bytes(pingJobRes
								.getResults());
						if (result != "") {
							JSONObject resultObj = (JSONObject) JSONValue
									.parse(result);
							String alive = resultObj.get("alive").toString();
							if (alive.equals("true")) {
								System.out.println(ipAddress + " alive");
								STATUS.put(clientID, 2); // alive
								PING_JOBS.remove(clientID);
							} else {
								System.out.println(ipAddress + " not alive");
								STATUS.put(clientID, 3); // not alive
								PING_JOBS.remove(clientID);
							}
						}
					}
				}
				break;
			case 2:
				ssh(client);
				break;
			case 3:
				wakeOnLan(client);
				break;
			case 4:
				GearmanJob wolJob = WOL_JOBS.get(clientID);
				if (wolJob != null) {
					GearmanJobStatus jobStatus = gearmanClient
							.getJobStatus(wolJob);
					if (!jobStatus.isKnown() && wolJob.isDone()) {
						GearmanJobResult wolJobRes = wolJob.get();
						String result = ByteUtils.fromUTF8Bytes(wolJobRes
								.getResults());
						if (result.equals("Magic packet send.")) {
							System.out
									.println(macAddress + "Magic packet send");
							STATUS.put(clientID, 5); // magic packet send
							WOL_JOBS.remove(clientID);
						}
					}
				}
				break;
			case 5:
				break;
			case 6:
				break;
			}
		}
	}

	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);
		STATUS.put(clientID, 1); // ping started
		gearmanClient.submit(job);
		PING_JOBS.put(clientID, job);
		System.out.println("ping " + ipAddress);
	}

	private void wakeOnLan(HashMap<String, String> client) {
		String macAddress = client.get("mac");
		int clientID = Integer.parseInt(client.get("id"));
		GearmanJob job = GearmanJobImpl.createJob("wol", macAddress.getBytes(),
				"wol" + clientID);
		STATUS.put(clientID, 4); // wake on lan started
		gearmanClient.submit(job);
		WOL_JOBS.put(clientID, job);
		System.out.println("wake on lan " + macAddress);
	}

	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);
		STATUS.put(clientID, 6); // ssh started
		gearmanClient.submit(job);
		SSH_JOBS.put(clientID, job);
		System.out.println("ssh " + ipAddress);
	}
}