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