KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > Bsh


1 // $Id: Bsh.java,v 1.6 2004/07/05 14:15:11 belaban Exp $
2

3
4 package org.jgroups.tests;
5
6
7 import org.jgroups.JChannel;
8 import org.jgroups.Message;
9 import org.jgroups.protocols.BSH;
10 import org.jgroups.stack.IpAddress;
11
12 import java.io.BufferedReader JavaDoc;
13 import java.io.InputStreamReader JavaDoc;
14
15
16 /**
17  * Interactive program to test a unicast channel
18  * @author Bela Ban March 16 2003
19  */

20 public class Bsh {
21     String JavaDoc host="localhost";
22     int port=0;
23     long timeout=0;
24     String JavaDoc props=null;
25     JChannel ch;
26
27
28     public void start(String JavaDoc[] args) throws Exception JavaDoc {
29
30         for(int i=0; i < args.length; i++) {
31             String JavaDoc tmp=args[i];
32
33             if("-props".equals(tmp)) {
34                 props=args[++i];
35                 continue;
36             }
37
38             if("-host".equals(tmp)) {
39                 host=args[++i];
40                 continue;
41             }
42
43             if("-port".equals(tmp)) {
44                 port=Integer.parseInt(args[++i]);
45                 continue;
46             }
47
48             if("-timeout".equals(tmp)) {
49                 timeout=Long.parseLong(args[++i]);
50                 continue;
51             }
52
53             help();
54             return;
55         }
56
57
58         runClient();
59     }
60
61     void runClient() throws Exception JavaDoc {
62         IpAddress addr;
63         Message msg;
64         String JavaDoc line;
65         BufferedReader JavaDoc reader;
66         BSH.BshHeader hdr;
67
68         ch=new JChannel(props);
69         ch.connect(null); // unicast channel
70

71         addr=new IpAddress(host, port);
72         reader= new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
73
74         while(true) {
75             System.out.print("> ");
76             line=reader.readLine();
77             if(line.startsWith("quit") || line.startsWith("exit")) {
78                 ch.close();
79                 return;
80             }
81             if(line.startsWith("get")) {
82                 int i=1;
83                 while(ch.getNumMessages() > 0) {
84                     Object JavaDoc obj=ch.receive(1000);
85                     System.out.println("#" + i++ + ": " + print(obj) +
86                                        ", obj=" + obj);
87                 }
88                 continue;
89             }
90             if(line.startsWith("destroyInterpreter")) {
91                 msg=new Message(addr, null, line.getBytes());
92                 hdr=new BSH.BshHeader(BSH.BshHeader.REQ);
93                 msg.putHeader("BSH", hdr);
94                 sendAndReceive(msg, 1000);
95                 continue;
96             }
97
98             msg=new Message(addr, null, line.getBytes());
99             hdr=new BSH.BshHeader(BSH.BshHeader.REQ);
100             msg.putHeader("BSH", hdr);
101             sendAndReceive(msg, timeout);
102         }
103     }
104
105     Object JavaDoc print(Object JavaDoc obj) {
106         if(obj == null)
107             return null;
108
109         if(obj instanceof Message)
110             return ((Message)obj).getObject();
111         else
112             return obj;
113     }
114
115
116     void sendAndReceive(Message msg, long timeout) {
117         Object JavaDoc obj, result;
118         try {
119             ch.send(msg);
120             obj=ch.receive(timeout);
121
122             if(obj == null || !(obj instanceof Message)) {
123                 System.err.println("<-- " + obj);
124             }
125             else {
126                 result=((Message)obj).getObject();
127                 System.out.println("<-- " + result);
128             }
129
130             // System.out.println("** " + ch.getNumMessages() + " are waiting");
131
}
132         catch(Throwable JavaDoc t) {
133             System.err.println("Bsh.sendAndReceive(): " + t);
134         }
135     }
136
137     void help() {
138         System.out.println("Bsh [-help] [-props <props>]" +
139                            "[-host <host>] [-port <port>] [-timeout <timeout>]");
140     }
141
142
143     public static void main(String JavaDoc[] args) {
144         try {
145             new Bsh().start(args);
146         }
147         catch(Exception JavaDoc ex) {
148             ex.printStackTrace();
149         }
150     }
151
152 }
153
Popular Tags