| 64 | |
| 65 | |
| 66 | static void startServer(final Base base) { |
| 67 | try { |
| 68 | Messages.log("Opening SingleInstance socket"); |
| 69 | final ServerSocket ss = |
| 70 | new ServerSocket(0, 0, InetAddress.getLoopbackAddress()); |
| 71 | Preferences.set(SERVER_PORT, "" + ss.getLocalPort()); |
| 72 | final String key = "" + Math.random(); |
| 73 | Preferences.set(SERVER_KEY, key); |
| 74 | Preferences.save(); |
| 75 | |
| 76 | Messages.log("Starting SingleInstance thread"); |
| 77 | new Thread(new Runnable() { |
| 78 | public void run() { |
| 79 | while (true) { |
| 80 | try { |
| 81 | Socket s = ss.accept(); // blocks (sleeps) until connection |
| 82 | final BufferedReader reader = PApplet.createReader(s.getInputStream()); |
| 83 | String receivedKey = reader.readLine(); |
| 84 | Messages.log(this, "key is " + key + ", received is " + receivedKey); |
| 85 | |
| 86 | if (key.equals(receivedKey)) { |
| 87 | EventQueue.invokeLater(new Runnable() { |
| 88 | public void run() { |
| 89 | try { |
| 90 | Messages.log(this, "about to read line"); |
| 91 | String path = reader.readLine(); |
| 92 | if (path == null) { |
| 93 | // Because an attempt was made to launch the PDE again, |
| 94 | // throw the user a bone by at least opening a new |
| 95 | // Untitled window for them. |
| 96 | Messages.log(this, "opening new empty sketch"); |
| 97 | // platform.base.handleNew(); |
| 98 | base.handleNew(); |
| 99 | |
| 100 | } else { |
| 101 | // loop through the sketches that were passed in |
| 102 | do { |
| 103 | Messages.log(this, "calling open with " + path); |
| 104 | // platform.base.handleOpen(filename); |
| 105 | base.handleOpen(path); |
| 106 | path = reader.readLine(); |
| 107 | } while (path != null); |
| 108 | } |
| 109 | } catch (IOException e) { |
| 110 | e.printStackTrace(); |
| 111 | } |
| 112 | } |
| 113 | }); |
| 114 | } else { |
| 115 | Messages.log(this, "keys do not match"); |
| 116 | } |
| 117 | // } |
| 118 | } catch (IOException e) { |
| 119 | Messages.err("SingleInstance error while listening", e); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | }, "SingleInstance Server").start(); |