KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests;
2
3 import org.jgroups.util.Util;
4
5 import java.net.DatagramPacket JavaDoc;
6 import java.net.DatagramSocket JavaDoc;
7 import java.net.InetAddress JavaDoc;
8 import java.net.MulticastSocket JavaDoc;
9
10 /**
11  * Discovers all UDP-based members running on a certain mcast address
12  * @author Bela Ban
13  * @version $Revision: 1.8 $
14  * Date: Jun 2, 2003
15  * Time: 4:35:29 PM
16  */

17 public class Probe {
18     MulticastSocket JavaDoc mcast_sock;
19
20
21     public Probe() {
22
23     }
24
25     public void start(InetAddress JavaDoc addr, InetAddress JavaDoc bind_addr, int port, int ttl, final long timeout) throws Exception JavaDoc {
26         mcast_sock=new MulticastSocket JavaDoc();
27         mcast_sock.setTimeToLive(ttl);
28         if(bind_addr != null)
29             mcast_sock.setInterface(bind_addr);
30
31         byte[] probe_buf=new byte[]{'d', 'i', 'a', 'g'};
32         DatagramPacket JavaDoc probe=new DatagramPacket JavaDoc(probe_buf, 0, probe_buf.length, addr, port);
33         mcast_sock.send(probe);
34         System.out.println("\n-- send probe on " + addr + ':' + port + '\n');
35         int i=0;
36
37         new Thread JavaDoc() {
38             public void run() {
39                 Util.sleep(timeout);
40                 mcast_sock.close();
41             }
42         }.start();
43
44         for(;;) {
45             byte[] buf=new byte[65000];
46             DatagramPacket JavaDoc rsp=new DatagramPacket JavaDoc(buf, 0, buf.length);
47             try {
48                 mcast_sock.receive(rsp);
49             }
50             catch(Throwable JavaDoc t) {
51                 System.out.println("\n");
52                 return;
53             }
54             byte[] data=rsp.getData();
55             System.out.println("\n#" + ++i + ": " + new String JavaDoc(data, 0, rsp.getLength()));
56         }
57     }
58
59
60     public static void main(String JavaDoc[] args) {
61         InetAddress JavaDoc addr=null, bind_addr=null;
62         int port=0;
63         int ttl=32;
64         long timeout=10000;
65
66         try {
67             for(int i=0; i < args.length; i++) {
68                 if("-addr".equals(args[i])) {
69                     addr=InetAddress.getByName(args[++i]);
70                     continue;
71                 }
72                 if("-bind_addr".equals(args[i])) {
73                     bind_addr=InetAddress.getByName(args[++i]);
74                     continue;
75                 }
76                 if("-port".equals(args[i])) {
77                     port=Integer.parseInt(args[++i]);
78                     continue;
79                 }
80                 if("-ttl".equals(args[i])) {
81                     ttl=Integer.parseInt(args[++i]);
82                     continue;
83                 }
84                 if("-timeout".equals(args[i])) {
85                     timeout=Long.parseLong(args[++i]);
86                     continue;
87                 }
88
89                 help();
90                 return;
91             }
92             Probe p=new Probe();
93             p.start(addr, bind_addr, port, ttl, timeout);
94         }
95         catch(Throwable JavaDoc t) {
96             t.printStackTrace();
97         }
98     }
99
100     static void help() {
101         System.out.println("Probe [-help] [-addr <addr>] [-bind_addr <addr>] [-port <port>] [-ttl <ttl>] [-timeout <timeout>]");
102     }
103 }
104
Popular Tags