Implemented rpc server change functionality and added some rpc trace messages

This commit is contained in:
Adrian Tiberius 2015-10-06 18:27:12 +03:00
parent c3261a469e
commit 24107a8e9c
8 changed files with 107 additions and 8 deletions

View File

@ -389,4 +389,9 @@ public class Ethereum implements org.ethereum.facade.Ethereum {
blockchain.setExitOn(number);
}
public EthereumListener getListener() {
return this.listener;
}
}

View File

@ -46,13 +46,29 @@ public final class JsonRpcServer extends org.ethereum.android.jsonrpc.JsonRpcSer
this.dispatcher.register(new proxy(this.ethereum));
addRemoteServer("http://139.162.13.89:8545/");
//addRemoteServer("http://rpc0.syng.io:8545/", true);
}
public static void addRemoteServer(String address) {
public static void addRemoteServer(String serverUrl) {
try {
RemoteServer.add(new URL(address));
RemoteServer.add(new URL(serverUrl));
} catch (Exception e) {
System.out.println("Exception adding remote server: " + e.getMessage());
}
}
public void addRemoteServer(String serverUrl, boolean clearList) {
try {
if (clearList) {
RemoteServer.clear();
}
RemoteServer.add(new URL(serverUrl));
System.out.println("Changed rpc remote server to: " + serverUrl);
this.ethereum.getListener().trace("Slaving to <" + serverUrl + ">");
} catch (Exception e) {
System.out.println("Exception adding remote server: " + e.getMessage());
this.ethereum.getListener().trace("Exception adding remote server: " + e.getMessage());
}
}

View File

@ -9,6 +9,7 @@ import org.ethereum.core.Transaction;
import org.ethereum.facade.Ethereum;
import org.spongycastle.util.encoders.Hex;
import java.math.BigInteger;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
@ -17,6 +18,7 @@ public abstract class JsonRpcServerMethod implements RequestHandler {
private String name = "";
protected Ethereum ethereum;
private JSONRPC2Session jpSession;
private String currentUrl;
public JsonRpcServerMethod(Ethereum ethereum) {
this.ethereum = ethereum;
@ -149,13 +151,25 @@ public abstract class JsonRpcServerMethod implements RequestHandler {
protected JSONRPC2Response getRemoteData(JSONRPC2Request req) {
if (jpSession == null) {
jpSession = new JSONRPC2Session(JsonRpcServer.getRemoteServer());
URL url = JsonRpcServer.getRemoteServer();
boolean isChanged = !url.toString().equals(currentUrl);
if (isChanged) {
currentUrl = url.toString();
}
try {
if (jpSession == null) {
jpSession = new JSONRPC2Session(url);
} else {
if (isChanged) {
currentUrl = url.toString();
jpSession = new JSONRPC2Session(url);
}
}
return jpSession.send(req);
} catch (Exception e) {
jpSession = new JSONRPC2Session(JsonRpcServer.getRemoteServer());
System.out.println("Exception getting remote rpc data: " + e.getMessage());
ethereum.getListener().trace("Exception getting remote rpc data: " + e.getMessage());
if (!JsonRpcServer.IsRemoteServerRecuring) {
return getRemoteData(req);
} else {

View File

@ -27,12 +27,14 @@ public class EthereumConnector extends ServiceConnector {
public void init(List<String> privateKeys) {
if (!isBound)
if (!isBound) {
System.out.println(" Not bound ???");
return;
}
Message msg = Message.obtain(null, EthereumServiceMessage.MSG_INIT, 0, 0);
Bundle data = new Bundle();
data.putStringArrayList("privateKeys", (ArrayList)privateKeys);
data.putStringArrayList("privateKeys", (ArrayList) privateKeys);
msg.setData(data);
try {
serviceMessenger.send(msg);
@ -115,6 +117,33 @@ public class EthereumConnector extends ServiceConnector {
}
}
/**
* Change the json rpc server url
*
* Sends message parameters: ( "key": type [description] ):
* {
* "rpc_server": String [Rpc server url]
* }
*/
public void changeJsonRpc(String serverUrl) {
if (!isBound) {
System.out.println("Connector is not bound.");
return;
}
Message msg = Message.obtain(null, EthereumServiceMessage.MSG_CHANGE_JSON_RPC_SERVER, 0, 0);
Bundle data = new Bundle();
data.putString("rpc_server", serverUrl);
msg.setData(data);
try {
serviceMessenger.send(msg);
System.out.println("Sent change rpc server message");
} catch (RemoteException e) {
logger.error("Exception sending message(changeJsonRpc) to service: " + e.getMessage());
}
}
/**
* Find an online peer
* @param identifier String Caller identifier used to return the response

View File

@ -154,6 +154,10 @@ public class EthereumRemoteService extends EthereumService {
startJsonRpc(message);
break;
case EthereumServiceMessage.MSG_CHANGE_JSON_RPC_SERVER:
changeJsonRpc(message);
break;
case EthereumServiceMessage.MSG_FIND_ONLINE_PEER:
findOnlinePeer(message);
break;
@ -342,6 +346,25 @@ public class EthereumRemoteService extends EthereumService {
}
}
/**
* Start the json rpc server
*
* Incoming message parameters: none
* Sends message: none
*/
protected void changeJsonRpc(Message message) {
Bundle data = message.getData();
String server = data.getString("rpc_server");
if (jsonRpcServer != null) {
((org.ethereum.android.jsonrpc.light.JsonRpcServer)jsonRpcServer).addRemoteServer(server, true);
} else {
System.out.println("jsonRpcServer is null on changeJsonRpc ??");
}
}
/**
* Find an online peer
*

View File

@ -82,4 +82,9 @@ public class EthereumServiceMessage {
* Command to the service to initialize with specific addresses. If already initialized, restart the service
*/
public static final int MSG_INIT = 16;
/**
* Command to the service to change the json rpc server
*/
public static final int MSG_CHANGE_JSON_RPC_SERVER = 17;
}

View File

@ -137,4 +137,6 @@ public interface Ethereum {
public BlockLoader getBlockLoader();
public void exitOn(long number);
public EthereumListener getListener();
}

View File

@ -265,4 +265,9 @@ public class EthereumImpl implements Ethereum {
public void exitOn(long number) {
worldManager.getBlockchain().setExitOn(number);
}
@Override
public EthereumListener getListener() {
return listener;
}
}