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))
.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();
}

View File

@ -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) {

View File

@ -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<Connection> connections = new LinkedList<>();
private InternalContext ctx;
private ServerSocket serverSocket;
private ExecutorService pool;
private Thread serverThread;
private Thread connectionManager;
private ConcurrentMap<InventoryVector, Long> 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