From 24107a8e9c55e7907b3b226c4ac28ef0c8ba61bf Mon Sep 17 00:00:00 2001 From: Adrian Tiberius Date: Tue, 6 Oct 2015 18:27:12 +0300 Subject: [PATCH] Implemented rpc server change functionality and added some rpc trace messages --- .../java/org/ethereum/android/Ethereum.java | 5 +++ .../android/jsonrpc/light/JsonRpcServer.java | 22 +++++++++++-- .../jsonrpc/light/JsonRpcServerMethod.java | 20 +++++++++-- .../android/service/EthereumConnector.java | 33 +++++++++++++++++-- .../service/EthereumRemoteService.java | 23 +++++++++++++ .../service/EthereumServiceMessage.java | 5 +++ .../java/org/ethereum/facade/Ethereum.java | 2 ++ .../org/ethereum/facade/EthereumImpl.java | 5 +++ 8 files changed, 107 insertions(+), 8 deletions(-) diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/Ethereum.java b/ethereumj-core-android/src/main/java/org/ethereum/android/Ethereum.java index a91b454a..c4a6bd92 100644 --- a/ethereumj-core-android/src/main/java/org/ethereum/android/Ethereum.java +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/Ethereum.java @@ -389,4 +389,9 @@ public class Ethereum implements org.ethereum.facade.Ethereum { blockchain.setExitOn(number); } + + public EthereumListener getListener() { + + return this.listener; + } } diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/jsonrpc/light/JsonRpcServer.java b/ethereumj-core-android/src/main/java/org/ethereum/android/jsonrpc/light/JsonRpcServer.java index 0eae5d58..8cf86e93 100644 --- a/ethereumj-core-android/src/main/java/org/ethereum/android/jsonrpc/light/JsonRpcServer.java +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/jsonrpc/light/JsonRpcServer.java @@ -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()); } } diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/jsonrpc/light/JsonRpcServerMethod.java b/ethereumj-core-android/src/main/java/org/ethereum/android/jsonrpc/light/JsonRpcServerMethod.java index 3840ee16..914aee00 100644 --- a/ethereumj-core-android/src/main/java/org/ethereum/android/jsonrpc/light/JsonRpcServerMethod.java +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/jsonrpc/light/JsonRpcServerMethod.java @@ -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 { diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumConnector.java b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumConnector.java index 8866b782..202e28ce 100644 --- a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumConnector.java +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumConnector.java @@ -27,12 +27,14 @@ public class EthereumConnector extends ServiceConnector { public void init(List 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 diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumRemoteService.java b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumRemoteService.java index 5b2acae7..91084add 100644 --- a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumRemoteService.java +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumRemoteService.java @@ -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 * diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumServiceMessage.java b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumServiceMessage.java index 157a940b..7deacc48 100644 --- a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumServiceMessage.java +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumServiceMessage.java @@ -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; } diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java b/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java index cdf83590..f7d41000 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java @@ -137,4 +137,6 @@ public interface Ethereum { public BlockLoader getBlockLoader(); public void exitOn(long number); + + public EthereumListener getListener(); } diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java index 519f3fe3..611466dc 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java @@ -265,4 +265,9 @@ public class EthereumImpl implements Ethereum { public void exitOn(long number) { worldManager.getBlockchain().setExitOn(number); } + + @Override + public EthereumListener getListener() { + return listener; + } } \ No newline at end of file