From 5369976f08047d05ed9d021c820260025a07851d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Tue, 18 Jul 2017 15:30:22 -0300 Subject: [PATCH 01/18] Introducing node hop feature to some blockchain API handler classes (GetAccountBalances, GetAccountByName, GetAccounts and GetAllAssetHolders) --- .gitignore | 1 + gradlew.bat | 90 ----------- .../graphenej/api/GetAccountBalances.java | 15 +- .../graphenej/api/GetAccountByName.java | 13 +- .../graphenej/api/GetAccounts.java | 19 ++- .../graphenej/api/GetAllAssetHolders.java | 15 +- .../api/android/NodeConnectionTest.java | 142 ++++++++++++++++++ 7 files changed, 193 insertions(+), 102 deletions(-) delete mode 100644 gradlew.bat diff --git a/.gitignore b/.gitignore index 094313f..0790958 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ # Gradle # ------ .gradle +gradlew.bat gradle graphenej/build /build diff --git a/gradlew.bat b/gradlew.bat deleted file mode 100644 index 8a0b282..0000000 --- a/gradlew.bat +++ /dev/null @@ -1,90 +0,0 @@ -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windowz variants - -if not "%OS%" == "Windows_NT" goto win9xME_args -if "%@eval[2+2]" == "4" goto 4NT_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* -goto execute - -:4NT_args -@rem Get arguments from the 4NT Shell from JP Software -set CMD_LINE_ARGS=%$ - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java index d7aeda4..cace112 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java @@ -25,11 +25,20 @@ public class GetAccountBalances extends BaseGrapheneHandler { private UserAccount mUserAccount; private List mAssetList; + private boolean mOneTime; - public GetAccountBalances(UserAccount userAccount, List assets, WitnessResponseListener listener) { + /* + * Constructor + */ + public GetAccountBalances(UserAccount userAccount, boolean oneTime, List assets, WitnessResponseListener listener) { super(listener); this.mUserAccount = userAccount; this.mAssetList = assets; + this.mOneTime = oneTime; + } + + public GetAccountBalances(UserAccount userAccount, List assets, WitnessResponseListener listener) { + this(userAccount, true, assets, listener); } @Override @@ -57,7 +66,9 @@ public class GetAccountBalances extends BaseGrapheneHandler { Type WitnessResponseType = new TypeToken>>(){}.getType(); WitnessResponse> witnessResponse = gsonBuilder.create().fromJson(response, WitnessResponseType); mListener.onSuccess(witnessResponse); - websocket.disconnect(); + if(mOneTime){ + websocket.disconnect(); + } } @Override diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java index f4f17f8..2d9c6bf 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java @@ -26,13 +26,19 @@ public class GetAccountByName extends BaseGrapheneHandler { private String accountName; private WitnessResponseListener mListener; + private boolean mOneTime; - public GetAccountByName(String accountName, WitnessResponseListener listener){ + public GetAccountByName(String accountName, boolean oneTime, WitnessResponseListener listener){ super(listener); this.accountName = accountName; + this.mOneTime = oneTime; this.mListener = listener; } + public GetAccountByName(String accountName, WitnessResponseListener listener){ + this(accountName, true, listener); + } + @Override public void onConnected(WebSocket websocket, Map> headers) throws Exception { ArrayList accountParams = new ArrayList<>(); @@ -58,8 +64,9 @@ public class GetAccountByName 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/GetAccounts.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccounts.java index e3d4a46..90e6855 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccounts.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccounts.java @@ -28,22 +28,33 @@ public class GetAccounts extends BaseGrapheneHandler { private String accountId; private List userAccounts; private WitnessResponseListener mListener; - private boolean oneTime; + private boolean mOneTime; + /* + * Construtor + */ public GetAccounts(String accountId, boolean oneTime, WitnessResponseListener listener){ super(listener); this.accountId = accountId; - this.oneTime = oneTime; + this.mOneTime = oneTime; this.mListener = listener; } public GetAccounts(List accounts, boolean oneTime, WitnessResponseListener listener){ super(listener); this.userAccounts = accounts; - this.oneTime = oneTime; + this.mOneTime = oneTime; this.mListener = listener; } + public GetAccounts(String accountId, WitnessResponseListener listener){ + this(accountId, true, listener); + } + + public GetAccounts(List accounts, WitnessResponseListener listener){ + this(accounts, true, listener); + } + @Override public void onConnected(WebSocket websocket, Map> headers) throws Exception { ArrayList params = new ArrayList(); @@ -76,7 +87,7 @@ public class GetAccounts extends BaseGrapheneHandler { } else { this.mListener.onSuccess(witnessResponse); } - if(oneTime){ + if(mOneTime){ websocket.disconnect(); } } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java index 9d92f8b..b4c986b 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java @@ -26,10 +26,15 @@ public class GetAllAssetHolders extends BaseGrapheneHandler { private int currentId = 1; private int assetApiId = -1; - public GetAllAssetHolders(WitnessResponseListener listener) { + private boolean mOneTime; + + public GetAllAssetHolders(boolean oneTime, WitnessResponseListener listener) { super(listener); + this.mOneTime = oneTime; } + public GetAllAssetHolders(WitnessResponseListener listener) { this(true, listener);} + @Override public void onConnected(WebSocket websocket, Map> headers) throws Exception { ArrayList loginParams = new ArrayList<>(); @@ -48,7 +53,9 @@ public class GetAllAssetHolders 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<>(); @@ -68,7 +75,9 @@ public class GetAllAssetHolders extends BaseGrapheneHandler { builder.registerTypeAdapter(AssetHolderCount.class, new AssetHolderCount.HoldersCountDeserializer()); WitnessResponse> witnessResponse = builder.create().fromJson(response, AssetTokenHolders); mListener.onSuccess(witnessResponse); - websocket.disconnect(); + if(mOneTime){ + websocket.disconnect(); + } }else{ System.out.println("current id: "+currentId); } 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 b014820..98bd624 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 @@ -2,20 +2,31 @@ package de.bitsharesmunich.graphenej.api.android; import org.junit.Test; +import java.util.ArrayList; import java.util.Timer; import java.util.TimerTask; +import de.bitsharesmunich.graphenej.Asset; 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.errors.RepeatedRequestIdException; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; import de.bitsharesmunich.graphenej.models.BaseResponse; import de.bitsharesmunich.graphenej.models.WitnessResponse; +import de.bitsharesmunich.graphenej.UserAccount; /** * Created by nelson on 6/26/17. */ 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 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 private NodeConnection nodeConnection; private TimerTask subscribeTask = new TimerTask() { @@ -41,6 +52,77 @@ 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() { @@ -71,6 +153,66 @@ public class NodeConnectionTest { } } + @Test + public void testGetAccountBalancesRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.connect("", "", true, mErrorListener); + + Timer timer = new Timer(); + timer.schedule(mSubscribeGetAccountBalancesTask, 5000); + timer.schedule(releaseTask, 30000); + + try{ + // Holding this thread while we get update notifications + synchronized (this){ + wait(); + } + }catch(InterruptedException e){ + System.out.println("InterruptedException. Msg: "+e.getMessage()); + } + } + + @Test + public void testGetAccountByNameRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.connect("", "", true, mErrorListener); + + Timer timer = new Timer(); + timer.schedule(mSubscribeGetAccountByNameTask, 5000); + timer.schedule(releaseTask, 30000); + + try{ + // Holding this thread while we get update notifications + synchronized (this){ + wait(); + } + }catch(InterruptedException e){ + System.out.println("InterruptedException. Msg: "+e.getMessage()); + } + } + + @Test + public void testGetAllAssetHoldersRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.connect("", "", true, mErrorListener); + + Timer timer = new Timer(); + timer.schedule(mSubscribeGetAllAssetHoldersTask, 5000); + timer.schedule(releaseTask, 30000); + + 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 From 7313279f95664385e7f922a4217532bcaedb102a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Tue, 18 Jul 2017 22:07:07 -0300 Subject: [PATCH 02/18] Introducing node hop feature to some blockchain API handler classes (GetBlockHeader, GetKeyReferences, GetLimitOrders, GetMarketHistory, GetObjects, GetRelativeAccountHistory) --- .../graphenej/api/GetBlockHeader.java | 17 +- .../graphenej/api/GetKeyReferences.java | 20 +- .../graphenej/api/GetLimitOrders.java | 22 ++- .../graphenej/api/GetMarketHistory.java | 15 +- .../graphenej/api/GetObjects.java | 13 +- .../api/GetRelativeAccountHistory.java | 60 ++++-- .../api/android/NodeConnectionTest.java | 177 ++++++++++-------- 7 files changed, 211 insertions(+), 113 deletions(-) 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 From 5a58cf557959f08da244cc6bb1d4bfaff9a2c9b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Wed, 19 Jul 2017 02:39:15 -0300 Subject: [PATCH 03/18] Introducing node hop feature to some blockchain API handler classes (GetRequiredFees, GetTradeHistory, ListAssets, LookupAccounts, LookupAssetSymbols and TransactionBroadcastSequence) --- .../graphenej/api/GetRequiredFees.java | 17 ++++- .../graphenej/api/GetTradeHistory.java | 13 +++- .../graphenej/api/ListAssets.java | 27 ++++++- .../graphenej/api/LookupAccounts.java | 20 ++++- .../graphenej/api/LookupAssetSymbols.java | 9 ++- .../api/TransactionBroadcastSequence.java | 39 ++++++++-- .../api/android/NodeConnectionTest.java | 74 ++++++++++++++++++- 7 files changed, 177 insertions(+), 22 deletions(-) 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() { From c29830da9bc3d80ea6ac3bb0a6da0ade7b34642d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Wed, 19 Jul 2017 12:03:15 -0300 Subject: [PATCH 04/18] Implementing add list of urls to NodeConnect --- .../graphenej/api/android/NodeConnection.java | 36 +++++++++++++++++++ .../api/android/NodeConnectionTest.java | 31 ++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java index a8a76f4..c02d9ba 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java @@ -13,7 +13,13 @@ import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; */ public class NodeConnection { + /** + * List of URLs of the nodes + */ private List mUrlList; + /** + * Index of the current node from the list + */ private int mUrlIndex; private WebsocketWorkerThread mThread; private SubscriptionMessagesHub mMessagesHub; @@ -21,6 +27,9 @@ public class NodeConnection { private static NodeConnection instance; + /* + * Ger the instance of the NodeConnection which is inteded to be used as a Singleton. + */ public static NodeConnection getInstance(){ if(instance == null){ instance = new NodeConnection(); @@ -32,14 +41,38 @@ public class NodeConnection { this.mUrlList = new ArrayList<>(); } + /** + * Add a websocket URL node that will be added to the list used at node hop scheme. + * + * @param url: URL of the node + */ public void addNodeUrl(String url){ this.mUrlList.add(url); } + /** + * Add a list of websocket URL nodes that will be added to the current list and + * be used at node hop scheme. + * + * @param urlList: List of URLs of the nodes + */ + public void addNodeUrls(List urlList){ + List newList = new ArrayList(mUrlList); + newList.addAll(urlList); + } + + /** + * Get the list of websocket URL nodes. + * + * @return List of URLs of the nodes + */ public List getNodeUrls(){ return this.mUrlList; } + /** + * Clear list of websocket URL nodes. + */ public void clearNodeList(){ this.mUrlList.clear(); } @@ -60,6 +93,9 @@ public class NodeConnection { } } + /** + * Add the API Handler to the node. + */ public void addRequestHandler(BaseGrapheneHandler handler) throws RepeatedRequestIdException { handler.setRequestId(requestCounter); requestCounter++; 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 0a6534f..1d1d596 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 @@ -14,6 +14,7 @@ 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.api.GetLimitOrders; import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException; import de.bitsharesmunich.graphenej.errors.MalformedAddressException; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; @@ -27,6 +28,9 @@ import de.bitsharesmunich.graphenej.Address; */ public class NodeConnectionTest { private String BLOCK_PAY_DE = System.getenv("OPENLEDGER_EU"); + private String NODE_URL_1 = System.getenv("NODE_URL_1"); + private String NODE_URL_2 = System.getenv("NODE_URL_2"); + private String NODE_URL_3 = System.getenv("NODE_URL_3"); 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")); @@ -88,6 +92,33 @@ public class NodeConnectionTest { } } + @Test + public void testNodeHopFeature(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(NODE_URL_1); + //Test adding a "sublist" + ArrayList urlList = new ArrayList(){{ + add(NODE_URL_1); + add(NODE_URL_2); + }}; + nodeConnection.addNodeUrl(BLOCK_PAY_DE); + + nodeConnection.connect("", "", true, mErrorListener); + + Timer timer = new Timer(); + timer.schedule(subscribeTask, 5000); + timer.schedule(releaseTask, 30000); + + try{ + // Holding this thread while we get update notifications + synchronized (this){ + wait(); + } + }catch(InterruptedException e){ + System.out.println("InterruptedException. Msg: "+e.getMessage()); + } + } + @Test public void testGetAccountBalancesRequest(){ nodeConnection = NodeConnection.getInstance(); From f367eb0021cca1a92e612d92a0ee8fe0677c5635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Wed, 19 Jul 2017 13:48:47 -0300 Subject: [PATCH 05/18] Adding docs and tests for some API handler classes (GetAccountBalances and GetAccountByName) --- .../graphenej/api/GetAccountBalances.java | 33 ++++++++-- .../graphenej/api/GetAccountByName.java | 26 ++++++++ .../graphenej/api/GetLimitOrders.java | 32 +++++++++- .../api/android/NodeConnectionTest.java | 63 +++++++++++++++---- 4 files changed, 137 insertions(+), 17 deletions(-) diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java index cace112..1d8a5d7 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java @@ -19,6 +19,14 @@ import java.util.List; import java.util.Map; /** + * Class that implements get_account_balances request handler. + * + * Get an account’s balances in various assets. + * + * The request returns the balances of the account + * + * @see get_account_balances API doc + * * Created by nelson on 1/13/17. */ public class GetAccountBalances extends BaseGrapheneHandler { @@ -27,18 +35,35 @@ public class GetAccountBalances extends BaseGrapheneHandler { private List mAssetList; private boolean mOneTime; - /* - * Constructor + /** + * Default Constructor + * + * @param userAccount account to get balances for + * @param assets list of the assets to get balances of; if empty, get all assets account has a balance in + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 GetAccountBalances(UserAccount userAccount, boolean oneTime, List assets, WitnessResponseListener listener) { + public GetAccountBalances(UserAccount userAccount, List assets, boolean oneTime, WitnessResponseListener listener) { super(listener); this.mUserAccount = userAccount; this.mAssetList = assets; this.mOneTime = oneTime; } + /** + * Using this constructor the websocket connection closes after the response. + * + * @param userAccount account to get balances for + * @param assets list of the assets to get balances of; if empty, get all assets account has a balance in + * @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 GetAccountBalances(UserAccount userAccount, List assets, WitnessResponseListener listener) { - this(userAccount, true, assets, listener); + this(userAccount, assets, true, listener); } @Override diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java index 2d9c6bf..a5fe247 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java @@ -20,6 +20,14 @@ import de.bitsharesmunich.graphenej.models.ApiCall; import de.bitsharesmunich.graphenej.models.WitnessResponse; /** + * Class that implements get_account_by_name request handler. + * + * Get an account’s info by name. + * + * The request returns account data that refer to the name. + * + * @see get_account_by_name API doc + * * Created by nelson on 11/15/16. */ public class GetAccountByName extends BaseGrapheneHandler { @@ -28,6 +36,16 @@ public class GetAccountByName extends BaseGrapheneHandler { private WitnessResponseListener mListener; private boolean mOneTime; + /** + * Default Constructor + * + * @param accountName name of the account to get info + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 GetAccountByName(String accountName, boolean oneTime, WitnessResponseListener listener){ super(listener); this.accountName = accountName; @@ -35,6 +53,14 @@ public class GetAccountByName extends BaseGrapheneHandler { this.mListener = listener; } + /** + * Using this constructor the websocket connection closes after the response. + * + * @param accountName name of the account to get info + * @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 GetAccountByName(String accountName, WitnessResponseListener listener){ this(accountName, true, listener); } 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 5429d13..b73429c 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java @@ -22,7 +22,15 @@ import de.bitsharesmunich.graphenej.models.BaseResponse; import de.bitsharesmunich.graphenej.models.WitnessResponse; /** - * Created by nelson on 11/15/16. + * Class that implements get_limit_orders request handler. + * + * Get limit orders in a given market. + * + * The request returns the limit orders, ordered from least price to greatest + * + * @see get_limit_orders API doc + * + * Created by nelson on 1/5/17. */ public class GetLimitOrders extends BaseGrapheneHandler { @@ -34,6 +42,18 @@ public class GetLimitOrders extends BaseGrapheneHandler { private boolean mOneTime; + /** + * Default Constructor + * + * @param a id of asset being sold + * @param b id of asset being purchased + * @param limit maximum number of orders to retrieve + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @param mListener 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 GetLimitOrders(String a, String b, int limit, boolean oneTime, WitnessResponseListener mListener) { super(mListener); this.a = a; @@ -43,6 +63,16 @@ public class GetLimitOrders extends BaseGrapheneHandler { this.mListener = mListener; } + /** + * Using this constructor the websocket connection closes after the response. + * + * @param a id of asset being sold + * @param b id of asset being purchased + * @param limit maximum number of orders to retrieve + * @param mListener 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 GetLimitOrders(String a, String b, int limit, WitnessResponseListener mListener) { this(a, b, limit, true, mListener); } 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 1d1d596..9536b1f 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 @@ -27,10 +27,10 @@ import de.bitsharesmunich.graphenej.Address; * Created by nelson on 6/26/17. */ public class NodeConnectionTest { - private String BLOCK_PAY_DE = System.getenv("OPENLEDGER_EU"); private String NODE_URL_1 = System.getenv("NODE_URL_1"); private String NODE_URL_2 = System.getenv("NODE_URL_2"); private String NODE_URL_3 = System.getenv("NODE_URL_3"); + private String NODE_URL_4 = System.getenv("NODE_URL_4"); 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")); @@ -75,7 +75,7 @@ public class NodeConnectionTest { @Test public void testNodeConnection(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", true, mErrorListener); Timer timer = new Timer(); @@ -93,15 +93,22 @@ public class NodeConnectionTest { } @Test + /** + * Test for NodeConnection's addNodeUrl and addNodeUrls working together. + * + * Need to setup the NODE_URL_(1 to 4) env to work. Some of the nodes may have invalid nodes + * websockets URL just to test the hop. + * + */ public void testNodeHopFeature(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(NODE_URL_1); + nodeConnection.addNodeUrl(NODE_URL_4); //Test adding a "sublist" ArrayList urlList = new ArrayList(){{ - add(NODE_URL_1); - add(NODE_URL_2); + add(NODE_URL_3); + add(NODE_URL_3); }}; - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", true, mErrorListener); @@ -119,10 +126,16 @@ public class NodeConnectionTest { } } + /** + * Test for GetAccountBalances Handler. + * + * Request balances for a valid account (Need to setup the ACCOUNT_ID env with desired account id) + * + */ @Test public void testGetAccountBalancesRequest(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", false, mErrorListener); System.out.println("Adding GetAccountBalances here"); @@ -132,7 +145,26 @@ public class NodeConnectionTest { assetList.add(BTS); assetList.add(BITDOLAR); assetList.add(BITEURO); - nodeConnection.addRequestHandler(new GetAccountBalances(userAccount, false, assetList, new WitnessResponseListener(){ + System.out.println("Test: Request to discrete asset list"); + nodeConnection.addRequestHandler(new GetAccountBalances(userAccount, assetList, false, 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{ + UserAccount userAccount = new UserAccount(ACCOUNT_ID); + System.out.println("Test: Request to all account' assets balance"); + nodeConnection.addRequestHandler(new GetAccountBalances(userAccount, null, false, new WitnessResponseListener(){ @Override public void onSuccess(WitnessResponse response) { System.out.println("getAccountBalances.onSuccess"); @@ -158,9 +190,16 @@ public class NodeConnectionTest { } @Test + /** + * Test for GetAccountByName Handler. + * + * Request for a valid account name by name (Need to setup the ACCOUNT_NAME env with desired + * account name) + * + */ public void testGetAccountByNameRequest(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", false, mErrorListener); System.out.println("Adding GetAccountByName here"); @@ -193,7 +232,7 @@ public class NodeConnectionTest { @Test public void testGetAllAssetHoldersRequest(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", false, mErrorListener); System.out.println("Adding GetAllAssetHolders request"); @@ -226,7 +265,7 @@ public class NodeConnectionTest { @Test public void testGetBlockHeaderRequest(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", false, mErrorListener); @@ -261,7 +300,7 @@ public class NodeConnectionTest { @Test public void testGetKeyReferencesRequest(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(BLOCK_PAY_DE); + nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", false, mErrorListener); Address address1 = null; From ad2e41454808d79ce5eac3a2cfa7a7424aba2ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Wed, 19 Jul 2017 21:29:42 -0300 Subject: [PATCH 06/18] Implementating node hop at NodeConnection and tests --- .../graphenej/api/BaseGrapheneHandler.java | 12 +- .../graphenej/api/GetAccountByName.java | 2 - .../graphenej/api/GetAccounts.java | 40 +++- .../graphenej/api/GetAllAssetHolders.java | 23 ++- .../graphenej/api/GetBlockHeader.java | 24 ++- .../graphenej/api/GetKeyReferences.java | 44 ++++- .../graphenej/api/GetLimitOrders.java | 13 +- .../graphenej/api/GetMarketHistory.java | 31 ++- .../graphenej/api/GetRequiredFees.java | 26 ++- .../api/SubscriptionMessagesHub.java | 43 ++++ .../graphenej/api/android/NodeConnection.java | 24 ++- .../api/android/WebsocketWorkerThread.java | 31 +++ .../interfaces/NodeErrorListener.java | 10 + .../api/android/NodeConnectionTest.java | 186 +++++++++++++++++- 14 files changed, 483 insertions(+), 26 deletions(-) create mode 100644 graphenej/src/main/java/de/bitsharesmunich/graphenej/interfaces/NodeErrorListener.java diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/BaseGrapheneHandler.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/BaseGrapheneHandler.java index e6dbab2..ea9437b 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/BaseGrapheneHandler.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/BaseGrapheneHandler.java @@ -3,6 +3,10 @@ package de.bitsharesmunich.graphenej.api; import com.neovisionaries.ws.client.WebSocket; import com.neovisionaries.ws.client.WebSocketAdapter; import com.neovisionaries.ws.client.WebSocketException; + +import org.w3c.dom.Node; + +import de.bitsharesmunich.graphenej.interfaces.NodeErrorListener; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; import de.bitsharesmunich.graphenej.models.BaseResponse; @@ -14,6 +18,7 @@ import de.bitsharesmunich.graphenej.models.BaseResponse; public abstract class BaseGrapheneHandler extends WebSocketAdapter { protected WitnessResponseListener mListener; + protected NodeErrorListener mErrorListener; /** * The 'id' field of a message to the node. This is used in order to multiplex different messages @@ -30,11 +35,14 @@ public abstract class BaseGrapheneHandler extends WebSocketAdapter { public BaseGrapheneHandler(WitnessResponseListener listener){ this.mListener = listener; } + public BaseGrapheneHandler(NodeErrorListener listener){ + this.mErrorListener = listener; + } @Override public void onError(WebSocket websocket, WebSocketException cause) throws Exception { System.out.println("onError. cause: "+cause.getMessage()); - mListener.onError(new BaseResponse.Error(cause.getMessage())); + mErrorListener.onError(new BaseResponse.Error(cause.getMessage())); websocket.disconnect(); } @@ -44,7 +52,7 @@ public abstract class BaseGrapheneHandler extends WebSocketAdapter { for (StackTraceElement element : cause.getStackTrace()){ System.out.println(element.getFileName()+"#"+element.getClassName()+":"+element.getLineNumber()); } - mListener.onError(new BaseResponse.Error(cause.getMessage())); + mErrorListener.onError(new BaseResponse.Error(cause.getMessage())); websocket.disconnect(); } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java index a5fe247..4998c40 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java @@ -27,8 +27,6 @@ import de.bitsharesmunich.graphenej.models.WitnessResponse; * The request returns account data that refer to the name. * * @see get_account_by_name API doc - * - * Created by nelson on 11/15/16. */ public class GetAccountByName extends BaseGrapheneHandler { diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccounts.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccounts.java index 90e6855..aa14bc3 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccounts.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccounts.java @@ -21,8 +21,13 @@ import de.bitsharesmunich.graphenej.models.ApiCall; import de.bitsharesmunich.graphenej.models.WitnessResponse; /** + * Class that implements get_accounts request handler. * - * @author henry + * Get a list of accounts by ID. + * + * The request returns the accounts corresponding to the provided IDs. + * + * @see get_accounts API doc */ public class GetAccounts extends BaseGrapheneHandler { private String accountId; @@ -30,8 +35,14 @@ public class GetAccounts extends BaseGrapheneHandler { private WitnessResponseListener mListener; private boolean mOneTime; - /* - * Construtor + /** + * Default Constructor + * @param accountId ID of the account to retrieve + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 GetAccounts(String accountId, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -40,6 +51,15 @@ public class GetAccounts extends BaseGrapheneHandler { this.mListener = listener; } + /** + * Using this constructor the websocket connection closes after the response. + * @param accounts list with the accounts to retrieve + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 GetAccounts(List accounts, boolean oneTime, WitnessResponseListener listener){ super(listener); this.userAccounts = accounts; @@ -47,10 +67,24 @@ public class GetAccounts extends BaseGrapheneHandler { this.mListener = listener; } + /** + * Using this constructor the websocket connection closes after the response. + * @param accountId ID of the account to retrieve + * @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 GetAccounts(String accountId, WitnessResponseListener listener){ this(accountId, true, listener); } + /** + * Using this constructor the websocket connection closes after the response. + * @param accounts list with the accounts to retrieve + * @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 GetAccounts(List accounts, WitnessResponseListener listener){ this(accounts, true, listener); } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java index b4c986b..26f10f1 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java @@ -16,7 +16,13 @@ import java.util.List; import java.util.Map; /** - * Created by nelson on 1/25/17. + * Class that implements get_all_asset_holders request handler. + * + * Get a list of all system assets with holders count. + * + * The request returns the list of all assets with holders count. + * + * @see get_all_asset_holders API doc */ public class GetAllAssetHolders extends BaseGrapheneHandler { private final static int LOGIN_ID = 1; @@ -28,11 +34,26 @@ public class GetAllAssetHolders extends BaseGrapheneHandler { private boolean mOneTime; + /** + * Constructor + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 GetAllAssetHolders(boolean oneTime, WitnessResponseListener listener) { super(listener); this.mOneTime = oneTime; } + /** + * Using this constructor the websocket connection closes after the response. + * + * @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 GetAllAssetHolders(WitnessResponseListener listener) { this(true, listener);} @Override 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 cd3bdf1..02d08a0 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetBlockHeader.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetBlockHeader.java @@ -19,7 +19,13 @@ import de.bitsharesmunich.graphenej.models.BlockHeader; import de.bitsharesmunich.graphenej.models.WitnessResponse; /** - * Created by nelson on 12/13/16. + * Class that implements get_block_header request handler. + * + * Retrieve a block header. + * + * The request returns the header of the referenced block, or null if no matching block was found + * + * @see get_block_header API doc */ public class GetBlockHeader extends BaseGrapheneHandler { @@ -36,6 +42,15 @@ public class GetBlockHeader extends BaseGrapheneHandler { private boolean mOneTime; + /** + * Constructor + * @param blockNumber height of the block whose header should be returned + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 GetBlockHeader(long blockNumber, boolean oneTime, WitnessResponseListener listener){ super(listener); this.blockNumber = blockNumber; @@ -43,6 +58,13 @@ public class GetBlockHeader extends BaseGrapheneHandler { this.mListener = listener; } + /** + * Using this constructor the websocket connection closes after the response. + * @param blockNumber height of the block whose header should be returned + * @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 GetBlockHeader(long blockNumber, WitnessResponseListener listener){ this(blockNumber, true, listener); } 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 9639ac5..d32b1d0 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetKeyReferences.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetKeyReferences.java @@ -19,7 +19,13 @@ import de.bitsharesmunich.graphenej.models.ApiCall; import de.bitsharesmunich.graphenej.models.WitnessResponse; /** - * Created by nelson on 11/15/16. + * Class that implements get_key_references request handler. + * + * Retrieve the keys that refer to the address/list of addresses. + * + * The request returns all accounts that refer to the key or account id in their owner or active authorities. + * + * @see get_key_references API doc */ public class GetKeyReferences extends BaseGrapheneHandler { @@ -27,14 +33,32 @@ public class GetKeyReferences extends BaseGrapheneHandler { private boolean mOneTime; + /** + * Constructor + * + * @param address address to be query + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 GetKeyReferences(Address address, boolean oneTime, WitnessResponseListener listener){ super(listener); addresses = new ArrayList<>(); addresses.add(address); this.mOneTime = oneTime; - } + /** + * + * @param addresses list of addresses to be query + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 GetKeyReferences(List
addresses, boolean oneTime, WitnessResponseListener listener) { super(listener); this.addresses = addresses; @@ -42,10 +66,26 @@ public class GetKeyReferences extends BaseGrapheneHandler { this.mOneTime = oneTime; } + /** + * Using this constructor the websocket connection closes after the response. + * + * @param address + * @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 GetKeyReferences(Address address, WitnessResponseListener listener){ this(address, true, listener); } + /** + * Using this constructor the websocket connection closes after the response. + * + * @param addresses + * @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 GetKeyReferences(List
addresses, WitnessResponseListener listener) { this(addresses, true, listener); } 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 b73429c..f399f0e 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java @@ -30,7 +30,6 @@ import de.bitsharesmunich.graphenej.models.WitnessResponse; * * @see get_limit_orders API doc * - * Created by nelson on 1/5/17. */ public class GetLimitOrders extends BaseGrapheneHandler { @@ -50,12 +49,12 @@ public class GetLimitOrders extends BaseGrapheneHandler { * @param limit maximum number of orders to retrieve * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) * after the response - * @param mListener A class implementing the WitnessResponseListener interface. This should + * @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 GetLimitOrders(String a, String b, int limit, boolean oneTime, WitnessResponseListener mListener) { - super(mListener); + public GetLimitOrders(String a, String b, int limit, boolean oneTime, WitnessResponseListener listener) { + super(listener); this.a = a; this.b = b; this.limit = limit; @@ -69,12 +68,12 @@ public class GetLimitOrders extends BaseGrapheneHandler { * @param a id of asset being sold * @param b id of asset being purchased * @param limit maximum number of orders to retrieve - * @param mListener A class implementing the WitnessResponseListener interface. This should + * @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 GetLimitOrders(String a, String b, int limit, WitnessResponseListener mListener) { - this(a, b, limit, true, mListener); + public GetLimitOrders(String a, String b, int limit, WitnessResponseListener listener) { + this(a, b, limit, true, listener); } @Override 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 9311a4c..07b65d0 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java @@ -23,7 +23,11 @@ import de.bitsharesmunich.graphenej.models.BucketObject; import de.bitsharesmunich.graphenej.models.WitnessResponse; /** - * Created by nelson on 12/22/16. + * Class that implements get_market_history request handler. + * + * + * @see get_market_history API doc + * */ public class GetMarketHistory extends BaseGrapheneHandler { @@ -46,6 +50,20 @@ public class GetMarketHistory extends BaseGrapheneHandler { private boolean mOneTime; + /** + * Constructor + * + * @param base + * @param quote + * @param bucket + * @param start + * @param end + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 GetMarketHistory(Asset base, Asset quote, long bucket, Date start, Date end, boolean oneTime, WitnessResponseListener listener){ super(listener); this.base = base; @@ -57,6 +75,17 @@ public class GetMarketHistory extends BaseGrapheneHandler { this.mListener = listener; } + /** + * Using this constructor the websocket connection closes after the response. + * + * @param base + * @param quote + * @param bucket + * @param start + * @param end + * @param listener a class implementing the WitnessResponseListener interface. This should + * be implemented by the party interested in being notified about the success/failure + */ public GetMarketHistory(Asset base, Asset quote, long bucket, Date start, Date end, WitnessResponseListener listener){ this(base, quote, bucket, start, end, true, listener); } 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 890bd21..418c51d 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java @@ -23,7 +23,11 @@ import java.util.List; import java.util.Map; /** - * Created by nelson on 11/15/16. + * Class that implements get_required_fees request handler. + * + * For each operation calculate the required fee in the specified asset type. + * + * @see get_required_fees API doc */ public class GetRequiredFees extends WebSocketAdapter { @@ -33,6 +37,17 @@ public class GetRequiredFees extends WebSocketAdapter { private boolean mOneTime; + /** + * Constructor + * + * @param operations list of operations that fee should be calculated + * @param asset specify the asset of the operations + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 GetRequiredFees(List operations, Asset asset, boolean oneTime, WitnessResponseListener listener){ this.operations = operations; this.asset = asset; @@ -40,6 +55,15 @@ public class GetRequiredFees extends WebSocketAdapter { this.mListener = listener; } + /** + * Using this constructor the websocket connection closes after the response. + * + * @param operations list of operations that fee should be calculated + * @param asset specify the asset of the operations + * @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 GetRequiredFees(List operations, Asset asset, WitnessResponseListener listener){ this(operations, asset, true, listener); } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java index 605a649..747d01b 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java @@ -18,6 +18,7 @@ import de.bitsharesmunich.graphenej.RPC; import de.bitsharesmunich.graphenej.Transaction; import de.bitsharesmunich.graphenej.UserAccount; import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException; +import de.bitsharesmunich.graphenej.interfaces.NodeErrorListener; import de.bitsharesmunich.graphenej.interfaces.SubscriptionHub; import de.bitsharesmunich.graphenej.interfaces.SubscriptionListener; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; @@ -107,6 +108,48 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs this(user, password, false, errorListener); } + /** + * Constructor used to create a subscription message hub that will call the set_subscribe_callback + * API with the clear_filter parameter set to false, meaning that it will only receive automatic updates + * from objects we register. + * + * A list of ObjectTypes must be provided, otherwise we won't get any update. + * + * @param user: User name, in case the node to which we're going to connect to requires authentication + * @param password: Password, same as above + * @param clearFilter: Whether to automatically subscribe of not to the notification feed. + * @param errorListener: Callback that will be fired in case there is an error. + */ + public SubscriptionMessagesHub(String user, String password, boolean clearFilter, NodeErrorListener errorListener){ + super(errorListener); + this.user = user; + this.password = password; + this.clearFilter = clearFilter; + this.mSubscriptionDeserializer = new SubscriptionResponse.SubscriptionResponseDeserializer(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(SubscriptionResponse.class, mSubscriptionDeserializer); + builder.registerTypeAdapter(Transaction.class, new Transaction.TransactionDeserializer()); + builder.registerTypeAdapter(TransferOperation.class, new TransferOperation.TransferDeserializer()); + builder.registerTypeAdapter(LimitOrderCreateOperation.class, new LimitOrderCreateOperation.LimitOrderCreateDeserializer()); + builder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); + builder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountSimpleDeserializer()); + builder.registerTypeAdapter(DynamicGlobalProperties.class, new DynamicGlobalProperties.DynamicGlobalPropertiesDeserializer()); + this.gson = builder.create(); + } + + /** + * Constructor used to create a subscription message hub that will call the set_subscribe_callback + * API with the clear_filter parameter set to false, meaning that it will only receive updates + * from objects we register. + * + * @param user: User name, in case the node to which we're going to connect to requires authentication + * @param password: Password, same as above + * @param errorListener: Callback that will be fired in case there is an error. + */ + public SubscriptionMessagesHub(String user, String password, NodeErrorListener errorListener){ + this(user, password, false, errorListener); + } + @Override public void addSubscriptionListener(SubscriptionListener listener){ this.mSubscriptionDeserializer.addSubscriptionListener(listener); diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java index c02d9ba..a28249a 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java @@ -6,7 +6,9 @@ import java.util.List; import de.bitsharesmunich.graphenej.api.BaseGrapheneHandler; import de.bitsharesmunich.graphenej.api.SubscriptionMessagesHub; import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException; +import de.bitsharesmunich.graphenej.interfaces.NodeErrorListener; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; +import de.bitsharesmunich.graphenej.models.BaseResponse; /** * Created by nelson on 6/26/17. @@ -24,9 +26,14 @@ public class NodeConnection { private WebsocketWorkerThread mThread; private SubscriptionMessagesHub mMessagesHub; private long requestCounter = SubscriptionMessagesHub.MANUAL_SUBSCRIPTION_ID + 1; + private WitnessResponseListener mErrorListener; private static NodeConnection instance; + private String mUser; + private String mPassword; + private boolean mSubscribe; + /* * Ger the instance of the NodeConnection which is inteded to be used as a Singleton. */ @@ -47,6 +54,7 @@ public class NodeConnection { * @param url: URL of the node */ public void addNodeUrl(String url){ + System.out.println("addNodeUrl: "+url); this.mUrlList.add(url); } @@ -77,6 +85,15 @@ public class NodeConnection { this.mUrlList.clear(); } + private NodeErrorListener mInternalErrorListener = new NodeErrorListener() { + @Override + public void onError(BaseResponse.Error error) { + System.out.println("NodeConnect Error. Msg: "+error); + + connect(mUser, mPassword, mSubscribe, mErrorListener); + } + }; + /** * Method that will try to connect to one of the nodes. If the connection fails * a subsequent call to this method will try to connect with the next node in the @@ -84,7 +101,12 @@ public class NodeConnection { */ public void connect(String user, String password, boolean subscribe, WitnessResponseListener errorListener) { if(this.mUrlList.size() > 0){ - mThread = new WebsocketWorkerThread(this.mUrlList.get(mUrlIndex)); + mUser = user; + mPassword = password; + mSubscribe = subscribe; + System.out.println("Connecting to: "+ this.mUrlList.get(mUrlIndex)); + mErrorListener = errorListener; + mThread = new WebsocketWorkerThread(this.mUrlList.get(mUrlIndex), mInternalErrorListener); mUrlIndex = mUrlIndex + 1 % this.mUrlList.size(); mMessagesHub = new SubscriptionMessagesHub(user, password, subscribe, errorListener); diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/WebsocketWorkerThread.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/WebsocketWorkerThread.java index 91aeba8..d14a0b2 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/WebsocketWorkerThread.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/WebsocketWorkerThread.java @@ -10,6 +10,8 @@ import java.security.NoSuchAlgorithmException; import javax.net.ssl.SSLContext; +import de.bitsharesmunich.graphenej.interfaces.NodeErrorListener; +import de.bitsharesmunich.graphenej.models.BaseResponse; import de.bitsharesmunich.graphenej.test.NaiveSSLContext; /** @@ -23,6 +25,7 @@ public class WebsocketWorkerThread extends Thread { private final int TIMEOUT = 5000; private WebSocket mWebSocket; + private NodeErrorListener mErrorListener; public WebsocketWorkerThread(String url){ try { @@ -45,12 +48,40 @@ public class WebsocketWorkerThread extends Thread { } } + /** + * Constructor with connection error listener + * @param url + * @param errorListener + */ + public WebsocketWorkerThread(String url, NodeErrorListener errorListener){ + try { + WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(TIMEOUT); + + if(DEBUG){ + SSLContext context = NaiveSSLContext.getInstance("TLS"); + + // Set the custom SSL context. + factory.setSSLContext(context); + } + + mWebSocket = factory.createSocket(url); + mErrorListener = errorListener; + } catch (IOException e) { + System.out.println("IOException. Msg: "+e.getMessage()); + } catch(NullPointerException e){ + System.out.println("NullPointerException at WebsocketWorkerThreas. Msg: "+e.getMessage()); + } catch (NoSuchAlgorithmException e) { + System.out.println("NoSuchAlgorithmException. Msg: "+e.getMessage()); + } + } + @Override public void run() { try { mWebSocket.connect(); } catch (WebSocketException e) { System.out.println("WebSocketException. Msg: "+e.getMessage()); + mErrorListener.onError(new BaseResponse.Error(e.getMessage())); } } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/interfaces/NodeErrorListener.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/interfaces/NodeErrorListener.java new file mode 100644 index 0000000..f0386b7 --- /dev/null +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/interfaces/NodeErrorListener.java @@ -0,0 +1,10 @@ +package de.bitsharesmunich.graphenej.interfaces; + +import de.bitsharesmunich.graphenej.models.BaseResponse; + +/** + * Interface to be implemented by any listener to network errors. + */ +public interface NodeErrorListener { + void onError(BaseResponse.Error error); +} 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 9536b1f..140e3bf 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 @@ -6,8 +6,10 @@ import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; +import com.google.common.primitives.UnsignedLong; import de.bitsharesmunich.graphenej.Asset; +import de.bitsharesmunich.graphenej.OperationType; import de.bitsharesmunich.graphenej.api.GetAccounts; import de.bitsharesmunich.graphenej.api.GetAccountBalances; import de.bitsharesmunich.graphenej.api.GetAccountByName; @@ -15,13 +17,17 @@ import de.bitsharesmunich.graphenej.api.GetAllAssetHolders; import de.bitsharesmunich.graphenej.api.GetBlockHeader; import de.bitsharesmunich.graphenej.api.GetKeyReferences; import de.bitsharesmunich.graphenej.api.GetLimitOrders; +import de.bitsharesmunich.graphenej.api.GetRequiredFees; 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.AssetAmount; import de.bitsharesmunich.graphenej.UserAccount; import de.bitsharesmunich.graphenej.Address; +import de.bitsharesmunich.graphenej.BaseOperation; +import de.bitsharesmunich.graphenej.operations.TransferOperation; /** * Created by nelson on 6/26/17. @@ -31,7 +37,8 @@ public class NodeConnectionTest { private String NODE_URL_2 = System.getenv("NODE_URL_2"); private String NODE_URL_3 = System.getenv("NODE_URL_3"); private String NODE_URL_4 = System.getenv("NODE_URL_4"); - private String ACCOUNT_ID = System.getenv("ACCOUNT_ID"); + private String ACCOUNT_ID_1 = System.getenv("ACCOUNT_ID_1"); + private String ACCOUNT_ID_2 = System.getenv("ACCOUNT_ID_2"); 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"); @@ -102,12 +109,13 @@ public class NodeConnectionTest { */ public void testNodeHopFeature(){ nodeConnection = NodeConnection.getInstance(); - nodeConnection.addNodeUrl(NODE_URL_4); + //nodeConnection.addNodeUrl(NODE_URL_4); //Test adding a "sublist" ArrayList urlList = new ArrayList(){{ add(NODE_URL_3); add(NODE_URL_3); }}; + //nodeConnection.addNodeUrls(urlList); nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", true, mErrorListener); @@ -129,7 +137,7 @@ public class NodeConnectionTest { /** * Test for GetAccountBalances Handler. * - * Request balances for a valid account (Need to setup the ACCOUNT_ID env with desired account id) + * Request balances for a valid account (Need to setup the ACCOUNT_ID_1 env with desired account id) * */ @Test @@ -140,7 +148,7 @@ public class NodeConnectionTest { System.out.println("Adding GetAccountBalances here"); try{ - UserAccount userAccount = new UserAccount(ACCOUNT_ID); + UserAccount userAccount = new UserAccount(ACCOUNT_ID_1); ArrayList assetList = new ArrayList<>(); assetList.add(BTS); assetList.add(BITDOLAR); @@ -162,7 +170,7 @@ public class NodeConnectionTest { } try{ - UserAccount userAccount = new UserAccount(ACCOUNT_ID); + UserAccount userAccount = new UserAccount(ACCOUNT_ID_1); System.out.println("Test: Request to all account' assets balance"); nodeConnection.addRequestHandler(new GetAccountBalances(userAccount, null, false, new WitnessResponseListener(){ @Override @@ -229,6 +237,73 @@ public class NodeConnectionTest { } } + /** + * Test for GetAccounts Handler. + * + * Request for a valid account name by name (Need to setup the ACCOUNT_NAME env with desired + * account name) + * + */ + public void testGetAccountsRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(NODE_URL_1); + + ArrayList accountList = new ArrayList(){{ + add(new UserAccount(ACCOUNT_ID_1)); + add(new UserAccount(ACCOUNT_ID_2)); + }}; + + nodeConnection.connect("", "", false, mErrorListener); + + System.out.println("Adding GetAccounts for one Account ID."); + try{ + nodeConnection.addRequestHandler(new GetAccounts(ACCOUNT_ID_1, false, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("GetAccounts.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("GetAccounts.onError. Msg: "+ error.message); + } + })); + }catch(RepeatedRequestIdException e){ + System.out.println("RepeatedRequestIdException. Msg: "+e.getMessage()); + } + + System.out.println("Adding GetAccounts for a list of Account IDs."); + try{ + nodeConnection.addRequestHandler(new GetAccounts(accountList, false, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("GetAccounts.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("GetAccounts.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()); + } + } + + /** + * Test for GetAllAssetHolders Handler. + * + */ @Test public void testGetAllAssetHoldersRequest(){ nodeConnection = NodeConnection.getInstance(); @@ -262,6 +337,12 @@ public class NodeConnectionTest { } } + /** + * Test for GetBlockHeader Handler. + * + * Request for a valid account block header (Need to setup the BlOCK_TEST_NUMBER env with desired + * block height) + */ @Test public void testGetBlockHeaderRequest(){ nodeConnection = NodeConnection.getInstance(); @@ -297,6 +378,10 @@ public class NodeConnectionTest { } } + /** + * Test for GetKeyReferences Handler. + * + */ @Test public void testGetKeyReferencesRequest(){ nodeConnection = NodeConnection.getInstance(); @@ -365,6 +450,97 @@ public class NodeConnectionTest { } } + /** + * Test for GetMarketHistory Handler. + * + * Request for a valid account block header (Need to setup the BlOCK_TEST_NUMBER env with desired + * block height) + */ + @Test + public void testGetMarketHistoryRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(NODE_URL_1); + 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()); + } + } + + /** + * Test for GetRequiredFees Handler. + * + */ + @Test + public void testGetRequiredFeesRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(NODE_URL_1); + nodeConnection.connect("", "", false, mErrorListener); + + UserAccount userAccount_from = new UserAccount(ACCOUNT_ID_1); + UserAccount userAccount_to = new UserAccount(ACCOUNT_ID_2); + + //Test with 2 BTS + Asset testAsset = new Asset("1.3.0"); + AssetAmount assetAmountTest = new AssetAmount(UnsignedLong.valueOf(200000), testAsset); + + TransferOperation transferOperation = new TransferOperation(userAccount_from, userAccount_to, assetAmountTest, assetAmountTest); + + ArrayList operations = new ArrayList<>(); + operations.add(transferOperation); + + System.out.println("Adding GetRequiredFees request"); + try{ + nodeConnection.addRequestHandler(new GetRequiredFees(operations, testAsset, false, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("GetRequiredFees.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("GetRequiredFees.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() { From e364fe678fec81a095b19379d54f2cfd135616f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Thu, 20 Jul 2017 16:37:16 -0300 Subject: [PATCH 07/18] Adding docs and some tests for a few API handler classes (GetRequiredFees, ListAssets, LookupAccounts, LookupAssetSymbols and SubscriptionMessagesHub --- .../graphenej/api/GetRequiredFees.java | 3 +- .../graphenej/api/ListAssets.java | 5 ++- .../graphenej/api/LookupAccounts.java | 44 ++++++++++++++++++- .../graphenej/api/LookupAssetSymbols.java | 29 +++++++++++- .../api/SubscriptionMessagesHub.java | 44 +------------------ 5 files changed, 77 insertions(+), 48 deletions(-) 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 418c51d..3c9fd54 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java @@ -29,7 +29,7 @@ import java.util.Map; * * @see get_required_fees API doc */ -public class GetRequiredFees extends WebSocketAdapter { +public class GetRequiredFees extends BaseGrapheneHandler { private WitnessResponseListener mListener; private List operations; @@ -49,6 +49,7 @@ public class GetRequiredFees extends WebSocketAdapter { * of the transaction broadcast operation. */ public GetRequiredFees(List operations, Asset asset, boolean oneTime, WitnessResponseListener listener){ + super(listener); this.operations = operations; this.asset = asset; this.mOneTime = oneTime; 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 ba2e5ef..cd3f1a6 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/ListAssets.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/ListAssets.java @@ -26,7 +26,6 @@ import java.util.Map; * constructor. Internally we are going to perform multiple calls in order to satisfy the user's * request. * - * Created by nelson on 1/5/17. */ public class ListAssets extends BaseGrapheneHandler { /** @@ -49,6 +48,7 @@ public class ListAssets extends BaseGrapheneHandler { /** * 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. @@ -61,7 +61,8 @@ public class ListAssets extends BaseGrapheneHandler { } /** - * Constructor with oneTime = true for compatibility issue. + * Using this constructor the WebSocket connection closes after the response. + * * @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. 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 7a34162..2ca951f 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAccounts.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAccounts.java @@ -18,7 +18,13 @@ import de.bitsharesmunich.graphenej.models.ApiCall; import de.bitsharesmunich.graphenej.models.WitnessResponse; /** - * Created by henry on 07/12/16. + * Class that implements lookup_accounts request handler. + * + * Get names and IDs for registered accounts. + * + * The request returns a map of account names to corresponding IDs. + * + * @see lookup_accounts API doc */ public class LookupAccounts extends BaseGrapheneHandler { @@ -29,6 +35,16 @@ public class LookupAccounts extends BaseGrapheneHandler { private boolean mOneTime; + /** + * Constructor + * + * @param accountName account name used at the query + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 LookupAccounts(String accountName, boolean oneTime, WitnessResponseListener listener){ super(listener); this.accountName = accountName; @@ -37,6 +53,17 @@ public class LookupAccounts extends BaseGrapheneHandler { this.mListener = listener; } + /** + * Constructor with maxAccounts + * + * @param accountName account name used at the query + * @param maxAccounts maximum number of results to return (must not exceed 1000) + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 LookupAccounts(String accountName, int maxAccounts, boolean oneTime, WitnessResponseListener listener){ super(listener); this.accountName = accountName; @@ -45,10 +72,25 @@ public class LookupAccounts extends BaseGrapheneHandler { this.mListener = listener; } + /** + * Using this constructor the WebSocket connection closes after the response. + * + * @param accountName account name used at the query + * @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 LookupAccounts(String accountName, WitnessResponseListener listener){ this(accountName, true, listener); } + /** + * Using this constructor the WebSocket connection closes after the response. + * + * @param accountName account name used at the query + * @param maxAccounts maximum number of results to return (must not exceed 1000) + * @param listener + */ public LookupAccounts(String accountName, int maxAccounts, WitnessResponseListener listener){ this(accountName, maxAccounts, true, listener); } 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 770f851..186a74e 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java @@ -18,7 +18,13 @@ import de.bitsharesmunich.graphenej.models.ApiCall; import de.bitsharesmunich.graphenej.models.WitnessResponse; /** - * Created by nelson on 12/12/16. + * Class that implements lookup_asset_symbols request handler. + * + * Get the assets corresponding to the provided IDs. + * + * The request returns the assets corresponding to the provided symbols or IDs. + * + * @see lookup_asset_symbols API doc */ public class LookupAssetSymbols extends BaseGrapheneHandler { private WitnessResponseListener mListener; @@ -26,6 +32,16 @@ public class LookupAssetSymbols extends BaseGrapheneHandler { private boolean mOneTime; + /** + * Constructor + * + * @param assets list of the assets to retrieve + * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * after the response + * @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 LookupAssetSymbols(List assets, boolean oneTime, WitnessResponseListener listener){ super(listener); this.assets = assets; @@ -33,6 +49,14 @@ public class LookupAssetSymbols extends BaseGrapheneHandler { this.mListener = listener; } + /** + * Using this constructor the WebSocket connection closes after the response. + * + * @param assets list of the assets to retrieve + * @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 LookupAssetSymbols(List assets, WitnessResponseListener listener){ this(assets, true, listener); } @@ -58,6 +82,9 @@ public class LookupAssetSymbols extends BaseGrapheneHandler { gsonBuilder.registerTypeAdapter(Asset.class, new Asset.AssetDeserializer()); WitnessResponse> witnessResponse = gsonBuilder.create().fromJson(response, LookupAssetSymbolsResponse); mListener.onSuccess(witnessResponse); + if(mOneTime){ + websocket.disconnect(); + } } @Override diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java index 747d01b..780564d 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java @@ -30,7 +30,7 @@ import de.bitsharesmunich.graphenej.operations.LimitOrderCreateOperation; import de.bitsharesmunich.graphenej.operations.TransferOperation; /** - * A websocket adapter prepared to be used as a basic dispatch hub for subscription messages. + * A WebSocket adapter prepared to be used as a basic dispatch hub for subscription messages. * * Created by nelson on 1/26/17. */ @@ -66,48 +66,6 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs private boolean isUnsubscribing; private boolean isSubscribed; - /** - * Constructor used to create a subscription message hub that will call the set_subscribe_callback - * API with the clear_filter parameter set to false, meaning that it will only receive automatic updates - * from objects we register. - * - * A list of ObjectTypes must be provided, otherwise we won't get any update. - * - * @param user: User name, in case the node to which we're going to connect to requires authentication - * @param password: Password, same as above - * @param clearFilter: Whether to automatically subscribe of not to the notification feed. - * @param errorListener: Callback that will be fired in case there is an error. - */ - public SubscriptionMessagesHub(String user, String password, boolean clearFilter, WitnessResponseListener errorListener){ - super(errorListener); - this.user = user; - this.password = password; - this.clearFilter = clearFilter; - this.mSubscriptionDeserializer = new SubscriptionResponse.SubscriptionResponseDeserializer(); - GsonBuilder builder = new GsonBuilder(); - builder.registerTypeAdapter(SubscriptionResponse.class, mSubscriptionDeserializer); - builder.registerTypeAdapter(Transaction.class, new Transaction.TransactionDeserializer()); - builder.registerTypeAdapter(TransferOperation.class, new TransferOperation.TransferDeserializer()); - builder.registerTypeAdapter(LimitOrderCreateOperation.class, new LimitOrderCreateOperation.LimitOrderCreateDeserializer()); - builder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); - builder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountSimpleDeserializer()); - builder.registerTypeAdapter(DynamicGlobalProperties.class, new DynamicGlobalProperties.DynamicGlobalPropertiesDeserializer()); - this.gson = builder.create(); - } - - /** - * Constructor used to create a subscription message hub that will call the set_subscribe_callback - * API with the clear_filter parameter set to false, meaning that it will only receive updates - * from objects we register. - * - * @param user: User name, in case the node to which we're going to connect to requires authentication - * @param password: Password, same as above - * @param errorListener: Callback that will be fired in case there is an error. - */ - public SubscriptionMessagesHub(String user, String password, WitnessResponseListener errorListener){ - this(user, password, false, errorListener); - } - /** * Constructor used to create a subscription message hub that will call the set_subscribe_callback * API with the clear_filter parameter set to false, meaning that it will only receive automatic updates From 240cff9c7cc74d0dc9acf13231100cf0293caec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Thu, 20 Jul 2017 19:35:37 -0300 Subject: [PATCH 08/18] Fix conflicts between NodeErrorListener and WitnessResponseListener --- .../graphenej/api/android/NodeConnection.java | 2 +- .../graphenej/api/SubscriptionMessagesHubTest.java | 8 ++------ .../graphenej/api/android/NodeConnectionTest.java | 9 +++++---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java index a28249a..54be1ce 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java @@ -109,7 +109,7 @@ public class NodeConnection { mThread = new WebsocketWorkerThread(this.mUrlList.get(mUrlIndex), mInternalErrorListener); mUrlIndex = mUrlIndex + 1 % this.mUrlList.size(); - mMessagesHub = new SubscriptionMessagesHub(user, password, subscribe, errorListener); + mMessagesHub = new SubscriptionMessagesHub(user, password, subscribe, mInternalErrorListener); mThread.addListener(mMessagesHub); mThread.start(); } diff --git a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHubTest.java b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHubTest.java index a0828d3..9d4e4b5 100644 --- a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHubTest.java +++ b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHubTest.java @@ -11,6 +11,7 @@ import java.util.TimerTask; import de.bitsharesmunich.graphenej.ObjectType; import de.bitsharesmunich.graphenej.Transaction; +import de.bitsharesmunich.graphenej.interfaces.NodeErrorListener; import de.bitsharesmunich.graphenej.interfaces.SubscriptionListener; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; import de.bitsharesmunich.graphenej.models.BaseResponse; @@ -29,12 +30,7 @@ public class SubscriptionMessagesHubTest extends BaseApiTest { /** * Error listener */ - private WitnessResponseListener mErrorListener = new WitnessResponseListener() { - @Override - public void onSuccess(WitnessResponse response) { - System.out.println("onSuccess"); - } - + private NodeErrorListener mErrorListener = new NodeErrorListener() { @Override public void onError(BaseResponse.Error error) { System.out.println("onError"); 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 140e3bf..e232636 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 @@ -465,7 +465,7 @@ public class NodeConnectionTest { System.out.println("Adding GetBlockHeader request"); try{ - nodeConnection.addRequestHandler(new GetBlockHeader(BlOCK_TEST_NUMBER,false, new WitnessResponseListener(){ + nodeConnection.addRequestHandler(new GetBlockHeader(BlOCK_TEST_NUMBER, true, new WitnessResponseListener(){ @Override public void onSuccess(WitnessResponse response) { System.out.println("GetBlockHeader.onSuccess"); @@ -507,15 +507,16 @@ public class NodeConnectionTest { //Test with 2 BTS Asset testAsset = new Asset("1.3.0"); AssetAmount assetAmountTest = new AssetAmount(UnsignedLong.valueOf(200000), testAsset); + AssetAmount feeAmountTest = new AssetAmount(UnsignedLong.valueOf(100000), testAsset); - TransferOperation transferOperation = new TransferOperation(userAccount_from, userAccount_to, assetAmountTest, assetAmountTest); + TransferOperation transferOperation = new TransferOperation(userAccount_from, userAccount_to, assetAmountTest, feeAmountTest); ArrayList operations = new ArrayList<>(); operations.add(transferOperation); - System.out.println("Adding GetRequiredFees request"); + System.out.println("Adding GetBlockHeader request"); try{ - nodeConnection.addRequestHandler(new GetRequiredFees(operations, testAsset, false, new WitnessResponseListener(){ + nodeConnection.addRequestHandler(new GetRequiredFees(operations, testAsset, true, new WitnessResponseListener(){ @Override public void onSuccess(WitnessResponse response) { System.out.println("GetRequiredFees.onSuccess"); From a824d8fc40edb482d5c8ea45bd749915e8eb8792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Thu, 20 Jul 2017 23:47:24 -0300 Subject: [PATCH 09/18] Finishing base docs for all API handler classes --- .../graphenej/api/BaseGrapheneHandler.java | 24 +++++++- .../graphenej/api/GetAccountBalances.java | 31 +++++----- .../graphenej/api/GetAccountByName.java | 24 ++++---- .../graphenej/api/GetAccountHistory.java | 1 + .../graphenej/api/GetAccounts.java | 55 +++++++++--------- .../graphenej/api/GetAllAssetHolders.java | 19 ++++--- .../graphenej/api/GetBlockHeader.java | 26 +++++---- .../graphenej/api/GetKeyReferences.java | 4 +- .../graphenej/api/GetLimitOrders.java | 24 ++++---- .../graphenej/api/GetMarketHistory.java | 52 ++++++++++------- .../graphenej/api/GetObjects.java | 28 +++++++++- .../api/GetRelativeAccountHistory.java | 56 ++++++++++++------- .../graphenej/api/GetRequiredFees.java | 28 +++++----- .../graphenej/api/GetTradeHistory.java | 46 +++++++++++++-- .../graphenej/api/ListAssets.java | 25 ++++++--- .../graphenej/api/LookupAccounts.java | 42 +++++++------- .../graphenej/api/LookupAssetSymbols.java | 22 ++++---- .../api/SubscriptionMessagesHub.java | 27 ++++----- .../api/TransactionBroadcastSequence.java | 25 +++++---- .../graphenej/api/android/NodeConnection.java | 33 ++++++++--- .../api/android/WebsocketWorkerThread.java | 27 +++++++-- 21 files changed, 393 insertions(+), 226 deletions(-) diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/BaseGrapheneHandler.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/BaseGrapheneHandler.java index ea9437b..80d7e2b 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/BaseGrapheneHandler.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/BaseGrapheneHandler.java @@ -12,8 +12,6 @@ import de.bitsharesmunich.graphenej.models.BaseResponse; /** * Base class that should be extended by any implementation of a specific request to the full node. - * - * Created by nelson on 1/5/17. */ public abstract class BaseGrapheneHandler extends WebSocketAdapter { @@ -32,9 +30,22 @@ public abstract class BaseGrapheneHandler extends WebSocketAdapter { */ protected long requestId; + /** + * Constructor (The original constructor, should be replaced with the one that receives + * NodeErrorListener instead of WitnessResponseListener) + * + * @param listener listener to be notified in if an error occurs + */ + @Deprecated public BaseGrapheneHandler(WitnessResponseListener listener){ this.mListener = listener; } + + /** + * Constructor + * + * @param listener listener to be notified if an error occurs + */ public BaseGrapheneHandler(NodeErrorListener listener){ this.mErrorListener = listener; } @@ -52,7 +63,14 @@ public abstract class BaseGrapheneHandler extends WebSocketAdapter { for (StackTraceElement element : cause.getStackTrace()){ System.out.println(element.getFileName()+"#"+element.getClassName()+":"+element.getLineNumber()); } - mErrorListener.onError(new BaseResponse.Error(cause.getMessage())); + // Should be replaced for mErrorListener (NodeErrorListener type) only in the future + if(mErrorListener != null){ + mErrorListener.onError(new BaseResponse.Error(cause.getMessage())); + } + else{ + mListener.onError(new BaseResponse.Error(cause.getMessage())); + } + websocket.disconnect(); } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java index 1d8a5d7..41f6287 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountBalances.java @@ -23,11 +23,10 @@ import java.util.Map; * * Get an account’s balances in various assets. * - * The request returns the balances of the account + * The response returns the balances of the account * * @see get_account_balances API doc * - * Created by nelson on 1/13/17. */ public class GetAccountBalances extends BaseGrapheneHandler { @@ -38,13 +37,14 @@ public class GetAccountBalances extends BaseGrapheneHandler { /** * Default Constructor * - * @param userAccount account to get balances for - * @param assets list of the assets to get balances of; if empty, get all assets account has a balance in - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) - * after the response - * @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. + * @param userAccount account to get balances for + * @param assets list of the assets to get balances of; if empty, get all assets account + * has a balance in + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public GetAccountBalances(UserAccount userAccount, List assets, boolean oneTime, WitnessResponseListener listener) { super(listener); @@ -54,13 +54,14 @@ public class GetAccountBalances extends BaseGrapheneHandler { } /** - * Using this constructor the websocket connection closes after the response. + * Using this constructor the WebSocket connection closes after the response. * - * @param userAccount account to get balances for - * @param assets list of the assets to get balances of; if empty, get all assets account has a balance in - * @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. + * @param userAccount account to get balances for + * @param assets list of the assets to get balances of; if empty, get all assets account + * has a balance in + * @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 operation. */ public GetAccountBalances(UserAccount userAccount, List assets, WitnessResponseListener listener) { this(userAccount, assets, true, listener); diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java index 4998c40..d11634f 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java @@ -24,7 +24,7 @@ import de.bitsharesmunich.graphenej.models.WitnessResponse; * * Get an account’s info by name. * - * The request returns account data that refer to the name. + * The response returns account data that refer to the name. * * @see get_account_by_name API doc */ @@ -37,12 +37,12 @@ public class GetAccountByName extends BaseGrapheneHandler { /** * Default Constructor * - * @param accountName name of the account to get info - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) - * after the response - * @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. + * @param accountName name of the account to get info + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public GetAccountByName(String accountName, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -52,12 +52,12 @@ public class GetAccountByName extends BaseGrapheneHandler { } /** - * Using this constructor the websocket connection closes after the response. + * Using this constructor the WebSocket connection closes after the response. * - * @param accountName name of the account to get info - * @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. + * @param accountName name of the account to get info + * @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 operation. */ public GetAccountByName(String accountName, WitnessResponseListener listener){ this(accountName, true, listener); diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountHistory.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountHistory.java index 1c1db62..061cedc 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountHistory.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountHistory.java @@ -3,5 +3,6 @@ package de.bitsharesmunich.graphenej.api; /** * Created by nelson on 12/26/16. */ +//TODO: Implement if needed: http://docs.bitshares.eu/api/history.html?highlight=get_market_history#account-history-api public class GetAccountHistory { } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccounts.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccounts.java index aa14bc3..9350ee5 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccounts.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAccounts.java @@ -25,7 +25,7 @@ import de.bitsharesmunich.graphenej.models.WitnessResponse; * * Get a list of accounts by ID. * - * The request returns the accounts corresponding to the provided IDs. + * The response returns the accounts corresponding to the provided IDs. * * @see get_accounts API doc */ @@ -36,13 +36,14 @@ public class GetAccounts extends BaseGrapheneHandler { private boolean mOneTime; /** - * Default Constructor - * @param accountId ID of the account to retrieve - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) - * after the response - * @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. + * Constructor for one account only. + * + * @param accountId ID of the account to retrieve + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public GetAccounts(String accountId, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -52,13 +53,14 @@ public class GetAccounts extends BaseGrapheneHandler { } /** - * Using this constructor the websocket connection closes after the response. - * @param accounts list with the accounts to retrieve - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) - * after the response - * @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. + * Constructor for account list. + * + * @param accounts list with the accounts to retrieve + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public GetAccounts(List accounts, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -68,22 +70,25 @@ public class GetAccounts extends BaseGrapheneHandler { } /** - * Using this constructor the websocket connection closes after the response. - * @param accountId ID of the account to retrieve - * @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. + * Using this constructor the WebSocket connection closes after the response. (Account based) + * + * @param accountId ID of the account to retrieve + * @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 operation. */ public GetAccounts(String accountId, WitnessResponseListener listener){ this(accountId, true, listener); } /** - * Using this constructor the websocket connection closes after the response. - * @param accounts list with the accounts to retrieve - * @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. + * Using this constructor the WebSocket connection closes after the response. (Account List + * based) + * + * @param accounts list with the accounts to retrieve + * @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 operation. */ public GetAccounts(List accounts, WitnessResponseListener listener){ this(accounts, true, listener); diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java index 26f10f1..4147bc5 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetAllAssetHolders.java @@ -20,9 +20,9 @@ import java.util.Map; * * Get a list of all system assets with holders count. * - * The request returns the list of all assets with holders count. + * The response returns the list of all assets with holders count. * - * @see get_all_asset_holders API doc + * @see get_all_asset_holders API doc (source code ref.) */ public class GetAllAssetHolders extends BaseGrapheneHandler { private final static int LOGIN_ID = 1; @@ -35,12 +35,13 @@ public class GetAllAssetHolders extends BaseGrapheneHandler { private boolean mOneTime; /** - * Constructor - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) - * after the response - * @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. + * Default Constructor + * + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public GetAllAssetHolders(boolean oneTime, WitnessResponseListener listener) { super(listener); @@ -48,7 +49,7 @@ public class GetAllAssetHolders extends BaseGrapheneHandler { } /** - * Using this constructor the websocket connection closes after the response. + * Using this constructor the WebSocket connection closes after the response. * * @param listener A class implementing the WitnessResponseListener interface. This should * be implemented by the party interested in being notified about the success/failure 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 02d08a0..cb59103 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetBlockHeader.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetBlockHeader.java @@ -43,13 +43,14 @@ public class GetBlockHeader extends BaseGrapheneHandler { private boolean mOneTime; /** - * Constructor - * @param blockNumber height of the block whose header should be returned - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) - * after the response - * @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. + * Default Constructor + * + * @param blockNumber height of the block whose header should be returned + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public GetBlockHeader(long blockNumber, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -59,11 +60,12 @@ public class GetBlockHeader extends BaseGrapheneHandler { } /** - * Using this constructor the websocket connection closes after the response. - * @param blockNumber height of the block whose header should be returned - * @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. + * Using this constructor the WebSocket connection closes after the response. + * + * @param blockNumber height of the block whose header should be returned + * @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 operation. */ public GetBlockHeader(long blockNumber, WitnessResponseListener listener){ this(blockNumber, true, listener); 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 d32b1d0..576d6a6 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetKeyReferences.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetKeyReferences.java @@ -37,7 +37,7 @@ public class GetKeyReferences extends BaseGrapheneHandler { * Constructor * * @param address address to be query - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not (false) * after the response * @param listener A class implementing the WitnessResponseListener interface. This should * be implemented by the party interested in being notified about the success/failure @@ -53,7 +53,7 @@ public class GetKeyReferences extends BaseGrapheneHandler { /** * * @param addresses list of addresses to be query - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not (false) * after the response * @param listener A class implementing the WitnessResponseListener interface. This should * be implemented by the party interested in being notified about the success/failure 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 f399f0e..9fa7236 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java @@ -47,11 +47,11 @@ public class GetLimitOrders extends BaseGrapheneHandler { * @param a id of asset being sold * @param b id of asset being purchased * @param limit maximum number of orders to retrieve - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) - * after the response - * @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. + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public GetLimitOrders(String a, String b, int limit, boolean oneTime, WitnessResponseListener listener) { super(listener); @@ -63,14 +63,14 @@ public class GetLimitOrders extends BaseGrapheneHandler { } /** - * Using this constructor the websocket connection closes after the response. + * Using this constructor the WebSocket connection closes after the response. * - * @param a id of asset being sold - * @param b id of asset being purchased - * @param limit maximum number of orders to retrieve - * @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. + * @param a id of asset being sold + * @param b id of asset being purchased + * @param limit maximum number of orders to retrieve + * @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 operation. */ public GetLimitOrders(String a, String b, int limit, WitnessResponseListener listener) { this(a, b, limit, true, listener); 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 07b65d0..bb6be94 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java @@ -25,6 +25,7 @@ import de.bitsharesmunich.graphenej.models.WitnessResponse; /** * Class that implements get_market_history request handler. * + * Get mar * * @see get_market_history API doc * @@ -51,18 +52,23 @@ public class GetMarketHistory extends BaseGrapheneHandler { private boolean mOneTime; /** - * Constructor + * Default Constructor * - * @param base - * @param quote - * @param bucket - * @param start - * @param end - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) - * after the response - * @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. + * @param base asset which history is desired + * @param quote asset which the base price asset will be compared to + * @param bucket the time interval (in seconds) for each point should be (analog to + * candles on a candle stick graph). + * Note: The bucket value is discrete and node dependent. The default value + * is 3600s. To get the available buckets of a node use + * get_all_asset_holders API call. + * @param start datetime of of the most recent operation to retrieve (Note: The name is + * counter intuitive, but it follow the original API parameter name) + * @param end datetime of the the earliest operation to retrieve + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public GetMarketHistory(Asset base, Asset quote, long bucket, Date start, Date end, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -76,15 +82,21 @@ public class GetMarketHistory extends BaseGrapheneHandler { } /** - * Using this constructor the websocket connection closes after the response. + * Using this constructor the WebSocket connection closes after the response. * - * @param base - * @param quote - * @param bucket - * @param start - * @param end - * @param listener a class implementing the WitnessResponseListener interface. This should - * be implemented by the party interested in being notified about the success/failure + * @param base asset which history is desired + * @param quote asset which the base price asset will be compared to + * @param bucket the time interval (in seconds) for each point should be (analog to + * candles on a candle stick graph). + * Note: The bucket value is discrete and node dependent. The default value + * is 3600s. To get the available buckets of a node use + * get_all_asset_holders API call. + * @param start datetime of of the most recent operation to retrieve (Note: The name is + * counter intuitive, but it follow the original API parameter name) + * @param end datetime of the the earliest operation to retrieve + * @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 operation. */ public GetMarketHistory(Asset base, Asset quote, long bucket, Date start, Date end, WitnessResponseListener listener){ this(base, quote, bucket, start, end, true, listener); @@ -135,7 +147,7 @@ public class GetMarketHistory extends BaseGrapheneHandler { } public void disconnect(){ - if(mWebsocket != null && mWebsocket.isOpen() && mOneTime){ + if(mWebsocket != null && mWebsocket.isOpen()){ mWebsocket.disconnect(); } } 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 e507186..9c34966 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetObjects.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetObjects.java @@ -28,19 +28,45 @@ import de.bitsharesmunich.graphenej.models.BitAssetData; import de.bitsharesmunich.graphenej.models.WitnessResponse; /** - * Created by nelson on 1/8/17. + * + * Class that implements get_objects request handler. + * + * Get the objects corresponding to the provided IDs. + * + * The response returns a list of objects retrieved, in the order they are mentioned in ids + * + * @see get_objects API doc + * */ public class GetObjects extends BaseGrapheneHandler { private List ids; private boolean mOneTime; + /** + * Default Constructor + * + * @param ids list of IDs of the objects to retrieve + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. + */ public GetObjects(List ids, boolean oneTime, WitnessResponseListener listener){ super(listener); this.ids = ids; this.mOneTime = oneTime; } + /** + * Using this constructor the WebSocket connection closes after the response. + * + * @param ids list of IDs of the objects to retrieve + * @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 operation. + */ public GetObjects(List ids, WitnessResponseListener listener){ this(ids, true, listener); } 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 b0d299b..c26f2be 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java @@ -52,12 +52,16 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler { /** * 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 + * + * @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 (true) or not + * (false) after the response + * @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 operation. */ public GetRelativeAccountHistory(UserAccount userAccount, int stop, int limit, int start, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -72,9 +76,13 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler { /** * 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 + * + * @param userAccount The user account to be queried + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public GetRelativeAccountHistory(UserAccount userAccount, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -87,12 +95,16 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler { } /** - * 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 + * Constructor that takes all possible parameters for the query. + * Using this constructor the WebSocket connection closes after the response. + * + * @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 A class implementing the WitnessResponseListener interface. This should + * be implemented by the party interested in being notified about the + * success/failure of the operation. */ public GetRelativeAccountHistory(UserAccount userAccount, int stop, int limit, int start, WitnessResponseListener listener){ this(userAccount, stop, limit, start, true, listener); @@ -100,9 +112,12 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler { /** * 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 + * Using this constructor the WebSocket connection closes after the response. + * + * @param userAccount The user account to be queried + * @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 operation. */ public GetRelativeAccountHistory(UserAccount userAccount, WitnessResponseListener listener){ this(userAccount, true, listener); @@ -167,7 +182,8 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler { } /** - * Updates the arguments and makes a new call to the get_relative_account_history API + * Updates the arguments and makes a new call to the get_relative_account_history API. + * * @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 @@ -180,7 +196,7 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler { } /** - * Disconnects the websocket + * Disconnects the WebSocket. */ public void disconnect(){ if(mWebsocket != null && mWebsocket.isOpen() && mOneTime){ 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 3c9fd54..553c95d 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRequiredFees.java @@ -38,15 +38,15 @@ public class GetRequiredFees extends BaseGrapheneHandler { private boolean mOneTime; /** - * Constructor + * Default Constructor * - * @param operations list of operations that fee should be calculated - * @param asset specify the asset of the operations - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) - * after the response - * @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. + * @param operations list of operations that fee should be calculated + * @param asset specify the asset of the operations + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public GetRequiredFees(List operations, Asset asset, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -57,13 +57,13 @@ public class GetRequiredFees extends BaseGrapheneHandler { } /** - * Using this constructor the websocket connection closes after the response. + * Using this constructor the WebSocket connection closes after the response. * - * @param operations list of operations that fee should be calculated - * @param asset specify the asset of the operations - * @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. + * @param operations list of operations that fee should be calculated + * @param asset specify the asset of the operations + * @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 operation. */ public GetRequiredFees(List operations, Asset asset, WitnessResponseListener listener){ this(operations, asset, true, listener); 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 d8166a0..dde5665 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetTradeHistory.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetTradeHistory.java @@ -18,7 +18,15 @@ import de.bitsharesmunich.graphenej.models.ApiCall; import de.bitsharesmunich.graphenej.models.WitnessResponse; /** - * @author henry + * Class that implements get_trade_history request handler. + * + * Get recent trades for the market assetA:assetB for a time interval + * Note: Currently, timezone offsets are not supported. The time must be UTC. + * + * The request returns the all trades of the passed pair of asset at a specific time interval. + * + * @see get_trade_history API doc + * */ public class GetTradeHistory extends BaseGrapheneHandler { @@ -31,19 +39,45 @@ public class GetTradeHistory extends BaseGrapheneHandler { private boolean mOneTime; - public GetTradeHistory(String a, String b, String toTime, String fromTime,int limit, boolean oneTime, WitnessResponseListener mListener) { - super(mListener); + /** + * Constructor + * + * @param a name of the first asset + * @param b name of the second asset + * @param toTime stop time as a UNIX timestamp + * @param fromTime start time as a UNIX timestamp + * @param limit number of transactions to retrieve, capped at 100 + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. + */ + public GetTradeHistory(String a, String b, String toTime, String fromTime,int limit, boolean oneTime, WitnessResponseListener listener) { + super(listener); this.a = a; this.b = b; this.toTime = toTime; this.fromTime = fromTime; this.limit = limit; this.mOneTime = oneTime; - this.mListener = mListener; + this.mListener = listener; } - public GetTradeHistory(String a, String b, String toTime, String fromTime,int limit, WitnessResponseListener mListener) { - this(a, b, toTime, fromTime, limit, true, mListener); + /** + * Using this constructor the WebSocket connection closes after the response. + * + * @param a name of the first asset + * @param b name of the second asset + * @param toTime stop time as a UNIX timestamp + * @param fromTime start time as a UNIX timestamp + * @param limit number of transactions to retrieve, capped at 100 + * @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 operation. + */ + public GetTradeHistory(String a, String b, String toTime, String fromTime,int limit, WitnessResponseListener listener) { + this(a, b, toTime, fromTime, limit, true, listener); } @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 cd3f1a6..28fb6bb 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/ListAssets.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/ListAssets.java @@ -19,13 +19,12 @@ import java.util.Map; /** * WebSocketAdapter class used to send a request a 'list_assets' API call to the witness node. * - * @see: - * * The API imposes a limit of of 100 assets per request, but if the user of this class wants * to get a list of all assets, the LIST_ALL constant must be used as second argument in the * constructor. Internally we are going to perform multiple calls in order to satisfy the user's * request. * + * @see: */ public class ListAssets extends BaseGrapheneHandler { /** @@ -49,9 +48,14 @@ public class ListAssets extends BaseGrapheneHandler { /** * 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. + * @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. + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public ListAssets(String lowerBoundSymbol, int limit, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -63,9 +67,14 @@ public class ListAssets extends BaseGrapheneHandler { /** * Using this constructor the WebSocket connection closes after the response. * - * @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. + * @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. + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public ListAssets(String lowerBoundSymbol, int limit, WitnessResponseListener listener){ this(lowerBoundSymbol, limit, true, listener); 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 2ca951f..97ab2bf 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAccounts.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAccounts.java @@ -38,12 +38,12 @@ public class LookupAccounts extends BaseGrapheneHandler { /** * Constructor * - * @param accountName account name used at the query - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) - * after the response - * @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. + * @param accountName account name used at the query + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public LookupAccounts(String accountName, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -56,13 +56,13 @@ public class LookupAccounts extends BaseGrapheneHandler { /** * Constructor with maxAccounts * - * @param accountName account name used at the query - * @param maxAccounts maximum number of results to return (must not exceed 1000) - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) - * after the response - * @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. + * @param accountName account name used at the query + * @param maxAccounts maximum number of results to return (must not exceed 1000) + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public LookupAccounts(String accountName, int maxAccounts, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -75,10 +75,10 @@ public class LookupAccounts extends BaseGrapheneHandler { /** * Using this constructor the WebSocket connection closes after the response. * - * @param accountName account name used at the query - * @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. + * @param accountName account name used at the query + * @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 operation. */ public LookupAccounts(String accountName, WitnessResponseListener listener){ this(accountName, true, listener); @@ -87,9 +87,11 @@ public class LookupAccounts extends BaseGrapheneHandler { /** * Using this constructor the WebSocket connection closes after the response. * - * @param accountName account name used at the query - * @param maxAccounts maximum number of results to return (must not exceed 1000) - * @param listener + * @param accountName account name used at the query + * @param maxAccounts maximum number of results to return (must not exceed 1000) + * @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 operation. */ public LookupAccounts(String accountName, int maxAccounts, WitnessResponseListener listener){ this(accountName, maxAccounts, true, listener); 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 186a74e..288c0e9 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java @@ -33,14 +33,14 @@ public class LookupAssetSymbols extends BaseGrapheneHandler { private boolean mOneTime; /** - * Constructor + * Default Constructor * - * @param assets list of the assets to retrieve - * @param oneTime boolean value indicating if websocket must be closed (true) or not (false) - * after the response - * @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. + * @param assets list of the assets to retrieve + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public LookupAssetSymbols(List assets, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -52,10 +52,10 @@ public class LookupAssetSymbols extends BaseGrapheneHandler { /** * Using this constructor the WebSocket connection closes after the response. * - * @param assets list of the assets to retrieve - * @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. + * @param assets list of the assets to retrieve + * @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 operation. */ public LookupAssetSymbols(List assets, WitnessResponseListener listener){ this(assets, true, listener); diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java index 780564d..bf80165 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java @@ -31,8 +31,6 @@ import de.bitsharesmunich.graphenej.operations.TransferOperation; /** * A WebSocket adapter prepared to be used as a basic dispatch hub for subscription messages. - * - * Created by nelson on 1/26/17. */ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements SubscriptionHub { @@ -73,10 +71,11 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs * * A list of ObjectTypes must be provided, otherwise we won't get any update. * - * @param user: User name, in case the node to which we're going to connect to requires authentication - * @param password: Password, same as above - * @param clearFilter: Whether to automatically subscribe of not to the notification feed. - * @param errorListener: Callback that will be fired in case there is an error. + * @param user User name, in case the node to which we're going to connect to requires + * authentication + * @param password Password, same as above + * @param clearFilter Whether to automatically subscribe of not to the notification feed. + * @param errorListener Callback that will be fired in case there is an error. */ public SubscriptionMessagesHub(String user, String password, boolean clearFilter, NodeErrorListener errorListener){ super(errorListener); @@ -96,13 +95,14 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs } /** - * Constructor used to create a subscription message hub that will call the set_subscribe_callback - * API with the clear_filter parameter set to false, meaning that it will only receive updates - * from objects we register. + * Constructor used to create a subscription message hub that will call the + * set_subscribe_callback API with the clear_filter parameter set to false, meaning that it will + * only receive updates from objects we register. * - * @param user: User name, in case the node to which we're going to connect to requires authentication - * @param password: Password, same as above - * @param errorListener: Callback that will be fired in case there is an error. + * @param user User name, in case the node to which we're going to connect to requires + * authentication + * @param password Password, same as above + * @param errorListener Callback that will be fired in case there is an error. */ public SubscriptionMessagesHub(String user, String password, NodeErrorListener errorListener){ this(user, password, false, errorListener); @@ -246,7 +246,8 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs /** * Method used to check the current state of the connection. - * @return: True if the websocket is open and there is an active subscription, false otherwise. + * + * @return True if the websocket is open and there is an active subscription, false otherwise. */ public boolean isSubscribed(){ return this.mWebsocket.isOpen() && isSubscribed; 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 cd4161e..6064df5 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/TransactionBroadcastSequence.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/TransactionBroadcastSequence.java @@ -46,12 +46,14 @@ public class TransactionBroadcastSequence extends BaseGrapheneHandler { 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. + * Default Constructor + * + * @param transaction transaction to be broadcasted. + * @param oneTime boolean value indicating if WebSocket must be closed (true) or not + * (false) after the response + * @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 operation. */ public TransactionBroadcastSequence(Transaction transaction, Asset feeAsset, boolean oneTime, WitnessResponseListener listener){ super(listener); @@ -62,11 +64,12 @@ public class TransactionBroadcastSequence extends BaseGrapheneHandler { } /** - * 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. + * Using this constructor the WebSocket connection closes after the response. + * + * @param transaction: 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 operation. */ public TransactionBroadcastSequence(Transaction transaction, Asset feeAsset, WitnessResponseListener listener){ this(transaction, feeAsset, true, listener); diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java index 54be1ce..3bd90e8 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/NodeConnection.java @@ -11,9 +11,11 @@ import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; import de.bitsharesmunich.graphenej.models.BaseResponse; /** - * Created by nelson on 6/26/17. + * Class used to encapsulate all connections that should be done to a node (with node hop support). + * + * This class is intended to be used as a central broker for all full node API requests. It should + * be used as a singleton under an application. */ - public class NodeConnection { /** * List of URLs of the nodes @@ -35,7 +37,7 @@ public class NodeConnection { private boolean mSubscribe; /* - * Ger the instance of the NodeConnection which is inteded to be used as a Singleton. + * Get the instance of the NodeConnection which is intended to be used as a Singleton. */ public static NodeConnection getInstance(){ if(instance == null){ @@ -49,7 +51,7 @@ public class NodeConnection { } /** - * Add a websocket URL node that will be added to the list used at node hop scheme. + * Add a WebSocket URL node that will be added to the list used at node hop scheme. * * @param url: URL of the node */ @@ -59,7 +61,7 @@ public class NodeConnection { } /** - * Add a list of websocket URL nodes that will be added to the current list and + * Add a list of WebSocket URL nodes that will be added to the current list and * be used at node hop scheme. * * @param urlList: List of URLs of the nodes @@ -70,7 +72,7 @@ public class NodeConnection { } /** - * Get the list of websocket URL nodes. + * Get the list of WebSocket URL nodes. * * @return List of URLs of the nodes */ @@ -79,7 +81,7 @@ public class NodeConnection { } /** - * Clear list of websocket URL nodes. + * Clear list of WebSocket URL nodes. */ public void clearNodeList(){ this.mUrlList.clear(); @@ -94,10 +96,22 @@ public class NodeConnection { } }; + /** + + */ /** * Method that will try to connect to one of the nodes. If the connection fails * a subsequent call to this method will try to connect with the next node in the * list if there is one. + * + * @param user user credential used for restricted requested that needed to be + * logged + * @param password password credential used for restricted requested that needed to be + * logged + * @param subscribe if the node should be subscribed to the node + * @param errorListener a class implementing the WitnessResponseListener interface. This + * should be implemented by the party interested in being notified + * about the failure of the desired broadcast operation. */ public void connect(String user, String password, boolean subscribe, WitnessResponseListener errorListener) { if(this.mUrlList.size() > 0){ @@ -116,7 +130,10 @@ public class NodeConnection { } /** - * Add the API Handler to the node. + * Add the API Handler to the node. + * + * @param handler request handler to be added to the connection + * @throws RepeatedRequestIdException */ public void addRequestHandler(BaseGrapheneHandler handler) throws RepeatedRequestIdException { handler.setRequestId(requestCounter); diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/WebsocketWorkerThread.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/WebsocketWorkerThread.java index d14a0b2..76db5e0 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/WebsocketWorkerThread.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/android/WebsocketWorkerThread.java @@ -15,7 +15,8 @@ import de.bitsharesmunich.graphenej.models.BaseResponse; import de.bitsharesmunich.graphenej.test.NaiveSSLContext; /** - * Created by nelson on 11/17/16. + * Class used to encapsulate the thread where the WebSocket does the requests. + * */ public class WebsocketWorkerThread extends Thread { private final String TAG = this.getClass().getName(); @@ -27,6 +28,11 @@ public class WebsocketWorkerThread extends Thread { private WebSocket mWebSocket; private NodeErrorListener mErrorListener; + /** + * Constructor + * + * @param url URL of the WebSocket + */ public WebsocketWorkerThread(String url){ try { WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(TIMEOUT); @@ -49,9 +55,12 @@ public class WebsocketWorkerThread extends Thread { } /** - * Constructor with connection error listener - * @param url - * @param errorListener + * Constructor with connection error listener. + * + * @param url URL of the WebSocket + * @param errorListener a class implementing the NodeErrorListener interface. This + * should be implemented by the party interested in being notified + * about the failure of the connection. */ public WebsocketWorkerThread(String url, NodeErrorListener errorListener){ try { @@ -75,6 +84,9 @@ public class WebsocketWorkerThread extends Thread { } } + /** + * Method call when the thread is started. + */ @Override public void run() { try { @@ -85,6 +97,13 @@ public class WebsocketWorkerThread extends Thread { } } + /** + * Add a WebSocketListener to the thread that will run. This should be implemented by the party + * interested in being notified about the response value of a request. + * + * @param listener listener implemented to be notified when the socket get a response from the + * node + */ public void addListener(WebSocketListener listener){ mWebSocket.addListener(listener); } From 158c9604b889611d4d9b40aaa08a7b5ecb008fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Fri, 21 Jul 2017 13:27:23 -0300 Subject: [PATCH 10/18] Fix GetLimitOrders listener and add GetLimitOrders and GetTradeHistory API tests --- .../graphenej/api/GetLimitOrders.java | 8 +- .../api/android/NodeConnectionTest.java | 91 +++++++++++++++++++ 2 files changed, 95 insertions(+), 4 deletions(-) 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 9fa7236..3327ff7 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetLimitOrders.java @@ -44,9 +44,9 @@ public class GetLimitOrders extends BaseGrapheneHandler { /** * Default Constructor * - * @param a id of asset being sold - * @param b id of asset being purchased - * @param limit maximum number of orders to retrieve + * @param a id of asset being sold + * @param b id of asset being purchased + * @param limit maximum number of orders to retrieve * @param oneTime boolean value indicating if WebSocket must be closed (true) or not * (false) after the response * @param listener A class implementing the WitnessResponseListener interface. This should @@ -59,7 +59,7 @@ public class GetLimitOrders extends BaseGrapheneHandler { this.b = b; this.limit = limit; this.mOneTime = oneTime; - this.mListener = mListener; + this.mListener = listener; } /** 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 e232636..0111738 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 @@ -18,6 +18,7 @@ import de.bitsharesmunich.graphenej.api.GetBlockHeader; import de.bitsharesmunich.graphenej.api.GetKeyReferences; import de.bitsharesmunich.graphenej.api.GetLimitOrders; import de.bitsharesmunich.graphenej.api.GetRequiredFees; +import de.bitsharesmunich.graphenej.api.GetTradeHistory; import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException; import de.bitsharesmunich.graphenej.errors.MalformedAddressException; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; @@ -42,6 +43,7 @@ public class NodeConnectionTest { 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 BLOCKPAY = new Asset("1.3.1072"); private Asset BITDOLAR = new Asset("1.3.121"); //USD Smartcoin private Asset BITEURO = new Asset("1.3.120"); //EUR Smartcoin private NodeConnection nodeConnection; @@ -450,6 +452,49 @@ public class NodeConnectionTest { } } + /** + * Test for GetLimitOrders Handler. + * + * Request for a limit orders between two assets + */ + @Test + public void testGetLimitOrdersRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(NODE_URL_1); + nodeConnection.connect("", "", false, mErrorListener); + + String asset_sold_id = BLOCKPAY.getBitassetId(); + String asset_purchased_id = BTS.getBitassetId(); + int limit = 10; + + System.out.println("Adding GetLimitOrders request"); + try{ + nodeConnection.addRequestHandler(new GetLimitOrders(asset_sold_id, asset_purchased_id, limit, true, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("GetLimitOrders.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("GetLimitOrders.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()); + } + } + /** * Test for GetMarketHistory Handler. * @@ -542,6 +587,52 @@ public class NodeConnectionTest { } } + /** + * Test for GetRequiredFees Handler. + * + */ + @Test + public void testGetTradeHistoryRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(NODE_URL_1); + nodeConnection.connect("", "", false, mErrorListener); + + UserAccount userAccount_from = new UserAccount(ACCOUNT_ID_1); + UserAccount userAccount_to = new UserAccount(ACCOUNT_ID_2); + + + String asset_sold_id = BLOCKPAY.getBitassetId(); + String asset_purchased_id = BTS.getBitassetId(); + int limit = 10; + + System.out.println("Adding GetRequiredFees request"); + try{ + nodeConnection.addRequestHandler(new GetTradeHistory(asset_sold_id, asset_purchased_id, "", "", limit, true, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("GetRequiredFees.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("GetRequiredFees.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() { From cadcca0230a67f0d6f6fd6df58c1073f9e742a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Fri, 21 Jul 2017 18:56:17 -0300 Subject: [PATCH 11/18] Add GetMarketHistory, GetObjects and GetRelativeAccountHistory API tests --- .../graphenej/api/GetMarketHistory.java | 8 +- .../api/android/NodeConnectionTest.java | 130 ++++++++++++++++-- 2 files changed, 124 insertions(+), 14 deletions(-) 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 bb6be94..06e2a7f 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetMarketHistory.java @@ -54,8 +54,8 @@ public class GetMarketHistory extends BaseGrapheneHandler { /** * Default Constructor * - * @param base asset which history is desired - * @param quote asset which the base price asset will be compared to + * @param base asset which history is desired + * @param quote asset which the base price asset will be compared to * @param bucket the time interval (in seconds) for each point should be (analog to * candles on a candle stick graph). * Note: The bucket value is discrete and node dependent. The default value @@ -84,8 +84,8 @@ public class GetMarketHistory extends BaseGrapheneHandler { /** * Using this constructor the WebSocket connection closes after the response. * - * @param base asset which history is desired - * @param quote asset which the base price asset will be compared to + * @param base asset which history is desired + * @param quote asset which the base price asset will be compared to * @param bucket the time interval (in seconds) for each point should be (analog to * candles on a candle stick graph). * Note: The bucket value is discrete and node dependent. The default value 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 0111738..9aa12a1 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.Date; import java.util.List; import java.util.Timer; import java.util.TimerTask; @@ -17,6 +18,9 @@ import de.bitsharesmunich.graphenej.api.GetAllAssetHolders; import de.bitsharesmunich.graphenej.api.GetBlockHeader; import de.bitsharesmunich.graphenej.api.GetKeyReferences; import de.bitsharesmunich.graphenej.api.GetLimitOrders; +import de.bitsharesmunich.graphenej.api.GetMarketHistory; +import de.bitsharesmunich.graphenej.api.GetObjects; +import de.bitsharesmunich.graphenej.api.GetRelativeAccountHistory; import de.bitsharesmunich.graphenej.api.GetRequiredFees; import de.bitsharesmunich.graphenej.api.GetTradeHistory; import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException; @@ -495,11 +499,11 @@ public class NodeConnectionTest { } } + /** * Test for GetMarketHistory Handler. * - * Request for a valid account block header (Need to setup the BlOCK_TEST_NUMBER env with desired - * block height) + * Request for market history of a base asset compared to a quote asset. */ @Test public void testGetMarketHistoryRequest(){ @@ -507,18 +511,124 @@ public class NodeConnectionTest { nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.connect("", "", false, mErrorListener); + Asset asset_base = BLOCKPAY; + Asset asset_quote = BTS; + //the time interval of the bucket in seconds + long bucket = 3600; - System.out.println("Adding GetBlockHeader request"); + //datetime of of the most recent operation to retrieve + Date start = new Date(); + //datetime of the the earliest operation to retrieve + //07/16/2017 1:33pm + Date end = new Date(1500211991); + + System.out.println("Adding GetMarketHistory request"); try{ - nodeConnection.addRequestHandler(new GetBlockHeader(BlOCK_TEST_NUMBER, true, new WitnessResponseListener(){ + nodeConnection.addRequestHandler(new GetMarketHistory(asset_base, asset_quote, bucket, start, end, true, new WitnessResponseListener(){ @Override public void onSuccess(WitnessResponse response) { - System.out.println("GetBlockHeader.onSuccess"); + System.out.println("GetMarketHistory.onSuccess"); } @Override public void onError(BaseResponse.Error error) { - System.out.println("GetBlockHeader.onError. Msg: "+ error.message); + System.out.println("GetMarketHistory.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()); + } + } + + /** + * Test for GetObjects Handler. + * + * Request for a limit orders between two assets + */ + @Test + public void testGetObjectsRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(NODE_URL_1); + nodeConnection.connect("", "", false, mErrorListener); + + String asset_sold_id = BLOCKPAY.getBitassetId(); + String asset_purchased_id = BTS.getBitassetId(); + int limit = 10; + + ArrayList objectList = new ArrayList(){{ + add(BLOCKPAY.getBitassetId()); + add(BTS.getBitassetId()); + add(BITEURO.getBitassetId()); + }}; + + System.out.println("Adding GetObjects request"); + try{ + nodeConnection.addRequestHandler(new GetObjects(objectList, true, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("GetObjects.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("GetObjects.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()); + } + } + + /** + * Test for GetRelativeAccount Handler. + * + * Request for the transaction history of a user account. + */ + @Test + public void testGetRelativeAccountHistoryRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(NODE_URL_1); + nodeConnection.connect("", "", false, mErrorListener); + + UserAccount userAccount = new UserAccount(ACCOUNT_ID_1); + + //Sequence number of earliest operation + int stop = 10; + int limit = 50; + //Sequence number of the most recent operation to retrieve + int start = 50; + + System.out.println("Adding GetRelativeAccountHistory request"); + try{ + nodeConnection.addRequestHandler(new GetRelativeAccountHistory(userAccount, stop, limit, start, true, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("GetRelativeAccountHistory.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("GetRelativeAccountHistory.onError. Msg: "+ error.message); } })); }catch(RepeatedRequestIdException e){ @@ -588,7 +698,7 @@ public class NodeConnectionTest { } /** - * Test for GetRequiredFees Handler. + * Test for GetTradeHistory Handler. * */ @Test @@ -605,17 +715,17 @@ public class NodeConnectionTest { String asset_purchased_id = BTS.getBitassetId(); int limit = 10; - System.out.println("Adding GetRequiredFees request"); + System.out.println("Adding GetTradeHistory request"); try{ nodeConnection.addRequestHandler(new GetTradeHistory(asset_sold_id, asset_purchased_id, "", "", limit, true, new WitnessResponseListener(){ @Override public void onSuccess(WitnessResponse response) { - System.out.println("GetRequiredFees.onSuccess"); + System.out.println("GetTradeHistory.onSuccess"); } @Override public void onError(BaseResponse.Error error) { - System.out.println("GetRequiredFees.onError. Msg: "+ error.message); + System.out.println("GetTradeHistory.onError. Msg: "+ error.message); } })); }catch(RepeatedRequestIdException e){ From 99769713eb25f188ca53264f0e952b73e88199e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Mon, 24 Jul 2017 19:46:33 -0300 Subject: [PATCH 12/18] Update project to minSdkVersion 9 because of usage of Arrays.copyOfRange at Address.java, Memo.java and Util.java --- graphenej/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphenej/build.gradle b/graphenej/build.gradle index e4badbf..6cf8a8c 100644 --- a/graphenej/build.gradle +++ b/graphenej/build.gradle @@ -19,7 +19,7 @@ android { buildToolsVersion "25.0.0" defaultConfig { - minSdkVersion 3 + minSdkVersion 9 targetSdkVersion 24 versionCode 4 versionName "0.4.2" From 7b7fd6e74ae22ee8f2c45e07be741540267e6b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Mon, 24 Jul 2017 21:49:43 -0300 Subject: [PATCH 13/18] Update Memo unit test --- .../graphenej/objects/MemoTest.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/graphenej/src/test/java/de/bitsharesmunich/graphenej/objects/MemoTest.java b/graphenej/src/test/java/de/bitsharesmunich/graphenej/objects/MemoTest.java index 33cd015..ab684c3 100644 --- a/graphenej/src/test/java/de/bitsharesmunich/graphenej/objects/MemoTest.java +++ b/graphenej/src/test/java/de/bitsharesmunich/graphenej/objects/MemoTest.java @@ -3,6 +3,7 @@ package de.bitsharesmunich.graphenej.objects; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import de.bitsharesmunich.graphenej.Address; +import de.bitsharesmunich.graphenej.PublicKey; import de.bitsharesmunich.graphenej.Util; import de.bitsharesmunich.graphenej.errors.ChecksumException; import org.bitcoinj.core.DumpedPrivateKey; @@ -24,8 +25,12 @@ public class MemoTest { private Address destinationAddress; private long nonce; + //private String sourceWIF = "5HyF3A4rpW7fGLumk81eEsKz2YiTsVmR7ickPKtEhKUVgFaJjWb"; + //private String sourceWIF = "5HyF3A4rpW7fGLumk81eEsKz2YiTsVmR7ickPKtEhKUVgFaJjWb"; + private String sourceWIF = "5J96pne45qWM1WpektoeazN6k9Mt93jQ7LyueRxFfEMTiy6yxjM"; + private String destinationWIF = "5HuGQT8qwHScBgD4XsGbQUmXQF18MrbzxaQDiGGXFNRrCtqgT5Q"; private String shortMessage = "test"; - private String longerMessage = "testing now longer string!!"; + private String longerMessage = "testing now longer string with some special charaters é ç o ú á í Í mMno!!"; private byte[] shortEncryptedMessage = Util.hexToBytes("4c81c2db6ebc61e3f9e0ead65c0559dd"); private byte[] longerEncryptedMessage = Util.hexToBytes("1f8a08f1ff53dcefd48eeb052d26fba425f2a917f508ce61fc3d5696b10efa17"); @@ -34,12 +39,17 @@ public class MemoTest { @Before public void setUp() throws Exception { - sourcePrivate = DumpedPrivateKey.fromBase58(null, "5J96pne45qWM1WpektoeazN6k9Mt93jQ7LyueRxFfEMTiy6yxjM").getKey(); - destinationPrivate = DumpedPrivateKey.fromBase58(null, "5HuGQT8qwHScBgD4XsGbQUmXQF18MrbzxaQDiGGXFNRrCtqgT5Q").getKey(); + //Source + sourcePrivate = DumpedPrivateKey.fromBase58(null, sourceWIF).getKey(); + PublicKey publicKey = new PublicKey(ECKey.fromPublicOnly(sourcePrivate.getPubKey())); + sourceAddress = new Address(publicKey.getKey()); - sourceAddress = new Address("BTS8RiFgs8HkcVPVobHLKEv6yL3iXcC9SWjbPVS15dDAXLG9GYhnY"); - destinationAddress = new Address("BTS8ADjGaswhfFoxMGxqCdBtzhTBJsrGadCLoc9Ey5AGc8eoVZ5bV"); + //Destination + destinationPrivate = DumpedPrivateKey.fromBase58(null, destinationWIF).getKey(); + publicKey = new PublicKey(ECKey.fromPublicOnly(destinationPrivate.getPubKey())); + destinationAddress = new Address(publicKey.getKey()); + //memo.getNonce() nonce = 5; } @@ -56,7 +66,8 @@ public class MemoTest { public void shouldEncryptAndDecryptShortMessage(){ try { byte[] encrypted = Memo.encryptMessage(sourcePrivate, destinationAddress, nonce, shortMessage); - String decrypted = decrypted = Memo.decryptMessage(destinationPrivate, sourceAddress, nonce, encrypted); + String decrypted = Memo.decryptMessage(destinationPrivate, sourceAddress, nonce, encrypted); + System.out.println("Short Decrypted Message: " + decrypted); assertEquals("Decrypted message must be equal to original", decrypted, shortMessage); } catch (ChecksumException e) { e.printStackTrace(); @@ -68,6 +79,7 @@ public class MemoTest { try{ byte[] longEncrypted = Memo.encryptMessage(sourcePrivate, destinationAddress, nonce, longerMessage); String longDecrypted = Memo.decryptMessage(destinationPrivate, sourceAddress, nonce, longEncrypted); + System.out.println("Long Decrypted Message: " + longDecrypted); assertEquals("The longer message must be equal to the original", longerMessage, longDecrypted); } catch (ChecksumException e) { e.printStackTrace(); From 7c05b8d3dbf3ef7469f588738546e22950ac5ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Tue, 25 Jul 2017 15:41:33 -0300 Subject: [PATCH 14/18] Add ListAsset and GetLookupAccount API tests --- .../api/android/NodeConnectionTest.java | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) 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 9aa12a1..e69a786 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 @@ -23,6 +23,8 @@ import de.bitsharesmunich.graphenej.api.GetObjects; import de.bitsharesmunich.graphenej.api.GetRelativeAccountHistory; import de.bitsharesmunich.graphenej.api.GetRequiredFees; import de.bitsharesmunich.graphenej.api.GetTradeHistory; +import de.bitsharesmunich.graphenej.api.ListAssets; +import de.bitsharesmunich.graphenej.api.LookupAccounts; import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException; import de.bitsharesmunich.graphenej.errors.MalformedAddressException; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; @@ -743,6 +745,114 @@ public class NodeConnectionTest { } } + /** + * Test for ListAsset Handler. + *' + * Request the 'list_assets' API call to the witness node + */ + @Test + public void testListAssetRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(NODE_URL_1); + nodeConnection.connect("", "", false, mErrorListener); + + UserAccount userAccount_from = new UserAccount(ACCOUNT_ID_1); + UserAccount userAccount_to = new UserAccount(ACCOUNT_ID_2); + + + String asset_symbol = BLOCKPAY.getSymbol(); + int limit = 10; + + System.out.println("Adding ListAssets request"); + try{ + nodeConnection.addRequestHandler(new ListAssets(asset_symbol, limit, true, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("ListAssets.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("ListAssets.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()); + } + } + + /** + * Test for GetRelativeAccount Handler. + * + * Request for the transaction history of a user account. + */ + @Test + public void testGetLookupAccountsRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(NODE_URL_1); + nodeConnection.connect("", "", false, mErrorListener); + + UserAccount userAccount = new UserAccount(ACCOUNT_ID_1); + UserAccount userAccount_2 = new UserAccount(ACCOUNT_ID_2); + + //Sequence number of earliest operation + int maxAccounts = 10; + + System.out.println("Adding LookupAccounts request"); + try{ + nodeConnection.addRequestHandler(new LookupAccounts(userAccount.getName(), true, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("LookupAccounts.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("LookupAccounts.onError. Msg: "+ error.message); + } + })); + }catch(RepeatedRequestIdException e){ + System.out.println("RepeatedRequestIdException. Msg: "+e.getMessage()); + } + + System.out.println("Adding LookupAccounts request . maxAccounts = "+maxAccounts); + try{ + nodeConnection.addRequestHandler(new LookupAccounts(userAccount_2.getName(), maxAccounts, true, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("LookupAccounts.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("LookupAccounts.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() { From c66ed9968176a6416528a413e745298c0fefcfcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Tue, 25 Jul 2017 20:52:15 -0300 Subject: [PATCH 15/18] Add LookupAssetSymbols and TransactionBroadcastSequence API tests --- .../graphenej/api/LookupAssetSymbols.java | 2 +- .../api/android/NodeConnectionTest.java | 137 +++++++++++++++++- 2 files changed, 137 insertions(+), 2 deletions(-) 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 288c0e9..2a649d0 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java @@ -22,7 +22,7 @@ import de.bitsharesmunich.graphenej.models.WitnessResponse; * * Get the assets corresponding to the provided IDs. * - * The request returns the assets corresponding to the provided symbols or IDs. + * The response returns the assets corresponding to the provided symbols or IDs. * * @see lookup_asset_symbols API doc */ 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 e69a786..9e241ec 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 @@ -1,16 +1,27 @@ package de.bitsharesmunich.graphenej.api.android; +import org.bitcoinj.core.ECKey; +import org.junit.Assert; import org.junit.Test; +import java.io.IOException; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Timer; import java.util.TimerTask; import com.google.common.primitives.UnsignedLong; +import com.neovisionaries.ws.client.WebSocket; +import com.neovisionaries.ws.client.WebSocketException; +import com.neovisionaries.ws.client.WebSocketFactory; + +import javax.net.ssl.SSLContext; import de.bitsharesmunich.graphenej.Asset; +import de.bitsharesmunich.graphenej.BrainKey; import de.bitsharesmunich.graphenej.OperationType; +import de.bitsharesmunich.graphenej.Transaction; import de.bitsharesmunich.graphenej.api.GetAccounts; import de.bitsharesmunich.graphenej.api.GetAccountBalances; import de.bitsharesmunich.graphenej.api.GetAccountByName; @@ -25,6 +36,8 @@ import de.bitsharesmunich.graphenej.api.GetRequiredFees; import de.bitsharesmunich.graphenej.api.GetTradeHistory; import de.bitsharesmunich.graphenej.api.ListAssets; import de.bitsharesmunich.graphenej.api.LookupAccounts; +import de.bitsharesmunich.graphenej.api.LookupAssetSymbols; +import de.bitsharesmunich.graphenej.api.TransactionBroadcastSequence; import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException; import de.bitsharesmunich.graphenej.errors.MalformedAddressException; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; @@ -35,6 +48,8 @@ import de.bitsharesmunich.graphenej.UserAccount; import de.bitsharesmunich.graphenej.Address; import de.bitsharesmunich.graphenej.BaseOperation; import de.bitsharesmunich.graphenej.operations.TransferOperation; +import de.bitsharesmunich.graphenej.operations.TransferOperationBuilder; +import de.bitsharesmunich.graphenej.test.NaiveSSLContext; /** * Created by nelson on 6/26/17. @@ -44,6 +59,7 @@ public class NodeConnectionTest { private String NODE_URL_2 = System.getenv("NODE_URL_2"); private String NODE_URL_3 = System.getenv("NODE_URL_3"); private String NODE_URL_4 = System.getenv("NODE_URL_4"); + private String TEST_ACCOUNT_BRAIN_KEY = System.getenv("TEST_ACCOUNT_BRAIN_KEY"); private String ACCOUNT_ID_1 = System.getenv("ACCOUNT_ID_1"); private String ACCOUNT_ID_2 = System.getenv("ACCOUNT_ID_2"); private String ACCOUNT_NAME = System.getenv("ACCOUNT_NAME"); @@ -805,7 +821,6 @@ public class NodeConnectionTest { UserAccount userAccount = new UserAccount(ACCOUNT_ID_1); UserAccount userAccount_2 = new UserAccount(ACCOUNT_ID_2); - //Sequence number of earliest operation int maxAccounts = 10; System.out.println("Adding LookupAccounts request"); @@ -852,6 +867,126 @@ public class NodeConnectionTest { } } + /** + * Test for LookupAssetSymbols Handler. + * + * Request for the assets corresponding to the provided symbols or IDs. + */ + @Test + public void testLookupAssetSymbolsRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(NODE_URL_1); + nodeConnection.connect("", "", false, mErrorListener); + + + ArrayList assetList = new ArrayList(){{ + add(BLOCKPAY); + add(BTS); + add(BITEURO); + }}; + + System.out.println("Adding LookupAssetSymbols request"); + try{ + nodeConnection.addRequestHandler(new LookupAssetSymbols(assetList, true, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("LookupAssetSymbols.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("LookupAssetSymbols.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()); + } + } + + /** + * Test for TransactionBroadcastSequence Handler. + * + */ + @Test + public void testTransactionBroadcastSequenceRequest(){ + nodeConnection = NodeConnection.getInstance(); + nodeConnection.addNodeUrl(NODE_URL_1); + nodeConnection.connect("", "", false, mErrorListener); + + + ArrayList assetList = new ArrayList(){{ + add(BLOCKPAY); + add(BTS); + add(BITEURO); + }}; + + ECKey privateKey = new BrainKey(TEST_ACCOUNT_BRAIN_KEY, 0).getPrivateKey(); + + UserAccount userAccount = new UserAccount(ACCOUNT_ID_1); + UserAccount userAccount_2 = new UserAccount(ACCOUNT_ID_2); + + TransferOperation transferOperation1 = new TransferOperationBuilder() + .setTransferAmount(new AssetAmount(UnsignedLong.valueOf(1), BTS)) + .setSource(userAccount) + .setDestination(userAccount_2) + .setFee(new AssetAmount(UnsignedLong.valueOf(264174), BTS)) + .build(); + + + ArrayList operationList = new ArrayList<>(); + operationList.add(transferOperation1); + + try{ + Transaction transaction = new Transaction(privateKey, null, operationList); + + SSLContext context = null; + context = NaiveSSLContext.getInstance("TLS"); + WebSocketFactory factory = new WebSocketFactory(); + + // Set the custom SSL context. + factory.setSSLContext(context); + + WebSocket mWebSocket = factory.createSocket(NODE_URL_1); + + mWebSocket.addListener(new TransactionBroadcastSequence(transaction, BTS, new WitnessResponseListener(){ + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("TransactionBroadcastSequence.onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("TransactionBroadcastSequence.onError. Msg: "+ error.message); + } + })); + mWebSocket.connect(); + + try{ + // Holding this thread while we get update notifications + synchronized (this){ + wait(); + } + }catch(InterruptedException e){ + System.out.println("InterruptedException. Msg: "+e.getMessage()); + } + + }catch(NoSuchAlgorithmException e){ + System.out.println("NoSuchAlgoritmException. Msg: " + e.getMessage()); + } catch (IOException e) { + System.out.println("IOException. Msg: " + e.getMessage()); + } catch (WebSocketException e) { + System.out.println("WebSocketException. Msg: " + e.getMessage()); + } + } private WitnessResponseListener mErrorListener = new WitnessResponseListener() { From 5de20a7fc85cf19d877b3697000aaae0b2513439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Sat, 29 Jul 2017 02:27:21 -0300 Subject: [PATCH 16/18] Improve memo test --- .../graphenej/objects/MemoTest.java | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/graphenej/src/test/java/de/bitsharesmunich/graphenej/objects/MemoTest.java b/graphenej/src/test/java/de/bitsharesmunich/graphenej/objects/MemoTest.java index ab684c3..cdc7101 100644 --- a/graphenej/src/test/java/de/bitsharesmunich/graphenej/objects/MemoTest.java +++ b/graphenej/src/test/java/de/bitsharesmunich/graphenej/objects/MemoTest.java @@ -14,7 +14,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; /** - * Created by nelson on 12/19/16. + * Unit Tests for Memo Related Classes. */ public class MemoTest { @@ -25,10 +25,11 @@ public class MemoTest { private Address destinationAddress; private long nonce; - //private String sourceWIF = "5HyF3A4rpW7fGLumk81eEsKz2YiTsVmR7ickPKtEhKUVgFaJjWb"; - //private String sourceWIF = "5HyF3A4rpW7fGLumk81eEsKz2YiTsVmR7ickPKtEhKUVgFaJjWb"; - private String sourceWIF = "5J96pne45qWM1WpektoeazN6k9Mt93jQ7LyueRxFfEMTiy6yxjM"; - private String destinationWIF = "5HuGQT8qwHScBgD4XsGbQUmXQF18MrbzxaQDiGGXFNRrCtqgT5Q"; + private String sourceWIF = System.getenv("SOURCE_WIF"); + private String destinationWIF = System.getenv("DESTINATION_WIF"); + private byte[] memoEncryptedEnvMessage = Util.hexToBytes(System.getenv("MEMO_MESSAGE")); + //private String sourceWIF = "5J96pne45qWM1WpektoeazN6k9Mt93jQ7LyueRxFfEMTiy6yxjM"; + //private String destinationWIF = "5HuGQT8qwHScBgD4XsGbQUmXQF18MrbzxaQDiGGXFNRrCtqgT5Q"; private String shortMessage = "test"; private String longerMessage = "testing now longer string with some special charaters é ç o ú á í Í mMno!!"; @@ -62,6 +63,39 @@ public class MemoTest { assertArrayEquals("Testing with longer message and nonce 1", encryptedLong, longerEncryptedMessage); } + @Test + public void shouldDecryptEnvMessage(){ + try { + String decrypted = Memo.decryptMessage(destinationPrivate, sourceAddress, nonce, memoEncryptedEnvMessage); + System.out.println("Short Decrypted Message: " + decrypted); + assertEquals("Decrypted message must be equal to original", decrypted, shortMessage); + } catch (ChecksumException e) { + e.printStackTrace(); + } + } + + @Test + public void shouldDecryptShortMessage(){ + try { + String decrypted = Memo.decryptMessage(destinationPrivate, sourceAddress, nonce, shortEncryptedMessage); + System.out.println("Short Decrypted Message: " + decrypted); + assertEquals("Decrypted message must be equal to original", decrypted, shortMessage); + } catch (ChecksumException e) { + e.printStackTrace(); + } + } + + @Test + public void shouldDecryptLongerMessage(){ + try{ + String longDecrypted = Memo.decryptMessage(destinationPrivate, sourceAddress, nonce, longerEncryptedMessage); + System.out.println("Long Decrypted Message: " + longDecrypted); + assertEquals("The longer message must be equal to the original", longerMessage, longDecrypted); + } catch (ChecksumException e) { + e.printStackTrace(); + } + } + @Test public void shouldEncryptAndDecryptShortMessage(){ try { From eab6990d737a584ddb227b36e2bbe6fdf9d3725a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Mon, 31 Jul 2017 12:52:20 -0300 Subject: [PATCH 17/18] Fix memo --- .../bitsharesmunich/graphenej/operations/TransferOperation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/TransferOperation.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/TransferOperation.java index 85bf2d4..47d34d4 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/TransferOperation.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/TransferOperation.java @@ -195,4 +195,4 @@ public class TransferOperation extends BaseOperation { } } } -} +} \ No newline at end of file From 9b292d220ed014f1754eabed04a6334bc2eab312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius?= Date: Mon, 31 Jul 2017 15:24:13 -0300 Subject: [PATCH 18/18] Fix memo deserialization --- .../operations/TransferOperation.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/TransferOperation.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/TransferOperation.java index 47d34d4..c4af581 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/TransferOperation.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/TransferOperation.java @@ -1,6 +1,7 @@ package de.bitsharesmunich.graphenej.operations; import com.google.common.primitives.Bytes; +import com.google.common.primitives.UnsignedLong; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonDeserializationContext; @@ -13,10 +14,13 @@ import com.google.gson.JsonSerializer; import java.lang.reflect.Type; +import de.bitsharesmunich.graphenej.Address; import de.bitsharesmunich.graphenej.AssetAmount; import de.bitsharesmunich.graphenej.BaseOperation; import de.bitsharesmunich.graphenej.OperationType; import de.bitsharesmunich.graphenej.UserAccount; +import de.bitsharesmunich.graphenej.Util; +import de.bitsharesmunich.graphenej.errors.MalformedAddressException; import de.bitsharesmunich.graphenej.objects.Memo; /** @@ -167,6 +171,7 @@ public class TransferOperation extends BaseOperation { @Override public TransferOperation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + System.out.println("Deserialized bitch start. Msg: "+ json.getAsString()); if(json.isJsonArray()){ // This block is used just to check if we are in the first step of the deserialization // when we are dealing with an array. @@ -191,8 +196,25 @@ public class TransferOperation extends BaseOperation { UserAccount from = new UserAccount(jsonObject.get(KEY_FROM).getAsString()); UserAccount to = new UserAccount(jsonObject.get(KEY_TO).getAsString()); TransferOperation transfer = new TransferOperation(from, to, amount, fee); + + // Deserializing Memo if it exists + System.out.println("Deserialized bitch. Msg: "+ jsonObject.getAsString()); + if(jsonObject.get(KEY_MEMO) != null){ + JsonObject memoObj = jsonObject.get(KEY_MEMO).getAsJsonObject(); + try{ + Address memoFrom = new Address(memoObj.get(Memo.KEY_FROM).getAsString()); + Address memoTo = new Address(memoObj.get(KEY_TO).getAsString()); + long nonce = UnsignedLong.valueOf(memoObj.get(Memo.KEY_NONCE).getAsString()).longValue(); + byte[] message = Util.hexToBytes(memoObj.get(Memo.KEY_MESSAGE).getAsString()); + Memo memo = new Memo(memoFrom, memoTo, nonce, message); + transfer.setMemo(memo); + }catch(MalformedAddressException e){ + System.out.println("MalformedAddressException. Msg: "+e.getMessage()); + } + } + return transfer; } } } -} \ No newline at end of file +}