From 0ac19e55b447e9bc966282e1ba9cae9f23ae4f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 2 Mar 2012 14:32:01 +0100 Subject: Rename Discovery* => Directory*. --- directory/pom.xml | 115 +++++++++++++++++++++ .../com/orbekk/same/directory/DirectoryApp.java | 13 +++ .../same/directory/DirectoryServiceImpl.java | 43 ++++++++ .../com/orbekk/same/directory/NetworkEntry.java | 30 ++++++ .../orbekk/same/discovery/NetworkEntryTest.java | 27 +++++ discovery/pom.xml | 115 --------------------- .../com/orbekk/same/discovery/DiscoveryApp.java | 13 --- .../orbekk/same/discovery/DiscoveryService.java | 22 ---- .../same/discovery/DiscoveryServiceImpl.java | 41 -------- .../com/orbekk/same/discovery/NetworkEntry.java | 30 ------ .../orbekk/same/discovery/NetworkEntryTest.java | 25 ----- 11 files changed, 228 insertions(+), 246 deletions(-) create mode 100644 directory/pom.xml create mode 100644 directory/src/main/java/com/orbekk/same/directory/DirectoryApp.java create mode 100644 directory/src/main/java/com/orbekk/same/directory/DirectoryServiceImpl.java create mode 100644 directory/src/main/java/com/orbekk/same/directory/NetworkEntry.java create mode 100644 directory/src/test/java/com/orbekk/same/discovery/NetworkEntryTest.java delete mode 100644 discovery/pom.xml delete mode 100644 discovery/src/main/java/com/orbekk/same/discovery/DiscoveryApp.java delete mode 100644 discovery/src/main/java/com/orbekk/same/discovery/DiscoveryService.java delete mode 100644 discovery/src/main/java/com/orbekk/same/discovery/DiscoveryServiceImpl.java delete mode 100644 discovery/src/main/java/com/orbekk/same/discovery/NetworkEntry.java delete mode 100644 discovery/src/test/java/com/orbekk/same/discovery/NetworkEntryTest.java diff --git a/directory/pom.xml b/directory/pom.xml new file mode 100644 index 0000000..b8d11f2 --- /dev/null +++ b/directory/pom.xml @@ -0,0 +1,115 @@ + + + 4.0.0 + com.orbekk.same.directory + discovery-server + 1.0-SNAPSHOT + jar + same + http://github.com/orbekk/master + + UTF-8 + + + + jsonrpc3j-webdav-maven-repo + jsonrpc4j maven repository + http://jsonrpc4j.googlecode.com/svn/maven/repo/ + default + + + + + com.googlecode + jsonrpc4j + 0.18 + + + org.codehaus.jackson + jackson-mapper-asl + 1.7.5 + + + junit + junit + 4.10 + test + + + org.mockito + mockito-all + 1.9.0 + test + + + org.slf4j + slf4j-api + 1.6.4 + + + org.slf4j + slf4j-log4j12 + 1.6.4 + + + javax.servlet + servlet-api + 2.5 + + + javax.portlet + portlet-api + 2.0 + + + org.eclipse.jetty + jetty-servlet + 8.0.0.M3 + + + org.eclipse.jetty + jetty-server + 8.0.0.M3 + + + org.jboss.resteasy + tjws + 2.3.1.GA + + + org.apache.httpcomponents + httpclient + 4.1.2 + + + com.orbekk + same + 0.0-SNAPSHOT + + + + + + maven-assembly-plugin + + + + com.orbekk.same.App + + + + jar-with-dependencies + + + + + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + + diff --git a/directory/src/main/java/com/orbekk/same/directory/DirectoryApp.java b/directory/src/main/java/com/orbekk/same/directory/DirectoryApp.java new file mode 100644 index 0000000..f142f18 --- /dev/null +++ b/directory/src/main/java/com/orbekk/same/directory/DirectoryApp.java @@ -0,0 +1,13 @@ +package com.orbekk.same.directory; + +import com.orbekk.same.http.ServerContainer; + +public class DirectoryApp { + public static final int DISCOVERY_PORT = 15072; + + public static void main(String[] args) { + /** + * TODO: Need to move DiscoveryService to same project. + */ + } +} diff --git a/directory/src/main/java/com/orbekk/same/directory/DirectoryServiceImpl.java b/directory/src/main/java/com/orbekk/same/directory/DirectoryServiceImpl.java new file mode 100644 index 0000000..8c6c6b9 --- /dev/null +++ b/directory/src/main/java/com/orbekk/same/directory/DirectoryServiceImpl.java @@ -0,0 +1,43 @@ +package com.orbekk.same.directory; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import com.orbekk.same.discovery.DirectoryService; + +public class DirectoryServiceImpl implements DirectoryService { + public final static long EXPIRE_TIME = 15 * 60l * 1000; // 15 minutes + List networkList = new ArrayList(); + + synchronized void cleanNetworkList() { + long expiredTime = System.currentTimeMillis() - EXPIRE_TIME; + for (Iterator it = networkList.iterator(); it.hasNext();) { + NetworkEntry e = it.next(); + if (e.hasExpired(expiredTime)) { + it.remove(); + } + } + } + + @Override + public List getNetworks() throws Exception { + cleanNetworkList(); + List networks = new ArrayList(); + for (NetworkEntry e : networkList) { + networks.add(e.networkName); + networks.add(e.masterUrl); + } + return networks; + } + + @Override + public void registerNetwork(String networkName, String masterUrl) + throws Exception { + cleanNetworkList(); + NetworkEntry entry = new NetworkEntry(networkName, masterUrl); + entry.register(System.currentTimeMillis()); + networkList.remove(entry); + networkList.add(entry); + } +} diff --git a/directory/src/main/java/com/orbekk/same/directory/NetworkEntry.java b/directory/src/main/java/com/orbekk/same/directory/NetworkEntry.java new file mode 100644 index 0000000..cac21cc --- /dev/null +++ b/directory/src/main/java/com/orbekk/same/directory/NetworkEntry.java @@ -0,0 +1,30 @@ +package com.orbekk.same.directory; + +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/directory/src/test/java/com/orbekk/same/discovery/NetworkEntryTest.java b/directory/src/test/java/com/orbekk/same/discovery/NetworkEntryTest.java new file mode 100644 index 0000000..455de90 --- /dev/null +++ b/directory/src/test/java/com/orbekk/same/discovery/NetworkEntryTest.java @@ -0,0 +1,27 @@ +package com.orbekk.same.discovery; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.orbekk.same.directory.NetworkEntry; + +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)); + } +} diff --git a/discovery/pom.xml b/discovery/pom.xml deleted file mode 100644 index 92cbc94..0000000 --- a/discovery/pom.xml +++ /dev/null @@ -1,115 +0,0 @@ - - - 4.0.0 - com.orbekk.same.discovery - discovery-server - 1.0-SNAPSHOT - jar - same - http://github.com/orbekk/master - - UTF-8 - - - - jsonrpc3j-webdav-maven-repo - jsonrpc4j maven repository - http://jsonrpc4j.googlecode.com/svn/maven/repo/ - default - - - - - com.googlecode - jsonrpc4j - 0.18 - - - org.codehaus.jackson - jackson-mapper-asl - 1.7.5 - - - junit - junit - 4.10 - test - - - org.mockito - mockito-all - 1.9.0 - test - - - org.slf4j - slf4j-api - 1.6.4 - - - org.slf4j - slf4j-log4j12 - 1.6.4 - - - javax.servlet - servlet-api - 2.5 - - - javax.portlet - portlet-api - 2.0 - - - org.eclipse.jetty - jetty-servlet - 8.0.0.M3 - - - org.eclipse.jetty - jetty-server - 8.0.0.M3 - - - org.jboss.resteasy - tjws - 2.3.1.GA - - - org.apache.httpcomponents - httpclient - 4.1.2 - - - com.orbekk - same - 0.0-SNAPSHOT - - - - - - maven-assembly-plugin - - - - com.orbekk.same.App - - - - jar-with-dependencies - - - - - maven-compiler-plugin - 2.3.2 - - 1.6 - 1.6 - - - - - diff --git a/discovery/src/main/java/com/orbekk/same/discovery/DiscoveryApp.java b/discovery/src/main/java/com/orbekk/same/discovery/DiscoveryApp.java deleted file mode 100644 index dbad697..0000000 --- a/discovery/src/main/java/com/orbekk/same/discovery/DiscoveryApp.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.orbekk.same.discovery; - -import com.orbekk.same.http.ServerContainer; - -public class DiscoveryApp { - public static final int DISCOVERY_PORT = 15072; - - public static void main(String[] args) { - /** - * TODO: Need to move DiscoveryService to same 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 deleted file mode 100644 index 04fdab0..0000000 --- a/discovery/src/main/java/com/orbekk/same/discovery/DiscoveryService.java +++ /dev/null @@ -1,22 +0,0 @@ -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 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 deleted file mode 100644 index fe97228..0000000 --- a/discovery/src/main/java/com/orbekk/same/discovery/DiscoveryServiceImpl.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.orbekk.same.discovery; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class DiscoveryServiceImpl implements DiscoveryService { - public final static long EXPIRE_TIME = 15 * 60l * 1000; // 15 minutes - List networkList = new ArrayList(); - - synchronized void cleanNetworkList() { - long expiredTime = System.currentTimeMillis() - EXPIRE_TIME; - for (Iterator it = networkList.iterator(); it.hasNext();) { - NetworkEntry e = it.next(); - if (e.hasExpired(expiredTime)) { - it.remove(); - } - } - } - - @Override - public List getNetworks() throws Exception { - cleanNetworkList(); - List networks = new ArrayList(); - for (NetworkEntry e : networkList) { - networks.add(e.networkName); - networks.add(e.masterUrl); - } - return networks; - } - - @Override - public void registerNetwork(String networkName, String masterUrl) - throws Exception { - cleanNetworkList(); - NetworkEntry entry = new NetworkEntry(networkName, masterUrl); - entry.register(System.currentTimeMillis()); - networkList.remove(entry); - networkList.add(entry); - } -} diff --git a/discovery/src/main/java/com/orbekk/same/discovery/NetworkEntry.java b/discovery/src/main/java/com/orbekk/same/discovery/NetworkEntry.java deleted file mode 100644 index 11c7f90..0000000 --- a/discovery/src/main/java/com/orbekk/same/discovery/NetworkEntry.java +++ /dev/null @@ -1,30 +0,0 @@ -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 deleted file mode 100644 index dacaf9c..0000000 --- a/discovery/src/test/java/com/orbekk/same/discovery/NetworkEntryTest.java +++ /dev/null @@ -1,25 +0,0 @@ -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)); - } -} -- cgit v1.2.3