1 3 package org.jgroups.tests; 4 5 import java.io.DataInputStream ; 6 import java.net.*; 7 import java.util.Enumeration ; 8 import java.util.Vector ; 9 10 11 12 18 public class McastSenderTest1_4 { 19 20 public static void main(String args[]) { 21 MulticastSocket[] sockets=null; 22 MulticastSocket sock; 23 InetAddress mcast_addr=null, bind_addr=null; 24 DatagramPacket packet; 25 byte[] buf=new byte[0]; 26 String tmp; 27 int ttl=32; 28 String line; 29 DataInputStream in; 30 AckReceiver ack_receiver=null; 31 boolean use_all_interfaces=false; 32 int port=5555; 33 34 35 try { 36 for(int i=0; i < args.length; i++) { 37 tmp=args[i]; 38 if("-help".equals(tmp)) { 39 help(); 40 return; 41 } 42 if("-bind_addr".equals(tmp)) { 43 bind_addr=InetAddress.getByName(args[++i]); 44 continue; 45 } 46 if("-mcast_addr".equals(tmp)) { 47 mcast_addr=InetAddress.getByName(args[++i]); 48 continue; 49 } 50 if("-ttl".equals(tmp)) { 51 ttl=Integer.parseInt(args[++i]); 52 continue; 53 } 54 if("-port".equals(tmp)) { 55 port=Integer.parseInt(args[++i]); 56 continue; 57 } 58 if("-use_all_interfaces".equals(tmp)) { 59 use_all_interfaces=true; 60 continue; 61 } 62 help(); 63 return; 64 } 65 if(mcast_addr == null) 66 mcast_addr=InetAddress.getByName("224.0.0.150"); 67 68 if(use_all_interfaces) { 69 if(!is1_4()) { 70 System.err.println("-use_all_interfaces flag requires JDK 1.4 or greater"); 71 return; 72 } 73 } 74 } 75 catch(Exception ex) { 76 System.err.println(ex); 77 return; 78 } 79 80 81 try { 82 if(use_all_interfaces) { 83 Vector v=new Vector (); 84 85 for(Enumeration en=NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { 86 NetworkInterface intf=(NetworkInterface)en.nextElement(); 87 for(Enumeration e2=intf.getInetAddresses(); e2.hasMoreElements();) { 88 bind_addr=(InetAddress)e2.nextElement(); 89 v.addElement(bind_addr); 90 } 91 } 92 sockets=new MulticastSocket[v.size()]; 93 for(int i=0; i < v.size(); i++) { 94 sock=new MulticastSocket(port); 95 sock.setTimeToLive(ttl); 96 sock.setInterface((InetAddress)v.elementAt(i)); 97 sockets[i]=sock; 98 ack_receiver=new AckReceiver(sock); 99 ack_receiver.start(); 100 } 101 } 102 else { 103 sockets=new MulticastSocket[1]; 104 sockets[0]=new MulticastSocket(port); 105 sockets[0].setTimeToLive(ttl); 106 if(bind_addr != null) 107 sockets[0].setInterface(bind_addr); 108 ack_receiver=new AckReceiver(sockets[0]); 109 ack_receiver.start(); 110 } 111 112 for(int i=0; i < sockets.length; i++) { 113 sock=sockets[i]; 114 if(sock == null) continue; 115 System.out.println("Socket #" + (i+1) + '=' + sock.getLocalAddress() + ':' + sock.getLocalPort() + 116 ", ttl=" + sock.getTimeToLive() + ", bind interface=" + sock.getInterface()); 117 } 118 119 in=new DataInputStream (System.in); 120 while(true) { 121 System.out.print("> "); 122 line=in.readLine(); 123 if(line.startsWith("quit") || line.startsWith("exit")) 124 System.exit(0); 125 buf=line.getBytes(); 126 packet=new DatagramPacket(buf, buf.length, mcast_addr, port); 127 send(packet, sockets); } 129 } 130 catch(Exception e) { 131 System.err.println(e); 132 } 133 } 134 135 136 137 static void send(DatagramPacket packet, MulticastSocket[] sockets) { 138 if(packet == null || sockets == null) return; 139 for(int i=0; i < sockets.length; i++) { 140 try { 141 if(sockets[i] != null) 142 sockets[i].send(packet); 143 } 144 catch(Exception ex) { 145 System.err.println("McastSenderTest1_4.send(): " + ex); 146 } 147 } 148 } 149 150 151 static boolean is1_4() { 152 Class cl; 153 154 try { 155 cl=Thread.currentThread().getContextClassLoader().loadClass("java.net.NetworkInterface"); 156 return true; 157 } 158 catch(Throwable ex) { 159 return false; 160 } 161 } 162 163 164 static void help() { 165 System.out.println("McastSenderTest1_4 [-bind_addr <bind address>] [-help] [-mcast_addr <multicast address>] " + 166 "[-port <multicast port that receivers are listening on>] " + 167 "[-ttl <time to live for mcast packets>] [-use_all_interfaces]"); 168 } 169 170 171 172 173 174 private static class AckReceiver implements Runnable { 175 DatagramSocket sock; 176 DatagramPacket packet; 177 byte[] buf; 178 Thread t=null; 179 180 AckReceiver(DatagramSocket sock) { 181 this.sock=sock; 182 } 183 184 public void run() { 185 while(t != null) { 186 try { 187 buf=new byte[256]; 188 packet=new DatagramPacket(buf, buf.length); 189 sock.receive(packet); 190 System.out.println("<< Received packet from " + 191 packet.getAddress().getHostAddress() + ':' + 192 packet.getPort() + ": " + new String (packet.getData())); 193 } 194 catch(Exception e) { 195 System.err.println(e); 196 break; 197 } 198 } 199 t=null; 200 } 201 202 void start() { 203 t=new Thread (this, "McastSenderTest1_4.AckReceiver thread"); 204 t.start(); 205 } 206 207 void stop() { 208 if(t != null && t.isAlive()) { 209 t=null; 210 try { 211 sock.close(); 212 } 213 catch(Exception e) { 214 } 215 } 216 } 217 } 218 219 220 221 } 222 | Popular Tags |