This repository has been archived by the owner on May 7, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Option to choose default network interface
Fixes #3884 Signed-off-by: Stefan Triller <stefan.triller@telekom.de>
- Loading branch information
1 parent
545a6c3
commit c355177
Showing
9 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...onfig.core/OSGI-INF/org.eclipse.smarthome.config.core.net.NetworkConfigOptionProvider.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.smarthome.config.core.net.NetworkConfigOptionProvider"> | ||
<service> | ||
<provide interface="org.eclipse.smarthome.config.core.ConfigOptionProvider"/> | ||
</service> | ||
<implementation class="org.eclipse.smarthome.config.core.net.NetworkConfigOptionProvider"/> | ||
</scr:component> |
81 changes: 81 additions & 0 deletions
81
...core/src/main/java/org/eclipse/smarthome/config/core/net/NetworkConfigOptionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright (c) 2014-2017 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.smarthome.config.core.net; | ||
|
||
import java.net.Inet6Address; | ||
import java.net.InetAddress; | ||
import java.net.NetworkInterface; | ||
import java.net.SocketException; | ||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import org.eclipse.smarthome.config.core.ConfigOptionProvider; | ||
import org.eclipse.smarthome.config.core.ParameterOption; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Provides a list of IPv4 addresses of the local machine and shows the user which interface belongs to which IP address | ||
* | ||
* @author Stefan Triller - initial contribution | ||
* | ||
*/ | ||
@Component | ||
public class NetworkConfigOptionProvider implements ConfigOptionProvider { | ||
|
||
static final URI CONFIG_URI = URI.create("system:network"); | ||
static final String PARAM_DEFAULT_INTERFACE = "defaultInterface"; | ||
|
||
private final Logger logger = LoggerFactory.getLogger(NetworkConfigOptionProvider.class); | ||
|
||
@Override | ||
public Collection<ParameterOption> getParameterOptions(URI uri, String param, Locale locale) { | ||
if (!uri.equals(CONFIG_URI)) { | ||
return null; | ||
} | ||
|
||
if (param.equals(PARAM_DEFAULT_INTERFACE)) { | ||
return getInterfaces(); | ||
} | ||
return null; | ||
} | ||
|
||
private List<ParameterOption> getInterfaces() { | ||
ArrayList<ParameterOption> interfaceOptions = new ArrayList<>(); | ||
try { | ||
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); | ||
while (interfaces.hasMoreElements()) { | ||
final NetworkInterface current = interfaces.nextElement(); | ||
if (!current.isUp() || current.isLoopback() || current.isVirtual()) { | ||
continue; | ||
} | ||
final Enumeration<InetAddress> addresses = current.getInetAddresses(); | ||
while (addresses.hasMoreElements()) { | ||
final InetAddress current_addr = addresses.nextElement(); | ||
if (current_addr.isLoopbackAddress() || (current_addr instanceof Inet6Address)) { | ||
continue; | ||
} | ||
|
||
String ipv4Address = current_addr.getHostAddress(); | ||
ParameterOption po = new ParameterOption(ipv4Address, ipv4Address + " (" + current.getName() + ")"); | ||
interfaceOptions.add(po); | ||
} | ||
} | ||
return interfaceOptions; | ||
} catch (SocketException ex) { | ||
logger.error("Could not retrieve network interface: {}", ex.getMessage(), ex); | ||
return null; | ||
} | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
bundles/core/org.eclipse.smarthome.core/ESH-INF/config/networkinterface.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<config-description:config-descriptions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:config-description="http://eclipse.org/smarthome/schemas/config-description/v1.0.0" | ||
xsi:schemaLocation="http://eclipse.org/smarthome/schemas/config-description/v1.0.0 | ||
http://eclipse.org/smarthome/schemas/config-description-1.0.0.xsd"> | ||
|
||
<config-description uri="system:network"> | ||
<parameter name="defaultInterface" type="text"> | ||
<label>Default Interface</label> | ||
<description><![CDATA[ | ||
<p>The default network interface to be used.</p> | ||
]]> | ||
</description> | ||
</parameter> | ||
</config-description> | ||
|
||
</config-description:config-descriptions> |
10 changes: 10 additions & 0 deletions
10
bundles/core/org.eclipse.smarthome.core/OSGI-INF/org.eclipse.smarthome.network.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" modified="modified" name="org.eclipse.smarthome.network"> | ||
<property name="service.config.description.uri" value="system:network"/> | ||
<property name="service.config.label" value="Network Settings"/> | ||
<property name="service.config.category" value="system"/> | ||
<service> | ||
<provide interface="org.eclipse.smarthome.core.net.NetworkInterfaceService"/> | ||
</service> | ||
<implementation class="org.eclipse.smarthome.core.net.NetworkInterfaceServiceImpl"/> | ||
</scr:component> |
25 changes: 25 additions & 0 deletions
25
....smarthome.core/src/main/java/org/eclipse/smarthome/core/net/NetworkInterfaceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Copyright (c) 2014-2017 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.smarthome.core.net; | ||
|
||
/** | ||
* Service that provides information about local network interfaces | ||
* | ||
* @author Stefan Triller - initial contribution | ||
* | ||
*/ | ||
public interface NetworkInterfaceService { | ||
|
||
/** | ||
* Obtain the local IPv4 address | ||
* | ||
* @return IPv4 address as string | ||
*/ | ||
String getLocalIpv4HostAddress(); | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...rthome.core/src/main/java/org/eclipse/smarthome/core/net/NetworkInterfaceServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright (c) 2014-2017 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.smarthome.core.net; | ||
|
||
import java.util.Dictionary; | ||
import java.util.Map; | ||
|
||
import org.osgi.service.component.ComponentContext; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Modified; | ||
|
||
/** | ||
* Service that provides the current configured IPv4 address of the system | ||
* | ||
* @author Stefan Triller - initial contribution | ||
* | ||
*/ | ||
@Component(name = "org.eclipse.smarthome.network", property = { "service.config.description.uri=system:network", | ||
"service.config.label=Network Settings", "service.config.category=system" }) | ||
public class NetworkInterfaceServiceImpl implements NetworkInterfaceService { | ||
|
||
private static final String DEFAULT_INTERFACE = "defaultInterface"; | ||
|
||
private String defaultInterface; | ||
|
||
@SuppressWarnings("unchecked") | ||
protected void activate(ComponentContext componentContext) { | ||
Dictionary<String, Object> props = componentContext.getProperties(); | ||
modified((Map<String, Object>) props); | ||
} | ||
|
||
@Modified | ||
public synchronized void modified(Map<String, Object> config) { | ||
String defaultInterfaceConfig = (String) config.get(DEFAULT_INTERFACE); | ||
if (defaultInterfaceConfig == null) { | ||
// if none is specified we return the default one for backward compatibility | ||
defaultInterface = NetUtil.getLocalIpv4HostAddress(); | ||
} else { | ||
defaultInterface = (String) config.get(DEFAULT_INTERFACE); | ||
} | ||
} | ||
|
||
@Override | ||
public String getLocalIpv4HostAddress() { | ||
return defaultInterface; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters