summaryrefslogtreecommitdiff
path: root/same/src
diff options
context:
space:
mode:
Diffstat (limited to 'same/src')
-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();
+ }
+}