Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
Implemented rpc server change functionality and added some rpc trace …
Browse files Browse the repository at this point in the history
…messages
  • Loading branch information
adrian-tiberius committed Oct 6, 2015
1 parent c3261a4 commit 24107a8
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,9 @@ public void exitOn(long number) {

blockchain.setExitOn(number);
}

public EthereumListener getListener() {

return this.listener;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,29 @@ public JsonRpcServer(Ethereum 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 {
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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;

Expand All @@ -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;
Expand Down Expand Up @@ -149,13 +151,25 @@ protected Transaction jsToTransaction(JSONObject obj) throws Exception {


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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public EthereumConnector(Context context, Class serviceClass) {

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);
Expand Down Expand Up @@ -115,6 +117,33 @@ public void startJsonRpc() {
}
}

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ protected boolean handleMessage(Message message) {
startJsonRpc(message);
break;

case EthereumServiceMessage.MSG_CHANGE_JSON_RPC_SERVER:
changeJsonRpc(message);
break;

case EthereumServiceMessage.MSG_FIND_ONLINE_PEER:
findOnlinePeer(message);
break;
Expand Down Expand Up @@ -342,6 +346,25 @@ public void run() {
}
}

/**
* 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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,6 @@ public Transaction createTransaction(BigInteger nonce,
public BlockLoader getBlockLoader();

public void exitOn(long number);

public EthereumListener getListener();
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,9 @@ public BlockLoader getBlockLoader(){
public void exitOn(long number) {
worldManager.getBlockchain().setExitOn(number);
}

@Override
public EthereumListener getListener() {
return listener;
}
}

0 comments on commit 24107a8

Please sign in to comment.