KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: EXAMPLE.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
10 import java.io.Serializable JavaDoc;
11 import java.util.Vector JavaDoc;
12
13
14
15 class ExampleHeader implements Serializable JavaDoc {
16     // your variables
17

18     public ExampleHeader() {
19     }
20
21     public String JavaDoc toString() {
22     return "[EXAMPLE: <variables> ]";
23     }
24 }
25
26
27 /**
28  * Example of a protocol layer. Contains no real functionality, can be used as a template.
29  */

30
31 public class EXAMPLE extends Protocol {
32     final Vector JavaDoc members=new Vector JavaDoc();
33
34     /** All protocol names have to be unique ! */
35     public String JavaDoc getName() {return "EXAMPLE";}
36
37
38
39
40
41     /** Just remove if you don't need to reset any state */
42     public void reset() {}
43
44
45
46
47     public void up(Event evt) {
48     Message msg;
49
50     switch(evt.getType()) {
51
52     case Event.MSG:
53         msg=(Message)evt.getArg();
54         // Do something with the event, e.g. extract the message and remove a header.
55
// Optionally pass up
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     case Event.TMP_VIEW:
71     case Event.VIEW_CHANGE:
72         Vector JavaDoc new_members=((View)evt.getArg()).getMembers();
73         synchronized(members) {
74         members.removeAllElements();
75         if(new_members != null && new_members.size() > 0)
76             for(int i=0; i < new_members.size(); i++)
77             members.addElement(new_members.elementAt(i));
78         }
79         passDown(evt);
80         break;
81
82     case Event.MSG:
83         msg=(Message)evt.getArg();
84         // Do something with the event, e.g. add a header to the message
85
// Optionally pass down
86
break;
87     }
88
89     passDown(evt); // Pass on to the layer below us
90
}
91
92
93
94 }
95
Popular Tags