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); 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)); 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 { try {
RemoteServer.add(new URL(address)); RemoteServer.add(new URL(serverUrl));
} catch (Exception e) { } 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.ethereum.facade.Ethereum;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -17,6 +18,7 @@ public abstract class JsonRpcServerMethod implements RequestHandler {
private String name = ""; private String name = "";
protected Ethereum ethereum; protected Ethereum ethereum;
private JSONRPC2Session jpSession; private JSONRPC2Session jpSession;
private String currentUrl;
public JsonRpcServerMethod(Ethereum ethereum) { public JsonRpcServerMethod(Ethereum ethereum) {
this.ethereum = ethereum; this.ethereum = ethereum;
@ -149,13 +151,25 @@ public abstract class JsonRpcServerMethod implements RequestHandler {
protected JSONRPC2Response getRemoteData(JSONRPC2Request req) { protected JSONRPC2Response getRemoteData(JSONRPC2Request req) {
if (jpSession == null) { URL url = JsonRpcServer.getRemoteServer();
jpSession = new JSONRPC2Session(JsonRpcServer.getRemoteServer()); boolean isChanged = !url.toString().equals(currentUrl);
if (isChanged) {
currentUrl = url.toString();
} }
try { try {
if (jpSession == null) {
jpSession = new JSONRPC2Session(url);
} else {
if (isChanged) {
currentUrl = url.toString();
jpSession = new JSONRPC2Session(url);
}
}
return jpSession.send(req); return jpSession.send(req);
} catch (Exception e) { } 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) { if (!JsonRpcServer.IsRemoteServerRecuring) {
return getRemoteData(req); return getRemoteData(req);
} else { } else {

View File

@ -27,8 +27,10 @@ public class EthereumConnector extends ServiceConnector {
public void init(List<String> privateKeys) { public void init(List<String> privateKeys) {
if (!isBound) if (!isBound) {
System.out.println(" Not bound ???");
return; return;
}
Message msg = Message.obtain(null, EthereumServiceMessage.MSG_INIT, 0, 0); Message msg = Message.obtain(null, EthereumServiceMessage.MSG_INIT, 0, 0);
Bundle data = new Bundle(); Bundle data = new Bundle();
@ -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 * Find an online peer
* @param identifier String Caller identifier used to return the response * @param identifier String Caller identifier used to return the response

View File

@ -154,6 +154,10 @@ public class EthereumRemoteService extends EthereumService {
startJsonRpc(message); startJsonRpc(message);
break; break;
case EthereumServiceMessage.MSG_CHANGE_JSON_RPC_SERVER:
changeJsonRpc(message);
break;
case EthereumServiceMessage.MSG_FIND_ONLINE_PEER: case EthereumServiceMessage.MSG_FIND_ONLINE_PEER:
findOnlinePeer(message); findOnlinePeer(message);
break; 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 * 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 * Command to the service to initialize with specific addresses. If already initialized, restart the service
*/ */
public static final int MSG_INIT = 16; 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 BlockLoader getBlockLoader();
public void exitOn(long number); public void exitOn(long number);
public EthereumListener getListener();
} }

View File

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