Skip to content

Commit

Permalink
This should fix the #2080 (#2159)
Browse files Browse the repository at this point in the history
* Use network interface names instead of their hardware description

* formatting

* formatting

* make this PR backwards-compatible

* Update description

* make it more general

* Update comment

* do not check the backwards compatibility for empty string

* Fix the interface name recognition

* removed unused imports

* removed not used method

* Use better argument name
  • Loading branch information
valib committed Jul 12, 2020
1 parent 5355cec commit ad850a8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/main/external-resources/UMS.conf
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ single_instance =

# Force networking on interface
# -----------------------------
# Specifies the (physical) network interface to attach to, should only be
# Specifies the network interface to attach to, should only be
# relevant when the server has more than one network interface and UMS picks
# the wrong one. The selector displays all available network interfaces.
# E.g. network_interface = eth0
# E.g. network_interface = Intel(R) Dual Band Wireless-AC 3160
# NOTE: Do not change it directly here but use the selector in the UMS GUI.
# Default: "", which means UMS will automatically select a network interface.
network_interface =

Expand Down
58 changes: 49 additions & 9 deletions src/main/java/net/pms/network/NetworkConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.*;
import net.pms.PMS;
import net.pms.configuration.PmsConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -84,11 +85,12 @@ public String getShortName() {
}

/**
* Returns the display name of the interface association.
* Returns the display name of the interface association
* with IP address if exists.
*
* @return The name.
*/
public String getDisplayName() {
public String getDisplayNameWithAddress() {
String displayName = iface.getDisplayName();

if (displayName != null) {
Expand All @@ -103,7 +105,24 @@ public String getDisplayName() {

return displayName;
}


/**
* Returns the display name of the interface association.
*
* @return The name.
*/
public String getDisplayName() {
String displayName = iface.getDisplayName();

if (displayName != null) {
displayName = displayName.trim();
} else {
displayName = iface.getName();
}

return displayName;
}

@Override
public String toString() {
return "InterfaceAssociation(addr=" + addr + ", iface=" + iface + ", parent=" + parentName + ')';
Expand Down Expand Up @@ -287,7 +306,7 @@ private void checkNetworkInterface(NetworkInterface networkInterface, String par
LOGGER.trace("found {} -> {}", networkInterface.getName(), address.getHostAddress());
final InterfaceAssociation ia = new InterfaceAssociation(address, networkInterface, parentName);
interfaces.add(ia);
mainAddress.put(networkInterface.getName(), ia);
mainAddress.put(networkInterface.getDisplayName(), ia);
foundAddress = true;
}
} else {
Expand All @@ -303,16 +322,18 @@ private void checkNetworkInterface(NetworkInterface networkInterface, String par
}
}


/**
* Returns the list of discovered interface names.
* Returns the list of user friendly names of interfaces with their IP
* address.
*
* @return The interface names.
* @return The list of names.
*/
public List<String> getKeys() {
public List<String> getDisplayNamesWithAddress() {
List<String> result = new ArrayList<>(interfaces.size());

for (InterfaceAssociation i : interfaces) {
result.add(i.getShortName());
result.add(i.getDisplayNameWithAddress());
}

return result;
Expand All @@ -328,7 +349,7 @@ public List<String> getDisplayNames() {
List<String> result = new ArrayList<>(interfaces.size());

for (InterfaceAssociation i : interfaces) {
result.add(i.getDisplayName());
result.add(i.getDisplayName());
}

return result;
Expand Down Expand Up @@ -386,6 +407,8 @@ private InterfaceAssociation getFirstInterfaceWithAddress() {
* @return The IP address.
*/
public InterfaceAssociation getAddressForNetworkInterfaceName(String name) {
// for backwards-compatibility check if the short network interface name is used
name = replaceShortInterfaceNameByDisplayName(name);
return mainAddress.get(name);
}

Expand Down Expand Up @@ -463,4 +486,21 @@ public static synchronized NetworkConfiguration getInstance() {
public static synchronized void forgetConfiguration() {
config = null;
}

/**
* for backwards-compatibility check if the short network interface name is used
*
* @return the standard display name
*/
public String replaceShortInterfaceNameByDisplayName(String interfaceName) {
if (StringUtils.isNotBlank(interfaceName)) {
for (InterfaceAssociation netInterface : interfaces) {
if (netInterface.getShortName().equals(interfaceName)) {
interfaceName = netInterface.getDisplayName();
break;
}
}
}
return interfaceName;
}
}
11 changes: 7 additions & 4 deletions src/main/java/net/pms/newgui/GeneralTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,15 @@ public void keyReleased(KeyEvent e) {

final KeyedComboBoxModel<String, String> networkInterfaces = createNetworkInterfacesModel();
networkinterfacesCBX = new JComboBox<>(networkInterfaces);
networkInterfaces.setSelectedKey(configuration.getNetworkInterface());
String savedNetworkInterface = configuration.getNetworkInterface();
// for backwards-compatibility check if the short network interface name is used
savedNetworkInterface = NetworkConfiguration.getInstance().replaceShortInterfaceNameByDisplayName(savedNetworkInterface);
networkInterfaces.setSelectedKey(savedNetworkInterface);
networkinterfacesCBX.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
configuration.setNetworkInterface((String) networkInterfaces.getSelectedKey());
configuration.setNetworkInterface(networkInterfaces.getSelectedKey());
}
}
});
Expand Down Expand Up @@ -612,8 +615,8 @@ public void actionPerformed(ActionEvent e) {
}

private KeyedComboBoxModel<String, String> createNetworkInterfacesModel() {
List<String> keys = NetworkConfiguration.getInstance().getKeys();
List<String> names = NetworkConfiguration.getInstance().getDisplayNames();
List<String> keys = NetworkConfiguration.getInstance().getDisplayNames();
List<String> names = NetworkConfiguration.getInstance().getDisplayNamesWithAddress();
keys.add(0, "");
names.add(0, "");
return new KeyedComboBoxModel<>(
Expand Down

0 comments on commit ad850a8

Please sign in to comment.