summaryrefslogtreecommitdiff
path: root/same/src/main/java
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-03-02 16:10:05 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-03-02 16:10:05 +0100
commit7992e3dae79f7c8fe95a926dc6a38aa0c539e53b (patch)
treecdbbea7ea0bdddd44200bab97173088761367288 /same/src/main/java
parentbe139aca1d1239d38898f7549ecde78b43b03b1f (diff)
Add missing discovery package.
Diffstat (limited to 'same/src/main/java')
-rw-r--r--same/src/main/java/com/orbekk/same/discovery/DirectoryService.java22
-rw-r--r--same/src/main/java/com/orbekk/same/discovery/DiscoveryListener.java5
-rw-r--r--same/src/main/java/com/orbekk/same/discovery/DiscoveryService.java52
3 files changed, 79 insertions, 0 deletions
diff --git a/same/src/main/java/com/orbekk/same/discovery/DirectoryService.java b/same/src/main/java/com/orbekk/same/discovery/DirectoryService.java
new file mode 100644
index 0000000..fae2bf6
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/discovery/DirectoryService.java
@@ -0,0 +1,22 @@
+package com.orbekk.same.discovery;
+
+import java.util.List;
+
+/**
+ * Maintains a registry of available networks.
+ *
+ * The discovery service is only meant to be used for debugging.
+ */
+public interface DirectoryService {
+ /**
+ * Returns a list of network names and master urls interleaved, i.e.,
+ *
+ * [NetworkName1, MasterUrl1, ...]
+ */
+ List<String> getNetworks() throws Exception;
+
+ /**
+ * Register a network.
+ */
+ void registerNetwork(String networkName, String masterUrl) throws Exception;
+}
diff --git a/same/src/main/java/com/orbekk/same/discovery/DiscoveryListener.java b/same/src/main/java/com/orbekk/same/discovery/DiscoveryListener.java
new file mode 100644
index 0000000..e006c77
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/discovery/DiscoveryListener.java
@@ -0,0 +1,5 @@
+package com.orbekk.same.discovery;
+
+public interface DiscoveryListener {
+ void discover(String url);
+}
diff --git a/same/src/main/java/com/orbekk/same/discovery/DiscoveryService.java b/same/src/main/java/com/orbekk/same/discovery/DiscoveryService.java
new file mode 100644
index 0000000..a1147cc
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/discovery/DiscoveryService.java
@@ -0,0 +1,52 @@
+package com.orbekk.same.discovery;
+
+import java.net.DatagramPacket;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.orbekk.net.BroadcastListener;
+
+public class DiscoveryService extends Thread {
+ private Logger logger = LoggerFactory.getLogger(getClass());
+ BroadcastListener broadcastListener;
+ DiscoveryListener listener;
+
+ public DiscoveryService(DiscoveryListener listener,
+ BroadcastListener broadcastListener) {
+ this.listener = listener;
+ this.broadcastListener = broadcastListener;
+ }
+
+ @Override
+ public void run() {
+ logger.info("DiscoveryService starting.");
+ while (!Thread.interrupted()) {
+ DatagramPacket packet = broadcastListener.listen();
+ if (packet == null) {
+ // An error or interrupt occurred.
+ continue;
+ }
+ String content = new String(packet.getData(), 0, packet.getLength());
+ String[] words = content.split(" ");
+
+ if (!content.startsWith("Discover") || words.length < 2) {
+ logger.warn("Invalid discovery message: {}", content);
+ continue;
+ }
+
+ String url = words[1];
+ logger.info("Received discovery from {}", url);
+ if (listener != null) {
+ listener.discover(url);
+ }
+ }
+ logger.info("DiscoveryService stopped.");
+ }
+
+ @Override public void interrupt() {
+ logger.info("Interrupt()");
+ super.interrupt();
+ broadcastListener.interrupt();
+ }
+}