KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: AckReceiverWindow.java,v 1.9 2005/01/28 09:53:08 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.Message;
9
10 import java.util.HashMap JavaDoc;
11
12
13 /**
14  * Counterpart of AckSenderWindow. Simple FIFO buffer.
15  * Every message received is ACK'ed (even duplicates) and added to a hashmap
16  * keyed by seqno. The next seqno to be received is stored in <code>next_to_remove</code>. When a message with
17  * a seqno less than next_to_remove is received, it will be discarded. The <code>remove()</code> method removes
18  * and returns a message whose seqno is equal to next_to_remove, or null if not found.<br>
19  * Change May 28 2002 (bela): replaced TreeSet with HashMap. Keys do not need to be sorted, and adding a key to
20  * a sorted set incurs overhead.
21  *
22  * @author Bela Ban
23  */

24 public class AckReceiverWindow {
25     final long initial_seqno;
26     long next_to_remove=0;
27     final HashMap JavaDoc msgs=new HashMap JavaDoc(); // keys: seqnos (Long), values: Messages
28
static final Log log=LogFactory.getLog(AckReceiverWindow.class);
29
30
31     public AckReceiverWindow(long initial_seqno) {
32         this.initial_seqno=initial_seqno;
33         next_to_remove=initial_seqno;
34     }
35
36
37     public void add(long seqno, Message msg) {
38         if(seqno < next_to_remove) {
39             if(log.isTraceEnabled())
40                 log.trace("discarded msg with seqno=" + seqno + " (next msg to receive is " + next_to_remove + ')');
41             return;
42         }
43         msgs.put(new Long JavaDoc(seqno), msg);
44     }
45
46
47     /**
48      * Removes a message whose seqno is equal to <code>next_to_remove</code>, increments the latter.
49      * Returns message that was removed, or null, if no message can be removed. Messages are thus
50      * removed in order.
51      */

52     public Message remove() {
53         Message retval=(Message)msgs.remove(new Long JavaDoc(next_to_remove));
54         if(retval != null)
55             next_to_remove++;
56         return retval;
57     }
58
59
60     public void reset() {
61         msgs.clear();
62         next_to_remove=initial_seqno;
63     }
64
65     public int size() {
66         return msgs.size();
67     }
68
69     public String JavaDoc toString() {
70         return msgs.keySet().toString();
71     }
72
73
74
75 }
76
Popular Tags