-
Notifications
You must be signed in to change notification settings - Fork 0
/
Host.java
194 lines (137 loc) · 4.64 KB
/
Host.java
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
184
185
186
187
188
189
190
191
192
193
194
package client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.NetworkInterface;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.json.JSONArray;
import org.json.JSONObject;
public class Host implements MqttCallback {
MqttClient client;
String name;
static String BrokerIp;
int cpu;
long ram;
int zone;
int direction;
long latency;
int battery;
public void setlocation(int z, int d){
zone = z;
direction = d;
latency = zone*10;
}
public static void main(String[] args) {
BrokerIp = args[0];
new Host().connect();
}
public void connect() {
try {
Thread t1 = new Geolocator(this);
t1.start();
//get interface and mac
InetAddress ip = InetAddress.getLocalHost();
NetworkInterface interf = NetworkInterface.getByInetAddress(ip);
byte[] mac = interf.getHardwareAddress();
//convert byte array to string (mac)
StringBuilder sb = new StringBuilder(18);
for (byte b : mac) {
if (sb.length() > 0) {
sb.append(':');
}
sb.append(String.format("%02x", b));
}
name = sb.toString();
//System.out.println(name);
MqttConnectOptions options = new MqttConnectOptions();
options.setWill("stats/" + name, "offline".getBytes(), 0, true);
client = new MqttClient("tcp://"+BrokerIp+":1883", name);
client.connect(options);
client.setCallback(this);
client.subscribe("fromBroker/" + name);
client.subscribe("stats/call");
MqttMessage stats = new MqttMessage();
stats.setPayload(getStat().getBytes());
stats.setRetained(true);
MqttTopic pubTopic = client.getTopic("stats/" + name);
Thread.sleep(latency);
pubTopic.publish(stats);
} catch (Exception e) {
e.printStackTrace();
}
}
private String getStat() {
cpu = Runtime.getRuntime().availableProcessors();
ram = Runtime.getRuntime().totalMemory();
return "online-"+cpu+"-"+ram+"-"+zone+"-"+direction+"-"+latency+"-"+battery;
}
public void disconnect() {
try {
client.disconnect();
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void connectionLost(Throwable cause) {
// TODO Auto-generated method stub
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
Thread.sleep(latency);
System.out.println("host received");
if (topic.equals("stats/call")) {
MqttMessage stats = new MqttMessage();
stats.setPayload(getStat().getBytes());
stats.setRetained(true);
MqttTopic pubTopic = client.getTopic("stats/" + name);
Thread.sleep(latency);
pubTopic.publish(stats);
}
else if (topic.equals("fromBroker/" + name)) {
System.out.print("host now publishing");
System.out.print("");
String jsonMsg = new String(message.getPayload(), "US-ASCII");
PrintWriter writer = new PrintWriter("event.json", "US-ASCII");
writer.println(jsonMsg);
writer.close();
JSONObject obj = new JSONObject(jsonMsg);
//System.out.println("reading json");
// TODO maybe is wrong
String id = (obj.getJSONObject("id")).toString();
JSONArray codeArray = new JSONArray();
codeArray = (obj.getJSONObject("execution_script")).getJSONArray("code");
//System.out.println("execution_script: " + (obj.getJSONObject("execution_script")).getJSONArray("code"));
writer = new PrintWriter("script.py", "US-ASCII");
for (int i = 0, size = codeArray.length(); i < size; i++) {
String str = codeArray.getString(i);
writer.println(str);
}
writer.close();
ProcessBuilder pb = new ProcessBuilder("python","script.py");
Process p = pb.start();
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String ret = in.readLine();
MqttMessage answer = new MqttMessage();
String tmp = new String();
tmp = name + "~" + id + "~" + ret;
answer.setPayload(tmp.getBytes());
MqttTopic pubTopic = client.getTopic("toBroker/taskResults");
Thread.sleep(latency);
pubTopic.publish(answer);
//client.publish("toBroker/taskResults", answer);
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
// TODO Auto-generated method stub
}
}