-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGateway.java
82 lines (60 loc) · 1.84 KB
/
Gateway.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
package client;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
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.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
public class Gateway implements MqttCallback {
MqttClient client;
static String BrokerIp;
public static void main(String[] args) {
BrokerIp = args[0];
try {
BufferedReader buffer = new BufferedReader(new FileReader(args[1]));
StringBuilder msg = new StringBuilder();
String line = buffer.readLine();
while (line != null) {
msg.append(line);
msg.append("\n");
line = buffer.readLine();
}
buffer.close();
new Gateway().connect(msg.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public void connect(String msgContent) {
try {
client = new MqttClient("tcp://"+BrokerIp+":1883", "gateway");
client.connect();
client.setCallback(this);
client.subscribe("fromBroker/results");
MqttMessage message = new MqttMessage();
message.setPayload(msgContent.getBytes());
MqttTopic pubTopic = client.getTopic("toBroker/toExec");
pubTopic.publish(message);
//client.publish("toBroker/toExec", message);
} catch (MqttException e) {
e.printStackTrace();
}
}
@Override
public void connectionLost(Throwable cause) {
// TODO Auto-generated method stub
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
if (topic.equals("fromBroker/results")) {
System.out.println(message);
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
// TODO Auto-generated method stub
}
}