001: package org.jgroups.tests;
002:
003: import org.jgroups.*;
004: import org.jgroups.util.RspList;
005: import org.jgroups.blocks.MessageDispatcher;
006: import org.jgroups.blocks.GroupRequest;
007: import org.jgroups.blocks.RequestHandler;
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: MessageDispatcherShunTest.java,v 1.2 2005/10/31 11:02:54 belaban Exp $
022: */
023: public class MessageDispatcherShunTest implements MembershipListener,
024: RequestHandler {
025: JChannel channel;
026: MessageDispatcher 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 MessageDispatcherShunTest().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.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
050: disp = new MessageDispatcher(channel, null, this , this , false, // deadlock detection is disabled
051: true); // concurrent processing is enabled
052: channel.connect("MessageDispatcherTestGroup");
053: mainLoop();
054: }
055:
056: private void mainLoop() throws IOException {
057: while (true) {
058: int c;
059: System.in.skip(System.in.available());
060: System.out
061: .println("\n[1] Send [2] Shun [3] Print view [q] Quit");
062: c = System.in.read();
063: switch (c) {
064: case -1:
065: break;
066: case '1':
067: sendMessage();
068: break;
069: case '2':
070: shun();
071: break;
072: case '3':
073: View v = channel.getView();
074: System.out.println("View: " + v);
075: break;
076: case 'q':
077: channel.close();
078: return;
079: default:
080: break;
081: }
082: }
083: }
084:
085: private void shun() {
086: System.out.println("shunning this member");
087: channel.up(new Event(Event.EXIT));
088: }
089:
090: private void sendMessage() {
091: RspList rsp_list;
092: Message msg = new Message(null, null, "Hello world");
093: View v = channel.getView();
094: if (v == null)
095: return;
096: Vector members = new Vector(v.getMembers());
097: System.out.println("sending to " + members);
098: rsp_list = disp.castMessage(members, msg, GroupRequest.GET_ALL,
099: 0);
100: System.out.println("responses:\n" + rsp_list);
101: }
102:
103: private static void help() {
104: System.out
105: .println("MessageDispatcherShunTest [-help] [-props <props>]");
106: }
107:
108: public Object handle(Message msg) {
109: return "same to you";
110: }
111:
112: public void viewAccepted(View new_view) {
113: System.out.println("-- view: " + new_view);
114: }
115:
116: public void suspect(Address suspected_mbr) {
117: }
118:
119: public void block() {
120: }
121: }
|