From b4bf46878d699ab2ca7685d2eca89d9843a9af40 Mon Sep 17 00:00:00 2001 From: Shabirmean Date: Thu, 7 Jan 2016 22:23:21 +0530 Subject: [PATCH] Setting the MQTT Queue URL to localhost by default --- ...igitalDisplayMqttCommunicationHandler.java | 1 - .../mgt/iot/controlqueue/mqtt/MqttConfig.java | 23 ++++++++++++++++--- .../carbon/device/mgt/iot/util/ZipUtil.java | 23 +++++++++++++++---- .../main/resources/conf/devicemgt-config.xml | 9 ++++---- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/components/device-mgt-iot-digitaldisplay/org.wso2.carbon.device.mgt.iot.digitaldisplay.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/digitaldisplay/api/util/DigitalDisplayMqttCommunicationHandler.java b/components/device-mgt-iot-digitaldisplay/org.wso2.carbon.device.mgt.iot.digitaldisplay.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/digitaldisplay/api/util/DigitalDisplayMqttCommunicationHandler.java index 154c9cd207..707331d0d7 100644 --- a/components/device-mgt-iot-digitaldisplay/org.wso2.carbon.device.mgt.iot.digitaldisplay.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/digitaldisplay/api/util/DigitalDisplayMqttCommunicationHandler.java +++ b/components/device-mgt-iot-digitaldisplay/org.wso2.carbon.device.mgt.iot.digitaldisplay.controller.api/src/main/java/org/wso2/carbon/device/mgt/iot/digitaldisplay/api/util/DigitalDisplayMqttCommunicationHandler.java @@ -47,7 +47,6 @@ public class DigitalDisplayMqttCommunicationHandler extends MQTTTransportHandler public void run() { while (!isConnected()) { try { - log.info("Trying to Connect.."); connectToQueue(); subscribeToQueue(); diff --git a/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot/src/main/java/org/wso2/carbon/device/mgt/iot/controlqueue/mqtt/MqttConfig.java b/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot/src/main/java/org/wso2/carbon/device/mgt/iot/controlqueue/mqtt/MqttConfig.java index c1e0e407f7..26e86fe6f2 100644 --- a/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot/src/main/java/org/wso2/carbon/device/mgt/iot/controlqueue/mqtt/MqttConfig.java +++ b/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot/src/main/java/org/wso2/carbon/device/mgt/iot/controlqueue/mqtt/MqttConfig.java @@ -28,6 +28,8 @@ public class MqttConfig { private boolean isEnabled; private static final String MQTT_QUEUE_CONFIG_NAME = "MQTT"; + private static final String LOCALHOST = "localhost"; + private static final String PORT_OFFSET_PROPERTY = "portOffset"; private ControlQueue mqttControlQueue; @@ -58,14 +60,29 @@ public class MqttConfig { } private MqttConfig() { - mqttControlQueue = DeviceManagementConfigurationManager.getInstance().getControlQueue( - MQTT_QUEUE_CONFIG_NAME); - mqttQueueEndpoint = mqttControlQueue.getServerURL() + ":" + mqttControlQueue.getPort(); + mqttControlQueue = DeviceManagementConfigurationManager.getInstance().getControlQueue(MQTT_QUEUE_CONFIG_NAME); + + int portOffset = Integer.parseInt(System.getProperty(PORT_OFFSET_PROPERTY)); + String brokerURL = mqttControlQueue.getServerURL(); + + + if (portOffset != 0 && brokerURL.contains(LOCALHOST)) { + // if using the internal MB (meaning URL is localhost and there is a portOffset) + // then increment port accordingly + int mqttPort = mqttControlQueue.getPort(); + mqttPort = mqttPort + portOffset; + mqttQueueEndpoint = mqttControlQueue.getServerURL() + ":" + mqttPort; + + } else { + mqttQueueEndpoint = mqttControlQueue.getServerURL() + ":" + mqttControlQueue.getPort(); + } + mqttQueueUsername = mqttControlQueue.getUsername(); mqttQueuePassword = mqttControlQueue.getPassword(); isEnabled = mqttControlQueue.isEnabled(); } + public static MqttConfig getInstance() { return mqttConfig; } diff --git a/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot/src/main/java/org/wso2/carbon/device/mgt/iot/util/ZipUtil.java b/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot/src/main/java/org/wso2/carbon/device/mgt/iot/util/ZipUtil.java index 9dcbc3fec4..92905be56f 100644 --- a/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot/src/main/java/org/wso2/carbon/device/mgt/iot/util/ZipUtil.java +++ b/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot/src/main/java/org/wso2/carbon/device/mgt/iot/util/ZipUtil.java @@ -33,6 +33,14 @@ import java.util.Map; public class ZipUtil { + private static final String LOCAL_BIND_ADDRESS_PROPERTY = "carbon.local.ip"; + private static final String HTTPS_PORT_PROPERTY = "httpsPort"; + private static final String HTTP_PORT_PROPERTY = "httpPort"; + + private static final String LOCALHOST = "localhost"; + private static final String HTTPS_PROTOCOL_APPENDER = "https://"; + private static final String HTTP_PROTOCOL_APPENDER = "http://"; + public ZipArchive createZipFile(String owner, String tenantDomain, String deviceType, String deviceId, String deviceName, String token, String refreshToken) @@ -48,12 +56,12 @@ public class ZipUtil { String templateSketchPath = sketchFolder + sep + deviceType; String serverName = DeviceManagementConfigurationManager.getInstance().getDeviceManagementServerInfo().getName(); - String iotServerIP = System.getProperty("carbon.local.ip"); // bind.address - String httpsServerPort = System.getProperty("httpsPort"); - String httpServerPort = System.getProperty("httpPort"); + String iotServerIP = System.getProperty(LOCAL_BIND_ADDRESS_PROPERTY); // bind.address + String httpsServerPort = System.getProperty(HTTPS_PORT_PROPERTY); + String httpServerPort = System.getProperty(HTTP_PORT_PROPERTY); - String httpsServerEP = "https://" + iotServerIP + ":" + httpsServerPort; - String httpServerEP = "http://" + iotServerIP + ":" + httpServerPort; + String httpsServerEP = HTTPS_PROTOCOL_APPENDER + iotServerIP + ":" + httpsServerPort; + String httpServerEP = HTTP_PROTOCOL_APPENDER + iotServerIP + ":" + httpServerPort; String apimHost = DeviceManagementConfigurationManager.getInstance().getDeviceCloudMgtConfig().getApiManager() @@ -65,6 +73,11 @@ public class ZipUtil { String apimEndpoint = apimHost + ":" + apimGatewayPort; String mqttEndpoint = MqttConfig.getInstance().getMqttQueueEndpoint(); + + if (mqttEndpoint.contains(LOCALHOST)) { + mqttEndpoint = mqttEndpoint.replace(LOCALHOST, iotServerIP); + } + String xmppEndpoint = XmppConfig.getInstance().getXmppEndpoint(); int indexOfChar = xmppEndpoint.lastIndexOf(":"); diff --git a/features/device-mgt-iot-feature/org.wso2.carbon.device.mgt.iot.feature/src/main/resources/conf/devicemgt-config.xml b/features/device-mgt-iot-feature/org.wso2.carbon.device.mgt.iot.feature/src/main/resources/conf/devicemgt-config.xml index 2afe3668aa..88163ab84e 100644 --- a/features/device-mgt-iot-feature/org.wso2.carbon.device.mgt.iot.feature/src/main/resources/conf/devicemgt-config.xml +++ b/features/device-mgt-iot-feature/org.wso2.carbon.device.mgt.iot.feature/src/main/resources/conf/devicemgt-config.xml @@ -32,7 +32,8 @@ true org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttControlPublisher MQTT - tcp://204.232.188.214 + + tcp://localhost 1883 . . @@ -58,9 +59,9 @@ true https://localhost:9443/oauth2/token - http://localhost - 9763 - 8280 + https://localhost + 9443 + 8243 /api-store/site/blocks/user/login/ajax/login.jag /api-store/site/blocks/subscription/subscription-list/ajax/subscription-list.jag admin