/ MCast.java
MCast.java
1 /* 2 * 3 * Copyright (C) Andrew Smith 2013 4 * 5 * Usage: java MCast [-v] code toaddr port replyport wait 6 * 7 * If any are missing or blank they use the defaults: 8 * 9 * -v means report how long the last reply took 10 * 11 * code = 'FTW' 12 * toaddr = '224.0.0.75' 13 * port = '4028' 14 * replyport = '4027' 15 * wait = '1000' 16 * 17 */ 18 19 import java.net.*; 20 import java.io.*; 21 import java.util.*; 22 23 class MCast implements Runnable 24 { 25 static private final String MCAST_CODE = "FTW"; 26 static private final String MCAST_ADDR = "224.0.0.75"; 27 static private final int MCAST_PORT = 4028; 28 static private final int MCAST_REPORT = 4027; 29 static private final int MCAST_WAIT4 = 1000; 30 31 static private String code = MCAST_CODE; 32 static private String addr = MCAST_ADDR; 33 static private int port = MCAST_PORT; 34 static private int report = MCAST_REPORT; 35 static private int wait4 = MCAST_WAIT4; 36 37 private InetAddress mcast_addr = null; 38 39 static private final Integer lock = new Integer(666); 40 41 static private boolean ready = false; 42 43 static private Thread listen = null; 44 45 static public boolean verbose = false; 46 47 static private Date start = null; 48 static private Date last = null; 49 static boolean got_last = false; 50 51 static public void usAge() 52 { 53 System.err.println("usAge: java MCast [-v] [code [toaddr [port [replyport [wait]]]]]"); 54 System.err.println(" -v=report elapsed ms to last reply"); 55 System.err.println(" Anything below missing or blank will use it's default"); 56 System.err.println(" code=X in cgminer-X-Port default="+MCAST_CODE); 57 System.err.println(" toaddr=multicast address default="+MCAST_ADDR); 58 System.err.println(" port=multicast port default="+MCAST_PORT); 59 System.err.println(" replyport=local post to listen for replies default="+MCAST_REPORT); 60 System.err.println(" wait=how long to wait for replies default="+MCAST_WAIT4+"ms"); 61 System.exit(1); 62 } 63 64 private int port(String _port, String name) 65 { 66 int tmp = 0; 67 68 try 69 { 70 tmp = Integer.parseInt(_port); 71 } 72 catch (NumberFormatException nfe) 73 { 74 System.err.println("Invalid " + name + " - must be a number between 1 and 65535"); 75 usAge(); 76 System.exit(1); 77 } 78 79 if (tmp < 1 || tmp > 65535) 80 { 81 System.err.println("Invalid " + name + " - must be a number between 1 and 65535"); 82 usAge(); 83 System.exit(1); 84 } 85 86 return tmp; 87 } 88 89 public void set_code(String _code) 90 { 91 if (_code.length() > 0) 92 code = _code; 93 } 94 95 public void set_addr(String _addr) 96 { 97 if (_addr.length() > 0) 98 { 99 addr = _addr; 100 101 try 102 { 103 mcast_addr = InetAddress.getByName(addr); 104 } 105 catch (Exception e) 106 { 107 System.err.println("ERR: Invalid multicast address"); 108 usAge(); 109 System.exit(1); 110 } 111 } 112 } 113 114 public void set_port(String _port) 115 { 116 if (_port.length() > 0) 117 port = port(_port, "port"); 118 } 119 120 public void set_report(String _report) 121 { 122 if (_report.length() > 0) 123 report = port(_report, "reply port"); 124 } 125 126 public void set_wait(String _wait4) 127 { 128 if (_wait4.length() > 0) 129 { 130 try 131 { 132 wait4 = Integer.parseInt(_wait4); 133 } 134 catch (NumberFormatException nfe) 135 { 136 System.err.println("Invalid wait - must be a number between 0ms and 60000ms"); 137 usAge(); 138 System.exit(1); 139 } 140 141 if (wait4 < 0 || wait4 > 60000) 142 { 143 System.err.println("Invalid wait - must be a number between 0ms and 60000ms"); 144 usAge(); 145 System.exit(1); 146 } 147 } 148 } 149 150 public void run() // listen 151 { 152 byte[] message = new byte[1024]; 153 DatagramSocket socket = null; 154 DatagramPacket packet = null; 155 156 try 157 { 158 socket = new DatagramSocket(report); 159 packet = new DatagramPacket(message, message.length); 160 161 synchronized (lock) 162 { 163 ready = true; 164 } 165 166 while (true) 167 { 168 socket.receive(packet); 169 170 synchronized (lock) 171 { 172 last = new Date(); 173 } 174 175 int off = packet.getOffset(); 176 int len = packet.getLength(); 177 178 System.out.println("Got: '" + new String(message, off, len) + "' from" + packet.getSocketAddress()); 179 } 180 } 181 catch (Exception e) 182 { 183 socket.close(); 184 } 185 } 186 187 public void sendMCast() 188 { 189 try 190 { 191 String message = new String("cgminer-" + code + "-" + report); 192 MulticastSocket socket = null; 193 DatagramPacket packet = null; 194 195 socket = new MulticastSocket(); 196 packet = new DatagramPacket(message.getBytes(), message.length(), mcast_addr, port); 197 198 System.out.println("About to send " + message + " to " + mcast_addr + ":" + port); 199 200 start = new Date(); 201 202 socket.send(packet); 203 204 socket.close(); 205 } 206 catch (Exception e) 207 { 208 e.printStackTrace(); 209 } 210 } 211 212 public void init() 213 { 214 MCast lis = new MCast(); 215 listen = new Thread(lis); 216 listen.start(); 217 218 while (true) 219 { 220 synchronized (lock) 221 { 222 if (ready) 223 break; 224 } 225 226 try 227 { 228 Thread.sleep(100); 229 } 230 catch (Exception sl1) 231 { 232 } 233 } 234 235 try 236 { 237 Thread.sleep(500); 238 } 239 catch (Exception sl2) 240 { 241 } 242 243 sendMCast(); 244 245 try 246 { 247 Thread.sleep(wait4); 248 } 249 catch (Exception sl3) 250 { 251 } 252 253 listen.interrupt(); 254 255 if (verbose) 256 { 257 try 258 { 259 Thread.sleep(100); 260 } 261 catch (Exception sl4) 262 { 263 } 264 265 synchronized (lock) 266 { 267 if (last == null) 268 System.out.println("No replies received"); 269 else 270 { 271 long diff = last.getTime() - start.getTime(); 272 System.out.println("Last reply took " + diff + "ms"); 273 } 274 } 275 } 276 277 System.exit(0); 278 } 279 280 public MCast() 281 { 282 } 283 284 public static void main(String[] params) throws Exception 285 { 286 int p = 0; 287 288 MCast mcast = new MCast(); 289 290 mcast.set_addr(MCAST_ADDR); 291 292 if (params.length > p) 293 { 294 if (params[p].equals("-?") 295 || params[p].equalsIgnoreCase("-h") 296 || params[p].equalsIgnoreCase("-help") 297 || params[p].equalsIgnoreCase("--help")) 298 MCast.usAge(); 299 else 300 { 301 if (params[p].equals("-v")) 302 { 303 mcast.verbose = true; 304 p++; 305 } 306 307 if (params.length > p) 308 { 309 mcast.set_code(params[p++]); 310 311 if (params.length > p) 312 { 313 mcast.set_addr(params[p++]); 314 315 if (params.length > p) 316 { 317 mcast.set_port(params[p++]); 318 319 if (params.length > p) 320 { 321 mcast.set_report(params[p++]); 322 if (params.length > p) 323 mcast.set_wait(params[p]); 324 } 325 } 326 } 327 } 328 } 329 } 330 331 mcast.init(); 332 } 333 }