Introducing node hop feature to some blockchain API handler classes (GetBlockHeader, GetKeyReferences, GetLimitOrders, GetMarketHistory, GetObjects, GetRelativeAccountHistory)

This commit is contained in:
Vinícius 2017-07-18 22:07:07 -03:00
parent 5369976f08
commit 7313279f95
7 changed files with 211 additions and 113 deletions

View file

@ -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<String, List<String>> headers) throws Exception {
ArrayList<Serializable> 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);
if(mOneTime){
websocket.disconnect();
}
}else {
currentId++;
ArrayList<Serializable> emptyParams = new ArrayList<>();
@ -80,9 +89,11 @@ public class GetBlockHeader extends BaseGrapheneHandler {
Type RelativeAccountHistoryResponse = new TypeToken<WitnessResponse<BlockHeader>>(){}.getType();
WitnessResponse<BlockHeader> transfersResponse = gson.fromJson(response, RelativeAccountHistoryResponse);
mListener.onSuccess(transfersResponse);
if(mOneTime){
websocket.disconnect();
}
}
}
}

View file

@ -25,17 +25,29 @@ public class GetKeyReferences extends BaseGrapheneHandler {
private List<Address> 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<Address> addresses, WitnessResponseListener listener) {
public GetKeyReferences(List<Address> 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<Address> addresses, WitnessResponseListener listener) {
this(addresses, true, listener);
}
@Override
@ -64,8 +76,10 @@ public class GetKeyReferences extends BaseGrapheneHandler {
} else {
this.mListener.onSuccess(witnessResponse);
}
if(mOneTime){
websocket.disconnect();
}
}
@Override
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {

View file

@ -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<String, List<String>> headers) throws Exception {
ArrayList<Serializable> accountParams = new ArrayList<>();
@ -70,8 +78,10 @@ public class GetLimitOrders extends BaseGrapheneHandler {
} catch (Exception e) {
e.printStackTrace();
}
if(mOneTime){
websocket.disconnect();
}
}
@Override
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {
@ -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()));
if(mOneTime){
websocket.disconnect();
}
}
@Override
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
mListener.onError(new BaseResponse.Error(cause.getMessage()));
if(mOneTime){
websocket.disconnect();
}
}
}

View file

@ -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);
if(mOneTime){
websocket.disconnect();
}
}else{
currentId++;
ArrayList<Serializable> emptyParams = new ArrayList<>();

View file

@ -33,9 +33,16 @@ import de.bitsharesmunich.graphenej.models.WitnessResponse;
public class GetObjects extends BaseGrapheneHandler {
private List<String> ids;
public GetObjects(List<String> ids, WitnessResponseListener listener){
private boolean mOneTime;
public GetObjects(List<String> ids, boolean oneTime, WitnessResponseListener listener){
super(listener);
this.ids = ids;
this.mOneTime = oneTime;
}
public GetObjects(List<String> ids, WitnessResponseListener listener){
this(ids, true, listener);
}
@Override
@ -92,8 +99,10 @@ public class GetObjects extends BaseGrapheneHandler {
WitnessResponse<List<GrapheneObject>> output = new WitnessResponse<>();
output.result = parsedResult;
mListener.onSuccess(output);
if(mOneTime){
websocket.disconnect();
}
}
@Override
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {

View file

@ -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);
if(mOneTime){
websocket.disconnect();
}
}else{
currentId++;
ArrayList<Serializable> 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();
}
}

View file

@ -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<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() {
@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<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());
}
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