diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-26 19:35:31 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-26 19:35:31 +0100 |
commit | 0049b1dd007d12163b81f081656896e11b20c16c (patch) | |
tree | 50bd6a8346b7ae3a04fe720819286f4a875bdc7f | |
parent | 0d5118a96273976d305496670b2ac5561aa22050 (diff) |
Use SameController in MasterApp.
-rw-r--r-- | same/src/main/java/com/orbekk/same/MasterApp.java | 57 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/MasterServiceImpl.java | 21 |
2 files changed, 52 insertions, 26 deletions
diff --git a/same/src/main/java/com/orbekk/same/MasterApp.java b/same/src/main/java/com/orbekk/same/MasterApp.java index 428336c..286b827 100644 --- a/same/src/main/java/com/orbekk/same/MasterApp.java +++ b/same/src/main/java/com/orbekk/same/MasterApp.java @@ -12,34 +12,41 @@ public class MasterApp { private static final int timeout = 1000; public void run(int port) { - ConnectionManagerImpl connections = new ConnectionManagerImpl(timeout, - timeout); - State state = new State("MasterNetwork"); - Broadcaster broadcaster = - BroadcasterImpl.getDefaultBroadcastRunner(); - MasterServiceImpl master = new MasterServiceImpl(state, connections, - broadcaster); - JsonRpcServer jsonServer = new JsonRpcServer(master, MasterService.class); - server = new Server(port); - RpcHandler rpcHandler = new RpcHandler(master); - rpcHandler.addRpcServer("/MasterService.json", jsonServer); - server.setHandler(rpcHandler); - - Thread masterThread = new Thread(master); - masterThread.start(); - +// ConnectionManagerImpl connections = new ConnectionManagerImpl(timeout, +// timeout); +// State state = new State("MasterNetwork"); +// Broadcaster broadcaster = +// BroadcasterImpl.getDefaultBroadcastRunner(); +// MasterServiceImpl master = new MasterServiceImpl(state, connections, +// broadcaster); +// JsonRpcServer jsonServer = new JsonRpcServer(master, MasterService.class); +// server = new Server(port); +// RpcHandler rpcHandler = new RpcHandler(master); +// rpcHandler.addRpcServer("/MasterService.json", jsonServer); +// server.setHandler(rpcHandler); +// +// Thread masterThread = new Thread(master); +// masterThread.start(); +// +// try { +// server.start(); +// } catch (Exception e) { +// logger.error("Could not start jetty server: {}", e); +// } +// +// try { +// server.join(); +// masterThread.join(); +// } catch (InterruptedException e) { +// logger.info("Received exception. Exiting. {}", e); +// } + SameController controller = SameController.create(port); try { - server.start(); + controller.start(); } catch (Exception e) { - logger.error("Could not start jetty server: {}", e); - } - - try { - server.join(); - masterThread.join(); - } catch (InterruptedException e) { - logger.info("Received exception. Exiting. {}", e); + logger.error("Failed to start Same", e); } + controller.join(); } public static void main(String[] args) { diff --git a/same/src/main/java/com/orbekk/same/MasterServiceImpl.java b/same/src/main/java/com/orbekk/same/MasterServiceImpl.java index dc5eed3..7b441b5 100644 --- a/same/src/main/java/com/orbekk/same/MasterServiceImpl.java +++ b/same/src/main/java/com/orbekk/same/MasterServiceImpl.java @@ -15,6 +15,7 @@ public class MasterServiceImpl implements MasterService, UrlReceiver, Runnable { private boolean stopped = false; private Broadcaster broadcaster; private List<String> _fullStateReceivers = new ArrayList<String>(); + private Thread workerThread = null; public MasterServiceImpl(State initialState, ConnectionManager connections, Broadcaster broadcaster) { @@ -132,10 +133,28 @@ public class MasterServiceImpl implements MasterService, UrlReceiver, Runnable { try { wait(500); } catch (InterruptedException e) { - // Ignore interrupt in wait loop. + stopped = true; } } } + if (Thread.interrupted()) { + stopped = true; + } + } + } + + public void start() { + if (workerThread == null) { + workerThread = new Thread(this); + workerThread.start(); } } + + public void join() throws InterruptedException { + workerThread.join(); + } + + public void interrupt() { + workerThread.interrupt(); + } } |