1 package org.jgroups.tests; 2 3 import org.jgroups.*; 4 import org.jgroups.blocks.GroupRequest; 5 import org.jgroups.blocks.RequestHandler; 6 import org.jgroups.blocks.RpcDispatcher; 7 import org.jgroups.util.RspList; 8 9 import java.util.Vector ; 10 11 16 public class RpcDispatcherMultiplexerTest implements MembershipListener, RequestHandler, ChannelListener { 17 Channel channel; 18 JChannelFactory factory=null; 19 RpcDispatcher disp; 20 String props=null; 21 boolean spurious_channel_created=false; 22 23 24 public static void main(String [] args) throws Exception { 25 String props=null; 26 for(int i=0; i < args.length; i++) { 27 String arg=args[i]; 28 if(arg.equals("-props")) { 29 props=args[++i]; 30 continue; 31 } 32 help(); 33 return; 34 } 35 new RpcDispatcherMultiplexerTest().start(props); 36 } 37 38 private void start(String props) throws Exception { 39 if(factory == null) 40 factory=new JChannelFactory(); 41 if(props == null) 42 this.props="stacks.xml"; 43 else 44 this.props=props; 45 factory.setMultiplexerConfig(this.props); 46 channel=factory.createMultiplexerChannel("udp", "MyId"); 47 48 if(!spurious_channel_created) { 49 Channel tmp=factory.createMultiplexerChannel("udp", "tempChannel"); 51 tmp.connect("bla"); 52 spurious_channel_created=true; 53 } 54 55 channel.addChannelListener(this); 56 channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE); 57 disp=new RpcDispatcher(channel, null, this, this, 58 false, true); channel.connect("MessageDispatcherTestGroup"); 61 mainLoop(); 62 } 63 64 65 66 private void stop() { 67 disp.stop(); 68 channel.close(); 69 } 70 71 private void mainLoop() throws Exception { 72 while(true) { 73 int c; 74 System.in.skip(System.in.available()); 75 System.out.println("\n[1] Send [2] Start service [3] Stop service [4] Print view [q] Quit"); 76 c=System.in.read(); 77 switch(c) { 78 case -1: 79 break; 80 case '1': 81 invokeGroupMethod(); 82 break; 83 case '2': 84 start(this.props); 85 break; 86 case '3': 87 stop(); 88 break; 89 case '4': 90 View v=channel.getView(); 91 System.out.println("View: " + v); 92 break; 93 case 'q': 94 channel.close(); 95 return; 96 default: 97 break; 98 } 99 } 100 } 101 102 public Object helloWorld() { 103 System.out.println("method helloWorld() was called"); 104 return "same to you"; 105 } 106 107 108 private void invokeGroupMethod() { 109 RspList rsp_list; 110 View v=channel.getView(); 111 if(v == null) 112 return; 113 Vector members=new Vector (v.getMembers()); 114 System.out.println("sending to " + members); 115 rsp_list=disp.callRemoteMethods(members, "helloWorld", null, (String [])null, GroupRequest.GET_ALL, 0); 116 System.out.println("responses:\n" + rsp_list); 117 } 118 119 private static void help() { 120 System.out.println("MessageDispatcherShunTest [-help] [-props <props>]"); 121 } 122 123 public Object handle(Message msg) { 124 return "same to you"; 125 } 126 127 public void viewAccepted(View new_view) { 128 System.out.println("-- view: " + new_view); 129 } 130 131 public void suspect(Address suspected_mbr) { 132 } 133 134 public void block() { 135 } 136 137 138 public void channelConnected(Channel channel) { 139 } 141 142 public void channelDisconnected(Channel channel) { 143 } 145 146 public void channelClosed(Channel channel) { 147 } 149 150 public void channelShunned() { 151 } 153 154 public void channelReconnected(Address addr) { 155 } 157 } 158 | Popular Tags |