KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: VIEW_ENFORCER.java,v 1.3 2004/04/23 19:36:13 belaban Exp $
2

3 package org.jgroups.protocols;
4
5 import org.jgroups.Address;
6 import org.jgroups.Event;
7 import org.jgroups.View;
8 import org.jgroups.stack.Protocol;
9
10 import java.util.Vector JavaDoc;
11
12
13 /**
14  * Used by a client until it becomes a member: all up messages are discarded until a VIEW_CHANGE
15  * is encountered. From then on, this layer just acts as a pass-through layer. Later, we may
16  * add some functionality that checks for the VIDs of messages and discards messages accordingly.
17  */

18
19 public class VIEW_ENFORCER extends Protocol {
20     Address local_addr=null;
21     boolean is_member=false;
22
23
24     /**
25      * All protocol names have to be unique !
26      */

27     public String JavaDoc getName() {
28         return "VIEW_ENFORCER";
29     }
30
31
32     public void up(Event evt) {
33
34         switch(evt.getType()) {
35
36             case Event.VIEW_CHANGE:
37                 if(is_member) // pass the view change up if we are already a member of the group
38
break;
39
40                 Vector JavaDoc new_members=((View)evt.getArg()).getMembers();
41                 if(new_members == null || new_members.size() == 0)
42                     break;
43                 if(local_addr == null) {
44                     System.err.println("VIEW_ENFORCER.up(VIEW_CHANGE): local address is null; cannot check " +
45                             "whether I'm a member of the group; discarding view change");
46                     return;
47                 }
48
49                 if(new_members.contains(local_addr))
50                     is_member=true;
51                 else
52                     return;
53
54                 break;
55
56             case Event.SET_LOCAL_ADDRESS:
57                 local_addr=(Address)evt.getArg();
58                 break;
59
60
61             case Event.MSG:
62                 if(!is_member) { // drop message if we are not yet member of the group
63
if(log.isInfoEnabled()) log.info("dropping message " + evt.getArg());
64                     return;
65                 }
66                 break;
67
68         }
69         passUp(evt); // Pass up to the layer above us
70
}
71
72
73 }
74
Popular Tags