|
| 1 | +package com.bencodez.simpleapi.servercomm.mqtt; |
| 2 | + |
| 3 | +import java.util.UUID; |
| 4 | +import java.util.concurrent.ConcurrentHashMap; |
| 5 | +import java.util.concurrent.Executors; |
| 6 | +import java.util.concurrent.ScheduledExecutorService; |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | + |
| 9 | +/** |
| 10 | + * General-purpose MQTT communication handler with support for RPC and pub/sub. |
| 11 | + */ |
| 12 | +public class MqttHandler { |
| 13 | + |
| 14 | + public interface MessageHandler { |
| 15 | + void onMessage(String topic, String payload); |
| 16 | + } |
| 17 | + |
| 18 | + public interface RpcCallback { |
| 19 | + void onComplete(RpcResponse response, Exception error); |
| 20 | + } |
| 21 | + |
| 22 | + public static class RpcResponse { |
| 23 | + private final String requestId; |
| 24 | + private final String payload; |
| 25 | + |
| 26 | + public RpcResponse(String requestId, String payload) { |
| 27 | + this.requestId = requestId; |
| 28 | + this.payload = payload; |
| 29 | + } |
| 30 | + |
| 31 | + public String getRequestId() { |
| 32 | + return requestId; |
| 33 | + } |
| 34 | + |
| 35 | + public String getPayload() { |
| 36 | + return payload; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private final MqttServerComm mqtt; |
| 41 | + private final ConcurrentHashMap<String, RpcCallback> pendingRpcs = new ConcurrentHashMap<>(); |
| 42 | + private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); |
| 43 | + private int defaultQos; |
| 44 | + |
| 45 | + public MqttHandler(MqttServerComm mqtt, int defaultQos) { |
| 46 | + this.mqtt = mqtt; |
| 47 | + setDefaultQos(defaultQos); |
| 48 | + } |
| 49 | + |
| 50 | + public MqttHandler(MqttServerComm mqtt) { |
| 51 | + this(mqtt, 2); |
| 52 | + } |
| 53 | + |
| 54 | + public void setDefaultQos(int qos) { |
| 55 | + if (qos < 0 || qos > 2) { |
| 56 | + throw new IllegalArgumentException("QoS must be 0, 1, or 2"); |
| 57 | + } |
| 58 | + this.defaultQos = qos; |
| 59 | + } |
| 60 | + |
| 61 | + public void connect() throws Exception { |
| 62 | + mqtt.connect(); |
| 63 | + } |
| 64 | + |
| 65 | + public void disconnect() throws Exception { |
| 66 | + mqtt.disconnect(); |
| 67 | + scheduler.shutdownNow(); |
| 68 | + } |
| 69 | + |
| 70 | + public boolean isConnected() { |
| 71 | + return mqtt.isConnected(); |
| 72 | + } |
| 73 | + |
| 74 | + public void publish(String topic, String message) throws Exception { |
| 75 | + mqtt.publish(topic, message, defaultQos, false); |
| 76 | + } |
| 77 | + |
| 78 | + public void subscribe(String topicFilter, MessageHandler handler) throws Exception { |
| 79 | + mqtt.subscribe(topicFilter, defaultQos, (topic, msg) -> { |
| 80 | + handler.onMessage(topic, new String(msg.getPayload())); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + public void unsubscribe(String topicFilter) throws Exception { |
| 85 | + mqtt.unsubscribe(topicFilter); |
| 86 | + } |
| 87 | + |
| 88 | + public void request(String topic, String message, long timeoutMillis, RpcCallback callback) throws Exception { |
| 89 | + String requestId = UUID.randomUUID().toString(); |
| 90 | + pendingRpcs.put(requestId, callback); |
| 91 | + scheduler.schedule(() -> { |
| 92 | + RpcCallback cb = pendingRpcs.remove(requestId); |
| 93 | + if (cb != null) { |
| 94 | + cb.onComplete(null, new Exception("RPC timeout")); |
| 95 | + } |
| 96 | + }, timeoutMillis, TimeUnit.MILLISECONDS); |
| 97 | + |
| 98 | + mqtt.publish(topic + "/" + requestId, message, defaultQos, false); |
| 99 | + } |
| 100 | + |
| 101 | + public void handleRpcResponse(String topic, String payload) { |
| 102 | + String[] parts = topic.split("/"); |
| 103 | + if (parts.length == 0) return; |
| 104 | + String requestId = parts[parts.length - 1]; |
| 105 | + RpcCallback cb = pendingRpcs.remove(requestId); |
| 106 | + if (cb != null) { |
| 107 | + cb.onComplete(new RpcResponse(requestId, payload), null); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments