KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jgroups.protocols;
4
5 import org.jgroups.Event;
6 import org.jgroups.Message;
7 import org.jgroups.View;
8 import org.jgroups.stack.Protocol;
9 import org.jgroups.util.Util;
10
11 import java.util.Vector JavaDoc;
12
13
14 /**
15  * Example of a protocol layer. Contains no real functionality, can be used as a template.
16  */

17
18 public class LOSS extends Protocol {
19     final Vector JavaDoc members=new Vector JavaDoc();
20     final long i=0;
21     boolean drop_next_msg=false;
22
23     /** All protocol names have to be unique ! */
24     public String JavaDoc getName() {return "LOSS";}
25
26
27
28     /** Just remove if you don't need to reset any state */
29     public void reset() {}
30
31
32
33
34 // public void up(Event evt) {
35
// Message msg;
36

37 // switch(evt.getType()) {
38

39 // case Event.MSG:
40
// msg=(Message)evt.getArg();
41
// if(msg.getDest() != null && !((Address)msg.getDest()).isMulticastAddress()) {
42
// // System.err.println("LOSS.up(): not dropping msg as it is unicast !");
43
// break;
44
// }
45

46 // i++;
47

48 // int r=((int)(Math.random() * 1000)) % 10;
49

50 // if(r != 0 && i % r == 0) { // drop
51
// System.out.println("####### LOSS.up(): dropping message " +
52
// Util.printEvent(evt));
53
// return;
54
// }
55

56 // break;
57
// }
58

59 // passUp(evt); // Pass up to the layer above us
60
// }
61

62
63
64
65
66     public void down(Event evt) {
67     Message msg;
68     
69     switch(evt.getType()) {
70
71     case Event.TMP_VIEW:
72     case Event.VIEW_CHANGE:
73         Vector JavaDoc new_members=((View)evt.getArg()).getMembers();
74         synchronized(members) {
75         members.removeAllElements();
76         if(new_members != null && new_members.size() > 0)
77             for(int i=0; i < new_members.size(); i++)
78             members.addElement(new_members.elementAt(i));
79         }
80         passDown(evt);
81         break;
82
83     case Event.MSG:
84         if(drop_next_msg) {
85         drop_next_msg=false;
86         msg=(Message)evt.getArg();
87
88         if(msg.getDest() != null && !msg.getDest().isMulticastAddress()) {
89             break;
90         }
91
92
93             System.out.println("###### LOSS.down(): dropping msg " + Util.printMessage(msg));
94         
95         return;
96         }
97         break;
98
99     case Event.DROP_NEXT_MSG:
100         drop_next_msg=true;
101         break;
102     }
103
104
105
106     passDown(evt); // Pass on to the layer below us
107
}
108
109
110
111 }
112
Popular Tags