1 3 package org.jgroups.stack; 4 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.jgroups.Address; 9 10 import java.util.Enumeration ; 11 import java.util.Hashtable ; 12 import java.util.Vector ; 13 14 15 16 17 25 public class AckMcastReceiverWindow { 26 final Hashtable msgs=new Hashtable (); 28 protected static final Log log=LogFactory.getLog(AckMcastReceiverWindow.class); 29 30 31 32 38 public boolean add(Object sender, long seqno) { 39 Vector seqnos=(Vector )msgs.get(sender); 40 Long val=new Long (seqno); 41 42 if(seqnos == null) { 43 seqnos=new Vector (); 44 seqnos.addElement(val); 45 msgs.put(sender, seqnos); 46 return true; 47 } 48 49 if(seqnos.contains(val)) 50 return false; 51 52 seqnos.addElement(val); 53 return true; 54 } 55 56 57 58 59 public void remove(Object sender, Vector seqnos) { 60 Vector v=(Vector )msgs.get(sender); 61 Long seqno; 62 63 if(v != null && seqnos != null) { 64 for(int i=0; i < seqnos.size(); i++) { 65 seqno=(Long )seqnos.elementAt(i); 66 v.removeElement(seqno); 67 } 68 } 69 } 70 71 72 73 public long size() { 74 long ret=0; 75 76 for(Enumeration e=msgs.elements(); e.hasMoreElements();) { 77 ret+=((Vector )e.nextElement()).size(); 78 } 79 80 return ret; 81 } 82 83 84 public void reset() { 85 removeAll(); 86 } 87 88 public void removeAll() {msgs.clear();} 89 90 91 public void suspect(Object sender) { 92 93 if(log.isInfoEnabled()) log.info("suspect is " + sender); 94 msgs.remove(sender); 95 } 96 97 98 99 public String toString() { 100 StringBuffer ret=new StringBuffer (); 101 Object sender; 102 103 for(Enumeration e=msgs.keys(); e.hasMoreElements();) { 104 sender=e.nextElement(); 105 ret.append(sender + " --> " + msgs.get(sender) + '\n'); 106 } 107 return ret.toString(); 108 } 109 110 111 112 113 114 115 public static void main(String [] args) { 116 AckMcastReceiverWindow win=new AckMcastReceiverWindow(); 117 Address sender1=new IpAddress("janet", 1111); 118 Address sender2=new IpAddress("janet", 4444); 119 Address sender3=new IpAddress("janet", 6767); 120 Address sender4=new IpAddress("janet", 3333); 121 122 win.add(sender1, 1); 123 win.add(sender1, 2); 124 125 win.add(sender3, 2); 126 win.add(sender2, 2); 127 win.add(sender4, 2); 128 win.add(sender1, 3); 129 win.add(sender1, 2); 130 131 132 System.out.println(win); 133 134 win.suspect(sender1); 135 System.out.println(win); 136 137 win.add(sender1, 1); 138 win.add(sender1, 2); 139 win.add(sender1, 3); 140 win.add(sender1, 4); 141 win.add(sender1, 5); 142 win.add(sender1, 6); 143 win.add(sender1, 7); 144 win.add(sender1, 8); 145 146 System.out.println(win); 147 148 149 Vector seqnos=new Vector (); 150 151 seqnos.addElement(new Long (4)); 152 seqnos.addElement(new Long (6)); 153 seqnos.addElement(new Long (8)); 154 155 win.remove(sender2, seqnos); 156 System.out.println(win); 157 158 win.remove(sender1, seqnos); 159 System.out.println(win); 160 161 162 } 163 164 } 165 | Popular Tags |