001: // $Id: Bsh.java,v 1.8 2005/05/30 16: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.protocols.BSH;
008: import org.jgroups.stack.IpAddress;
009:
010: import java.io.BufferedReader;
011: import java.io.InputStreamReader;
012:
013: /**
014: * Interactive program to test a unicast channel
015: * @author Bela Ban March 16 2003
016: */
017: public class Bsh {
018: String host = "localhost";
019: int port = 0;
020: long timeout = 0;
021: String props = null;
022: JChannel ch;
023:
024: public void start(String[] args) throws Exception {
025:
026: for (int i = 0; i < args.length; i++) {
027: String tmp = args[i];
028:
029: if ("-props".equals(tmp)) {
030: props = args[++i];
031: continue;
032: }
033:
034: if ("-host".equals(tmp)) {
035: host = args[++i];
036: continue;
037: }
038:
039: if ("-port".equals(tmp)) {
040: port = Integer.parseInt(args[++i]);
041: continue;
042: }
043:
044: if ("-timeout".equals(tmp)) {
045: timeout = Long.parseLong(args[++i]);
046: continue;
047: }
048:
049: help();
050: return;
051: }
052:
053: runClient();
054: }
055:
056: void runClient() throws Exception {
057: IpAddress addr;
058: Message msg;
059: String line;
060: BufferedReader reader;
061: BSH.BshHeader hdr;
062:
063: ch = new JChannel(props);
064: ch.connect(null); // unicast channel
065:
066: addr = new IpAddress(host, port);
067: reader = new BufferedReader(new InputStreamReader(System.in));
068:
069: while (true) {
070: System.out.print("> ");
071: line = reader.readLine();
072: if (line.startsWith("quit") || line.startsWith("exit")) {
073: ch.close();
074: return;
075: }
076: if (line.startsWith("get")) {
077: int i = 1;
078: while (ch.getNumMessages() > 0) {
079: Object obj = ch.receive(1000);
080: System.out.println("#" + i++ + ": " + print(obj)
081: + ", obj=" + obj);
082: }
083: continue;
084: }
085: if (line.startsWith("destroyInterpreter")) {
086: msg = new Message(addr, null, line.getBytes());
087: hdr = new BSH.BshHeader(BSH.BshHeader.REQ);
088: msg.putHeader("BSH", hdr);
089: sendAndReceive(msg, 1000);
090: continue;
091: }
092:
093: msg = new Message(addr, null, line.getBytes());
094: hdr = new BSH.BshHeader(BSH.BshHeader.REQ);
095: msg.putHeader("BSH", hdr);
096: sendAndReceive(msg, timeout);
097: }
098: }
099:
100: Object print(Object obj) {
101: if (obj == null)
102: return null;
103:
104: if (obj instanceof Message)
105: return ((Message) obj).getObject();
106: else
107: return obj;
108: }
109:
110: void sendAndReceive(Message msg, long timeout) {
111: Object obj, result;
112: try {
113: ch.send(msg);
114: obj = ch.receive(timeout);
115:
116: if (obj == null || !(obj instanceof Message)) {
117: System.err.println("<-- " + obj);
118: } else {
119: result = ((Message) obj).getObject();
120: System.out.println("<-- " + result);
121: }
122:
123: // System.out.println("** " + ch.getNumMessages() + " are waiting");
124: } catch (Throwable t) {
125: System.err.println("Bsh.sendAndReceive(): " + t);
126: }
127: }
128:
129: void help() {
130: System.out.println("Bsh [-help] [-props <props>]"
131: + "[-host <host>] [-port <port>] [-timeout <timeout>]");
132: }
133:
134: public static void main(String[] args) {
135: try {
136: new Bsh().start(args);
137: } catch (Exception ex) {
138: ex.printStackTrace();
139: }
140: }
141:
142: }
|