1 3 package org.jgroups.tests; 4 5 import org.jgroups.Address; 6 import org.jgroups.blocks.ConnectionTableNIO; 7 import org.jgroups.stack.IpAddress; 8 9 import java.io.BufferedReader ; 10 import java.io.InputStreamReader ; 11 12 13 public class ConnectionTableDemo_NIO implements ConnectionTableNIO.Receiver, ConnectionTableNIO.ConnectionListener { 14 ConnectionTableNIO 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 Address dest; 38 String line; 39 40 if(reaper_interval > 0 || conn_expire_time > 0) 41 ct=new ConnectionTableNIO(local_port, reaper_interval, conn_expire_time); 42 else 43 ct=new ConnectionTableNIO(local_port); 44 ct.addConnectionListener(this); 45 this.dst_host=dst_host; 46 this.dst_port=dst_port; 47 dest=new IpAddress(dst_host, dst_port); 48 ct.setReceiver(this); 49 50 52 in=new BufferedReader (new InputStreamReader (System.in)); 53 byte[] data; 54 while(true) { 55 try { 56 System.out.print("> "); 57 System.out.flush(); 58 line=in.readLine(); 59 if(line == null || line.startsWith("quit".toLowerCase()) || 60 line.startsWith("exit".toLowerCase())) 61 break; 62 if(line.startsWith("conns")) { 63 System.out.println(ct); 64 continue; 65 } 66 data=line.getBytes(); 67 ct.send(dest, data, 0, data.length); 68 } 69 catch(Exception e) { 70 e.printStackTrace(); 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_NIO().start(local_port, host, port, reaper_interval, conn_expire_time); 114 } 115 else { 116 new ConnectionTableDemo_NIO().start(local_port, host, port, 0, 0); 117 } 118 } 119 catch(Exception ex) { 120 System.err.println("ConnectionTableTest_NIO.main(): " + ex); 121 ex.printStackTrace(); 122 } 123 } 124 125 126 static void help() { 127 System.out.println("ConnectionTableTest_NIO [-help] [-local_port <port>] [-remote_host <host>] " + 128 "[-remote_port <port>] [-reaper_interval <interval (msecs)>] " + 129 "[-conn_expire_time <time (msecs)>]"); 130 } 131 132 133 } 134 | Popular Tags |