Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package at.ac.tuwien.infosys.rnue.implementation.multicast;
- import at.ac.tuwien.infosys.rnue.interfaces.IConstants;
- import at.ac.tuwien.infosys.rnue.interfaces.IHostInfo;
- import at.ac.tuwien.infosys.rnue.interfaces.IHostList;
- import java.util.Enumeration;
- import java.util.Hashtable;
- /**
- *
- * @author Gengar
- */
- public class HostListImpl implements IHostList
- {
- private class GarbageCollector implements Runnable
- {
- private IHostList gcList = null;
- private boolean gcAllowed = true;
- public GarbageCollector(IHostList ihl)
- {
- gcList = ihl;
- }
- public void run()
- {
- synchronized (this)
- {
- while (gcAllowed)
- {
- try
- {
- long currentTime = System.currentTimeMillis();
- Enumeration elems = gcList.elements();
- while (elems.hasMoreElements())
- {
- IHostInfo hostInfo = (IHostInfo) elems.nextElement();
- if (currentTime - hostInfo.getTimestamp() > IConstants.LIFETIME_OF_HOSTINFOS)
- {
- gcList.remove(generateHostInfoKey(hostInfo));
- }
- }
- ((java.lang.Object) this).wait(IConstants.GC_INTERVAL);
- } catch (InterruptedException ie)
- {
- //nerob nic, len uplynul casovy interval
- }
- }
- }
- }
- public void stop()
- {
- synchronized (this)
- {
- this.gcAllowed = false;
- this.notify();
- }
- }
- }
- private final Hashtable<String, IHostInfo> hostList = new Hashtable<String, IHostInfo>();
- private GarbageCollector gc;
- public HostListImpl()
- {
- gc = new GarbageCollector(this);
- new Thread(gc).start();
- }
- public IHostInfo get(String string)
- {
- synchronized (hostList)
- {
- return hostList.get(string);
- }
- }
- public IHostInfo put(IHostInfo ihi)
- {
- synchronized (hostList)
- {
- return hostList.put(generateHostInfoKey(ihi), ihi);
- }
- }
- public IHostInfo remove(String string)
- {
- synchronized (hostList)
- {
- return hostList.remove(string);
- }
- }
- public boolean contains(IHostInfo ihi)
- {
- return hostList.contains(ihi);
- }
- public Enumeration keys()
- {
- return hostList.keys();
- }
- public Enumeration elements()
- {
- return hostList.elements();
- }
- public void stopGarbageCollector()
- {
- gc.stop();
- }
- private String generateHostInfoKey(IHostInfo ihi)
- {
- return ihi.getHostInfoMessage().getRegistryHost() + ":" + ihi.getHostInfoMessage().getRegistryPort();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement