From 411e81d8f9d9258c6458eec2cec09c8d71f409de Mon Sep 17 00:00:00 2001 From: henry Date: Tue, 6 Mar 2018 22:57:22 -0400 Subject: [PATCH] Implemented Crypto Coin Network Manager and Verifier --- .../network/BitsharesCryptoNetVerifier.java | 46 +++++++ .../network/CryptoNetManager.java | 115 ++++++++++++++++++ .../network/CryptoNetVerifier.java | 21 ++++ .../crystalwallet/network/GetChainId.java | 61 ++++++++++ 4 files changed, 243 insertions(+) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/network/BitsharesCryptoNetVerifier.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetManager.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetVerifier.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/network/GetChainId.java diff --git a/app/src/main/java/cy/agorise/crystalwallet/network/BitsharesCryptoNetVerifier.java b/app/src/main/java/cy/agorise/crystalwallet/network/BitsharesCryptoNetVerifier.java new file mode 100644 index 0000000..c1839b8 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/network/BitsharesCryptoNetVerifier.java @@ -0,0 +1,46 @@ +package cy.agorise.crystalwallet.network; + +import cy.agorise.crystalwallet.enums.CryptoNet; +import cy.agorise.graphenej.interfaces.WitnessResponseListener; +import cy.agorise.graphenej.models.BaseResponse; +import cy.agorise.graphenej.models.WitnessResponse; + +/** + * + * Created by henry on 28/2/2018. + */ + +public class BitsharesCryptoNetVerifier extends CryptoNetVerifier { + + /** + * TODO We need to change this to a type of subCryptoNet + */ + private final CryptoNet cryptoNet = CryptoNet.BITSHARES; + /** + * Todo info need to be on the SubCryptoNet + */ + private final String CHAIN_ID = "9cf6f255a208100d2bb275a3c52f4b1589b7ec9c9bfc2cb2a5fe6411295106d8";//testnet + + + @Override + public void checkURL(final String url) { + final long startTime = System.currentTimeMillis(); + WebSocketThread thread = new WebSocketThread(new GetChainId(new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + if(response.result instanceof String) { + if(response.result.equals(CHAIN_ID)) { + CryptoNetManager.verifiedCryptoNetURL(cryptoNet, url, System.currentTimeMillis() - startTime); + }else{ + System.out.println("Error we are not in the net current chain id " + response.result + " excepted " + CHAIN_ID); + } + } + } + + @Override + public void onError(BaseResponse.Error error) { + + } + }),url); + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetManager.java b/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetManager.java new file mode 100644 index 0000000..f8bed24 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetManager.java @@ -0,0 +1,115 @@ +package cy.agorise.crystalwallet.network; + +import android.support.annotation.NonNull; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; + +import cy.agorise.crystalwallet.enums.CryptoNet; + +/** + * Created by henry on 6/3/2018. + */ + +public abstract class CryptoNetManager { + /** + * This map contains the list of the urls to be tested + */ + private static HashMap> CryptoNetURLs = new HashMap<>(); + + /** + * This map contains the list of urls been tested and ordered by the fastests + */ + private static HashMap> TestedURLs = new HashMap<>(); + + public static String getURL(CryptoNet crypto, int index){ + if(TestedURLs.containsKey(crypto) && TestedURLs.get(crypto).size()()); + } + + CryptoNetURLs.get(crypto).add(url); + } + + public static void removeCryptoNetURL(CryptoNet crypto, String url){ + if(CryptoNetURLs.containsKey(crypto)){ + CryptoNetURLs.get(crypto).remove(url); + } + } + + public static void verifiedCryptoNetURL(CryptoNet crypto, String url, long time){ + if(CryptoNetURLs.containsKey(crypto) && CryptoNetURLs.get(crypto).contains(url)){ + if(!TestedURLs.containsKey(crypto)){ + TestedURLs.put(crypto,new ArrayList()); + } + TestedURL testedUrl = new TestedURL(time,url); + if(!TestedURLs.get(crypto).contains(testedUrl)){ + TestedURLs.get(crypto).add(testedUrl); + Collections.reverse(TestedURLs.get(crypto)); + } + } + } + + private static class TestedURL implements Comparable{ + private long time; + private String url; + + public TestedURL(long time, String url) { + this.time = time; + this.url = url; + } + + public long getTime() { + return time; + } + + String getUrl() { + return url; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof TestedURL)) return false; + + TestedURL testedURL = (TestedURL) o; + + return getUrl().equals(testedURL.getUrl()); + } + + @Override + public int hashCode() { + return getUrl().hashCode(); + } + + @Override + public int compareTo(@NonNull Object o) { + if (this == o) return 0; + if (!(o instanceof TestedURL)) return 0; + + TestedURL testedURL = (TestedURL) o; + return (int) (this.getTime() - testedURL.getTime()); + } + } + +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetVerifier.java b/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetVerifier.java new file mode 100644 index 0000000..5d507a8 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/network/CryptoNetVerifier.java @@ -0,0 +1,21 @@ +package cy.agorise.crystalwallet.network; + +import cy.agorise.crystalwallet.enums.CryptoNet; + +/** + * This is used to check if the connection is stable and fast. + * + * Also verifies if the connection with the server is valid. + * + * Created by henry on 28/2/2018. + */ + +public abstract class CryptoNetVerifier { + + public static CryptoNetVerifier getNetworkVerify(CryptoNet cryptoNet){ + + return null; + } + + public abstract void checkURL(String url); +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/network/GetChainId.java b/app/src/main/java/cy/agorise/crystalwallet/network/GetChainId.java new file mode 100644 index 0000000..c52b1cb --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/network/GetChainId.java @@ -0,0 +1,61 @@ +package cy.agorise.crystalwallet.network; + +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import com.neovisionaries.ws.client.WebSocket; +import com.neovisionaries.ws.client.WebSocketFrame; + +import java.io.Serializable; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import cy.agorise.graphenej.RPC; +import cy.agorise.graphenej.api.BaseGrapheneHandler; +import cy.agorise.graphenej.interfaces.WitnessResponseListener; +import cy.agorise.graphenej.models.ApiCall; +import cy.agorise.graphenej.models.WitnessResponse; + +/** + * Created by henry on 28/2/2018. + */ + +public class GetChainId extends BaseGrapheneHandler { + + private final WitnessResponseListener mListener; + + public GetChainId(WitnessResponseListener listener) { + super(listener); + this.mListener = listener; + } + + @Override + public void onConnected(WebSocket websocket, Map> headers) throws Exception { + ApiCall getAccountByName = new ApiCall(0, "Get_Chain_Id", new ArrayList(), RPC.VERSION, 1); + websocket.sendText(getAccountByName.toJsonString()); + } + + @Override + public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + System.out.println("<<< "+frame.getPayloadText()); + String response = frame.getPayloadText(); + + Type GetChainIdResponse = new TypeToken>(){}.getType(); + GsonBuilder builder = new GsonBuilder(); + WitnessResponse> witnessResponse = builder.create().fromJson(response, GetChainIdResponse); + if(witnessResponse.error != null){ + this.mListener.onError(witnessResponse.error); + }else{ + this.mListener.onSuccess(witnessResponse); + } + + websocket.disconnect(); + } + + @Override + public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { + if(frame.isTextFrame()) + System.out.println(">>> "+frame.getPayloadText()); + } +}