01: // $Id: EXAMPLE.java,v 1.4 2005/08/08 12:45:42 belaban Exp $
02:
03: package org.jgroups.protocols;
04:
05: import org.jgroups.Event;
06: import org.jgroups.Message;
07: import org.jgroups.View;
08: import org.jgroups.stack.Protocol;
09:
10: import java.io.Serializable;
11: import java.util.Vector;
12:
13: class ExampleHeader implements Serializable {
14: private static final long serialVersionUID = -8802317525466899597L;
15:
16: // your variables
17:
18: ExampleHeader() {
19: }
20:
21: public String toString() {
22: return "[EXAMPLE: <variables> ]";
23: }
24: }
25:
26: /**
27: * Example of a protocol layer. Contains no real functionality, can be used as a template.
28: */
29:
30: public class EXAMPLE extends Protocol {
31: final Vector members = new Vector();
32:
33: /** All protocol names have to be unique ! */
34: public String getName() {
35: return "EXAMPLE";
36: }
37:
38: /** Just remove if you don't need to reset any state */
39: public static void reset() {
40: }
41:
42: public void up(Event evt) {
43: Message msg;
44:
45: switch (evt.getType()) {
46:
47: case Event.MSG:
48: msg = (Message) evt.getArg();
49: // Do something with the event, e.g. extract the message and remove a header.
50: // Optionally pass up
51: break;
52: }
53:
54: passUp(evt); // Pass up to the layer above us
55: }
56:
57: public void down(Event evt) {
58: Message msg;
59:
60: switch (evt.getType()) {
61: case Event.TMP_VIEW:
62: case Event.VIEW_CHANGE:
63: Vector new_members = ((View) evt.getArg()).getMembers();
64: synchronized (members) {
65: members.removeAllElements();
66: if (new_members != null && new_members.size() > 0)
67: for (int i = 0; i < new_members.size(); i++)
68: members.addElement(new_members.elementAt(i));
69: }
70: passDown(evt);
71: break;
72:
73: case Event.MSG:
74: msg = (Message) evt.getArg();
75: // Do something with the event, e.g. add a header to the message
76: // Optionally pass down
77: break;
78: }
79:
80: passDown(evt); // Pass on to the layer below us
81: }
82:
83: }
|