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.io.IOException;
010: import java.util.Vector;
011:
012: /**
013: * Tests message sending and shunning.Use:
014: * <ol>
015: * <li>Start multiple instances
016: * <li>Send a few messages
017: * <li>Shun a member
018: * <li>Send a message from another member (must work !)
019: * </ol>
020: * @author Bela Ban
021: * @version $Id: RpcDispatcherShunTest.java,v 1.2 2005/11/12 06:38:44 belaban Exp $
022: */
023: public class RpcDispatcherShunTest implements MembershipListener,
024: RequestHandler, ChannelListener {
025: JChannel channel;
026: RpcDispatcher disp;
027:
028: public static void main(String[] args) {
029: String props = null;
030: for (int i = 0; i < args.length; i++) {
031: String arg = args[i];
032: if (arg.equals("-props")) {
033: props = args[++i];
034: continue;
035: }
036: help();
037: return;
038: }
039: try {
040: new RpcDispatcherShunTest().start(props);
041: } catch (Exception e) {
042: e.printStackTrace();
043: }
044: }
045:
046: private void start(String props) throws IOException,
047: ChannelException {
048: channel = new JChannel(props);
049: channel.addChannelListener(this );
050: channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
051: disp = new RpcDispatcher(channel, null, this , this , false, // deadlock detection is disabled
052: true); // concurrent processing is enabled
053: channel.connect("MessageDispatcherTestGroup");
054: mainLoop();
055: }
056:
057: private void mainLoop() throws IOException {
058: while (true) {
059: int c;
060: System.in.skip(System.in.available());
061: System.out
062: .println("\n[1] Send [2] Shun [3] Print view [q] Quit");
063: c = System.in.read();
064: switch (c) {
065: case -1:
066: break;
067: case '1':
068: invokeGroupMethod();
069: break;
070: case '2':
071: shun();
072: break;
073: case '3':
074: View v = channel.getView();
075: System.out.println("View: " + v);
076: break;
077: case 'q':
078: channel.close();
079: return;
080: default:
081: break;
082: }
083: }
084: }
085:
086: public Object helloWorld() {
087: System.out.println("method helloWorld() was called");
088: return "same to you";
089: }
090:
091: private void shun() {
092: System.out.println("shunning this member");
093: channel.up(new Event(Event.EXIT));
094: }
095:
096: private void invokeGroupMethod() {
097: RspList rsp_list;
098: View v = channel.getView();
099: if (v == null)
100: return;
101: Vector members = new Vector(v.getMembers());
102: System.out.println("sending to " + members);
103: rsp_list = disp.callRemoteMethods(members, "helloWorld", null,
104: (String[]) null, GroupRequest.GET_ALL, 0);
105: System.out.println("responses:\n" + rsp_list);
106: }
107:
108: private static void help() {
109: System.out
110: .println("MessageDispatcherShunTest [-help] [-props <props>]");
111: }
112:
113: public Object handle(Message msg) {
114: return "same to you";
115: }
116:
117: public void viewAccepted(View new_view) {
118: System.out.println("-- view: " + new_view);
119: }
120:
121: public void suspect(Address suspected_mbr) {
122: }
123:
124: public void block() {
125: }
126:
127: public void channelConnected(Channel channel) {
128: // System.out.println("-- channel connected");
129: }
130:
131: public void channelDisconnected(Channel channel) {
132: // System.out.println("-- channel disconnected");
133: }
134:
135: public void channelClosed(Channel channel) {
136: // System.out.println("-- channel closed");
137: }
138:
139: public void channelShunned() {
140: // System.out.println("-- channel shunned");
141: }
142:
143: public void channelReconnected(Address addr) {
144: // System.out.println("-- channel reconnected");
145: }
146: }
|