From e611e5e20a764630add6c6fb7ff3a335db993861 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 17 Apr 2015 13:01:46 +0200 Subject: [PATCH] More or less properly shutdown client / network node --- .../java/ch/dissem/bitmessage/demo/Main.java | 7 ++-- .../bitmessage/ports/NetworkHandler.java | 4 +-- .../bitmessage/networking/Connection.java | 2 +- .../bitmessage/networking/NetworkNode.java | 35 ++++++++++++------- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/demo/src/main/java/ch/dissem/bitmessage/demo/Main.java b/demo/src/main/java/ch/dissem/bitmessage/demo/Main.java index 05d2e36..ac0cad8 100644 --- a/demo/src/main/java/ch/dissem/bitmessage/demo/Main.java +++ b/demo/src/main/java/ch/dissem/bitmessage/demo/Main.java @@ -39,17 +39,18 @@ public class Main { DatabaseRepository repo = new DatabaseRepository(); Context.init(repo, repo, networkNode, 48444); Context.getInstance().addStream(1); - networkNode.setListener(new NetworkHandler.MessageListener() { + networkNode.start(new NetworkHandler.MessageListener() { @Override public void receive(ObjectPayload payload) { // LOG.info("message received: " + payload); // System.out.print('.'); } }); - networkNode.start(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - System.out.print("Enter String"); + System.out.print("Press Enter to exit\n"); br.readLine(); + LOG.info("Shutting down client"); + networkNode.stop(); } } diff --git a/domain/src/main/java/ch/dissem/bitmessage/ports/NetworkHandler.java b/domain/src/main/java/ch/dissem/bitmessage/ports/NetworkHandler.java index 95b37da..70bda06 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/ports/NetworkHandler.java +++ b/domain/src/main/java/ch/dissem/bitmessage/ports/NetworkHandler.java @@ -22,9 +22,9 @@ import ch.dissem.bitmessage.entity.payload.ObjectPayload; * Handles incoming messages */ public interface NetworkHandler { - void setListener(MessageListener listener); + void start(MessageListener listener); - void start(); + void stop(); void send(ObjectPayload payload); diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java index e68e55d..a6e2d0a 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java @@ -183,7 +183,7 @@ public class Connection implements Runnable { } } - private void disconnect() { + public void disconnect() { try { state = DISCONNECTED; socket.close(); diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/NetworkNode.java b/networking/src/main/java/ch/dissem/bitmessage/networking/NetworkNode.java index f5e19a9..82a146b 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/NetworkNode.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/NetworkNode.java @@ -41,41 +41,34 @@ public class NetworkNode implements NetworkHandler { private final static Logger LOG = LoggerFactory.getLogger(NetworkNode.class); private final ExecutorService pool; private final List connections = new LinkedList<>(); - private MessageListener listener; + private ServerSocket serverSocket; + private Thread connectionManager; public NetworkNode() { pool = Executors.newCachedThreadPool(); } @Override - public void setListener(final MessageListener listener) { - if (this.listener != null) { - throw new IllegalStateException("Listener can only be set once"); - } - this.listener = listener; - } - - @Override - public void start() { + public void start(final MessageListener listener) { final Context ctx = Context.getInstance(); if (listener == null) { throw new IllegalStateException("Listener must be set at start"); } try { - final ServerSocket serverSocket = new ServerSocket(Context.getInstance().getPort()); + serverSocket = new ServerSocket(Context.getInstance().getPort()); pool.execute(new Runnable() { @Override public void run() { try { Socket socket = serverSocket.accept(); - socket.setSoTimeout(20000); + socket.setSoTimeout(10000); startConnection(new Connection(SERVER, socket, listener)); } catch (IOException e) { LOG.debug(e.getMessage(), e); } } }); - Thread connectionManager = new Thread(new Runnable() { + connectionManager = new Thread(new Runnable() { @Override public void run() { while (!Thread.interrupted()) { @@ -114,6 +107,22 @@ public class NetworkNode implements NetworkHandler { } } + @Override + public void stop() { + connectionManager.interrupt(); + try { + serverSocket.close(); + } catch (IOException e) { + LOG.debug(e.getMessage(), e); + } + synchronized (connections) { + for (Connection c : connections) { + c.disconnect(); + } + } + pool.shutdownNow(); + } + private void startConnection(Connection c) { synchronized (connections) { connections.add(c);