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