001: // $Id: ProtocolTester.java,v 1.7 2006/01/28 10:51:19 belaban Exp $
002:
003: package org.jgroups.debug;
004:
005: import org.apache.commons.logging.Log;
006: import org.apache.commons.logging.LogFactory;
007: import org.jgroups.Event;
008: import org.jgroups.Message;
009: import org.jgroups.stack.Configurator;
010: import org.jgroups.stack.Protocol;
011: import org.jgroups.stack.ProtocolStack;
012: import org.jgroups.util.Util;
013:
014: /**
015: * Generic class to test one or more protocol layers directly. Sets up a protocol stack consisting of
016: * the top layer (which is essentially given by the user and is the test harness), the specified protocol(s) and
017: * a bottom layer (which is automatically added), which sends all received messages immediately back up the
018: * stack (swapping sender and receiver address of the message).
019: *
020: * @author Bela Ban
021: * @author March 23 2001
022: */
023: public class ProtocolTester {
024: Protocol harness = null, top, bottom;
025: String props = null;
026: Configurator config = null;
027:
028: protected final Log log = LogFactory.getLog(this .getClass());
029:
030: public ProtocolTester(String prot_spec, Protocol harness)
031: throws Exception {
032: if (prot_spec == null || harness == null)
033: throw new Exception(
034: "ProtocolTester(): prot_spec or harness is null");
035:
036: props = prot_spec;
037: this .harness = harness;
038: props = "LOOPBACK:" + props; // add a loopback layer at the bottom of the stack
039:
040: config = new Configurator();
041: ProtocolStack stack = new ProtocolStack();
042: top = config.setupProtocolStack(props, stack);
043: harness.setDownProtocol(top);
044: top.setUpProtocol(harness); // +++
045:
046: bottom = getBottomProtocol(top);
047: config.startProtocolStack(bottom);
048:
049: // has to be set after StartProtocolStack, otherwise the up and down handler threads in the harness
050: // will be started as well (we don't want that) !
051: // top.setUpProtocol(harness);
052: }
053:
054: public String getProtocolSpec() {
055: return props;
056: }
057:
058: public void start() throws Exception {
059: Protocol p;
060: if (harness != null) {
061: p = harness;
062: while (p != null) {
063: p.start();
064: p = p.getDownProtocol();
065: }
066: config.startProtocolStack(harness);
067: } else if (top != null) {
068: p = top;
069: while (p != null) {
070: p.start();
071: p = p.getDownProtocol();
072: }
073: config.startProtocolStack(top);
074: }
075: }
076:
077: public void stop() {
078: Protocol p;
079: if (harness != null) {
080: p = harness;
081: while (p != null) {
082: p.stop();
083: p = p.getDownProtocol();
084: }
085: config.stopProtocolStack(harness);
086: } else if (top != null) {
087: p = top;
088: while (p != null) {
089: p.stop();
090: p = p.getDownProtocol();
091: }
092: config.stopProtocolStack(top);
093: }
094: }
095:
096: private final Protocol getBottomProtocol(Protocol top) {
097: Protocol tmp;
098:
099: if (top == null)
100: return null;
101:
102: tmp = top;
103: while (tmp.getDownProtocol() != null)
104: tmp = tmp.getDownProtocol();
105: return tmp;
106: }
107:
108: public static void main(String[] args) {
109: String props;
110: ProtocolTester t;
111: Harness h;
112:
113: if (args.length < 1 || args.length > 2) {
114: System.out
115: .println("ProtocolTester <protocol stack spec> [-trace]");
116: return;
117: }
118: props = args[0];
119:
120: try {
121: h = new Harness();
122: t = new ProtocolTester(props, h);
123: System.out.println("protocol specification is "
124: + t.getProtocolSpec());
125: h.down(new Event(Event.BECOME_SERVER));
126: for (int i = 0; i < 5; i++) {
127: System.out.println("Sending msg #" + i);
128: h.down(new Event(Event.MSG, new Message(null, null,
129: "Hello world #" + i)));
130: }
131: Util.sleep(500);
132: t.stop();
133: } catch (Exception ex) {
134: System.err.println(ex);
135: }
136: }
137:
138: private static class Harness extends Protocol {
139:
140: public String getName() {
141: return "Harness";
142: }
143:
144: public void up(Event evt) {
145: System.out.println("Harness.up(): " + evt);
146: }
147:
148: }
149:
150: }
|