001: // $Id: McastSenderTest.java,v 1.8 2005/09/12 14:03:19 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import java.io.DataInputStream;
006: import java.net.*;
007: import java.util.ArrayList;
008: import java.util.Enumeration;
009:
010: /**
011: Tests IP multicast. Start one or more instances of McastReceiverTest which listen for IP mcast packets
012: and then start McastSenderTest, which sends IP mcast packets (all have to have the same IPMCAST address and port).
013: A TTL of 0 for the McastSenderTest means that packets will only be sent to receivers on the same host. If TTL > 0,
014: other hosts will receive the packets too. Since many routers are dropping IPMCAST traffic, this is a good way to
015: test whether IPMCAST works between different subnets.
016: @see McastReceiverTest
017: @author Bela Ban
018: @version $Revision: 1.8 $
019: */
020: public class McastSenderTest {
021:
022: public static void main(String args[]) {
023: MulticastSocket sock = null;
024: ArrayList sockets = null;
025: InetAddress mcast_addr = null, bind_addr = null;
026: DatagramPacket packet;
027: byte[] buf = new byte[0];
028: String tmp;
029: int ttl = 32;
030: String line;
031: DataInputStream in;
032: AckReceiver ack_receiver = null;
033: int port = 5555;
034: boolean send_on_all_interfaces = false;
035:
036: try {
037: for (int i = 0; i < args.length; i++) {
038: tmp = args[i];
039: if ("-help".equals(tmp)) {
040: help();
041: return;
042: }
043: if ("-bind_addr".equals(tmp)) {
044: bind_addr = InetAddress.getByName(args[++i]);
045: continue;
046: }
047: if ("-mcast_addr".equals(tmp)) {
048: mcast_addr = InetAddress.getByName(args[++i]);
049: continue;
050: }
051: if ("-ttl".equals(tmp)) {
052: ttl = Integer.parseInt(args[++i]);
053: continue;
054: }
055: if ("-port".equals(tmp)) {
056: port = Integer.parseInt(args[++i]);
057: continue;
058: }
059: if ("-send_on_all_interfaces".equals(args[i])) {
060: send_on_all_interfaces = true;
061: continue;
062: }
063: help();
064: return;
065: }
066: if (mcast_addr == null)
067: mcast_addr = InetAddress.getByName("224.0.0.150");
068: } catch (Exception ex) {
069: System.err.println(ex);
070: return;
071: }
072:
073: try {
074: if (send_on_all_interfaces) {
075: sockets = new ArrayList(10);
076: NetworkInterface intf;
077: MulticastSocket s;
078:
079: for (Enumeration en = NetworkInterface
080: .getNetworkInterfaces(); en.hasMoreElements();) {
081: intf = (NetworkInterface) en.nextElement();
082: s = new MulticastSocket();
083: sockets.add(s);
084: s.setTimeToLive(ttl);
085: s.setNetworkInterface(intf);
086: System.out.println("Socket=" + s.getLocalAddress()
087: + ':' + s.getLocalPort() + ", ttl="
088: + s.getTimeToLive() + ", bind interface="
089: + s.getInterface());
090: ack_receiver = new AckReceiver(s);
091: ack_receiver.start();
092: }
093: } else {
094: sock = new MulticastSocket();
095: sock.setTimeToLive(ttl);
096: if (bind_addr != null)
097: sock.setInterface(bind_addr);
098:
099: System.out.println("Socket=" + sock.getLocalAddress()
100: + ':' + sock.getLocalPort() + ", ttl="
101: + sock.getTimeToLive() + ", bind interface="
102: + sock.getInterface());
103: ack_receiver = new AckReceiver(sock);
104: ack_receiver.start();
105: }
106:
107: in = new DataInputStream(System.in);
108: while (true) {
109: System.out.print("> ");
110: line = in.readLine();
111: if (line.startsWith("quit") || line.startsWith("exit")) {
112: if (ack_receiver != null)
113: ack_receiver.stop();
114: break;
115: }
116: buf = line.getBytes();
117: packet = new DatagramPacket(buf, buf.length,
118: mcast_addr, port);
119: if (sock != null) {
120: sock.send(packet);
121: } else {
122: for (int i = 0; i < sockets.size(); i++) {
123: MulticastSocket s = (MulticastSocket) sockets
124: .get(i);
125: s.send(packet);
126: }
127: }
128: }
129: } catch (Exception e) {
130: System.err.println(e);
131: }
132:
133: }
134:
135: static void help() {
136: System.out
137: .println("McastSenderTest [-bind_addr <bind address>] [-help] [-mcast_addr <multicast address>] "
138: + "[-port <multicast port that receivers are listening on>] [-ttl <time to live for mcast packets>] "
139: + "[-send_on_all_interfaces]");
140: }
141:
142: private static class AckReceiver implements Runnable {
143: DatagramSocket sock;
144: DatagramPacket packet;
145: byte[] buf;
146: Thread t = null;
147:
148: AckReceiver(DatagramSocket sock) {
149: this .sock = sock;
150: }
151:
152: public void run() {
153: while (t != null) {
154: try {
155: buf = new byte[256];
156: packet = new DatagramPacket(buf, buf.length);
157: sock.receive(packet);
158: System.out.println("<< Received response from "
159: + packet.getAddress().getHostAddress()
160: + ':'
161: + packet.getPort()
162: + ": "
163: + new String(packet.getData(), 0, packet
164: .getLength()));
165: } catch (Exception e) {
166: System.err.println(e);
167: break;
168: }
169: }
170: t = null;
171: }
172:
173: void start() {
174: t = new Thread(this , "McastSenderTest.AckReceiver thread");
175: t.start();
176: }
177:
178: void stop() {
179: if (t != null && t.isAlive()) {
180: t = null;
181: try {
182: sock.close();
183: } catch (Exception e) {
184: }
185: }
186: }
187: }
188:
189: }
|