From 5c9058d980d3704ef8de366278e43bad029386bf Mon Sep 17 00:00:00 2001 From: Yaroslav Dmytrotsa Date: Thu, 9 Jul 2015 20:50:50 +0300 Subject: [PATCH] add addRemoteServer for light JSON-RPC to support multiple servers and auto recurring over them to find working one. --- .../android/jsonrpc/light/JsonRpcServer.java | 25 ++++++++++++++++++- .../jsonrpc/light/JsonRpcServerMethod.java | 13 +++++----- 2 files changed, 31 insertions(+), 7 deletions(-) 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 a6d633af..15807de0 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 @@ -37,12 +37,16 @@ import static io.netty.handler.codec.http.HttpHeaders.Names.*; import org.ethereum.android.jsonrpc.light.method.*; import java.net.InetAddress; +import java.net.URL; +import java.util.ArrayList; public final class JsonRpcServer extends org.ethereum.android.jsonrpc.JsonRpcServer{ static public final int PORT = 8545; - static public final String RemoteServer = "http://192.168.1.30:8545/"; + static private ArrayList RemoteServer = new ArrayList<>(); + static private int currentRemoteServer = 0; + static public boolean IsRemoteServerRecuring = false; private Ethereum ethereum; private Dispatcher dispatcher; @@ -72,6 +76,25 @@ public final class JsonRpcServer extends org.ethereum.android.jsonrpc.JsonRpcSer this.dispatcher.register(new proxy(this.ethereum)); FilterManager.getInstance(); + + addRemoteServer("http://192.168.1.30:8545/"); + } + + public static void addRemoteServer(String address) { + try { + RemoteServer.add(new URL(address)); + } catch (Exception e) { + } + } + + public static URL getRemoteServer() { + if (currentRemoteServer >= RemoteServer.size()){ + currentRemoteServer = 0; + IsRemoteServerRecuring = true; + } + URL res = RemoteServer.get(currentRemoteServer); + currentRemoteServer++; + return res; } public void start() throws Exception { 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 c2e33148..bfe59963 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 @@ -147,16 +147,17 @@ public abstract class JsonRpcServerMethod implements RequestHandler { protected JSONRPC2Response getRemoteData(JSONRPC2Request req) { if (jpSession == null) { - try { - jpSession = new JSONRPC2Session(new URL(JsonRpcServer.RemoteServer)); - } catch (Exception e) { - return null; - } + jpSession = new JSONRPC2Session(JsonRpcServer.getRemoteServer()); } try { return jpSession.send(req); } catch (Exception e) { - return null; + jpSession = new JSONRPC2Session(JsonRpcServer.getRemoteServer()); + if (!JsonRpcServer.IsRemoteServerRecuring) { + return getRemoteData(req); + } else { + return null; + } } }