KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > DISCARD_PAYLOAD


1 package org.jgroups.protocols;
2
3 import org.jgroups.Event;
4 import org.jgroups.Message;
5 import org.jgroups.stack.Protocol;
6
7 import java.util.Properties JavaDoc;
8
9 /**
10  * Discards a message whose sequence number (in the payload, as a Long) matches seqno 2 times,
11  * before passing it up. Used for unit testing
12  * of OOB messages
13  * @author Bela Ban
14  * @version $Id: DISCARD_PAYLOAD.java,v 1.6 2007/01/12 14:19:40 belaban Exp $
15  */

16 public class DISCARD_PAYLOAD extends Protocol {
17     long seqno=3; // drop 3
18
long duplicate=4; // duplicate 4 (one time)
19
int num_discards=0;
20
21     public DISCARD_PAYLOAD() {
22     }
23
24     public String JavaDoc getName() {
25         return "DISCARD_PAYLOAD";
26     }
27
28     public boolean setProperties(Properties JavaDoc props) {
29         String JavaDoc str;
30
31         super.setProperties(props);
32
33         str=props.getProperty("seqno");
34         if(str != null) {
35             seqno=Long.parseLong(str);
36             props.remove("seqno");
37         }
38
39         str=props.getProperty("duplicate");
40         if(str != null) {
41             duplicate=Long.parseLong(str);
42             props.remove("duplicate");
43         }
44
45         if(!props.isEmpty()) {
46             log.error("these properties are not recognized: " + props);
47             return false;
48         }
49         return true;
50     }
51
52
53     public Object JavaDoc up(Event evt) {
54         if(evt.getType() == Event.MSG) {
55             Message msg=(Message)evt.getArg();
56             if(msg.getLength() > 0) {
57                 try {
58                     Long JavaDoc payload=(Long JavaDoc)msg.getObject();
59                     if(payload != null) {
60                         long val=payload.longValue();
61
62                         if(val == seqno) {
63                             synchronized(this) {
64                                 if(num_discards < 3) {
65                                     num_discards++;
66                                     return null;
67                                 }
68                             }
69                         }
70                         if(val == duplicate) { // inject a duplicate message
71
super.up(evt); // pass it up, will passed up a second time by the default up_prot.up(evt)
72
}
73                     }
74                 }
75                 catch(Throwable JavaDoc t) {
76                     ;
77                 }
78             }
79         }
80         return up_prot.up(evt);
81     }
82 }
83
Popular Tags