001: package org.jgroups.tests;
002:
003: import org.jgroups.util.Util;
004:
005: import java.net.DatagramPacket;
006: import java.net.InetAddress;
007: import java.net.MulticastSocket;
008: import java.util.List;
009: import java.util.ArrayList;
010:
011: /**
012: * Discovers all UDP-based members running on a certain mcast address
013: * @author Bela Ban
014: * @version $Revision: 1.10 $
015: * Date: Jun 2, 2003
016: * Time: 4:35:29 PM
017: */
018: public class Probe {
019: MulticastSocket mcast_sock;
020: boolean running = true;
021:
022: public Probe() {
023:
024: }
025:
026: public void start(InetAddress addr, InetAddress bind_addr,
027: int port, int ttl, final long timeout, List query,
028: String match) throws Exception {
029: mcast_sock = new MulticastSocket();
030: mcast_sock.setTimeToLive(ttl);
031: if (bind_addr != null)
032: mcast_sock.setInterface(bind_addr);
033:
034: StringBuffer request = new StringBuffer("QUERY: ");
035: for (int i = 0; i < query.size(); i++) {
036: request.append(query.get(i)).append(" ");
037: }
038: byte[] probe_buf = request.toString().getBytes();
039:
040: DatagramPacket probe = new DatagramPacket(probe_buf, 0,
041: probe_buf.length, addr, port);
042: mcast_sock.send(probe);
043: System.out.println("\n-- send probe on " + addr + ':' + port
044: + '\n');
045:
046: new Thread() {
047: public void run() {
048: Util.sleep(timeout);
049: mcast_sock.close();
050: running = false;
051: }
052: }.start();
053:
054: int matched = 0, not_matched = 0, count = 0;
055: String response;
056: while (running) {
057: byte[] buf = new byte[65000];
058: DatagramPacket rsp = new DatagramPacket(buf, 0, buf.length);
059: try {
060: mcast_sock.receive(rsp);
061: } catch (Throwable t) {
062: System.out.println("\n");
063: return;
064: }
065: byte[] data = rsp.getData();
066: // System.out.println("received " + rsp.getLength() + " bytes from " + rsp.getSocketAddress());
067: response = new String(data, 0, rsp.getLength());
068: if (matches(response, match)) {
069: matched++;
070: System.out.println("\n#" + ++count + " ("
071: + rsp.getLength() + " bytes): " + response);
072: } else
073: not_matched++;
074: }
075: System.out
076: .println("\nTotal responses=" + count + ", " + matched
077: + " matches, " + not_matched + " non-matches");
078: }
079:
080: private boolean matches(String response, String match) {
081: if (response == null)
082: return false;
083: if (match == null)
084: return true;
085: int index = response.indexOf(match);
086: return index > -1;
087: }
088:
089: public static void main(String[] args) {
090: InetAddress addr = null, bind_addr = null;
091: int port = 0;
092: int ttl = 32;
093: long timeout = 10000;
094: final String DEFAULT_DIAG_ADDR = "224.0.0.75";
095: final int DEFAULT_DIAG_PORT = 7500;
096: List query = new ArrayList();
097: String match = null;
098:
099: try {
100: for (int i = 0; i < args.length; i++) {
101: if ("-addr".equals(args[i])) {
102: addr = InetAddress.getByName(args[++i]);
103: continue;
104: }
105: if ("-bind_addr".equals(args[i])) {
106: bind_addr = InetAddress.getByName(args[++i]);
107: continue;
108: }
109: if ("-port".equals(args[i])) {
110: port = Integer.parseInt(args[++i]);
111: continue;
112: }
113: if ("-ttl".equals(args[i])) {
114: ttl = Integer.parseInt(args[++i]);
115: continue;
116: }
117: if ("-timeout".equals(args[i])) {
118: timeout = Long.parseLong(args[++i]);
119: continue;
120: }
121: if ("-query".equals(args[i])) {
122: query.add(args[++i]);
123: continue;
124: }
125: if ("-match".equals(args[i])) {
126: match = args[++i];
127: continue;
128: }
129:
130: help();
131: return;
132: }
133: Probe p = new Probe();
134: if (addr == null)
135: addr = InetAddress.getByName(DEFAULT_DIAG_ADDR);
136: if (port == 0)
137: port = DEFAULT_DIAG_PORT;
138: p.start(addr, bind_addr, port, ttl, timeout, query, match);
139: } catch (Throwable t) {
140: t.printStackTrace();
141: }
142: }
143:
144: static void help() {
145: System.out
146: .println("Probe [-help] [-addr <addr>] [-bind_addr <addr>] "
147: + "[-port <port>] [-ttl <ttl>] [-timeout <timeout>] [-query <query>] [-match <pattern>]"
148: + " (query can be jmx or props)");
149: }
150: }
|