diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetBlockHeader.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetBlockHeader.java index 4cab506..cd3bdf1 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetBlockHeader.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetBlockHeader.java @@ -34,12 +34,19 @@ public class GetBlockHeader extends BaseGrapheneHandler { private int currentId = LOGIN_ID; private int apiId = -1; - public GetBlockHeader(long blockNumber, WitnessResponseListener listener){ + private boolean mOneTime; + + public GetBlockHeader(long blockNumber, boolean oneTime, WitnessResponseListener listener){ super(listener); this.blockNumber = blockNumber; + this.mOneTime = oneTime; this.mListener = listener; } + public GetBlockHeader(long blockNumber, WitnessResponseListener listener){ + this(blockNumber, true, listener); + } + @Override public void onConnected(WebSocket websocket, Map> headers) throws Exception { ArrayList loginParams = new ArrayList<>(); @@ -58,7 +65,9 @@ public class GetBlockHeader 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<>(); @@ -80,7 +89,9 @@ public class GetBlockHeader extends BaseGrapheneHandler { Type RelativeAccountHistoryResponse = new TypeToken>(){}.getType(); WitnessResponse transfersResponse = gson.fromJson(response, RelativeAccountHistoryResponse); mListener.onSuccess(transfersResponse); - websocket.disconnect(); + if(mOneTime){ + websocket.disconnect(); + } } } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetKeyReferences.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetKeyReferences.java index 5eb4243..9639ac5 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetKeyReferences.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetKeyReferences.java @@ -25,17 +25,29 @@ public class GetKeyReferences extends BaseGrapheneHandler { private List
addresses; - public GetKeyReferences(Address address, WitnessResponseListener listener){ + private boolean mOneTime; + + public GetKeyReferences(Address address, boolean oneTime, WitnessResponseListener listener){ super(listener); addresses = new ArrayList<>(); addresses.add(address); + this.mOneTime = oneTime; } - public GetKeyReferences(List
addresses, WitnessResponseListener listener) { + public GetKeyReferences(List
addresses, boolean oneTime, WitnessResponseListener listener) { super(listener); this.addresses = addresses; this.mListener = listener; + this.mOneTime = oneTime; + } + + public GetKeyReferences(Address address, WitnessResponseListener listener){ + this(address, true, listener); + } + + public GetKeyReferences(List
addresses, WitnessResponseListener listener) { + this(addresses, true, listener); } @Override @@ -64,7 +76,9 @@ public class GetKeyReferences extends BaseGrapheneHandler { } else { this.mListener.onSuccess(witnessResponse); } - websocket.disconnect(); + if(mOneTime){ + websocket.disconnect(); + } } @Override diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java index eacdda9..5429d13 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java @@ -31,14 +31,22 @@ public class GetLimitOrders extends BaseGrapheneHandler { private int limit; private WitnessResponseListener mListener; - public GetLimitOrders(String a, String b, int limit, WitnessResponseListener mListener) { + private boolean mOneTime; + + + public GetLimitOrders(String a, String b, int limit, boolean oneTime, WitnessResponseListener mListener) { super(mListener); this.a = a; this.b = b; this.limit = limit; + this.mOneTime = oneTime; this.mListener = mListener; } + public GetLimitOrders(String a, String b, int limit, WitnessResponseListener mListener) { + this(a, b, limit, true, mListener); + } + @Override public void onConnected(WebSocket websocket, Map> headers) throws Exception { ArrayList accountParams = new ArrayList<>(); @@ -70,7 +78,9 @@ public class GetLimitOrders extends BaseGrapheneHandler { } catch (Exception e) { e.printStackTrace(); } - websocket.disconnect(); + if(mOneTime){ + websocket.disconnect(); + } } @Override @@ -83,12 +93,16 @@ public class GetLimitOrders extends BaseGrapheneHandler { @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/GetMarketHistory.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java index b570839..9311a4c 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java @@ -44,16 +44,23 @@ public class GetMarketHistory extends BaseGrapheneHandler { private int apiId = -1; private int counter = 0; - public GetMarketHistory(Asset base, Asset quote, long bucket, Date start, Date end, WitnessResponseListener listener){ + private boolean mOneTime; + + public GetMarketHistory(Asset base, Asset quote, long bucket, Date start, Date end, boolean oneTime, WitnessResponseListener listener){ super(listener); this.base = base; this.quote = quote; this.bucket = bucket; this.start = start; this.end = end; + this.mOneTime = oneTime; this.mListener = listener; } + public GetMarketHistory(Asset base, Asset quote, long bucket, Date start, Date end, WitnessResponseListener listener){ + this(base, quote, bucket, start, end, true, listener); + } + public Asset getBase() { return base; } @@ -99,7 +106,7 @@ public class GetMarketHistory extends BaseGrapheneHandler { } public void disconnect(){ - if(mWebsocket != null && mWebsocket.isOpen()){ + if(mWebsocket != null && mWebsocket.isOpen() && mOneTime){ mWebsocket.disconnect(); } } @@ -131,7 +138,9 @@ public class GetMarketHistory 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<>(); diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetObjects.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetObjects.java index 548dfcd..e507186 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetObjects.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetObjects.java @@ -33,9 +33,16 @@ import de.bitsharesmunich.graphenej.models.WitnessResponse; public class GetObjects extends BaseGrapheneHandler { private List ids; - public GetObjects(List ids, WitnessResponseListener listener){ + private boolean mOneTime; + + public GetObjects(List ids, boolean oneTime, WitnessResponseListener listener){ super(listener); this.ids = ids; + this.mOneTime = oneTime; + } + + public GetObjects(List ids, WitnessResponseListener listener){ + this(ids, true, listener); } @Override @@ -92,7 +99,9 @@ public class GetObjects extends BaseGrapheneHandler { WitnessResponse> output = new WitnessResponse<>(); output.result = parsedResult; mListener.onSuccess(output); - websocket.disconnect(); + if(mOneTime){ + websocket.disconnect(); + } } @Override diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java index 4228288..b0d299b 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java @@ -48,36 +48,64 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler { private int currentId = 1; private int apiId = -1; + private boolean mOneTime; + /** * Constructor that takes all possible parameters. * @param userAccount The user account to be queried * @param stop Sequence number of earliest operation * @param limit Maximum number of operations to retrieve (must not exceed 100) * @param start Sequence number of the most recent operation to retrieve + * @param oneTime Boolean value indicating if websocket must be closed or not after request + * @param listener Listener to be notified with the result of this query + */ + public GetRelativeAccountHistory(UserAccount userAccount, int stop, int limit, int start, boolean oneTime, WitnessResponseListener listener){ + super(listener); + if(limit > MAX_LIMIT) limit = MAX_LIMIT; + this.mUserAccount = userAccount; + this.stop = stop; + this.limit = limit; + this.start = start; + this.mOneTime = oneTime; + this.mListener = listener; + } + + /** + * Constructor that uses the default values, and sets the limit to its maximum possible value. + * @param userAccount The user account to be queried + * @param oneTime Boolean value indicating if websocket must be closed or not after request + * @param listener Listener to be notified with the result of this query + */ + public GetRelativeAccountHistory(UserAccount userAccount, boolean oneTime, WitnessResponseListener listener){ + super(listener); + this.mUserAccount = userAccount; + this.stop = DEFAULT_STOP; + this.limit = MAX_LIMIT; + this.start = DEFAULT_START; + this.mOneTime = oneTime; + this.mListener = listener; + } + + /** + * Constructor that takes all possible parameters except for oneTime (which will be always true here) + * @param userAccount The user account to be queried + * @param stop Sequence number of earliest operation + * @param limit Maximum number of operations to retrieve (must not exceed 100) + * @param start Sequence number of the most recent operation to retrieve * @param listener Listener to be notified with the result of this query */ public GetRelativeAccountHistory(UserAccount userAccount, int stop, int limit, int start, WitnessResponseListener listener){ - super(listener); - if(limit > MAX_LIMIT) limit = MAX_LIMIT; - this.mUserAccount = userAccount; - this.stop = stop; - this.limit = limit; - this.start = start; - this.mListener = listener; + this(userAccount, stop, limit, start, true, listener); } /** * Constructor that uses the default values, and sets the limit to its maximum possible value. + * oneTime is always set to true at this constructor. * @param userAccount The user account to be queried * @param listener Listener to be notified with the result of this query */ public GetRelativeAccountHistory(UserAccount userAccount, WitnessResponseListener listener){ - super(listener); - this.mUserAccount = userAccount; - this.stop = DEFAULT_STOP; - this.limit = MAX_LIMIT; - this.start = DEFAULT_START; - this.mListener = listener; + this(userAccount, true, listener); } @Override @@ -98,7 +126,9 @@ public class GetRelativeAccountHistory 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<>(); @@ -153,7 +183,7 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler { * Disconnects the websocket */ public void disconnect(){ - if(mWebsocket != null && mWebsocket.isOpen()){ + if(mWebsocket != null && mWebsocket.isOpen() && mOneTime){ mWebsocket.disconnect(); } } 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 98bd624..661ae9f 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 @@ -11,6 +11,7 @@ import de.bitsharesmunich.graphenej.api.GetAccounts; 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.errors.RepeatedRequestIdException; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; import de.bitsharesmunich.graphenej.models.BaseResponse; @@ -24,6 +25,7 @@ public class NodeConnectionTest { private String BLOCK_PAY_DE = System.getenv("OPENLEDGER_EU"); private String ACCOUNT_ID = System.getenv("ACCOUNT_ID"); private String ACCOUNT_NAME = System.getenv("ACCOUNT_NAME"); + private long BlOCK_TEST_NUMBER = Long.parseLong(System.getenv("BlOCK_TEST_NUMBER")); private Asset BTS = new Asset("1.3.0"); private Asset BITDOLAR = new Asset("1.3.121"); //USD Smartcoin private Asset BITEURO = new Asset("1.3.120"); //EUR Smartcoin @@ -52,77 +54,6 @@ public class NodeConnectionTest { } }; - private TimerTask mSubscribeGetAccountBalancesTask = new TimerTask() { - @Override - public void run() { - System.out.println("Adding GetAccountBalances here"); - try{ - UserAccount userAccount = new UserAccount(ACCOUNT_ID); - ArrayList assetList = new ArrayList<>(); - assetList.add(BTS); - assetList.add(BITDOLAR); - assetList.add(BITEURO); - nodeConnection.addRequestHandler(new GetAccountBalances(userAccount, false, assetList, new WitnessResponseListener(){ - @Override - public void onSuccess(WitnessResponse response) { - System.out.println("getAccountBalances.onSuccess"); - } - - @Override - public void onError(BaseResponse.Error error) { - System.out.println("getAccountBalances.onError. Msg: "+ error.message); - } - })); - }catch(RepeatedRequestIdException e){ - System.out.println("RepeatedRequestIdException. Msg: "+e.getMessage()); - } - } - }; - - private TimerTask mSubscribeGetAccountByNameTask = new TimerTask() { - @Override - public void run() { - System.out.println("Adding GetAccountByName here"); - try{ - nodeConnection.addRequestHandler(new GetAccountByName(ACCOUNT_NAME, false, new WitnessResponseListener(){ - @Override - public void onSuccess(WitnessResponse response) { - System.out.println("GetAccountByName.onSuccess"); - } - - @Override - public void onError(BaseResponse.Error error) { - System.out.println("GetAccountByName.onError. Msg: "+ error.message); - } - })); - }catch(RepeatedRequestIdException e){ - System.out.println("RepeatedRequestIdException. Msg: "+e.getMessage()); - } - } - }; - - private TimerTask mSubscribeGetAllAssetHoldersTask = new TimerTask() { - @Override - public void run() { - System.out.println("Adding GetAllAssetHolders request"); - try{ - nodeConnection.addRequestHandler(new GetAllAssetHolders(false, new WitnessResponseListener(){ - @Override - public void onSuccess(WitnessResponse response) { - System.out.println("GetAllAssetHolders.onSuccess"); - } - - @Override - public void onError(BaseResponse.Error error) { - System.out.println("GetAllAssetHolders.onError. Msg: "+ error.message); - } - })); - }catch(RepeatedRequestIdException e){ - System.out.println("RepeatedRequestIdException. Msg: "+e.getMessage()); - } - } - }; - private TimerTask releaseTask = new TimerTask() { @Override public void run() { @@ -157,11 +88,29 @@ public class NodeConnectionTest { public void testGetAccountBalancesRequest(){ nodeConnection = NodeConnection.getInstance(); nodeConnection.addNodeUrl(BLOCK_PAY_DE); - nodeConnection.connect("", "", true, mErrorListener); + nodeConnection.connect("", "", false, mErrorListener); - Timer timer = new Timer(); - timer.schedule(mSubscribeGetAccountBalancesTask, 5000); - timer.schedule(releaseTask, 30000); + System.out.println("Adding GetAccountBalances here"); + try{ + UserAccount userAccount = new UserAccount(ACCOUNT_ID); + ArrayList assetList = new ArrayList<>(); + assetList.add(BTS); + assetList.add(BITDOLAR); + assetList.add(BITEURO); + nodeConnection.addRequestHandler(new GetAccountBalances(userAccount, false, assetList, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("getAccountBalances.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("getAccountBalances.onError. Msg: "+ error.message); + } + })); + }catch(RepeatedRequestIdException e){ + System.out.println("RepeatedRequestIdException. Msg: "+e.getMessage()); + } try{ // Holding this thread while we get update notifications @@ -177,11 +126,24 @@ public class NodeConnectionTest { public void testGetAccountByNameRequest(){ nodeConnection = NodeConnection.getInstance(); nodeConnection.addNodeUrl(BLOCK_PAY_DE); - nodeConnection.connect("", "", true, mErrorListener); + nodeConnection.connect("", "", false, mErrorListener); - Timer timer = new Timer(); - timer.schedule(mSubscribeGetAccountByNameTask, 5000); - timer.schedule(releaseTask, 30000); + System.out.println("Adding GetAccountByName here"); + try{ + nodeConnection.addRequestHandler(new GetAccountByName(ACCOUNT_NAME, false, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("GetAccountByName.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("GetAccountByName.onError. Msg: "+ error.message); + } + })); + }catch(RepeatedRequestIdException e){ + System.out.println("RepeatedRequestIdException. Msg: "+e.getMessage()); + } try{ // Holding this thread while we get update notifications @@ -197,11 +159,24 @@ public class NodeConnectionTest { public void testGetAllAssetHoldersRequest(){ nodeConnection = NodeConnection.getInstance(); nodeConnection.addNodeUrl(BLOCK_PAY_DE); - nodeConnection.connect("", "", true, mErrorListener); + nodeConnection.connect("", "", false, mErrorListener); - Timer timer = new Timer(); - timer.schedule(mSubscribeGetAllAssetHoldersTask, 5000); - timer.schedule(releaseTask, 30000); + System.out.println("Adding GetAllAssetHolders request"); + try{ + nodeConnection.addRequestHandler(new GetAllAssetHolders(false, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("GetAllAssetHolders.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("GetAllAssetHolders.onError. Msg: "+ error.message); + } + })); + }catch(RepeatedRequestIdException e){ + System.out.println("RepeatedRequestIdException. Msg: "+e.getMessage()); + } try{ // Holding this thread while we get update notifications @@ -213,6 +188,42 @@ public class NodeConnectionTest { } } + @Test + public void testGetBlockHeaderRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.connect("", "", false, mErrorListener); + + + System.out.println("Adding GetBlockHeader request"); + try{ + nodeConnection.addRequestHandler(new GetBlockHeader(BlOCK_TEST_NUMBER,false, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("GetBlockHeader.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("GetBlockHeader.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() { @Override