01: package org.jgroups.tests;
02:
03: import org.jgroups.*;
04: import org.jgroups.util.Util;
05:
06: import java.io.Serializable;
07:
08: /**
09: * Simple test for multicast discovery. Use with ./conf/bare-bones.xml (without the UNICAST protocol).
10: * Has no error checking and only rudimentary tracing (System.err :-))
11: * @author Bela Ban
12: * @version $Id: DiscoveryTest.java,v 1.1 2006/03/16 15:47:12 belaban Exp $
13: */
14: public class DiscoveryTest {
15: Channel ch;
16:
17: public static void main(String[] args) throws Exception {
18: String props = null;
19:
20: for (int i = 0; i < args.length; i++) {
21: if (args[i].equals("-props")) {
22: props = args[++i];
23: continue;
24: }
25: System.out.println("DiscoveryTest [-props <properties>]");
26: }
27:
28: new DiscoveryTest().start(props);
29: }
30:
31: private void start(String props) throws Exception {
32: ch = new JChannel(props);
33: ch.connect("discovery");
34:
35: new Thread() {
36: public void run() {
37: while (true) {
38: try {
39: ch.send(null, null, new Data(
40: Data.DISCOVERY_REQ, null));
41: } catch (ChannelNotConnectedException e) {
42: e.printStackTrace();
43: } catch (ChannelClosedException e) {
44: e.printStackTrace();
45: }
46: Util.sleep(5000);
47: }
48: }
49: }.start();
50:
51: Object obj;
52: Data d;
53: Message msg;
54: while (ch.isConnected()) {
55: obj = ch.receive(0);
56: if (obj instanceof Message) {
57: msg = (Message) obj;
58: d = (Data) msg.getObject();
59: switch (d.type) {
60: case Data.DISCOVERY_REQ:
61: ch.send(msg.getSrc(), null, new Data(
62: Data.DISCOVEY_RSP, " my address is "
63: + ch.getLocalAddress()));
64: break;
65: case Data.DISCOVEY_RSP:
66: Address sender = msg.getSrc();
67:
68: System.out.println("received response from "
69: + sender + ": " + d.payload);
70: break;
71: default:
72: System.err.println("type " + d.type + " not known");
73: break;
74: }
75: }
76: }
77: ch.close();
78: }
79:
80: private static class Data implements Serializable {
81: private static final int DISCOVERY_REQ = 1;
82: private static final int DISCOVEY_RSP = 2;
83: private static final long serialVersionUID = 9193522684840201133L;
84:
85: int type = 0;
86: Serializable payload = null;
87:
88: public Data(int type, Serializable payload) {
89: this.type = type;
90: this.payload = payload;
91: }
92:
93: }
94: }
|