diff --git a/demo/src/main/java/ch/dissem/bitmessage/demo/Application.java b/demo/src/main/java/ch/dissem/bitmessage/demo/Application.java index d93c3c8..7644ed8 100644 --- a/demo/src/main/java/ch/dissem/bitmessage/demo/Application.java +++ b/demo/src/main/java/ch/dissem/bitmessage/demo/Application.java @@ -52,7 +52,7 @@ public class Application { .messageRepo(new JdbcMessageRepository(jdbcConfig)) .networkHandler(new DefaultNetworkHandler()) .security(new BouncySecurity()) - .port(48444) +// .port(48444) .listener(new BitmessageContext.Listener() { @Override public void receive(Plaintext plaintext) { @@ -109,6 +109,7 @@ public class Application { } } while (!"e".equals(command)); LOG.info("Shutting down client"); + ctx.cleanup(); ctx.shutdown(); } diff --git a/domain/src/main/java/ch/dissem/bitmessage/ports/MultiThreadedPOWEngine.java b/domain/src/main/java/ch/dissem/bitmessage/ports/MultiThreadedPOWEngine.java index 9814c3a..3c4572e 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/ports/MultiThreadedPOWEngine.java +++ b/domain/src/main/java/ch/dissem/bitmessage/ports/MultiThreadedPOWEngine.java @@ -43,8 +43,6 @@ public class MultiThreadedPOWEngine implements ProofOfWorkEngine { for (int i = 0; i < cores; i++) { Worker w = new Worker(workers, (byte) cores, i, initialHash, target); workers.add(w); - } - for (Worker w : workers) { w.start(); } for (Worker w : workers) { diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/DefaultNetworkHandler.java b/networking/src/main/java/ch/dissem/bitmessage/networking/DefaultNetworkHandler.java index b6b3c69..69e9e16 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/DefaultNetworkHandler.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/DefaultNetworkHandler.java @@ -48,19 +48,15 @@ import static ch.dissem.bitmessage.utils.DebugUtils.inc; public class DefaultNetworkHandler implements NetworkHandler, ContextHolder { public final static int NETWORK_MAGIC_NUMBER = 8; private final static Logger LOG = LoggerFactory.getLogger(DefaultNetworkHandler.class); - private final ExecutorService pool; private final List connections = new LinkedList<>(); private InternalContext ctx; private ServerSocket serverSocket; + private ExecutorService pool; private Thread serverThread; private Thread connectionManager; private ConcurrentMap requestedObjects = new ConcurrentHashMap<>(); - public DefaultNetworkHandler() { - pool = Executors.newCachedThreadPool(); - } - @Override public void setContext(InternalContext context) { this.ctx = context; @@ -82,7 +78,12 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder { if (listener == null) { throw new IllegalStateException("Listener must be set at start"); } + if (pool != null && !pool.isShutdown()) { + throw new IllegalStateException("Network already running - you need to stop first."); + } try { + pool = Executors.newCachedThreadPool(); + connections.clear(); serverSocket = new ServerSocket(ctx.getPort()); serverThread = new Thread(new Runnable() { @Override