|
4 | 4 | import java.net.BindException; |
5 | 5 | import java.net.InetAddress; |
6 | 6 | import java.net.ServerSocket; |
| 7 | +import java.net.Socket; |
7 | 8 |
|
8 | 9 | /** |
9 | 10 | * Used for singe-instance-Programms <br> |
10 | 11 | * <a href="https://stackoverflow.com/questions/920386/how-to-allow-running-only-one-instance-of-a-java-program-at-a-time">Source</a> |
11 | 12 | */ |
12 | 13 | public class PInstance { |
13 | 14 |
|
14 | | - private static int PORT; |
15 | | - private static ServerSocket socket; |
| 15 | + private int PORT; |
| 16 | + private Runnable connectAction; |
| 17 | + private ServerSocket serverSocket; |
16 | 18 |
|
17 | 19 | /** |
18 | 20 | * Creates/reserves a local Port to indicate that this Program is running. |
19 | | - * @param port has to be in Range and not be allocated by <a href="https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml">another program</a> |
20 | | - * @throws BindException if Another Instance already exists |
21 | | - * <br>IOException if the socket failed |
| 21 | + * |
| 22 | + * @param port has to be in Range and not be allocated by <a href="https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml">another program</a> |
| 23 | + * @param connectAction is the Function that is called when some program connects to it |
| 24 | + * @throws IOException if the socket failed or Another Instance already exists |
22 | 25 | */ |
23 | | - public PInstance(int port) throws IOException, BindException { |
| 26 | + public PInstance(int port, Runnable connectAction) throws IOException { |
24 | 27 | PORT = port; |
| 28 | + this.connectAction = connectAction; |
25 | 29 |
|
26 | | - //Bind to localhost adapter with a zero connection queue |
27 | | - socket = new ServerSocket(PORT, 0, InetAddress.getByAddress(new byte[]{127, 0, 0, 1})); |
| 30 | + callServer(); |
| 31 | + initServer(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Looks for a Server/Process that waits for a connection |
| 36 | + */ |
| 37 | + private void callServer() { |
| 38 | + try { |
| 39 | + Socket s = new Socket("127.0.0.1", PORT); |
| 40 | + } catch (IOException e) { |
| 41 | + // No Other Instance found (or other error) |
| 42 | + } |
28 | 43 | } |
29 | 44 |
|
30 | 45 | /** |
| 46 | + * Inits Server |
31 | 47 | * |
| 48 | + * @throws IOException if the socket failed or Another Instance already exists |
| 49 | + */ |
| 50 | + private void initServer() throws IOException { |
| 51 | + |
| 52 | + //Bind to localhost adapter with a zero connection queue |
| 53 | + serverSocket = new ServerSocket(PORT, 0, InetAddress.getByAddress(new byte[]{127, 0, 0, 1})); |
| 54 | + Thread t = new Thread(new Runnable() { |
| 55 | + @Override |
| 56 | + public void run() { |
| 57 | + while (true) { |
| 58 | + try { |
| 59 | + |
| 60 | + // look for connection |
| 61 | + serverSocket.accept(); |
| 62 | + |
| 63 | + // execute Function if some other program connected (to e.g. focus the older UI-Instance) |
| 64 | + if (connectAction != null) |
| 65 | + connectAction.run(); |
| 66 | + |
| 67 | + // close and reopen server |
| 68 | + serverSocket.close(); |
| 69 | + serverSocket = new ServerSocket(PORT, 0, InetAddress.getByAddress(new byte[]{127, 0, 0, 1})); |
| 70 | + } catch (IOException e) { |
| 71 | + // Address already in Use |
| 72 | + throw new RuntimeException(e); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + }); |
| 77 | + t.start(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
32 | 81 | * @param port |
33 | 82 | * @return if a local Program, which reserves/uses a Port, is running. |
34 | 83 | */ |
35 | 84 | public static boolean isInstanceRunning(int port) { |
36 | 85 | try { |
37 | 86 | //Bind to localhost adapter with a zero connection queue |
38 | | - socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[]{127, 0, 0, 1})); |
| 87 | + ServerSocket serverSocket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[]{127, 0, 0, 1})); |
39 | 88 | } catch (BindException e) { |
40 | 89 | return true; |
41 | 90 | } catch (IOException e) { |
|
0 commit comments