Introducing node hop feature to some blockchain API handler classes (GetAccountBalances, GetAccountByName, GetAccounts and GetAllAssetHolders)

This commit is contained in:
Vinícius 2017-07-18 15:30:22 -03:00
parent 184bbfafa3
commit 5369976f08
7 changed files with 193 additions and 102 deletions

1
.gitignore vendored
View file

@ -7,6 +7,7 @@
# Gradle # Gradle
# ------ # ------
.gradle .gradle
gradlew.bat
gradle gradle
graphenej/build graphenej/build
/build /build

90
gradlew.bat vendored
View file

@ -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

View file

@ -25,11 +25,20 @@ public class GetAccountBalances extends BaseGrapheneHandler {
private UserAccount mUserAccount; private UserAccount mUserAccount;
private List<Asset> mAssetList; private List<Asset> mAssetList;
private boolean mOneTime;
public GetAccountBalances(UserAccount userAccount, List<Asset> assets, WitnessResponseListener listener) { /*
* Constructor
*/
public GetAccountBalances(UserAccount userAccount, boolean oneTime, List<Asset> assets, WitnessResponseListener listener) {
super(listener); super(listener);
this.mUserAccount = userAccount; this.mUserAccount = userAccount;
this.mAssetList = assets; this.mAssetList = assets;
this.mOneTime = oneTime;
}
public GetAccountBalances(UserAccount userAccount, List<Asset> assets, WitnessResponseListener listener) {
this(userAccount, true, assets, listener);
} }
@Override @Override
@ -57,8 +66,10 @@ public class GetAccountBalances extends BaseGrapheneHandler {
Type WitnessResponseType = new TypeToken<WitnessResponse<List<AssetAmount>>>(){}.getType(); Type WitnessResponseType = new TypeToken<WitnessResponse<List<AssetAmount>>>(){}.getType();
WitnessResponse<List<AssetAmount>> witnessResponse = gsonBuilder.create().fromJson(response, WitnessResponseType); WitnessResponse<List<AssetAmount>> witnessResponse = gsonBuilder.create().fromJson(response, WitnessResponseType);
mListener.onSuccess(witnessResponse); mListener.onSuccess(witnessResponse);
if(mOneTime){
websocket.disconnect(); websocket.disconnect();
} }
}
@Override @Override
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {

View file

@ -26,13 +26,19 @@ public class GetAccountByName extends BaseGrapheneHandler {
private String accountName; private String accountName;
private WitnessResponseListener mListener; private WitnessResponseListener mListener;
private boolean mOneTime;
public GetAccountByName(String accountName, WitnessResponseListener listener){ public GetAccountByName(String accountName, boolean oneTime, WitnessResponseListener listener){
super(listener); super(listener);
this.accountName = accountName; this.accountName = accountName;
this.mOneTime = oneTime;
this.mListener = listener; this.mListener = listener;
} }
public GetAccountByName(String accountName, WitnessResponseListener listener){
this(accountName, true, listener);
}
@Override @Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception { public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<Serializable> accountParams = new ArrayList<>(); ArrayList<Serializable> accountParams = new ArrayList<>();
@ -58,9 +64,10 @@ public class GetAccountByName extends BaseGrapheneHandler {
}else{ }else{
this.mListener.onSuccess(witnessResponse); this.mListener.onSuccess(witnessResponse);
} }
if(mOneTime){
websocket.disconnect(); websocket.disconnect();
} }
}
@Override @Override
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {

View file

@ -28,22 +28,33 @@ public class GetAccounts extends BaseGrapheneHandler {
private String accountId; private String accountId;
private List<UserAccount> userAccounts; private List<UserAccount> userAccounts;
private WitnessResponseListener mListener; private WitnessResponseListener mListener;
private boolean oneTime; private boolean mOneTime;
/*
* Construtor
*/
public GetAccounts(String accountId, boolean oneTime, WitnessResponseListener listener){ public GetAccounts(String accountId, boolean oneTime, WitnessResponseListener listener){
super(listener); super(listener);
this.accountId = accountId; this.accountId = accountId;
this.oneTime = oneTime; this.mOneTime = oneTime;
this.mListener = listener; this.mListener = listener;
} }
public GetAccounts(List<UserAccount> accounts, boolean oneTime, WitnessResponseListener listener){ public GetAccounts(List<UserAccount> accounts, boolean oneTime, WitnessResponseListener listener){
super(listener); super(listener);
this.userAccounts = accounts; this.userAccounts = accounts;
this.oneTime = oneTime; this.mOneTime = oneTime;
this.mListener = listener; this.mListener = listener;
} }
public GetAccounts(String accountId, WitnessResponseListener listener){
this(accountId, true, listener);
}
public GetAccounts(List<UserAccount> accounts, WitnessResponseListener listener){
this(accounts, true, listener);
}
@Override @Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception { public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<Serializable> params = new ArrayList(); ArrayList<Serializable> params = new ArrayList();
@ -76,7 +87,7 @@ public class GetAccounts extends BaseGrapheneHandler {
} else { } else {
this.mListener.onSuccess(witnessResponse); this.mListener.onSuccess(witnessResponse);
} }
if(oneTime){ if(mOneTime){
websocket.disconnect(); websocket.disconnect();
} }
} }

View file

@ -26,10 +26,15 @@ public class GetAllAssetHolders extends BaseGrapheneHandler {
private int currentId = 1; private int currentId = 1;
private int assetApiId = -1; private int assetApiId = -1;
public GetAllAssetHolders(WitnessResponseListener listener) { private boolean mOneTime;
public GetAllAssetHolders(boolean oneTime, WitnessResponseListener listener) {
super(listener); super(listener);
this.mOneTime = oneTime;
} }
public GetAllAssetHolders(WitnessResponseListener listener) { this(true, listener);}
@Override @Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception { public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<Serializable> loginParams = new ArrayList<>(); ArrayList<Serializable> loginParams = new ArrayList<>();
@ -48,7 +53,9 @@ public class GetAllAssetHolders extends BaseGrapheneHandler {
BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class); BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
if(baseResponse.error != null){ if(baseResponse.error != null){
mListener.onError(baseResponse.error); mListener.onError(baseResponse.error);
if(mOneTime){
websocket.disconnect(); websocket.disconnect();
}
}else { }else {
currentId++; currentId++;
ArrayList<Serializable> emptyParams = new ArrayList<>(); ArrayList<Serializable> emptyParams = new ArrayList<>();
@ -68,7 +75,9 @@ public class GetAllAssetHolders extends BaseGrapheneHandler {
builder.registerTypeAdapter(AssetHolderCount.class, new AssetHolderCount.HoldersCountDeserializer()); builder.registerTypeAdapter(AssetHolderCount.class, new AssetHolderCount.HoldersCountDeserializer());
WitnessResponse<List<AssetHolderCount>> witnessResponse = builder.create().fromJson(response, AssetTokenHolders); WitnessResponse<List<AssetHolderCount>> witnessResponse = builder.create().fromJson(response, AssetTokenHolders);
mListener.onSuccess(witnessResponse); mListener.onSuccess(witnessResponse);
if(mOneTime){
websocket.disconnect(); websocket.disconnect();
}
}else{ }else{
System.out.println("current id: "+currentId); System.out.println("current id: "+currentId);
} }

View file

@ -2,20 +2,31 @@ package de.bitsharesmunich.graphenej.api.android;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import de.bitsharesmunich.graphenej.Asset;
import de.bitsharesmunich.graphenej.api.GetAccounts; 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.errors.RepeatedRequestIdException;
import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener;
import de.bitsharesmunich.graphenej.models.BaseResponse; import de.bitsharesmunich.graphenej.models.BaseResponse;
import de.bitsharesmunich.graphenej.models.WitnessResponse; import de.bitsharesmunich.graphenej.models.WitnessResponse;
import de.bitsharesmunich.graphenej.UserAccount;
/** /**
* Created by nelson on 6/26/17. * Created by nelson on 6/26/17.
*/ */
public class NodeConnectionTest { public class NodeConnectionTest {
private String BLOCK_PAY_DE = System.getenv("OPENLEDGER_EU"); 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 NodeConnection nodeConnection;
private TimerTask subscribeTask = new TimerTask() { 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<Asset> 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() { private TimerTask releaseTask = new TimerTask() {
@Override @Override
public void run() { 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() { private WitnessResponseListener mErrorListener = new WitnessResponseListener() {
@Override @Override