diff options
Diffstat (limited to 'directory/src/main/java/com/orbekk/same')
3 files changed, 86 insertions, 0 deletions
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<NetworkEntry> networkList = new ArrayList<NetworkEntry>(); + + synchronized void cleanNetworkList() { + long expiredTime = System.currentTimeMillis() - EXPIRE_TIME; + for (Iterator<NetworkEntry> it = networkList.iterator(); it.hasNext();) { + NetworkEntry e = it.next(); + if (e.hasExpired(expiredTime)) { + it.remove(); + } + } + } + + @Override + public List<String> getNetworks() throws Exception { + cleanNetworkList(); + List<String> networks = new ArrayList<String>(); + 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; + } +} |