001package jmri.jmrit.z21server;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006
007import static java.lang.Thread.State.NEW;
008import static java.lang.Thread.State.TERMINATED;
009
010public class FacelessServer {
011
012    private static FacelessServer instance;
013    private static MainServer server;
014    private static Thread currentThread;
015
016    private final static Logger log = LoggerFactory.getLogger(FacelessServer.class);
017
018    private FacelessServer() {
019        initServer();
020        createThread();
021    }
022
023    private static void initServer() {
024        server = new MainServer();
025    }
026
027    private static synchronized void createThread() {
028        currentThread = new Thread(server);
029        currentThread.setName("Z21 App Server");
030    }
031
032    synchronized public static FacelessServer getInstance() {
033        if (instance == null) {
034            instance =  new FacelessServer();
035        }
036        return instance;
037    }
038
039    public synchronized void start() {
040        if (currentThread.getState() == TERMINATED) {
041            createThread();
042        }
043        if (currentThread.getState() == NEW) {
044            log.info("Trying to start new z21 server...");
045            currentThread.start();
046        }
047    }
048
049    public synchronized void stop() {
050        currentThread.interrupt();
051    }
052
053
054}