Skip to content

Commit

Permalink
Some review fixes regarding PR #14 (issue #13), async Bridge discoverer
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroOne3010 committed Jan 6, 2020
1 parent 30f7bc8 commit 9a46d5d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ First, import the classes from this library:
[//]: # (imports)
```java
import io.github.zeroone3010.yahueapi.*;
import io.github.zeroone3010.yahueapi.discovery.*;
```

### Initializing the API with a connection to the Bridge
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.zeroone3010.yahueapi.discovery;

public interface HueBridgeDiscovererAsync extends Runnable{
/**
* An interface for an automatic asynchronous Hue Bridge discovery.
*/
public interface HueBridgeDiscovererAsync extends Runnable {
void onBridgeDiscovered(String ip);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import static java.nio.charset.StandardCharsets.UTF_8;

public abstract class UpnpDiscoverer implements HueBridgeDiscovererAsync{
public abstract class UPnPDiscoverer implements HueBridgeDiscovererAsync {
private static final Logger logger = Logger.getLogger("UPnPDiscoverer");

private static final long SSDP_REQUEST_TIMER = 10;
private static final long SSDP_REQUEST_TIMER_INTERVAL_SECONDS = 10;
private static InetAddress multicastAddress;
private static final int port = 1900;
private static final int PORT = 1900;
private MulticastSocket socket;
private ScheduledExecutorService ssdpRequestSender;
private ScheduledFuture requestSendTask;
private final DatagramPacket requestPacket;
private final Set<String> ips = new HashSet<>();
private DiscoverState state = DiscoverState.IDLE;

public UpnpDiscoverer() throws IOException {
public UPnPDiscoverer() throws IOException {
multicastAddress = InetAddress.getByName("239.255.255.250");
requestPacket = createRequestPacket();
}
Expand All @@ -38,11 +40,11 @@ public void run() {
state = DiscoverState.SEARCHING;
}
while (state == DiscoverState.SEARCHING) {
byte[] buffer = new byte[8192];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
final byte[] buffer = new byte[8192];
final DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try {
socket.receive(packet);
} catch (IOException e) {
} catch (final IOException e) {
if (state == DiscoverState.STOPPED) {
return;
} else {
Expand All @@ -51,7 +53,6 @@ public void run() {
}
packetHandler(packet);
}

}

private void startSocket() {
Expand All @@ -70,14 +71,14 @@ private void scheduleMessages() {
if (state == DiscoverState.SEARCHING) {
ssdpRequestSender = Executors.newSingleThreadScheduledExecutor();
requestSendTask = ssdpRequestSender.scheduleAtFixedRate(() -> {
System.out.println("sending discover message");
logger.info("Sending discover message");
try {
socket.send(requestPacket);
} catch (IOException e) {
state = DiscoverState.CRASHED;
e.printStackTrace();
}
}, 0, SSDP_REQUEST_TIMER, TimeUnit.SECONDS);
}, 0, SSDP_REQUEST_TIMER_INTERVAL_SECONDS, TimeUnit.SECONDS);
}
}

Expand All @@ -86,18 +87,18 @@ public void stop() {
if (requestSendTask != null) {
ssdpRequestSender.shutdown();
}
if ( socket != null && !socket.isClosed()) {
if (socket != null && !socket.isClosed()) {
socket.close();
}
}

private void packetHandler(DatagramPacket packet) {
String data = new String(packet.getData());
final String data = new String(packet.getData());
if (data.contains("IpBridge")) {
int startIndex = data.indexOf("http://");
int endIndex = data.indexOf("/description.xml");
final int startIndex = data.indexOf("http://");
final int endIndex = data.indexOf("/description.xml");
if (startIndex != -1 && endIndex != -1) {
String ip = data.substring(startIndex + 7, endIndex);
final String ip = data.substring(startIndex + 7, endIndex);
if (ips.add(ip)) {
onBridgeDiscovered(ip);
}
Expand All @@ -110,13 +111,13 @@ public Set<String> getIps() {
}

private DatagramPacket createRequestPacket() {
StringBuilder sb = new StringBuilder("M-SEARCH * HTTP/1.1\r\n");
sb.append("HOST: " + multicastAddress.getHostAddress() + ":" + port + "\r\n");
final StringBuilder sb = new StringBuilder("M-SEARCH * HTTP/1.1\r\n");
sb.append("HOST: " + multicastAddress.getHostAddress() + ":" + PORT + "\r\n");
sb.append("MAN: ssdp:discover\r\n");
sb.append("MX: 3\r\n");
sb.append("USER-AGENT: Resourcepool SSDP Client\r\n");
sb.append("ST: ssdp:all\r\n");
byte[] content = sb.toString().getBytes(UTF_8);
return new DatagramPacket(content, content.length, multicastAddress, port);
final byte[] content = sb.toString().getBytes(UTF_8);
return new DatagramPacket(content, content.length, multicastAddress, PORT);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.github.zeroone3010.yahueapi;
package io.github.zeroone3010.yahueapi.discovery;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.github.zeroone3010.yahueapi.HueBridge;
import io.github.zeroone3010.yahueapi.discovery.NUPnPDiscoverer.UPnPDeserializer;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.zeroone3010.yahueapi;
package io.github.zeroone3010.yahueapi.discovery;

import io.github.zeroone3010.yahueapi.HueBridge;
import io.github.zeroone3010.yahueapi.discovery.NUPnPDiscoverer;

import java.util.Collection;
Expand Down

0 comments on commit 9a46d5d

Please sign in to comment.