Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Added methods to find broadcastAddress(es), useful in bindings for discovery. #3747

Merged
merged 2 commits into from
Jun 27, 2017

Conversation

mherwege
Copy link
Contributor

Signed-off-by: Mark Herwege [email protected]

@mherwege
Copy link
Contributor Author

As suggested by @kaikreuzer in review of openHAB Niko Home Control binding #1589.

*/
public static List<String> getAllBroadcastAddresses() {
try {
List<String> broadcastAddresses = new ArrayList<String>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't a LinkedList better if we don't know the size (to reduce reallocations and copies)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced with a LinkedList.

@@ -55,9 +60,53 @@ public static String getLocalIpv4HostAddress() {
}
return hostAddress;
} catch (SocketException ex) {
LOGGER.error("Could not retrieve network interface: " + ex.getMessage(), ex);
LOGGER.error("Could not retrieve network interface: {}", ex.getMessage(), ex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

/**
* Get all broadcast addresses on the current host
*
* @return list of broadcast addresses
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the SocketException is thrown you return null.
So, this should be stated in the JavaDoc.
But wouldn't it make sense to throw the exception, so the caller is notified about the error -- or if the caller cannot handle it at all (does he need to know about the error) return an empty list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning an empty list, and documented.

final List<String> broadcastAddresses = getAllBroadcastAddresses();
for (String current : broadcastAddresses) {
if (broadcastAddress != null) {
LOGGER.warn("Found multiple broadcast addresses - ignoring {}", broadcastAddress);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JavaDoc state "get the first candidate", so I don't see any reason to throw a warning if there are multiple candidates. The function should return the first one and not check if there is more then one, or should it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the same structure as in the getLocalIpv4HostAddress() method. It does not add value, so I removed it and just return the first one, or null if there is none. Null return is documented.

Signed-off-by: Mark Herwege <[email protected]>
* @return list of broadcast addresses, empty list if no broadcast addresses found
*/
public static List<String> getAllBroadcastAddresses() {
List<String> broadcastAddresses = new LinkedList<String>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, shouldn't the formatter replace new LinkedList<String>() be replaced by an Diamond Operator?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, okay, seems it is not activated by the ESH formatter.

* @return list of broadcast addresses, empty list if no broadcast addresses found
*/
public static List<String> getAllBroadcastAddresses() {
List<String> broadcastAddresses = new LinkedList<String>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, okay, seems it is not activated by the ESH formatter.

@maggu2810 maggu2810 changed the title Added methods to find broadcastAddress(es), usuful in bindings for discovery. Added methods to find broadcastAddress(es), useful in bindings for discovery. Jun 27, 2017
@maggu2810
Copy link
Contributor

core and core tests succeeded, the error is not related

@maggu2810 maggu2810 merged commit a015460 into eclipse-archived:master Jun 27, 2017
@kaikreuzer kaikreuzer added this to the 0.9.0 milestone Nov 30, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants