001: package org.jgroups.tests;
002:
003: import org.jgroups.*;
004: import org.jgroups.blocks.GroupRequest;
005: import org.jgroups.blocks.RequestHandler;
006: import org.jgroups.blocks.RpcDispatcher;
007: import org.jgroups.util.RspList;
008:
009: import java.util.Vector;
010:
011: /**
012: * Tests cluster method invocations on disconnected and connected services
013: * @author Bela Ban
014: * @version $Id: RpcDispatcherMultiplexerTest.java,v 1.5 2006/09/01 07:44:16 belaban Exp $
015: */
016: public class RpcDispatcherMultiplexerTest implements
017: MembershipListener, RequestHandler, ChannelListener {
018: Channel channel;
019: JChannelFactory factory = null;
020: RpcDispatcher disp;
021: String props = null;
022: boolean spurious_channel_created = false;
023:
024: public static void main(String[] args) throws Exception {
025: String props = null;
026: for (int i = 0; i < args.length; i++) {
027: String arg = args[i];
028: if (arg.equals("-props")) {
029: props = args[++i];
030: continue;
031: }
032: help();
033: return;
034: }
035: new RpcDispatcherMultiplexerTest().start(props);
036: }
037:
038: private void start(String props) throws Exception {
039: if (factory == null)
040: factory = new JChannelFactory();
041: if (props == null)
042: this .props = "stacks.xml";
043: else
044: this .props = props;
045: factory.setMultiplexerConfig(this .props);
046: channel = factory.createMultiplexerChannel("udp", "MyId");
047:
048: if (!spurious_channel_created) {
049: // create one additional channel so that we don't close the JGroups channel when disconnecting from MuxChannel !
050: Channel tmp = factory.createMultiplexerChannel("udp",
051: "tempChannel");
052: tmp.connect("bla");
053: spurious_channel_created = true;
054: }
055:
056: channel.addChannelListener(this );
057: channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
058: disp = new RpcDispatcher(channel, null, this , this , false, // deadlock detection is disabled
059: true); // concurrent processing is enabled
060: channel.connect("MessageDispatcherTestGroup");
061: mainLoop();
062: }
063:
064: private void stop() {
065: disp.stop();
066: channel.close();
067: }
068:
069: private void mainLoop() throws Exception {
070: while (true) {
071: int c;
072: System.in.skip(System.in.available());
073: System.out
074: .println("\n[1] Send [2] Start service [3] Stop service [4] Print view [q] Quit");
075: c = System.in.read();
076: switch (c) {
077: case -1:
078: break;
079: case '1':
080: invokeGroupMethod();
081: break;
082: case '2':
083: start(this .props);
084: break;
085: case '3':
086: stop();
087: break;
088: case '4':
089: View v = channel.getView();
090: System.out.println("View: " + v);
091: break;
092: case 'q':
093: channel.close();
094: return;
095: default:
096: break;
097: }
098: }
099: }
100:
101: public Object helloWorld() {
102: System.out.println("method helloWorld() was called");
103: return "same to you";
104: }
105:
106: private void invokeGroupMethod() {
107: RspList rsp_list;
108: View v = channel.getView();
109: if (v == null)
110: return;
111: Vector members = new Vector(v.getMembers());
112: System.out.println("sending to " + members);
113: rsp_list = disp.callRemoteMethods(members, "helloWorld", null,
114: (String[]) null, GroupRequest.GET_ALL, 0);
115: System.out.println("responses:\n" + rsp_list);
116: }
117:
118: private static void help() {
119: System.out
120: .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: public void channelConnected(Channel channel) {
138: // System.out.println("-- channel connected");
139: }
140:
141: public void channelDisconnected(Channel channel) {
142: // System.out.println("-- channel disconnected");
143: }
144:
145: public void channelClosed(Channel channel) {
146: // System.out.println("-- channel closed");
147: }
148:
149: public void channelShunned() {
150: // System.out.println("-- channel shunned");
151: }
152:
153: public void channelReconnected(Address addr) {
154: // System.out.println("-- channel reconnected");
155: }
156: }
|