1 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 24 public class ProtocolTester { 25 Protocol harness=null, top, bottom; 26 String props=null; 27 Configurator config=null; 28 29 protected final Log log=LogFactory.getLog(this.getClass()); 30 31 32 public ProtocolTester(String prot_spec, Protocol harness) throws Exception { 33 if(prot_spec == null || harness == null) 34 throw new Exception ("ProtocolTester(): prot_spec or harness is null"); 35 36 props=prot_spec; 37 this.harness=harness; 38 props="LOOPBACK:" + props; 40 config=new Configurator(); 41 top=config.setupProtocolStack(props, null); 42 harness.setDownProtocol(top); 43 top.setUpProtocol(harness); 45 bottom=getBottomProtocol(top); 46 config.startProtocolStack(bottom); 47 48 } 52 53 54 public String 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 [] args) { 94 String 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 ex) { 117 System.err.println(ex); 118 } 119 } 120 121 122 private static class Harness extends Protocol { 123 124 public String 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 |