PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/card/arcane/Util.java

http://magic--another-game-engine.googlecode.com/
Java | 100 lines | 88 code | 11 blank | 1 comment | 5 complexity | c275552ea0f027b0f650d07f088be7b1 MD5 | raw file
  1. package org.mage.card.arcane;
  2. import java.awt.AWTException;
  3. import java.awt.Robot;
  4. import java.io.IOException;
  5. import java.net.DatagramPacket;
  6. import java.net.DatagramSocket;
  7. import java.net.InetAddress;
  8. import java.net.NetworkInterface;
  9. import java.util.Collections;
  10. import java.util.Enumeration;
  11. import java.util.concurrent.LinkedBlockingQueue;
  12. import java.util.concurrent.ThreadFactory;
  13. import java.util.concurrent.ThreadPoolExecutor;
  14. import java.util.concurrent.TimeUnit;
  15. import javax.swing.SwingUtilities;
  16. @SuppressWarnings({ "rawtypes", "unchecked" })
  17. public class Util {
  18. static public final boolean isMac = System.getProperty("os.name").toLowerCase().indexOf("mac") != -1;
  19. static public final boolean isWindows = System.getProperty("os.name").toLowerCase().indexOf("windows") == -1;
  20. static public Robot robot;
  21. static {
  22. try {
  23. new Robot();
  24. } catch (AWTException ex) {
  25. throw new RuntimeException("Error creating robot.", ex);
  26. }
  27. }
  28. static public ThreadPoolExecutor threadPool;
  29. static private int threadCount;
  30. static {
  31. threadPool = new ThreadPoolExecutor(4, 4, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new ThreadFactory() {
  32. public Thread newThread (Runnable runnable) {
  33. threadCount++;
  34. Thread thread = new Thread(runnable, "Util" + threadCount);
  35. thread.setDaemon(true);
  36. return thread;
  37. }
  38. });
  39. threadPool.prestartAllCoreThreads();
  40. }
  41. public static void broadcast (byte[] data, int port) throws IOException {
  42. DatagramSocket socket = new DatagramSocket();
  43. broadcast(socket, data, port, NetworkInterface.getNetworkInterfaces());
  44. socket.close();
  45. }
  46. private static void broadcast (DatagramSocket socket, byte[] data, int port, Enumeration<NetworkInterface> ifaces)
  47. throws IOException {
  48. for (NetworkInterface iface : Collections.list(ifaces)) {
  49. for (InetAddress address : Collections.list(iface.getInetAddresses())) {
  50. if (!address.isSiteLocalAddress()) continue;
  51. // Java 1.5 doesn't support getting the subnet mask, so try the two most common.
  52. byte[] ip = address.getAddress();
  53. ip[3] = -1; // 255.255.255.0
  54. socket.send(new DatagramPacket(data, data.length, InetAddress.getByAddress(ip), port));
  55. ip[2] = -1; // 255.255.0.0
  56. socket.send(new DatagramPacket(data, data.length, InetAddress.getByAddress(ip), port));
  57. }
  58. }
  59. }
  60. static public void sleep (int millis) {
  61. try {
  62. Thread.sleep(millis);
  63. } catch (InterruptedException ignored) {
  64. }
  65. }
  66. static public boolean classExists (String className) {
  67. try {
  68. Class.forName(className);
  69. return true;
  70. } catch (ClassNotFoundException ex) {
  71. return false;
  72. }
  73. }
  74. static public void wait (Object lock) {
  75. synchronized (lock) {
  76. try {
  77. lock.wait();
  78. } catch (InterruptedException ex) {
  79. }
  80. }
  81. }
  82. public static void invokeAndWait (Runnable runnable) {
  83. try {
  84. SwingUtilities.invokeAndWait(runnable);
  85. } catch (Exception ex) {
  86. throw new RuntimeException("Error invoking runnable in UI thread.", ex);
  87. }
  88. }
  89. }