1 3 package org.jgroups.tests; 4 5 import org.jgroups.Address; 6 import org.jgroups.blocks.ConnectionTable; 7 import org.jgroups.stack.IpAddress; 8 9 import java.io.BufferedReader ; 10 import java.io.InputStreamReader ; 11 12 13 public class ConnectionTableDemo implements ConnectionTable.Receiver, ConnectionTable.ConnectionListener { 14 ConnectionTable ct=null; 15 String dst_host=null; 16 int dst_port=0; 17 18 19 public void receive(Address sender, byte[] data, int offset, int length) { 20 String s=new String (data, offset, length); 21 System.out.println("<-- " + s + " (from " + sender + ')'); 22 } 23 24 25 public void connectionOpened(Address peer_addr) { 26 System.out.println("** Connection to " + peer_addr + " opened"); 27 } 28 29 public void connectionClosed(Address peer_addr) { 30 System.out.println("** Connection to " + peer_addr + " closed"); 31 } 32 33 34 public void start(int local_port, String dst_host, int dst_port, 35 long reaper_interval, long conn_expire_time) throws Exception { 36 BufferedReader in; 37 String line; 38 Address dest; 39 byte[] data; 40 41 if(reaper_interval > 0 || conn_expire_time > 0) 42 ct=new ConnectionTable(local_port, reaper_interval, conn_expire_time); 43 else 44 ct=new ConnectionTable(local_port); 45 ct.addConnectionListener(this); 46 this.dst_host=dst_host; 47 this.dst_port=dst_port; 48 ct.setReceiver(this); 49 50 52 in=new BufferedReader (new InputStreamReader (System.in)); 53 while(true) { 54 try { 55 System.out.print("> "); 56 System.out.flush(); 57 line=in.readLine(); 58 if(line.startsWith("quit".toLowerCase()) || 59 line.startsWith("exit".toLowerCase())) 60 break; 61 if(line.startsWith("conns")) { 62 System.out.println(ct); 63 continue; 64 } 65 dest=new IpAddress(dst_host, dst_port); 66 data=line.getBytes(); 67 ct.send(dest, data, 0, data.length); 68 } 69 catch(Exception e) { 70 System.err.println(e); 71 } 72 } 73 ct.stop(); 74 } 75 76 77 public static void main(String [] args) { 78 String host="localhost", tmp; 79 int port=6666, local_port=5555; 80 long reaper_interval=0; 81 long conn_expire_time=0; 82 83 for(int i=0; i < args.length; i++) { 84 tmp=args[i]; 85 if("-local_port".equals(tmp)) { 86 local_port=Integer.parseInt(args[++i]); 87 continue; 88 } 89 if("-remote_host".equals(tmp)) { 90 host=args[++i]; 91 continue; 92 } 93 if("-remote_port".equals(tmp)) { 94 port=Integer.parseInt(args[++i]); 95 continue; 96 } 97 if("-reaper_interval".equals(tmp)) { 98 reaper_interval=Long.parseLong(args[++i]); 99 continue; 100 } 101 if("-conn_expire_time".equals(tmp)) { 102 conn_expire_time=Long.parseLong(args[++i]); 103 continue; 104 } 105 help(); 106 return; 107 } 108 109 try { 110 if(reaper_interval > 0 || conn_expire_time > 0) { 111 if(reaper_interval <= 0) reaper_interval=60000; 112 if(conn_expire_time <= 0) conn_expire_time=300000; 113 new ConnectionTableDemo().start(local_port, host, port, reaper_interval, conn_expire_time); 114 } 115 else { 116 new ConnectionTableDemo().start(local_port, host, port, 0, 0); 117 } 118 } 119 catch(Exception ex) { 120 System.err.println("ConnectionTableTest.main(): " + ex); 121 } 122 } 123 124 125 static void help() { 126 System.out.println("ConnectionTableTest [-help] [-local_port <port>] [-remote_host <host>] " + 127 "[-remote_port <port>] [-reaper_interval <interval (msecs)>] " + 128 "[-conn_expire_time <time (msecs)>]"); 129 } 130 131 132 } 133 | Popular Tags |