001: // $Id: ConnectionTableDemo_NIO.java,v 1.1 2006/09/14 08:11:31 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import org.jgroups.Address;
006: import org.jgroups.blocks.ConnectionTableNIO;
007: import org.jgroups.stack.IpAddress;
008:
009: import java.io.BufferedReader;
010: import java.io.InputStreamReader;
011:
012: public class ConnectionTableDemo_NIO implements
013: ConnectionTableNIO.Receiver,
014: ConnectionTableNIO.ConnectionListener {
015: ConnectionTableNIO ct = null;
016: String dst_host = null;
017: int dst_port = 0;
018:
019: public void receive(Address sender, byte[] data, int offset,
020: int length) {
021: String s = new String(data, offset, length);
022: System.out.println("<-- " + s + " (from " + sender + ')');
023: }
024:
025: public void connectionOpened(Address peer_addr) {
026: System.out.println("** Connection to " + peer_addr + " opened");
027: }
028:
029: public void connectionClosed(Address peer_addr) {
030: System.out.println("** Connection to " + peer_addr + " closed");
031: }
032:
033: public void start(int local_port, String dst_host, int dst_port,
034: long reaper_interval, long conn_expire_time)
035: throws Exception {
036: BufferedReader in;
037: Address dest;
038: String line;
039:
040: if (reaper_interval > 0 || conn_expire_time > 0)
041: ct = new ConnectionTableNIO(local_port, reaper_interval,
042: conn_expire_time);
043: else
044: ct = new ConnectionTableNIO(local_port);
045: ct.addConnectionListener(this );
046: this .dst_host = dst_host;
047: this .dst_port = dst_port;
048: dest = new IpAddress(dst_host, dst_port);
049: ct.setReceiver(this );
050:
051: // System.out.println("**local addr is " + ct.getLocalAddress());
052:
053: in = new BufferedReader(new InputStreamReader(System.in));
054: byte[] data;
055: while (true) {
056: try {
057: System.out.print("> ");
058: System.out.flush();
059: line = in.readLine();
060: if (line == null
061: || line.startsWith("quit".toLowerCase())
062: || line.startsWith("exit".toLowerCase()))
063: break;
064: if (line.startsWith("conns")) {
065: System.out.println(ct);
066: continue;
067: }
068: data = line.getBytes();
069: ct.send(dest, data, 0, data.length);
070: } catch (Exception e) {
071: e.printStackTrace();
072: }
073: }
074: ct.stop();
075: }
076:
077: public static void main(String[] args) {
078: String host = "localhost", tmp;
079: int port = 6666, local_port = 5555;
080: long reaper_interval = 0;
081: long conn_expire_time = 0;
082:
083: for (int i = 0; i < args.length; i++) {
084: tmp = args[i];
085: if ("-local_port".equals(tmp)) {
086: local_port = Integer.parseInt(args[++i]);
087: continue;
088: }
089: if ("-remote_host".equals(tmp)) {
090: host = args[++i];
091: continue;
092: }
093: if ("-remote_port".equals(tmp)) {
094: port = Integer.parseInt(args[++i]);
095: continue;
096: }
097: if ("-reaper_interval".equals(tmp)) {
098: reaper_interval = Long.parseLong(args[++i]);
099: 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)
112: reaper_interval = 60000;
113: if (conn_expire_time <= 0)
114: conn_expire_time = 300000;
115: new ConnectionTableDemo_NIO().start(local_port, host,
116: port, reaper_interval, conn_expire_time);
117: } else {
118: new ConnectionTableDemo_NIO().start(local_port, host,
119: port, 0, 0);
120: }
121: } catch (Exception ex) {
122: System.err.println("ConnectionTableTest_NIO.main(): " + ex);
123: ex.printStackTrace();
124: }
125: }
126:
127: static void help() {
128: System.out
129: .println("ConnectionTableTest_NIO [-help] [-local_port <port>] [-remote_host <host>] "
130: + "[-remote_port <port>] [-reaper_interval <interval (msecs)>] "
131: + "[-conn_expire_time <time (msecs)>]");
132: }
133:
134: }
|