KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > debug > ProtocolTester


1 // $Id: ProtocolTester.java,v 1.4 2004/09/23 16:29:16 belaban Exp $
2

3 package org.jgroups.debug;
4
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.jgroups.Event;
9 import org.jgroups.Message;
10 import org.jgroups.stack.Configurator;
11 import org.jgroups.stack.Protocol;
12 import org.jgroups.util.Util;
13
14
15 /**
16  * Generic class to test one or more protocol layers directly. Sets up a protocol stack consisting of
17  * the top layer (which is essentially given by the user and is the test harness), the specified protocol(s) and
18  * a bottom layer (which is automatically added), which sends all received messages immediately back up the
19  * stack (swapping sender and receiver address of the message).
20  *
21  * @author Bela Ban
22  * @author March 23 2001
23  */

24 public class ProtocolTester {
25     Protocol harness=null, top, bottom;
26     String JavaDoc props=null;
27     Configurator config=null;
28
29     protected final Log log=LogFactory.getLog(this.getClass());
30
31
32     public ProtocolTester(String JavaDoc prot_spec, Protocol harness) throws Exception JavaDoc {
33         if(prot_spec == null || harness == null)
34             throw new Exception JavaDoc("ProtocolTester(): prot_spec or harness is null");
35
36         props=prot_spec;
37         this.harness=harness;
38         props="LOOPBACK:" + props; // add a loopback layer at the bottom of the stack
39

40         config=new Configurator();
41         top=config.setupProtocolStack(props, null);
42         harness.setDownProtocol(top);
43         top.setUpProtocol(harness); // +++
44

45         bottom=getBottomProtocol(top);
46         config.startProtocolStack(bottom);
47
48         // has to be set after StartProtocolStack, otherwise the up and down handler threads in the harness
49
// will be started as well (we don't want that) !
50
// top.setUpProtocol(harness);
51
}
52
53
54     public String JavaDoc getProtocolSpec() {
55         return props;
56     }
57
58
59     public void stop() {
60         Protocol p;
61         if(harness != null) {
62             p=harness;
63             while(p != null) {
64                 p.stop();
65                 p=p.getDownProtocol();
66             }
67             config.stopProtocolStack(harness);
68         }
69         else if(top != null) {
70             p=top;
71             while(p != null) {
72                 p.stop();
73                 p=p.getDownProtocol();
74             }
75             config.stopProtocolStack(top);
76         }
77     }
78
79
80     Protocol getBottomProtocol(Protocol top) {
81         Protocol tmp;
82
83         if(top == null)
84             return null;
85
86         tmp=top;
87         while(tmp.getDownProtocol() != null)
88             tmp=tmp.getDownProtocol();
89         return tmp;
90     }
91
92
93     public static void main(String JavaDoc[] args) {
94         String JavaDoc props;
95         ProtocolTester t;
96         Harness h;
97
98         if(args.length < 1 || args.length > 2) {
99             System.out.println("ProtocolTester <protocol stack spec> [-trace]");
100             return;
101         }
102         props=args[0];
103
104         try {
105             h=new Harness();
106             t=new ProtocolTester(props, h);
107             System.out.println("protocol specification is " + t.getProtocolSpec());
108             h.down(new Event(Event.BECOME_SERVER));
109             for(int i=0; i < 5; i++) {
110                 System.out.println("Sending msg #" + i);
111                 h.down(new Event(Event.MSG, new Message(null, null, "Hello world #" + i)));
112             }
113             Util.sleep(500);
114             t.stop();
115         }
116         catch(Exception JavaDoc ex) {
117             System.err.println(ex);
118         }
119     }
120
121
122     private static class Harness extends Protocol {
123
124         public String JavaDoc getName() {
125             return "Harness";
126         }
127
128
129         public void up(Event evt) {
130             System.out.println("Harness.up(): " + evt);
131         }
132
133     }
134
135 }
136
Popular Tags