You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ze...@apache.org on 2022/06/02 13:40:58 UTC

[incubator-streampipes] branch dev updated: [STREAMPIPES-541] Add method to set ip for development

This is an automated email from the ASF dual-hosted git repository.

zehnder pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new 255468371 [STREAMPIPES-541] Add method to set ip for development
255468371 is described below

commit 255468371f2eaf4717dca4ac472ceba46e2e7300
Author: Philipp Zehnder <ze...@fzi.de>
AuthorDate: Thu Jun 2 15:40:16 2022 +0200

    [STREAMPIPES-541] Add method to set ip for development
---
 .idea/runConfigurations/all_extensions_jvm.xml     |  1 -
 .idea/runConfigurations/backend.xml                |  1 -
 .../streampipes/commons/networking/Networking.java | 37 ++++++++++++++++++++--
 3 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/.idea/runConfigurations/all_extensions_jvm.xml b/.idea/runConfigurations/all_extensions_jvm.xml
index 9e7a24f74..20063d047 100644
--- a/.idea/runConfigurations/all_extensions_jvm.xml
+++ b/.idea/runConfigurations/all_extensions_jvm.xml
@@ -9,7 +9,6 @@
       <env name="SP_DATA_LAKE_HOST" value="localhost" />
       <env name="SP_DATA_LAKE_PORT" value="8086" />
       <env name="SP_IMAGE_STORAGE_LOCATION" value=".streampipes/spImages/" />
-      <env name="SP_HOST" value="10.0.61.59" />
     </envs>
     <option name="MAIN_CLASS_NAME" value="org.apache.streampipes.extensions.all.jvm.AllExtensionsInit" />
     <module name="streampipes-extensions-all-jvm" />
diff --git a/.idea/runConfigurations/backend.xml b/.idea/runConfigurations/backend.xml
index e80cb7f07..ff3c77297 100644
--- a/.idea/runConfigurations/backend.xml
+++ b/.idea/runConfigurations/backend.xml
@@ -14,7 +14,6 @@
       <env name="SP_JMS_HOST" value="localhost" />
       <env name="SP_DEBUG" value="true " />
       <env name="SP_PRIORITIZED_PROTOCOL" value="kafka" />
-      <env name="SP_HOST" value="10.0.61.59" />
     </envs>
     <method v="2">
       <option name="Make" enabled="true" />
diff --git a/streampipes-commons/src/main/java/org/apache/streampipes/commons/networking/Networking.java b/streampipes-commons/src/main/java/org/apache/streampipes/commons/networking/Networking.java
index 489a5575c..bb9834993 100644
--- a/streampipes-commons/src/main/java/org/apache/streampipes/commons/networking/Networking.java
+++ b/streampipes-commons/src/main/java/org/apache/streampipes/commons/networking/Networking.java
@@ -21,13 +21,15 @@ import org.apache.streampipes.commons.constants.Envs;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.net.InetAddress;
-import java.net.UnknownHostException;
+import java.io.IOException;
+import java.net.*;
 
 public class Networking {
 
   private static final Logger LOG = LoggerFactory.getLogger(Networking.class);
 
+  private static final String DEFAULT_LOCALHOST_IP = "127.0.0.1";
+
   public static String getHostname() throws UnknownHostException {
     String selectedAddress;
     if (Envs.SP_HOST.exists()) {
@@ -35,12 +37,43 @@ public class Networking {
       LOG.info("Using IP from provided environment variable {}: {}", Envs.SP_HOST, selectedAddress);
     } else {
       selectedAddress = InetAddress.getLocalHost().getHostAddress();
+
+      // this condition is only used as a workaround when the IP address was not derived correctly
+      // when this also does not work, you must set the environment variable SP_HOST manually
+      if (selectedAddress.equals(DEFAULT_LOCALHOST_IP)) {
+        selectedAddress = getIpAddressForOsx();
+      }
+
       LOG.info("Using auto-discovered IP: {}", selectedAddress);
     }
 
     return selectedAddress;
   }
 
+  /**
+   * this method is a workaround for developers using osx
+   * in OSX InetAddress.getLocalHost().getHostAddress() always returns 127.0.0.1
+   * as a workaround developers must manually set the SP_HOST environment variable with the actual ip
+   * with this method the IP is set automatically
+   *
+   * @return IP
+   */
+  private static String getIpAddressForOsx() {
+
+    Socket socket = new Socket();
+    String result = DEFAULT_LOCALHOST_IP;
+    try {
+      socket.connect(new InetSocketAddress("streampipes.apache.org", 80));
+      result = socket.getLocalAddress().getHostAddress();
+      socket.close();
+    } catch (IOException e) {
+      LOG.error(e.getMessage());
+      LOG.error("IP address was not set automatically. Use the environment variable SP_HOST to set it manually.");
+    }
+
+    return result;
+  }
+
   public static Integer getPort(Integer defaultPort) {
     Integer selectedPort;
     if (Envs.SP_PORT.exists()) {