MCast.java 6.1 KB

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