Introducing node hop feature to some blockchain API handler classes (GetRequiredFees, GetTradeHistory, ListAssets, LookupAccounts, LookupAssetSymbols and TransactionBroadcastSequence)
This commit is contained in:
parent
7313279f95
commit
5a58cf5579
7 changed files with 177 additions and 22 deletions
|
@ -31,12 +31,19 @@ public class GetRequiredFees extends WebSocketAdapter {
|
|||
private List<BaseOperation> operations;
|
||||
private Asset asset;
|
||||
|
||||
public GetRequiredFees(List<BaseOperation> operations, Asset asset, WitnessResponseListener listener){
|
||||
private boolean mOneTime;
|
||||
|
||||
public GetRequiredFees(List<BaseOperation> operations, Asset asset, boolean oneTime, WitnessResponseListener listener){
|
||||
this.operations = operations;
|
||||
this.asset = asset;
|
||||
this.mOneTime = oneTime;
|
||||
this.mListener = listener;
|
||||
}
|
||||
|
||||
public GetRequiredFees(List<BaseOperation> operations, Asset asset, WitnessResponseListener listener){
|
||||
this(operations, asset, true, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
|
||||
ArrayList<Serializable> accountParams = new ArrayList<>();
|
||||
|
@ -66,12 +73,16 @@ public class GetRequiredFees extends WebSocketAdapter {
|
|||
@Override
|
||||
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
|
||||
mListener.onError(new BaseResponse.Error(cause.getMessage()));
|
||||
websocket.disconnect();
|
||||
if(mOneTime){
|
||||
websocket.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
|
||||
mListener.onError(new BaseResponse.Error(cause.getMessage()));
|
||||
websocket.disconnect();
|
||||
if(mOneTime){
|
||||
websocket.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,16 +29,23 @@ public class GetTradeHistory extends BaseGrapheneHandler {
|
|||
private int limit;
|
||||
private WitnessResponseListener mListener;
|
||||
|
||||
public GetTradeHistory(String a, String b, String toTime, String fromTime,int limit, WitnessResponseListener mListener) {
|
||||
private boolean mOneTime;
|
||||
|
||||
public GetTradeHistory(String a, String b, String toTime, String fromTime,int limit, boolean oneTime, WitnessResponseListener mListener) {
|
||||
super(mListener);
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.toTime = toTime;
|
||||
this.fromTime = fromTime;
|
||||
this.limit = limit;
|
||||
this.mOneTime = oneTime;
|
||||
this.mListener = mListener;
|
||||
}
|
||||
|
||||
public GetTradeHistory(String a, String b, String toTime, String fromTime,int limit, WitnessResponseListener mListener) {
|
||||
this(a, b, toTime, fromTime, limit, true, mListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
|
||||
ArrayList<Serializable> accountParams = new ArrayList<>();
|
||||
|
@ -72,7 +79,9 @@ public class GetTradeHistory extends BaseGrapheneHandler {
|
|||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
websocket.disconnect();
|
||||
if(mOneTime){
|
||||
websocket.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -45,16 +45,29 @@ public class ListAssets extends BaseGrapheneHandler {
|
|||
private int limit;
|
||||
private int requestCounter = 0;
|
||||
|
||||
private boolean mOneTime;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param lowerBoundSymbol: Lower bound of symbol names to retrieve
|
||||
* @param limit: Maximum number of assets to fetch, if the constant LIST_ALL
|
||||
* is passed, all existing assets will be retrieved.
|
||||
*/
|
||||
public ListAssets(String lowerBoundSymbol, int limit, WitnessResponseListener listener){
|
||||
public ListAssets(String lowerBoundSymbol, int limit, boolean oneTime, WitnessResponseListener listener){
|
||||
super(listener);
|
||||
this.lowerBound = lowerBoundSymbol;
|
||||
this.limit = limit;
|
||||
this.mOneTime = oneTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with oneTime = true for compatibility issue.
|
||||
* @param lowerBoundSymbol: Lower bound of symbol names to retrieve
|
||||
* @param limit: Maximum number of assets to fetch, if the constant LIST_ALL
|
||||
* is passed, all existing assets will be retrieved.
|
||||
*/
|
||||
public ListAssets(String lowerBoundSymbol, int limit, WitnessResponseListener listener){
|
||||
this(lowerBoundSymbol, limit, true, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -81,7 +94,9 @@ public class ListAssets extends BaseGrapheneHandler {
|
|||
// If the requested number of assets was below
|
||||
// the limit, we just call the listener.
|
||||
mListener.onSuccess(witnessResponse);
|
||||
websocket.disconnect();
|
||||
if(mOneTime){
|
||||
websocket.disconnect();
|
||||
}
|
||||
}else{
|
||||
// Updating counter to keep track of how many batches we already retrieved.
|
||||
requestCounter++;
|
||||
|
@ -96,12 +111,16 @@ public class ListAssets extends BaseGrapheneHandler {
|
|||
// we got less than the requested amount.
|
||||
witnessResponse.result = this.assets;
|
||||
mListener.onSuccess(witnessResponse);
|
||||
websocket.disconnect();
|
||||
if(mOneTime){
|
||||
websocket.disconnect();
|
||||
}
|
||||
}else if(this.assets.size() == this.limit){
|
||||
// We already have the required amount of assets
|
||||
witnessResponse.result = this.assets;
|
||||
mListener.onSuccess(witnessResponse);
|
||||
websocket.disconnect();
|
||||
if(mOneTime){
|
||||
websocket.disconnect();
|
||||
}
|
||||
}else{
|
||||
// We still need to fetch some more assets
|
||||
this.lowerBound = this.assets.get(this.assets.size() - 1).getSymbol();
|
||||
|
|
|
@ -27,20 +27,32 @@ public class LookupAccounts extends BaseGrapheneHandler {
|
|||
private int maxAccounts = DEFAULT_MAX;
|
||||
private final WitnessResponseListener mListener;
|
||||
|
||||
public LookupAccounts(String accountName, WitnessResponseListener listener){
|
||||
private boolean mOneTime;
|
||||
|
||||
public LookupAccounts(String accountName, boolean oneTime, WitnessResponseListener listener){
|
||||
super(listener);
|
||||
this.accountName = accountName;
|
||||
this.maxAccounts = DEFAULT_MAX;
|
||||
this.mOneTime = oneTime;
|
||||
this.mListener = listener;
|
||||
}
|
||||
|
||||
public LookupAccounts(String accountName, int maxAccounts, WitnessResponseListener listener){
|
||||
public LookupAccounts(String accountName, int maxAccounts, boolean oneTime, WitnessResponseListener listener){
|
||||
super(listener);
|
||||
this.accountName = accountName;
|
||||
this.maxAccounts = maxAccounts;
|
||||
this.mOneTime = oneTime;
|
||||
this.mListener = listener;
|
||||
}
|
||||
|
||||
public LookupAccounts(String accountName, WitnessResponseListener listener){
|
||||
this(accountName, true, listener);
|
||||
}
|
||||
|
||||
public LookupAccounts(String accountName, int maxAccounts, WitnessResponseListener listener){
|
||||
this(accountName, maxAccounts, true, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
|
||||
ArrayList<Serializable> accountParams = new ArrayList<>();
|
||||
|
@ -65,7 +77,9 @@ public class LookupAccounts extends BaseGrapheneHandler {
|
|||
this.mListener.onSuccess(witnessResponse);
|
||||
}
|
||||
|
||||
websocket.disconnect();
|
||||
if(mOneTime){
|
||||
websocket.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -24,12 +24,19 @@ public class LookupAssetSymbols extends BaseGrapheneHandler {
|
|||
private WitnessResponseListener mListener;
|
||||
private List<Asset> assets;
|
||||
|
||||
public LookupAssetSymbols(List<Asset> assets, WitnessResponseListener listener){
|
||||
private boolean mOneTime;
|
||||
|
||||
public LookupAssetSymbols(List<Asset> assets, boolean oneTime, WitnessResponseListener listener){
|
||||
super(listener);
|
||||
this.assets = assets;
|
||||
this.mOneTime = oneTime;
|
||||
this.mListener = listener;
|
||||
}
|
||||
|
||||
public LookupAssetSymbols(List<Asset> assets, WitnessResponseListener listener){
|
||||
this(assets, true, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
|
||||
ArrayList<Serializable> params = new ArrayList<>();
|
||||
|
|
|
@ -43,18 +43,33 @@ public class TransactionBroadcastSequence extends BaseGrapheneHandler {
|
|||
private int currentId = 1;
|
||||
private int broadcastApiId = -1;
|
||||
|
||||
private boolean mOneTime;
|
||||
|
||||
/**
|
||||
* Constructor of this class. The ids required
|
||||
* @param transaction: The transaction to be broadcasted.
|
||||
* @param oneTime Boolean value indicating if websocket must be closed or not after request
|
||||
* @param listener: A class implementing the WitnessResponseListener interface. This should
|
||||
* be implemented by the party interested in being notified about the success/failure
|
||||
* of the transaction broadcast operation.
|
||||
*/
|
||||
public TransactionBroadcastSequence(Transaction transaction, Asset feeAsset, boolean oneTime, WitnessResponseListener listener){
|
||||
super(listener);
|
||||
this.transaction = transaction;
|
||||
this.feeAsset = feeAsset;
|
||||
this.mOneTime = oneTime;
|
||||
this.mListener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor of this class with oneTime=true
|
||||
* @param transaction: The transaction to be broadcasted.
|
||||
* @param listener: A class implementing the WitnessResponseListener interface. This should
|
||||
* be implemented by the party interested in being notified about the success/failure
|
||||
* of the transaction broadcast operation.
|
||||
*/
|
||||
public TransactionBroadcastSequence(Transaction transaction, Asset feeAsset, WitnessResponseListener listener){
|
||||
super(listener);
|
||||
this.transaction = transaction;
|
||||
this.feeAsset = feeAsset;
|
||||
this.mListener = listener;
|
||||
this(transaction, feeAsset, true, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -77,7 +92,9 @@ public class TransactionBroadcastSequence extends BaseGrapheneHandler {
|
|||
BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
|
||||
if(baseResponse.error != null){
|
||||
mListener.onError(baseResponse.error);
|
||||
websocket.disconnect();
|
||||
if(mOneTime){
|
||||
websocket.disconnect();
|
||||
}
|
||||
}else{
|
||||
currentId++;
|
||||
ArrayList<Serializable> emptyParams = new ArrayList<>();
|
||||
|
@ -140,7 +157,9 @@ public class TransactionBroadcastSequence extends BaseGrapheneHandler {
|
|||
Type WitnessResponseType = new TypeToken<WitnessResponse<String>>(){}.getType();
|
||||
WitnessResponse<String> witnessResponse = gson.fromJson(response, WitnessResponseType);
|
||||
mListener.onSuccess(witnessResponse);
|
||||
websocket.disconnect();
|
||||
if(mOneTime){
|
||||
websocket.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -156,7 +175,9 @@ public class TransactionBroadcastSequence extends BaseGrapheneHandler {
|
|||
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
|
||||
System.out.println("onError. cause: "+cause.getMessage());
|
||||
mListener.onError(new BaseResponse.Error(cause.getMessage()));
|
||||
websocket.disconnect();
|
||||
if(mOneTime){
|
||||
websocket.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -166,6 +187,8 @@ public class TransactionBroadcastSequence extends BaseGrapheneHandler {
|
|||
System.out.println(element.getFileName()+"#"+element.getClassName()+":"+element.getLineNumber());
|
||||
}
|
||||
mListener.onError(new BaseResponse.Error(cause.getMessage()));
|
||||
websocket.disconnect();
|
||||
if(mOneTime){
|
||||
websocket.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ package de.bitsharesmunich.graphenej.api.android;
|
|||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
|
@ -12,11 +13,14 @@ import de.bitsharesmunich.graphenej.api.GetAccountBalances;
|
|||
import de.bitsharesmunich.graphenej.api.GetAccountByName;
|
||||
import de.bitsharesmunich.graphenej.api.GetAllAssetHolders;
|
||||
import de.bitsharesmunich.graphenej.api.GetBlockHeader;
|
||||
import de.bitsharesmunich.graphenej.api.GetKeyReferences;
|
||||
import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException;
|
||||
import de.bitsharesmunich.graphenej.errors.MalformedAddressException;
|
||||
import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener;
|
||||
import de.bitsharesmunich.graphenej.models.BaseResponse;
|
||||
import de.bitsharesmunich.graphenej.models.WitnessResponse;
|
||||
import de.bitsharesmunich.graphenej.UserAccount;
|
||||
import de.bitsharesmunich.graphenej.Address;
|
||||
|
||||
/**
|
||||
* Created by nelson on 6/26/17.
|
||||
|
@ -222,7 +226,75 @@ public class NodeConnectionTest {
|
|||
System.out.println("InterruptedException. Msg: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetKeyReferencesRequest(){
|
||||
nodeConnection = NodeConnection.getInstance();
|
||||
nodeConnection.addNodeUrl(BLOCK_PAY_DE);
|
||||
nodeConnection.connect("", "", false, mErrorListener);
|
||||
|
||||
Address address1 = null;
|
||||
Address address2 = null;
|
||||
Address address3 = null;
|
||||
try {
|
||||
address1 = new Address("BTS8RiFgs8HkcVPVobHLKEv6yL3iXcC9SWjbPVS15dDAXLG9GYhnY");
|
||||
address2 = new Address("BTS8RiFgs8HkcVPVobHLKEv6yL3iXcC9SWjbPVS15dDAXLG9GYhnY");
|
||||
address3 = new Address("BTS8RiFgs8HkcVPVobHLKEv6yL3iXcC9SWjbPVS15dDAXLG9GYp00");
|
||||
} catch (MalformedAddressException e) {
|
||||
System.out.println("MalformedAddressException. Msg: " + e.getMessage());
|
||||
}
|
||||
|
||||
ArrayList<Address> addresses = new ArrayList<>();
|
||||
addresses.add(address1);
|
||||
addresses.add(address2);
|
||||
addresses.add(address3);
|
||||
|
||||
// Test with the one address constructor
|
||||
System.out.println("Adding GetKeyReferences one address request (One address)");
|
||||
try{
|
||||
nodeConnection.addRequestHandler(new GetKeyReferences(address1, false, new WitnessResponseListener(){
|
||||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
System.out.println("GetKeyReferences.onSuccess");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(BaseResponse.Error error) {
|
||||
System.out.println("GetKeyReferences.onError. Msg: "+ error.message);
|
||||
}
|
||||
}));
|
||||
}catch(RepeatedRequestIdException e){
|
||||
System.out.println("RepeatedRequestIdException. Msg: "+e.getMessage());
|
||||
}
|
||||
|
||||
// Test with the list of addresses constructor
|
||||
System.out.println("Adding GetKeyReferences address request (List of Addresses)");
|
||||
try{
|
||||
nodeConnection.addRequestHandler(new GetKeyReferences(addresses, false, new WitnessResponseListener(){
|
||||
@Override
|
||||
public void onSuccess(WitnessResponse response) {
|
||||
System.out.println("GetKeyReferences.onSuccess");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(BaseResponse.Error error) {
|
||||
System.out.println("GetKeyReferences.onError. Msg: "+ error.message);
|
||||
}
|
||||
}));
|
||||
}catch(RepeatedRequestIdException e){
|
||||
System.out.println("RepeatedRequestIdException. Msg: "+e.getMessage());
|
||||
}
|
||||
|
||||
try{
|
||||
// Holding this thread while we get update notifications
|
||||
synchronized (this){
|
||||
wait();
|
||||
}
|
||||
}catch(InterruptedException e){
|
||||
System.out.println("InterruptedException. Msg: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private WitnessResponseListener mErrorListener = new WitnessResponseListener() {
|
||||
|
||||
|
|
Loading…
Reference in a new issue