Fixed some problems and added cleanup on shutdown

This commit is contained in:
Christian Basler 2015-10-12 12:44:13 +02:00
parent 3d1bd7227b
commit 117ac3ca73
3 changed files with 8 additions and 8 deletions

View File

@ -52,7 +52,7 @@ public class Application {
.messageRepo(new JdbcMessageRepository(jdbcConfig)) .messageRepo(new JdbcMessageRepository(jdbcConfig))
.networkHandler(new DefaultNetworkHandler()) .networkHandler(new DefaultNetworkHandler())
.security(new BouncySecurity()) .security(new BouncySecurity())
.port(48444) // .port(48444)
.listener(new BitmessageContext.Listener() { .listener(new BitmessageContext.Listener() {
@Override @Override
public void receive(Plaintext plaintext) { public void receive(Plaintext plaintext) {
@ -109,6 +109,7 @@ public class Application {
} }
} while (!"e".equals(command)); } while (!"e".equals(command));
LOG.info("Shutting down client"); LOG.info("Shutting down client");
ctx.cleanup();
ctx.shutdown(); ctx.shutdown();
} }

View File

@ -43,8 +43,6 @@ public class MultiThreadedPOWEngine implements ProofOfWorkEngine {
for (int i = 0; i < cores; i++) { for (int i = 0; i < cores; i++) {
Worker w = new Worker(workers, (byte) cores, i, initialHash, target); Worker w = new Worker(workers, (byte) cores, i, initialHash, target);
workers.add(w); workers.add(w);
}
for (Worker w : workers) {
w.start(); w.start();
} }
for (Worker w : workers) { for (Worker w : workers) {

View File

@ -48,19 +48,15 @@ import static ch.dissem.bitmessage.utils.DebugUtils.inc;
public class DefaultNetworkHandler implements NetworkHandler, ContextHolder { public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
public final static int NETWORK_MAGIC_NUMBER = 8; public final static int NETWORK_MAGIC_NUMBER = 8;
private final static Logger LOG = LoggerFactory.getLogger(DefaultNetworkHandler.class); private final static Logger LOG = LoggerFactory.getLogger(DefaultNetworkHandler.class);
private final ExecutorService pool;
private final List<Connection> connections = new LinkedList<>(); private final List<Connection> connections = new LinkedList<>();
private InternalContext ctx; private InternalContext ctx;
private ServerSocket serverSocket; private ServerSocket serverSocket;
private ExecutorService pool;
private Thread serverThread; private Thread serverThread;
private Thread connectionManager; private Thread connectionManager;
private ConcurrentMap<InventoryVector, Long> requestedObjects = new ConcurrentHashMap<>(); private ConcurrentMap<InventoryVector, Long> requestedObjects = new ConcurrentHashMap<>();
public DefaultNetworkHandler() {
pool = Executors.newCachedThreadPool();
}
@Override @Override
public void setContext(InternalContext context) { public void setContext(InternalContext context) {
this.ctx = context; this.ctx = context;
@ -82,7 +78,12 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
if (listener == null) { if (listener == null) {
throw new IllegalStateException("Listener must be set at start"); 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 { try {
pool = Executors.newCachedThreadPool();
connections.clear();
serverSocket = new ServerSocket(ctx.getPort()); serverSocket = new ServerSocket(ctx.getPort());
serverThread = new Thread(new Runnable() { serverThread = new Thread(new Runnable() {
@Override @Override