KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > cluster > mcast > McastMembership


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.catalina.cluster.mcast;
18
19
20 import java.util.HashMap JavaDoc;
21 /**
22  * A <b>membership</b> implementation using simple multicast.
23  * This is the representation of a multicast membership.
24  * This class is responsible for maintaining a list of active cluster nodes in the cluster.
25  * If a node fails to send out a heartbeat, the node will be dismissed.
26  *
27  * @author Filip Hanik
28  * @version $Revision: 1.6 $, $Date: 2004/07/01 09:44:26 $
29  */

30
31
32 public class McastMembership
33 {
34     /**
35      * The name of this membership, has to be the same as the name for the local
36      * member
37      */

38     protected String JavaDoc name;
39     /**
40      * A map of all the members in the cluster.
41      */

42     protected HashMap JavaDoc map = new java.util.HashMap JavaDoc();
43
44     /**
45      * Constructs a new membership
46      * @param myName - has to be the name of the local member. Used to filter the local member from the cluster membership
47      */

48     public McastMembership(String JavaDoc myName) {
49         name = myName;
50     }
51
52     /**
53      * Reset the membership and start over fresh.
54      * Ie, delete all the members and wait for them to ping again and join this membership
55      */

56     public synchronized void reset() {
57         map.clear();
58     }
59
60     /**
61      * Notify the membership that this member has announced itself.
62      *
63      * @param m - the member that just pinged us
64      * @return - true if this member is new to the cluster, false otherwise.
65      * @return - false if this member is the local member.
66      */

67     public synchronized boolean memberAlive(McastMember m) {
68         boolean result = false;
69         //ignore ourselves
70
if ( m.getName().equals(name) ) return result;
71
72         //return true if the membership has changed
73
MbrEntry entry = (MbrEntry)map.get(m.getName());
74         if ( entry == null ) {
75             entry = new MbrEntry(m);
76             map.put(m.getName(),entry);
77             result = true;
78         } else {
79             //update the member alive time
80
entry.getMember().setMemberAliveTime(m.getMemberAliveTime());
81         }//end if
82
entry.accessed();
83         return result;
84     }
85
86     /**
87      * Runs a refresh cycle and returns a list of members that has expired.
88      * This also removes the members from the membership, in such a way that
89      * getMembers() = getMembers() - expire()
90      * @param maxtime - the max time a member can remain unannounced before it is considered dead.
91      * @return the list of expired members
92      */

93     public synchronized McastMember[] expire(long maxtime) {
94         MbrEntry[] members = getMemberEntries();
95         java.util.ArrayList JavaDoc list = new java.util.ArrayList JavaDoc();
96         for (int i=0; i<members.length; i++) {
97             MbrEntry entry = members[i];
98             if ( entry.hasExpired(maxtime) ) {
99                 list.add(entry.getMember());
100             }//end if
101
}//while
102
McastMember[] result = new McastMember[list.size()];
103         list.toArray(result);
104         for ( int j=0; j<result.length; j++) map.remove(result[j].getName());
105         return result;
106
107     }//expire
108

109     /**
110      * Returning a list of all the members in the membership
111      */

112     public synchronized McastMember[] getMembers() {
113         McastMember[] result = new McastMember[map.size()];
114         java.util.Iterator JavaDoc i = map.entrySet().iterator();
115         int pos = 0;
116         while ( i.hasNext() )
117             result[pos++] = ((MbrEntry)((java.util.Map.Entry)i.next()).getValue()).getMember();
118         return result;
119     }
120
121     protected synchronized MbrEntry[] getMemberEntries()
122     {
123         MbrEntry[] result = new MbrEntry[map.size()];
124         java.util.Iterator JavaDoc i = map.entrySet().iterator();
125         int pos = 0;
126         while ( i.hasNext() )
127             result[pos++] = ((MbrEntry)((java.util.Map.Entry)i.next()).getValue());
128         return result;
129     }
130
131
132     /**
133      * Inner class that represents a member entry
134      */

135     protected static class MbrEntry
136     {
137
138         protected McastMember mbr;
139         protected long lastHeardFrom;
140         public MbrEntry(McastMember mbr) {
141             this.mbr = mbr;
142         }
143         /**
144          * Indicate that this member has been accessed.
145          */

146         public void accessed(){
147             lastHeardFrom = System.currentTimeMillis();
148         }
149         /**
150          * Return the actual McastMember object
151          */

152         public McastMember getMember() {
153             return mbr;
154         }
155
156         /**
157          * Check if this dude has expired
158          * @param maxtime The time threshold
159          */

160         public boolean hasExpired(long maxtime) {
161             long delta = System.currentTimeMillis() - lastHeardFrom;
162             return delta > maxtime;
163         }
164     }//MbrEntry
165
}
166
Popular Tags