KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jgroups.protocols;
4
5 import org.jgroups.Address;
6 import org.jgroups.Event;
7 import org.jgroups.Message;
8 import org.jgroups.stack.Protocol;
9
10 import java.util.Properties JavaDoc;
11 import java.util.Vector JavaDoc;
12
13
14
15 /**
16  Discards up or down messages based on a percentage, e.g. setting property 'up' to 0.1 causes 10%
17  of all up messages to be discarded. Setting 'down' or 'up' to 0 causes no loss, whereas 1 discards
18  all messages (not very useful).
19  */

20
21 public class DISCARD extends Protocol
22 {
23    final Vector JavaDoc members = new Vector JavaDoc();
24    double up = 0.0; // probability of dropping up msgs
25
double down = 0.0; // probability of dropping down msgs
26
boolean excludeItself = false; //if true don't discard messages sent/received in this stack
27
Address localAddress;
28    final int num_sent = 25; // don't discard the first 5 messages. Temporary, +++remove later !
29

30
31    /** All protocol names have to be unique ! */
32    public String JavaDoc getName()
33    {
34       return "DISCARD";
35    }
36
37
38    public boolean setProperties(Properties JavaDoc props)
39    {
40       String JavaDoc str;
41
42        super.setProperties(props);
43       str = props.getProperty("up");
44       if (str != null)
45       {
46          up = Double.parseDouble(str);
47          props.remove("up");
48       }
49
50       str = props.getProperty("down");
51       if (str != null)
52       {
53          down = Double.parseDouble(str);
54          props.remove("down");
55       }
56
57       str = props.getProperty("excludeitself");
58       if (str != null)
59       {
60          excludeItself = Boolean.valueOf(str).booleanValue();
61          props.remove("excludeitself");
62       }
63
64
65       if (props.size() > 0)
66       {
67          System.err.println("DISCARD.setProperties(): these properties are not recognized:");
68          props.list(System.out);
69          return false;
70       }
71       return true;
72    }
73
74
75    public void up(Event evt)
76    {
77       Message msg;
78       double r;
79
80       if (evt.getType() == Event.SET_LOCAL_ADDRESS)
81          localAddress = (Address) evt.getArg();
82
83
84       if (evt.getType() == Event.MSG)
85       {
86          msg = (Message) evt.getArg();
87          if (up > 0)
88          {
89             /*if(num_sent > 0) {
90                     num_sent--;
91                     passUp(evt);
92                     return;
93                 }*/

94
95
96             r = Math.random();
97             if (r < up)
98             {
99                if (excludeItself && msg.getSrc().equals(localAddress))
100                {
101                    if(log.isInfoEnabled()) log.info("excluding itself");
102                }
103                else
104                {
105                    if(log.isInfoEnabled()) log.info("dropping message");
106                   return;
107                }
108             }
109          }
110       }
111
112
113       passUp(evt);
114    }
115
116
117    public void down(Event evt)
118    {
119       Message msg;
120       double r;
121
122       if (evt.getType() == Event.MSG)
123       {
124          msg = (Message) evt.getArg();
125
126          if (down > 0)
127          {
128             /*if(num_sent > 0) {
129                     num_sent--;
130                     passDown(evt);
131                     return;
132                 }*/

133             r = Math.random();
134             if (r < down)
135             {
136
137                if (excludeItself && msg.getSrc().equals(localAddress))
138                {
139                    if(log.isInfoEnabled()) log.info("excluding itself");
140                }
141                else
142                {
143                    if(log.isInfoEnabled()) log.info("dropping message");
144                   return;
145                }
146             }
147          }
148
149       }
150       passDown(evt);
151    }
152 }
153
Popular Tags