1 16 17 package org.apache.catalina.cluster.mcast; 18 19 20 import java.util.HashMap ; 21 30 31 32 public class McastMembership 33 { 34 38 protected String name; 39 42 protected HashMap map = new java.util.HashMap (); 43 44 48 public McastMembership(String myName) { 49 name = myName; 50 } 51 52 56 public synchronized void reset() { 57 map.clear(); 58 } 59 60 67 public synchronized boolean memberAlive(McastMember m) { 68 boolean result = false; 69 if ( m.getName().equals(name) ) return result; 71 72 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 entry.getMember().setMemberAliveTime(m.getMemberAliveTime()); 81 } entry.accessed(); 83 return result; 84 } 85 86 93 public synchronized McastMember[] expire(long maxtime) { 94 MbrEntry[] members = getMemberEntries(); 95 java.util.ArrayList list = new java.util.ArrayList (); 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 } } 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 } 109 112 public synchronized McastMember[] getMembers() { 113 McastMember[] result = new McastMember[map.size()]; 114 java.util.Iterator 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 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 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 146 public void accessed(){ 147 lastHeardFrom = System.currentTimeMillis(); 148 } 149 152 public McastMember getMember() { 153 return mbr; 154 } 155 156 160 public boolean hasExpired(long maxtime) { 161 long delta = System.currentTimeMillis() - lastHeardFrom; 162 return delta > maxtime; 163 } 164 }} 166 | Popular Tags |