Fix bug where changing profile causes jsonRpcServer to become unusable.

This commit is contained in:
Adrian Tiberius 2015-10-22 02:09:38 +02:00
parent 3a9486fd26
commit ed8a2b6f5f
4 changed files with 37 additions and 4 deletions

View File

@ -5,4 +5,5 @@ import org.ethereum.facade.Ethereum;
public abstract class JsonRpcServer { public abstract class JsonRpcServer {
public JsonRpcServer(Ethereum ethereum) {}; public JsonRpcServer(Ethereum ethereum) {};
public abstract void start() throws Exception; public abstract void start() throws Exception;
public abstract void stop();
} }

View File

@ -26,6 +26,8 @@ public final class JsonRpcServer extends org.ethereum.android.jsonrpc.JsonRpcSer
private Ethereum ethereum; private Ethereum ethereum;
private Dispatcher dispatcher; private Dispatcher dispatcher;
EventLoopGroup bossGroup;
EventLoopGroup workerGroup;
public JsonRpcServer(Ethereum ethereum) { public JsonRpcServer(Ethereum ethereum) {
super(ethereum); super(ethereum);
@ -105,8 +107,8 @@ public final class JsonRpcServer extends org.ethereum.android.jsonrpc.JsonRpcSer
} }
public void start() throws Exception { public void start() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1); bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup();
try { try {
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024); b.option(ChannelOption.SO_BACKLOG, 1024);
@ -125,6 +127,13 @@ public final class JsonRpcServer extends org.ethereum.android.jsonrpc.JsonRpcSer
} }
} }
public void stop() {
if (bossGroup != null) {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
class JsonRpcServerInitializer extends ChannelInitializer<SocketChannel> { class JsonRpcServerInitializer extends ChannelInitializer<SocketChannel> {
@Override @Override
public void initChannel(SocketChannel ch) { public void initChannel(SocketChannel ch) {

View File

@ -30,6 +30,9 @@ public final class JsonRpcServer extends org.ethereum.android.jsonrpc.JsonRpcSer
private Ethereum ethereum; private Ethereum ethereum;
private Dispatcher dispatcher; private Dispatcher dispatcher;
EventLoopGroup bossGroup;
EventLoopGroup workerGroup;
public JsonRpcServer(Ethereum ethereum) { public JsonRpcServer(Ethereum ethereum) {
super(ethereum); super(ethereum);
this.ethereum = ethereum; this.ethereum = ethereum;
@ -83,8 +86,8 @@ public final class JsonRpcServer extends org.ethereum.android.jsonrpc.JsonRpcSer
} }
public void start() throws Exception { public void start() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1); bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup();
try { try {
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024); b.option(ChannelOption.SO_BACKLOG, 1024);
@ -103,6 +106,13 @@ public final class JsonRpcServer extends org.ethereum.android.jsonrpc.JsonRpcSer
} }
} }
public void stop() {
if (bossGroup != null) {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
class JsonRpcServerInitializer extends ChannelInitializer<SocketChannel> { class JsonRpcServerInitializer extends ChannelInitializer<SocketChannel> {
@Override @Override
public void initChannel(SocketChannel ch) { public void initChannel(SocketChannel ch) {

View File

@ -216,6 +216,7 @@ public class EthereumRemoteService extends EthereumService {
protected void init(Message message) { protected void init(Message message) {
if (isEthereumStarted) { if (isEthereumStarted) {
stopJsonRpcServer();
closeEthereum(null); closeEthereum(null);
ethereum = null; ethereum = null;
component = null; component = null;
@ -225,6 +226,7 @@ public class EthereumRemoteService extends EthereumService {
Bundle data = message.getData(); Bundle data = message.getData();
List<String> privateKeys = data.getStringArrayList("privateKeys"); List<String> privateKeys = data.getStringArrayList("privateKeys");
ethereum.init(privateKeys); ethereum.init(privateKeys);
startJsonRpc(null);
isEthereumStarted = true; isEthereumStarted = true;
} }
@ -318,6 +320,17 @@ public class EthereumRemoteService extends EthereumService {
} }
} }
protected void stopJsonRpcServer() {
if (jsonRpcServerThread != null) {
jsonRpcServerThread.interrupt();
jsonRpcServerThread = null;
}
if (jsonRpcServer != null) {
jsonRpcServer.stop();
jsonRpcServer = null;
}
}
/** /**
* Start the json rpc server * Start the json rpc server
* *