001: // $Id: UnicastChannelTest.java,v 1.4 2004/07/05 14:15:11 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import org.jgroups.JChannel;
006: import org.jgroups.Message;
007: import org.jgroups.stack.IpAddress;
008:
009: import java.io.BufferedReader;
010: import java.io.InputStreamReader;
011:
012: /**
013: * Interactive program to test a unicast channel
014: * @author Bela Ban March 16 2003
015: */
016: public class UnicastChannelTest {
017: boolean server = false;
018: String host = "localhost";
019: int port = 0;
020: String props = null;
021: JChannel ch;
022:
023: public void start(String[] args) throws Exception {
024:
025: for (int i = 0; i < args.length; i++) {
026: String tmp = args[i];
027:
028: if ("-server".equals(tmp)) {
029: server = true;
030: continue;
031: }
032:
033: if ("-props".equals(tmp)) {
034: props = args[++i];
035: continue;
036: }
037:
038: if ("-host".equals(tmp)) {
039: host = args[++i];
040: continue;
041: }
042:
043: if ("-port".equals(tmp)) {
044: port = Integer.parseInt(args[++i]);
045: continue;
046: }
047:
048: help();
049: return;
050: }
051:
052: if (server) {
053: runServer();
054: } else {
055: runClient();
056: }
057:
058: }
059:
060: void runClient() throws Exception {
061: IpAddress addr;
062: Message msg;
063: Object obj;
064: String line;
065: BufferedReader reader;
066:
067: ch = new JChannel(props);
068: ch.connect(null); // unicast channel
069:
070: addr = new IpAddress(host, port);
071: reader = new BufferedReader(new InputStreamReader(System.in));
072:
073: while (true) {
074: System.out.print("> ");
075: line = reader.readLine();
076: if (line.startsWith("quit") || line.startsWith("exit")) {
077: ch.close();
078: return;
079: }
080: msg = new Message(addr, null, line);
081: ch.send(msg);
082: while ((obj = ch.peek(1000)) != null) {
083: obj = ch.receive(1000);
084: if (obj == null)
085: break;
086: if (!(obj instanceof Message)) {
087: System.out.println("<-- " + obj);
088: } else {
089: System.out.println("<-- "
090: + ((Message) obj).getObject());
091: }
092: }
093: }
094: }
095:
096: void runServer() throws Exception {
097: Object obj;
098: Message msg, rsp;
099:
100: ch = new JChannel(props);
101: ch.connect(null); // this makes it a unicast channel
102: System.out.println("server started at " + new java.util.Date()
103: + ", listening on " + ch.getLocalAddress());
104: while (true) {
105: obj = ch.receive(0);
106: if (!(obj instanceof Message)) {
107: System.out.println(obj);
108: } else {
109: msg = (Message) obj;
110: System.out.println("-- " + msg.getObject());
111: rsp = new Message(msg.getSrc(), null, "ack for "
112: + msg.getObject());
113: ch.send(rsp);
114: }
115: }
116: }
117:
118: void help() {
119: System.out
120: .println("UnicastChannelTest [-help] [-server] [-props <props>]"
121: + "[-host <host>] [-port <port>]");
122: }
123:
124: public static void main(String[] args) {
125: try {
126: new UnicastChannelTest().start(args);
127: } catch (Exception ex) {
128: ex.printStackTrace();
129: }
130: }
131:
132: }
|