68 lines
1.8 KiB
Java
68 lines
1.8 KiB
Java
import java.nio.charset.StandardCharsets;
|
|
import java.util.UUID;
|
|
|
|
import org.eclipse.paho.mqttv5.client.IMqttAsyncClient;
|
|
import org.eclipse.paho.mqttv5.client.MqttAsyncClient;
|
|
import org.eclipse.paho.mqttv5.client.persist.MemoryPersistence;
|
|
import org.eclipse.paho.mqttv5.common.MqttMessage;
|
|
|
|
void publish(
|
|
IMqttAsyncClient client,
|
|
String topic,
|
|
String content,
|
|
int qos,
|
|
boolean retained)
|
|
throws Exception {
|
|
var message = new MqttMessage(
|
|
content.getBytes(StandardCharsets.UTF_8)
|
|
);
|
|
message.setQos(qos);
|
|
message.setRetained(retained);
|
|
client.publish(topic, message).waitForCompletion();
|
|
}
|
|
|
|
void main(String[] arguments) throws Exception {
|
|
var consoleInUnit = arguments.length > 0 ? arguments[0] : "alice";
|
|
var rot13Unit = arguments.length > 1 ? arguments[1] : "alice";
|
|
var consoleOutUnit = arguments.length > 2 ? arguments[2] : "alice";
|
|
var broker = System.getProperty("mqtt.broker", "tcp://localhost:1883");
|
|
var client = new MqttAsyncClient(
|
|
broker,
|
|
"configure-" + UUID.randomUUID(),
|
|
new MemoryPersistence()
|
|
);
|
|
|
|
client.connect().waitForCompletion();
|
|
|
|
var rot13Intent = """
|
|
subscribe:
|
|
topic: Tutorial/ConsoleIn/U/%s/E/text
|
|
""".formatted(consoleInUnit);
|
|
|
|
var consoleOutIntent = """
|
|
subscribe:
|
|
topic: Tutorial/ROT13/U/%s/E/crypted
|
|
""".formatted(rot13Unit);
|
|
|
|
publish(
|
|
client,
|
|
"Tutorial/ROT13/U/" + rot13Unit + "/I",
|
|
rot13Intent,
|
|
1,
|
|
false
|
|
);
|
|
|
|
publish(
|
|
client,
|
|
"Tutorial/ConsoleOut/U/" + consoleOutUnit + "/I",
|
|
consoleOutIntent,
|
|
1,
|
|
false
|
|
);
|
|
|
|
client.disconnect().waitForCompletion();
|
|
client.close();
|
|
|
|
IO.println("Wired: ConsoleIn -> ROT13 -> ConsoleOut");
|
|
}
|