diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java index f82b950..890bd21 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java @@ -31,12 +31,19 @@ public class GetRequiredFees extends WebSocketAdapter { private List operations; private Asset asset; - public GetRequiredFees(List operations, Asset asset, WitnessResponseListener listener){ + private boolean mOneTime; + + public GetRequiredFees(List operations, Asset asset, boolean oneTime, WitnessResponseListener listener){ this.operations = operations; this.asset = asset; + this.mOneTime = oneTime; this.mListener = listener; } + public GetRequiredFees(List operations, Asset asset, WitnessResponseListener listener){ + this(operations, asset, true, listener); + } + @Override public void onConnected(WebSocket websocket, Map> headers) throws Exception { ArrayList 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(); + } } } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetTradeHistory.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetTradeHistory.java index 46dd72d..d8166a0 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetTradeHistory.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetTradeHistory.java @@ -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> headers) throws Exception { ArrayList accountParams = new ArrayList<>(); @@ -72,7 +79,9 @@ public class GetTradeHistory extends BaseGrapheneHandler { } catch (Exception e) { e.printStackTrace(); } - websocket.disconnect(); + if(mOneTime){ + websocket.disconnect(); + } } @Override diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/ListAssets.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/ListAssets.java index 5a674cc..ba2e5ef 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/ListAssets.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/ListAssets.java @@ -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(); diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAccounts.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAccounts.java index 6b424b9..7a34162 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAccounts.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAccounts.java @@ -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> headers) throws Exception { ArrayList accountParams = new ArrayList<>(); @@ -65,7 +77,9 @@ public class LookupAccounts extends BaseGrapheneHandler { this.mListener.onSuccess(witnessResponse); } - websocket.disconnect(); + if(mOneTime){ + websocket.disconnect(); + } } @Override diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java index 3c24247..770f851 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java @@ -24,12 +24,19 @@ public class LookupAssetSymbols extends BaseGrapheneHandler { private WitnessResponseListener mListener; private List assets; - public LookupAssetSymbols(List assets, WitnessResponseListener listener){ + private boolean mOneTime; + + public LookupAssetSymbols(List assets, boolean oneTime, WitnessResponseListener listener){ super(listener); this.assets = assets; + this.mOneTime = oneTime; this.mListener = listener; } + public LookupAssetSymbols(List assets, WitnessResponseListener listener){ + this(assets, true, listener); + } + @Override public void onConnected(WebSocket websocket, Map> headers) throws Exception { ArrayList params = new ArrayList<>(); diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/TransactionBroadcastSequence.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/TransactionBroadcastSequence.java index 30549d9..cd4161e 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/TransactionBroadcastSequence.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/TransactionBroadcastSequence.java @@ -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 emptyParams = new ArrayList<>(); @@ -140,7 +157,9 @@ public class TransactionBroadcastSequence extends BaseGrapheneHandler { Type WitnessResponseType = new TypeToken>(){}.getType(); WitnessResponse 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(); + } } } \ No newline at end of file diff --git a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/android/NodeConnectionTest.java b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/android/NodeConnectionTest.java index 661ae9f..0a6534f 100644 --- a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/android/NodeConnectionTest.java +++ b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/android/NodeConnectionTest.java @@ -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
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() {