summaryrefslogtreecommitdiff
path: root/discovery
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-03-02 13:58:09 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-03-02 13:58:09 +0100
commit4a4dc574fcb44f5d92b488de1efeba40d1e6469f (patch)
treeccbd3fdccbf5bbf22c696d8f35231bf55df4f711 /discovery
parent6572275c069699c753c906078580d7c507e120c6 (diff)
Begin implementing discovery service.
Diffstat (limited to 'discovery')
-rw-r--r--discovery/pom.xml110
-rw-r--r--discovery/src/main/java/com/orbekk/same/discovery/DiscoveryService.java22
-rw-r--r--discovery/src/main/java/com/orbekk/same/discovery/DiscoveryServiceImpl.java5
-rw-r--r--discovery/src/main/java/com/orbekk/same/discovery/NetworkEntry.java30
-rw-r--r--discovery/src/test/java/com/orbekk/same/discovery/NetworkEntryTest.java25
5 files changed, 192 insertions, 0 deletions
diff --git a/discovery/pom.xml b/discovery/pom.xml
new file mode 100644
index 0000000..d3e9cd7
--- /dev/null
+++ b/discovery/pom.xml
@@ -0,0 +1,110 @@
+<?xml version="1.0" encoding="utf8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.orbekk.same.discovery</groupId>
+ <artifactId>discovery-server</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>jar</packaging>
+ <name>same</name>
+ <url>http://github.com/orbekk/master</url>
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+ <repositories>
+ <repository>
+ <id>jsonrpc3j-webdav-maven-repo</id>
+ <name>jsonrpc4j maven repository</name>
+ <url>http://jsonrpc4j.googlecode.com/svn/maven/repo/</url>
+ <layout>default</layout>
+ </repository>
+ </repositories>
+ <dependencies>
+ <dependency>
+ <groupId>com.googlecode</groupId>
+ <artifactId>jsonrpc4j</artifactId>
+ <version>0.18</version>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.jackson</groupId>
+ <artifactId>jackson-mapper-asl</artifactId>
+ <version>1.7.5</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <version>1.9.0</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.6.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>1.6.4</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.5</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.portlet</groupId>
+ <artifactId>portlet-api</artifactId>
+ <version>2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-servlet</artifactId>
+ <version>8.0.0.M3</version>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-server</artifactId>
+ <version>8.0.0.M3</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.resteasy</groupId>
+ <artifactId>tjws</artifactId>
+ <version>2.3.1.GA</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.1.2</version>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <configuration>
+ <archive>
+ <manifest>
+ <mainClass>com.orbekk.same.App</mainClass>
+ </manifest>
+ </archive>
+ <descriptorRefs>
+ <descriptorRef>jar-with-dependencies</descriptorRef>
+ </descriptorRefs>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/discovery/src/main/java/com/orbekk/same/discovery/DiscoveryService.java b/discovery/src/main/java/com/orbekk/same/discovery/DiscoveryService.java
new file mode 100644
index 0000000..04fdab0
--- /dev/null
+++ b/discovery/src/main/java/com/orbekk/same/discovery/DiscoveryService.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 DiscoveryService {
+ /**
+ * 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/discovery/src/main/java/com/orbekk/same/discovery/DiscoveryServiceImpl.java b/discovery/src/main/java/com/orbekk/same/discovery/DiscoveryServiceImpl.java
new file mode 100644
index 0000000..f568213
--- /dev/null
+++ b/discovery/src/main/java/com/orbekk/same/discovery/DiscoveryServiceImpl.java
@@ -0,0 +1,5 @@
+package com.orbekk.same.discovery;
+
+public class DiscoveryServiceImpl {
+
+}
diff --git a/discovery/src/main/java/com/orbekk/same/discovery/NetworkEntry.java b/discovery/src/main/java/com/orbekk/same/discovery/NetworkEntry.java
new file mode 100644
index 0000000..11c7f90
--- /dev/null
+++ b/discovery/src/main/java/com/orbekk/same/discovery/NetworkEntry.java
@@ -0,0 +1,30 @@
+package com.orbekk.same.discovery;
+
+public class NetworkEntry {
+ public String networkName;
+ public String masterUrl;
+ private long lastRegisteredMillis = -1;
+
+ public NetworkEntry(String networkName, String masterUrl) {
+ this.networkName = networkName;
+ this.masterUrl = masterUrl;
+ }
+
+ public void register(long registeredTime) {
+ lastRegisteredMillis = registeredTime;
+ }
+
+ public boolean hasExpired(long latestValidTimeMillis) {
+ return lastRegisteredMillis < latestValidTimeMillis;
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (other instanceof NetworkEntry) {
+ NetworkEntry otherEntry = (NetworkEntry)other;
+ return networkName.equals(otherEntry.networkName) &&
+ masterUrl.equals(otherEntry.masterUrl);
+ }
+ return false;
+ }
+}
diff --git a/discovery/src/test/java/com/orbekk/same/discovery/NetworkEntryTest.java b/discovery/src/test/java/com/orbekk/same/discovery/NetworkEntryTest.java
new file mode 100644
index 0000000..dacaf9c
--- /dev/null
+++ b/discovery/src/test/java/com/orbekk/same/discovery/NetworkEntryTest.java
@@ -0,0 +1,25 @@
+package com.orbekk.same.discovery;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class NetworkEntryTest {
+ @Test
+ public void testExpiry() {
+ NetworkEntry entry = new NetworkEntry("NetworkA", "UrlA");
+ entry.register(100);
+ assertFalse(entry.hasExpired(50));
+ assertTrue(entry.hasExpired(101));
+ }
+
+ @Test
+ public void testRegister() {
+ NetworkEntry entry = new NetworkEntry("NetworkB", "UrlB");
+ long time = 1000000000000l;
+ entry.register(time);
+ assertTrue(entry.hasExpired(time+1));
+ entry.register(time + 1000);
+ assertFalse(entry.hasExpired(time + 500));
+ }
+}