1 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 ; 13 import java.io.InputStreamReader ; 14 15 16 20 public class Bsh { 21 String host="localhost"; 22 int port=0; 23 long timeout=0; 24 String props=null; 25 JChannel ch; 26 27 28 public void start(String [] args) throws Exception { 29 30 for(int i=0; i < args.length; i++) { 31 String 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 { 62 IpAddress addr; 63 Message msg; 64 String line; 65 BufferedReader reader; 66 BSH.BshHeader hdr; 67 68 ch=new JChannel(props); 69 ch.connect(null); 71 addr=new IpAddress(host, port); 72 reader= new BufferedReader (new InputStreamReader (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 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 print(Object 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 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 } 132 catch(Throwable 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 [] args) { 144 try { 145 new Bsh().start(args); 146 } 147 catch(Exception ex) { 148 ex.printStackTrace(); 149 } 150 } 151 152 } 153 | Popular Tags |