Implemented Crypto Coin Network Manager and Verifier

master
henry 2018-03-06 22:57:22 -04:00
parent 8464ee18ec
commit 411e81d8f9
4 changed files with 243 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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<CryptoNet,HashSet<String>> CryptoNetURLs = new HashMap<>();
/**
* This map contains the list of urls been tested and ordered by the fastests
*/
private static HashMap<CryptoNet,ArrayList<TestedURL>> TestedURLs = new HashMap<>();
public static String getURL(CryptoNet crypto, int index){
if(TestedURLs.containsKey(crypto) && TestedURLs.get(crypto).size()<index){
return TestedURLs.get(crypto).get(index).getUrl();
}
if(CryptoNetURLs.containsKey(crypto) && !CryptoNetURLs.get(crypto).isEmpty()){
}
return null;
}
public static int getURLSize(CryptoNet crypto){
if(TestedURLs.containsKey(crypto)){
return TestedURLs.get(crypto).size();
}
return 0;
}
public static void addCryptoNetURL(CryptoNet crypto, String url){
if(!CryptoNetURLs.containsKey(crypto)){
CryptoNetURLs.put(crypto,new HashSet<String>());
}
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 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());
}
}
}

View File

@ -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);
}

View File

@ -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<String, List<String>> headers) throws Exception {
ApiCall getAccountByName = new ApiCall(0, "Get_Chain_Id", new ArrayList<Serializable>(), 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<WitnessResponse<String>>(){}.getType();
GsonBuilder builder = new GsonBuilder();
WitnessResponse<List<String>> 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());
}
}