Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package at.ac.tuwien.infosys.rnue.implementation.multicast;
  6.  
  7. import at.ac.tuwien.infosys.rnue.interfaces.IConstants;
  8. import at.ac.tuwien.infosys.rnue.interfaces.IHostInfo;
  9. import at.ac.tuwien.infosys.rnue.interfaces.IHostList;
  10. import java.util.Enumeration;
  11. import java.util.Hashtable;
  12.  
  13. /**
  14.  *
  15.  * @author Gengar
  16.  */
  17. public class HostListImpl implements IHostList
  18. {
  19.  
  20.     private class GarbageCollector implements Runnable
  21.     {
  22.  
  23.         private IHostList gcList = null;
  24.         private boolean gcAllowed = true;
  25.  
  26.         public GarbageCollector(IHostList ihl)
  27.         {
  28.             gcList = ihl;
  29.         }
  30.  
  31.         public void run()
  32.         {
  33.             synchronized (this)
  34.             {
  35.                 while (gcAllowed)
  36.                 {
  37.                     try
  38.                     {
  39.                         long currentTime = System.currentTimeMillis();
  40.                         Enumeration elems = gcList.elements();
  41.  
  42.                         while (elems.hasMoreElements())
  43.                         {
  44.                             IHostInfo hostInfo = (IHostInfo) elems.nextElement();
  45.  
  46.                             if (currentTime - hostInfo.getTimestamp() > IConstants.LIFETIME_OF_HOSTINFOS)
  47.                             {
  48.                                 gcList.remove(generateHostInfoKey(hostInfo));
  49.                             }
  50.                         }
  51.                         ((java.lang.Object) this).wait(IConstants.GC_INTERVAL);
  52.  
  53.  
  54.                     } catch (InterruptedException ie)
  55.                     {
  56.                         //nerob nic, len uplynul casovy interval
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.  
  62.         public void stop()
  63.         {
  64.             synchronized (this)
  65.             {
  66.                 this.gcAllowed = false;
  67.                 this.notify();
  68.             }
  69.         }
  70.     }
  71.     private final Hashtable<String, IHostInfo> hostList = new Hashtable<String, IHostInfo>();
  72.  
  73.     private GarbageCollector gc;
  74.     public HostListImpl()
  75.     {
  76.         gc = new GarbageCollector(this);
  77.  
  78.         new Thread(gc).start();
  79.     }
  80.     public IHostInfo get(String string)
  81.     {
  82.         synchronized (hostList)
  83.         {
  84.             return hostList.get(string);
  85.         }
  86.     }
  87.  
  88.     public IHostInfo put(IHostInfo ihi)
  89.     {
  90.         synchronized (hostList)
  91.         {
  92.             return hostList.put(generateHostInfoKey(ihi), ihi);
  93.         }
  94.     }
  95.  
  96.     public IHostInfo remove(String string)
  97.     {
  98.         synchronized (hostList)
  99.         {
  100.             return hostList.remove(string);
  101.         }
  102.     }
  103.  
  104.     public boolean contains(IHostInfo ihi)
  105.     {
  106.         return hostList.contains(ihi);
  107.     }
  108.  
  109.     public Enumeration keys()
  110.     {
  111.         return hostList.keys();
  112.     }
  113.  
  114.     public Enumeration elements()
  115.     {
  116.         return hostList.elements();
  117.     }
  118.  
  119.     public void stopGarbageCollector()
  120.     {
  121.         gc.stop();
  122.     }
  123.  
  124.     private String generateHostInfoKey(IHostInfo ihi)
  125.     {
  126.         return ihi.getHostInfoMessage().getRegistryHost() + ":" + ihi.getHostInfoMessage().getRegistryPort();
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement