KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > ConnectionTableTest


1 // $Id: ConnectionTableTest.java,v 1.5 2004/07/05 14:15:11 belaban Exp $
2

3 package org.jgroups.tests;
4
5 import org.jgroups.Address;
6 import org.jgroups.Message;
7 import org.jgroups.blocks.ConnectionTable;
8 import org.jgroups.stack.IpAddress;
9
10 import java.io.BufferedReader JavaDoc;
11 import java.io.InputStreamReader JavaDoc;
12
13
14 public class ConnectionTableTest implements ConnectionTable.Receiver, ConnectionTable.ConnectionListener {
15     ConnectionTable ct=null;
16     String JavaDoc dst_host=null;
17     int dst_port=0;
18
19
20     public void receive(Message m) {
21         String JavaDoc s=(String JavaDoc)m.getObject();
22         System.out.println("<-- " + s + " (from " + m.getSrc() + ')');
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 JavaDoc dst_host, int dst_port,
35                       long reaper_interval, long conn_expire_time) throws Exception JavaDoc {
36         BufferedReader JavaDoc in;
37         String JavaDoc line;
38         Message msg;
39
40         if(reaper_interval > 0 || conn_expire_time > 0)
41             ct=new ConnectionTable(local_port, reaper_interval, conn_expire_time);
42         else
43             ct=new ConnectionTable(local_port);
44         ct.addConnectionListener(this);
45         this.dst_host=dst_host;
46         this.dst_port=dst_port;
47         ct.setReceiver(this);
48
49         // System.out.println("**local addr is " + ct.getLocalAddress());
50

51         in=new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
52         while(true) {
53             try {
54                 System.out.print("> ");
55                 System.out.flush();
56                 line=in.readLine();
57                 if(line.startsWith("quit".toLowerCase()) ||
58                         line.startsWith("exit".toLowerCase()))
59                     break;
60                 if(line.startsWith("conns")) {
61                     System.out.println(ct);
62                     continue;
63                 }
64                 msg=new Message(new IpAddress(dst_host, dst_port), null, line);
65                 ct.send(msg);
66             }
67             catch(Exception JavaDoc e) {
68                 System.err.println(e);
69             }
70         }
71         ct.stop();
72     }
73
74
75     public static void main(String JavaDoc[] args) {
76         String JavaDoc host="localhost", tmp;
77         int port=6666, local_port=5555;
78         long reaper_interval=0;
79         long conn_expire_time=0;
80
81         for(int i=0; i < args.length; i++) {
82             tmp=args[i];
83             if("-local_port".equals(tmp)) {
84                 local_port=Integer.parseInt(args[++i]);
85                 continue;
86             }
87             if("-remote_host".equals(tmp)) {
88                 host=args[++i];
89                 continue;
90             }
91             if("-remote_port".equals(tmp)) {
92                 port=Integer.parseInt(args[++i]);
93                 continue;
94             }
95             if("-reaper_interval".equals(tmp)) {
96                 reaper_interval=Long.parseLong(args[++i]);
97                 continue;
98             }
99             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) reaper_interval=60000;
110                 if(conn_expire_time <= 0) conn_expire_time=300000;
111                 new ConnectionTableTest().start(local_port, host, port, reaper_interval, conn_expire_time);
112             }
113             else {
114                 new ConnectionTableTest().start(local_port, host, port, 0, 0);
115             }
116         }
117         catch(Exception JavaDoc ex) {
118             System.err.println("ConnectionTableTest.main(): " + ex);
119         }
120     }
121
122
123     static void help() {
124         System.out.println("ConnectionTableTest [-help] [-local_port <port>] [-remote_host <host>] " +
125                            "[-remote_port <port>] [-reaper_interval <interval (msecs)>] " +
126                            "[-conn_expire_time <time (msecs)>]");
127     }
128
129
130 }
131
Popular Tags