| 1 | |
package org.jtheque.core.managers.lifecycle; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import org.jtheque.core.managers.Managers; |
| 20 | |
import org.jtheque.core.managers.log.ILoggingManager; |
| 21 | |
import org.jtheque.utils.io.FileUtils; |
| 22 | |
import org.jtheque.utils.io.SocketUtils; |
| 23 | |
import org.slf4j.LoggerFactory; |
| 24 | |
|
| 25 | |
import java.io.IOException; |
| 26 | |
import java.io.PrintStream; |
| 27 | |
import java.net.ServerSocket; |
| 28 | |
import java.net.Socket; |
| 29 | |
import java.net.SocketException; |
| 30 | |
import java.net.UnknownHostException; |
| 31 | |
import java.util.ArrayList; |
| 32 | |
import java.util.Collection; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | final class Instances { |
| 40 | |
private ServerSocket socket; |
| 41 | 0 | private final Collection<Client> clients = new ArrayList<Client>(5); |
| 42 | |
private Thread thread; |
| 43 | |
|
| 44 | |
private static final String LOCALHOST = "127.0.0.1"; |
| 45 | |
private static final int PORT = 12345; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
@SuppressWarnings({"SocketOpenedButNotSafelyClosed"}) |
| 52 | |
public void launchApplication() { |
| 53 | 0 | boolean canLaunch = true; |
| 54 | |
|
| 55 | |
try { |
| 56 | 0 | socket = new ServerSocket(PORT); |
| 57 | |
|
| 58 | 0 | registerNewClient(); |
| 59 | 0 | } catch (IOException e) { |
| 60 | 0 | LoggerFactory.getLogger(getClass()).error("Unable to register socket", e); |
| 61 | |
|
| 62 | 0 | canLaunch = false; |
| 63 | 0 | } |
| 64 | |
|
| 65 | 0 | if (!canLaunch) { |
| 66 | 0 | exit(); |
| 67 | |
} |
| 68 | 0 | } |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
private void registerNewClient() { |
| 74 | 0 | thread = new ServerThread(); |
| 75 | |
|
| 76 | 0 | thread.start(); |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
private void exit() { |
| 83 | 0 | callInstance(); |
| 84 | |
|
| 85 | 0 | System.exit(1); |
| 86 | 0 | } |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
@SuppressWarnings({"SocketOpenedButNotSafelyClosed", "IOResourceOpenedButNotSafelyClosed"}) |
| 92 | |
private void callInstance() { |
| 93 | 0 | Socket clientSocket = null; |
| 94 | 0 | PrintStream stream = null; |
| 95 | |
|
| 96 | |
try { |
| 97 | 0 | clientSocket = new Socket(LOCALHOST, PORT); |
| 98 | 0 | stream = new PrintStream(clientSocket.getOutputStream()); |
| 99 | |
|
| 100 | 0 | stream.println("open"); |
| 101 | 0 | } catch (UnknownHostException e) { |
| 102 | 0 | LoggerFactory.getLogger(getClass()).error("The host is unknown", e); |
| 103 | 0 | } catch (IOException e) { |
| 104 | 0 | LoggerFactory.getLogger(getClass()).error("I/O error", e); |
| 105 | |
} finally { |
| 106 | 0 | FileUtils.close(stream); |
| 107 | 0 | SocketUtils.close(clientSocket); |
| 108 | 0 | } |
| 109 | 0 | } |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public void closeInstance() { |
| 115 | 0 | stopClients(); |
| 116 | 0 | stopServer(); |
| 117 | 0 | SocketUtils.close(socket); |
| 118 | 0 | } |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
private void stopClients() { |
| 124 | 0 | for (Client client : clients) { |
| 125 | 0 | client.interrupt(); |
| 126 | |
} |
| 127 | 0 | } |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
private void stopServer() { |
| 133 | 0 | if (thread != null) { |
| 134 | 0 | thread.interrupt(); |
| 135 | |
} |
| 136 | 0 | } |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | 0 | @SuppressWarnings({"SocketOpenedButNotSafelyClosed"}) |
| 144 | 0 | private final class ServerThread extends Thread { |
| 145 | |
private boolean closing; |
| 146 | |
|
| 147 | |
@Override |
| 148 | |
public void run() { |
| 149 | 0 | while (!socket.isClosed() && socket != null) { |
| 150 | |
try { |
| 151 | 0 | Client client = new Client(socket.accept()); |
| 152 | 0 | clients.add(client); |
| 153 | |
|
| 154 | 0 | client.start(); |
| 155 | 0 | } catch (SocketException e1) { |
| 156 | 0 | if (!closing) { |
| 157 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e1, "Socket closed"); |
| 158 | |
} |
| 159 | 0 | } catch (IOException e2) { |
| 160 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e2); |
| 161 | 0 | interrupt(); |
| 162 | 0 | } |
| 163 | |
} |
| 164 | 0 | } |
| 165 | |
|
| 166 | |
@Override |
| 167 | |
public void interrupt() { |
| 168 | 0 | closing = true; |
| 169 | |
|
| 170 | 0 | SocketUtils.close(socket); |
| 171 | |
|
| 172 | 0 | super.interrupt(); |
| 173 | 0 | } |
| 174 | |
} |
| 175 | |
} |