KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > mcast > View


1 package org.sapia.ubik.mcast;
2
3 import org.sapia.ubik.net.ServerAddress;
4
5 import java.util.*;
6
7
8 /**
9  * Encapsulates the addresses of the nodes that compose an event channel. An
10  * instance of this class is encapsulated by an <code>EventChannel</code>. Its
11  * provides a "view" of the domain.
12  * <p>
13  * An instance of this class encapsulates the address of each of the sibling of
14  * an <code>EventChannel</code> node.
15  *
16  * @author Yanick Duchesne
17  * <dl>
18  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
19  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
20  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
21  * </dl>
22  */

23 public class View {
24   private Map _addresses = new HashMap();
25   private Map _nodeToAddr = new HashMap();
26   private long _timeout;
27
28   /**
29    * Constructor for View.
30    */

31   public View(long timeout) {
32     _timeout = timeout;
33   }
34
35   /**
36    * Returns this instance's <code>List</code> of <code>ServerAddress</code>es.
37    *
38    * @return a <code>List</code> of <code>ServerAddress</code>es.
39    */

40   public synchronized List getHosts() {
41     return new AddressList(_addresses.keySet());
42   }
43
44   /**
45    * Returns the <code>ServerAddress</code> corresponding to the given
46    * node.
47    *
48    * @return a <code>ServerAddress</code>.
49    */

50   public ServerAddress getAddressFor(String JavaDoc node) {
51     NodeInfo info = (NodeInfo) _nodeToAddr.get(node);
52
53     return info.addr;
54   }
55   
56   /**
57    * @param timeout the timeout after which nodes that haven't sent a heartbeat
58    * are removed from this instance.
59    */

60   public void setTimeout(long timeout){
61     _timeout = timeout;
62   }
63   
64   /**
65    * Adds the given address to this instance.
66    *
67    * @param addr the <code>ServerAddress</code> corresponding to a remote
68    * <code>EventChannel</code>.
69    * @param node node identifier.
70    */

71   void addHost(ServerAddress addr, String JavaDoc node) {
72     NodeInfo info = new NodeInfo(addr, node);
73     _nodeToAddr.put(node, info);
74     _addresses.put(info, new Long JavaDoc(System.currentTimeMillis()));
75   }
76
77   /**
78    * Updates the "last access" flag corresponding to the passed in
79    * <code>ServerAddress</code>.
80    *
81    * @param <code>ServerAddress</code>.
82    * @param node node identifier.
83    */

84   void heartbeat(ServerAddress addr, String JavaDoc node) {
85     _addresses.put(new NodeInfo(addr, node),
86       new Long JavaDoc(System.currentTimeMillis()));
87   }
88
89   /**
90    * Removes the "dead" (timed-out) hosts from this instance.
91    */

92   void removeDeadHosts() {
93     Map.Entry entry;
94     Map.Entry[] hosts;
95
96     synchronized (_addresses) {
97       hosts = (Map.Entry[]) _addresses.entrySet().toArray(new Map.Entry[_addresses.size()]);
98
99       NodeInfo info;
100
101       for (int i = 0; i < hosts.length; i++) {
102         if ((System.currentTimeMillis() -
103               ((Long JavaDoc) hosts[i].getValue()).longValue()) > _timeout) {
104           info = (NodeInfo) hosts[i].getKey();
105           _addresses.remove(info);
106           _nodeToAddr.remove(info.node);
107         }
108       }
109     }
110   }
111
112   /*//////////////////////////////////////////////////
113                       INNER CLASSES
114   //////////////////////////////////////////////////*/

115   static class NodeInfo {
116     final ServerAddress addr;
117     final String JavaDoc node;
118
119     NodeInfo(ServerAddress addr, String JavaDoc node) {
120       this.addr = addr;
121       this.node = node;
122     }
123
124     public boolean equals(Object JavaDoc obj) {
125       NodeInfo inf = (NodeInfo) obj;
126
127       return inf.addr.equals(addr) && inf.node.equals(node);
128     }
129
130     public int hashCode() {
131       return addr.hashCode();
132     }
133   }
134
135   public static class AddressList extends ArrayList {
136     AddressList(Collection infos) {
137       super(infos);
138     }
139
140     public Object JavaDoc get(int idx) {
141       try {
142         return ((NodeInfo) super.get(idx)).addr;
143       } catch (ClassCastException JavaDoc e) {
144         throw e;
145       }
146     }
147   }
148 }
149
Popular Tags