diff --git a/lc-esp-sdk/src/main/java/lc/esp/sdk/ESPDestination.java b/lc-esp-sdk/src/main/java/lc/esp/sdk/ESPDestination.java new file mode 100644 index 0000000000000000000000000000000000000000..494c5793e469b9d0f10613284ba52fb884488dc2 --- /dev/null +++ b/lc-esp-sdk/src/main/java/lc/esp/sdk/ESPDestination.java @@ -0,0 +1,66 @@ +package lc.esp.sdk; + +import lc.mecha.util.StringAccumulatorV2; + +import javax.jms.Destination; +import javax.jms.IllegalStateException; +import javax.jms.JMSException; +import javax.jms.Session; +import java.util.Locale; + +/** + * This class holds information about a semantic destination in the ESP messaging fabric. + * + * @author Alex Leigh + * @since mk18 (GIPSY DANGER) + */ +public class ESPDestination { + private final String domain; + private final String service; + private final String name; + private final ESPServiceClass serviceClass; + private final ESPDestinationClass destClass; + + public ESPDestination(String domain, String service, String name, ESPServiceClass serviceClass, ESPDestinationClass destClass) { + this.domain = domain; + this.service = service; + this.name = name; + this.serviceClass = serviceClass; + this.destClass = destClass; + } + + /** + * Returns this destination in OpenWire format. Note that the OpenWire address does not include the service + * class, so the caller will need to deal with that appropriately. + */ + public String toOpenWireAddress() { + StringAccumulatorV2 sa = new StringAccumulatorV2("."); + sa.push(domain.toUpperCase(Locale.ROOT)); + sa.push(service.toUpperCase(Locale.ROOT)); + sa.push(name.toUpperCase(Locale.ROOT)); + sa.push(destClass.toString().toUpperCase(Locale.ROOT)); + return sa.asString(); + } + + /** + * Return this destination in STOMP format. + */ + public String toStompAddress() { + StringAccumulatorV2 sa = new StringAccumulatorV2("/"); + sa.push(serviceClass.name().toLowerCase(Locale.ROOT)); + sa.push(domain.toLowerCase(Locale.ROOT)); + sa.push(service.toLowerCase(Locale.ROOT)); + sa.push(name.toLowerCase(Locale.ROOT)); + sa.push(destClass.toString().toUpperCase(Locale.ROOT)); + return sa.asString(); + } + + public Destination toDestination(Session session) throws JMSException { + return switch (serviceClass) { + case QUEUE -> session.createQueue(toOpenWireAddress()); + case TOPIC -> session.createTopic(toOpenWireAddress()); + default -> throw new IllegalStateException("Unknown service class."); + }; + } +} + diff --git a/lc-esp-sdk/src/main/java/lc/esp/sdk/ESPDestinationClass.java b/lc-esp-sdk/src/main/java/lc/esp/sdk/ESPDestinationClass.java new file mode 100644 index 0000000000000000000000000000000000000000..5f5386d053ced2d2fee6fa30d8c8a03b37b98ae6 --- /dev/null +++ b/lc-esp-sdk/src/main/java/lc/esp/sdk/ESPDestinationClass.java @@ -0,0 +1,21 @@ +package lc.esp.sdk; + +/** + * This enum contains valid destination classes. It is capable of converting the symantic meaning to the correct + * abbreviation for ESP18 addresses. + * + * @author Alex Leigh + * @since mk18 (GIPSY DANGER) + */ +public enum ESPDestinationClass { + TELEMETRY { + public String toString() { + return "tlm"; + } + }, + COMMAND { + public String toString() { + return "cmd"; + } + } +} \ No newline at end of file diff --git a/lc-esp-sdk/src/main/java/lc/esp/sdk/ESPServiceClass.java b/lc-esp-sdk/src/main/java/lc/esp/sdk/ESPServiceClass.java new file mode 100644 index 0000000000000000000000000000000000000000..d65443f985955c03b61262a0252f9e0030ae933e --- /dev/null +++ b/lc-esp-sdk/src/main/java/lc/esp/sdk/ESPServiceClass.java @@ -0,0 +1,5 @@ +package lc.esp.sdk; + +public enum ESPServiceClass { + TOPIC, QUEUE +}