KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > stack > AckMcastReceiverWindow


1 // $Id: AckMcastReceiverWindow.java,v 1.6 2004/09/23 16:29:53 belaban Exp $
2

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 JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Vector JavaDoc;
13
14
15
16
17 /**
18  * Keeps track of messages received from various senders. Acks each message received and checks whether
19  * it was already delivered. If yes, the message is discarded, otherwise it is delivered (passed up).
20  * The messages contain sequence numbers of old messages to be deleted, those are removed from the
21  * message table.
22  *
23  * @author Bela Ban June 17 1999
24  */

25 public class AckMcastReceiverWindow {
26     final Hashtable JavaDoc msgs=new Hashtable JavaDoc(); // sender -- Vector (of seqnos)
27

28     protected static final Log log=LogFactory.getLog(AckMcastReceiverWindow.class);
29
30
31
32     /**
33        Records the sender/seqno pair in the message table
34        @param sender The sender of the message
35        @param seqno The sequence number associated with the message
36        @return boolean If false, message is already present. Otherwise true.
37      */

38     public boolean add(Object JavaDoc sender, long seqno) {
39     Vector JavaDoc seqnos=(Vector JavaDoc)msgs.get(sender);
40     Long JavaDoc val=new Long JavaDoc(seqno);
41
42     if(seqnos == null) {
43         seqnos=new Vector JavaDoc();
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 JavaDoc sender, Vector JavaDoc seqnos) {
60     Vector JavaDoc v=(Vector JavaDoc)msgs.get(sender);
61     Long JavaDoc seqno;
62
63     if(v != null && seqnos != null) {
64         for(int i=0; i < seqnos.size(); i++) {
65         seqno=(Long JavaDoc)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 JavaDoc e=msgs.elements(); e.hasMoreElements();) {
77         ret+=((Vector JavaDoc)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 JavaDoc sender) {
92
93         if(log.isInfoEnabled()) log.info("suspect is " + sender);
94     msgs.remove(sender);
95     }
96
97
98
99     public String JavaDoc toString() {
100     StringBuffer JavaDoc ret=new StringBuffer JavaDoc();
101     Object JavaDoc sender;
102     
103     for(Enumeration JavaDoc 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 JavaDoc[] 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 JavaDoc seqnos=new Vector JavaDoc();
150
151     seqnos.addElement(new Long JavaDoc(4));
152     seqnos.addElement(new Long JavaDoc(6));
153     seqnos.addElement(new Long JavaDoc(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