Implementating node hop at NodeConnection and tests

This commit is contained in:
Vinícius 2017-07-19 21:29:42 -03:00
parent f367eb0021
commit ad2e414548
14 changed files with 483 additions and 26 deletions

View file

@ -3,6 +3,10 @@ package de.bitsharesmunich.graphenej.api;
import com.neovisionaries.ws.client.WebSocket; import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketAdapter; import com.neovisionaries.ws.client.WebSocketAdapter;
import com.neovisionaries.ws.client.WebSocketException; 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.interfaces.WitnessResponseListener;
import de.bitsharesmunich.graphenej.models.BaseResponse; import de.bitsharesmunich.graphenej.models.BaseResponse;
@ -14,6 +18,7 @@ import de.bitsharesmunich.graphenej.models.BaseResponse;
public abstract class BaseGrapheneHandler extends WebSocketAdapter { public abstract class BaseGrapheneHandler extends WebSocketAdapter {
protected WitnessResponseListener mListener; protected WitnessResponseListener mListener;
protected NodeErrorListener mErrorListener;
/** /**
* The 'id' field of a message to the node. This is used in order to multiplex different messages * 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){ public BaseGrapheneHandler(WitnessResponseListener listener){
this.mListener = listener; this.mListener = listener;
} }
public BaseGrapheneHandler(NodeErrorListener listener){
this.mErrorListener = listener;
}
@Override @Override
public void onError(WebSocket websocket, WebSocketException cause) throws Exception { public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
System.out.println("onError. cause: "+cause.getMessage()); System.out.println("onError. cause: "+cause.getMessage());
mListener.onError(new BaseResponse.Error(cause.getMessage())); mErrorListener.onError(new BaseResponse.Error(cause.getMessage()));
websocket.disconnect(); websocket.disconnect();
} }
@ -44,7 +52,7 @@ public abstract class BaseGrapheneHandler extends WebSocketAdapter {
for (StackTraceElement element : cause.getStackTrace()){ for (StackTraceElement element : cause.getStackTrace()){
System.out.println(element.getFileName()+"#"+element.getClassName()+":"+element.getLineNumber()); 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(); websocket.disconnect();
} }

View file

@ -27,8 +27,6 @@ import de.bitsharesmunich.graphenej.models.WitnessResponse;
* The request returns account data that refer to the name. * The request returns account data that refer to the name.
* *
* @see <a href="https://goo.gl/w75qjV">get_account_by_name API doc</a> * @see <a href="https://goo.gl/w75qjV">get_account_by_name API doc</a>
*
* Created by nelson on 11/15/16.
*/ */
public class GetAccountByName extends BaseGrapheneHandler { public class GetAccountByName extends BaseGrapheneHandler {

View file

@ -21,8 +21,13 @@ import de.bitsharesmunich.graphenej.models.ApiCall;
import de.bitsharesmunich.graphenej.models.WitnessResponse; 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 <a href="https://goo.gl/r5RqKG">get_accounts API doc</a>
*/ */
public class GetAccounts extends BaseGrapheneHandler { public class GetAccounts extends BaseGrapheneHandler {
private String accountId; private String accountId;
@ -30,8 +35,14 @@ public class GetAccounts extends BaseGrapheneHandler {
private WitnessResponseListener mListener; private WitnessResponseListener mListener;
private boolean mOneTime; 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){ public GetAccounts(String accountId, boolean oneTime, WitnessResponseListener listener){
super(listener); super(listener);
@ -40,6 +51,15 @@ public class GetAccounts extends BaseGrapheneHandler {
this.mListener = listener; 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<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;
@ -47,10 +67,24 @@ public class GetAccounts extends BaseGrapheneHandler {
this.mListener = listener; 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){ public GetAccounts(String accountId, WitnessResponseListener listener){
this(accountId, true, 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<UserAccount> accounts, WitnessResponseListener listener){ public GetAccounts(List<UserAccount> accounts, WitnessResponseListener listener){
this(accounts, true, listener); this(accounts, true, listener);
} }

View file

@ -16,7 +16,13 @@ import java.util.List;
import java.util.Map; 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 <a href="https://goo.gl/AgTSLU">get_all_asset_holders API doc</a>
*/ */
public class GetAllAssetHolders extends BaseGrapheneHandler { public class GetAllAssetHolders extends BaseGrapheneHandler {
private final static int LOGIN_ID = 1; private final static int LOGIN_ID = 1;
@ -28,11 +34,26 @@ public class GetAllAssetHolders extends BaseGrapheneHandler {
private boolean mOneTime; 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) { public GetAllAssetHolders(boolean oneTime, WitnessResponseListener listener) {
super(listener); super(listener);
this.mOneTime = oneTime; 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);} public GetAllAssetHolders(WitnessResponseListener listener) { this(true, listener);}
@Override @Override

View file

@ -19,7 +19,13 @@ import de.bitsharesmunich.graphenej.models.BlockHeader;
import de.bitsharesmunich.graphenej.models.WitnessResponse; 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 <a href="https://goo.gl/qw1eeb">get_block_header API doc</a>
*/ */
public class GetBlockHeader extends BaseGrapheneHandler { public class GetBlockHeader extends BaseGrapheneHandler {
@ -36,6 +42,15 @@ public class GetBlockHeader extends BaseGrapheneHandler {
private boolean mOneTime; 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){ public GetBlockHeader(long blockNumber, boolean oneTime, WitnessResponseListener listener){
super(listener); super(listener);
this.blockNumber = blockNumber; this.blockNumber = blockNumber;
@ -43,6 +58,13 @@ public class GetBlockHeader extends BaseGrapheneHandler {
this.mListener = listener; 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){ public GetBlockHeader(long blockNumber, WitnessResponseListener listener){
this(blockNumber, true, listener); this(blockNumber, true, listener);
} }

View file

@ -19,7 +19,13 @@ import de.bitsharesmunich.graphenej.models.ApiCall;
import de.bitsharesmunich.graphenej.models.WitnessResponse; 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 <a href="https://goo.gl/np8CYF">get_key_references API doc</a>
*/ */
public class GetKeyReferences extends BaseGrapheneHandler { public class GetKeyReferences extends BaseGrapheneHandler {
@ -27,14 +33,32 @@ public class GetKeyReferences extends BaseGrapheneHandler {
private boolean mOneTime; 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){ public GetKeyReferences(Address address, boolean oneTime, WitnessResponseListener listener){
super(listener); super(listener);
addresses = new ArrayList<>(); addresses = new ArrayList<>();
addresses.add(address); addresses.add(address);
this.mOneTime = oneTime; 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<Address> addresses, boolean oneTime, WitnessResponseListener listener) { public GetKeyReferences(List<Address> addresses, boolean oneTime, WitnessResponseListener listener) {
super(listener); super(listener);
this.addresses = addresses; this.addresses = addresses;
@ -42,10 +66,26 @@ public class GetKeyReferences extends BaseGrapheneHandler {
this.mOneTime = oneTime; 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){ public GetKeyReferences(Address address, WitnessResponseListener listener){
this(address, true, 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<Address> addresses, WitnessResponseListener listener) { public GetKeyReferences(List<Address> addresses, WitnessResponseListener listener) {
this(addresses, true, listener); this(addresses, true, listener);
} }

View file

@ -30,7 +30,6 @@ import de.bitsharesmunich.graphenej.models.WitnessResponse;
* *
* @see <a href="https://goo.gl/5sRTRq">get_limit_orders API doc</a> * @see <a href="https://goo.gl/5sRTRq">get_limit_orders API doc</a>
* *
* Created by nelson on 1/5/17.
*/ */
public class GetLimitOrders extends BaseGrapheneHandler { public class GetLimitOrders extends BaseGrapheneHandler {
@ -50,12 +49,12 @@ public class GetLimitOrders extends BaseGrapheneHandler {
* @param limit maximum number of orders to retrieve * @param limit maximum number of orders to retrieve
* @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 * 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 * be implemented by the party interested in being notified about the success/failure
* of the transaction broadcast operation. * of the transaction broadcast operation.
*/ */
public GetLimitOrders(String a, String b, int limit, boolean oneTime, WitnessResponseListener mListener) { public GetLimitOrders(String a, String b, int limit, boolean oneTime, WitnessResponseListener listener) {
super(mListener); super(listener);
this.a = a; this.a = a;
this.b = b; this.b = b;
this.limit = limit; this.limit = limit;
@ -69,12 +68,12 @@ public class GetLimitOrders extends BaseGrapheneHandler {
* @param a id of asset being sold * @param a id of asset being sold
* @param b id of asset being purchased * @param b id of asset being purchased
* @param limit maximum number of orders to retrieve * @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 * be implemented by the party interested in being notified about the success/failure
* of the transaction broadcast operation. * of the transaction broadcast operation.
*/ */
public GetLimitOrders(String a, String b, int limit, WitnessResponseListener mListener) { public GetLimitOrders(String a, String b, int limit, WitnessResponseListener listener) {
this(a, b, limit, true, mListener); this(a, b, limit, true, listener);
} }
@Override @Override

View file

@ -23,7 +23,11 @@ import de.bitsharesmunich.graphenej.models.BucketObject;
import de.bitsharesmunich.graphenej.models.WitnessResponse; import de.bitsharesmunich.graphenej.models.WitnessResponse;
/** /**
* Created by nelson on 12/22/16. * Class that implements get_market_history request handler.
*
*
* @see <a href="https://goo.gl/hfVFBW">get_market_history API doc</a>
*
*/ */
public class GetMarketHistory extends BaseGrapheneHandler { public class GetMarketHistory extends BaseGrapheneHandler {
@ -46,6 +50,20 @@ public class GetMarketHistory extends BaseGrapheneHandler {
private boolean mOneTime; 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){ public GetMarketHistory(Asset base, Asset quote, long bucket, Date start, Date end, boolean oneTime, WitnessResponseListener listener){
super(listener); super(listener);
this.base = base; this.base = base;
@ -57,6 +75,17 @@ public class GetMarketHistory extends BaseGrapheneHandler {
this.mListener = listener; 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){ public GetMarketHistory(Asset base, Asset quote, long bucket, Date start, Date end, WitnessResponseListener listener){
this(base, quote, bucket, start, end, true, listener); this(base, quote, bucket, start, end, true, listener);
} }

View file

@ -23,7 +23,11 @@ import java.util.List;
import java.util.Map; 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 <a href="https://goo.gl/MB4TXq">get_required_fees API doc</a>
*/ */
public class GetRequiredFees extends WebSocketAdapter { public class GetRequiredFees extends WebSocketAdapter {
@ -33,6 +37,17 @@ public class GetRequiredFees extends WebSocketAdapter {
private boolean mOneTime; 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<BaseOperation> operations, Asset asset, boolean oneTime, WitnessResponseListener listener){ public GetRequiredFees(List<BaseOperation> operations, Asset asset, boolean oneTime, WitnessResponseListener listener){
this.operations = operations; this.operations = operations;
this.asset = asset; this.asset = asset;
@ -40,6 +55,15 @@ public class GetRequiredFees extends WebSocketAdapter {
this.mListener = listener; 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<BaseOperation> operations, Asset asset, WitnessResponseListener listener){ public GetRequiredFees(List<BaseOperation> operations, Asset asset, WitnessResponseListener listener){
this(operations, asset, true, listener); this(operations, asset, true, listener);
} }

View file

@ -18,6 +18,7 @@ import de.bitsharesmunich.graphenej.RPC;
import de.bitsharesmunich.graphenej.Transaction; import de.bitsharesmunich.graphenej.Transaction;
import de.bitsharesmunich.graphenej.UserAccount; import de.bitsharesmunich.graphenej.UserAccount;
import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException; import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException;
import de.bitsharesmunich.graphenej.interfaces.NodeErrorListener;
import de.bitsharesmunich.graphenej.interfaces.SubscriptionHub; import de.bitsharesmunich.graphenej.interfaces.SubscriptionHub;
import de.bitsharesmunich.graphenej.interfaces.SubscriptionListener; import de.bitsharesmunich.graphenej.interfaces.SubscriptionListener;
import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener;
@ -107,6 +108,48 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
this(user, password, false, 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 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 @Override
public void addSubscriptionListener(SubscriptionListener listener){ public void addSubscriptionListener(SubscriptionListener listener){
this.mSubscriptionDeserializer.addSubscriptionListener(listener); this.mSubscriptionDeserializer.addSubscriptionListener(listener);

View file

@ -6,7 +6,9 @@ import java.util.List;
import de.bitsharesmunich.graphenej.api.BaseGrapheneHandler; import de.bitsharesmunich.graphenej.api.BaseGrapheneHandler;
import de.bitsharesmunich.graphenej.api.SubscriptionMessagesHub; import de.bitsharesmunich.graphenej.api.SubscriptionMessagesHub;
import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException; import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException;
import de.bitsharesmunich.graphenej.interfaces.NodeErrorListener;
import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener;
import de.bitsharesmunich.graphenej.models.BaseResponse;
/** /**
* Created by nelson on 6/26/17. * Created by nelson on 6/26/17.
@ -24,9 +26,14 @@ public class NodeConnection {
private WebsocketWorkerThread mThread; private WebsocketWorkerThread mThread;
private SubscriptionMessagesHub mMessagesHub; private SubscriptionMessagesHub mMessagesHub;
private long requestCounter = SubscriptionMessagesHub.MANUAL_SUBSCRIPTION_ID + 1; private long requestCounter = SubscriptionMessagesHub.MANUAL_SUBSCRIPTION_ID + 1;
private WitnessResponseListener mErrorListener;
private static NodeConnection instance; 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. * 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 * @param url: URL of the node
*/ */
public void addNodeUrl(String url){ public void addNodeUrl(String url){
System.out.println("addNodeUrl: "+url);
this.mUrlList.add(url); this.mUrlList.add(url);
} }
@ -77,6 +85,15 @@ public class NodeConnection {
this.mUrlList.clear(); 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 * 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 * 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) { public void connect(String user, String password, boolean subscribe, WitnessResponseListener errorListener) {
if(this.mUrlList.size() > 0){ 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(); mUrlIndex = mUrlIndex + 1 % this.mUrlList.size();
mMessagesHub = new SubscriptionMessagesHub(user, password, subscribe, errorListener); mMessagesHub = new SubscriptionMessagesHub(user, password, subscribe, errorListener);

View file

@ -10,6 +10,8 @@ import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import de.bitsharesmunich.graphenej.interfaces.NodeErrorListener;
import de.bitsharesmunich.graphenej.models.BaseResponse;
import de.bitsharesmunich.graphenej.test.NaiveSSLContext; import de.bitsharesmunich.graphenej.test.NaiveSSLContext;
/** /**
@ -23,6 +25,7 @@ public class WebsocketWorkerThread extends Thread {
private final int TIMEOUT = 5000; private final int TIMEOUT = 5000;
private WebSocket mWebSocket; private WebSocket mWebSocket;
private NodeErrorListener mErrorListener;
public WebsocketWorkerThread(String url){ public WebsocketWorkerThread(String url){
try { 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 @Override
public void run() { public void run() {
try { try {
mWebSocket.connect(); mWebSocket.connect();
} catch (WebSocketException e) { } catch (WebSocketException e) {
System.out.println("WebSocketException. Msg: "+e.getMessage()); System.out.println("WebSocketException. Msg: "+e.getMessage());
mErrorListener.onError(new BaseResponse.Error(e.getMessage()));
} }
} }

View file

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

View file

@ -6,8 +6,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import com.google.common.primitives.UnsignedLong;
import de.bitsharesmunich.graphenej.Asset; import de.bitsharesmunich.graphenej.Asset;
import de.bitsharesmunich.graphenej.OperationType;
import de.bitsharesmunich.graphenej.api.GetAccounts; import de.bitsharesmunich.graphenej.api.GetAccounts;
import de.bitsharesmunich.graphenej.api.GetAccountBalances; import de.bitsharesmunich.graphenej.api.GetAccountBalances;
import de.bitsharesmunich.graphenej.api.GetAccountByName; 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.GetBlockHeader;
import de.bitsharesmunich.graphenej.api.GetKeyReferences; import de.bitsharesmunich.graphenej.api.GetKeyReferences;
import de.bitsharesmunich.graphenej.api.GetLimitOrders; import de.bitsharesmunich.graphenej.api.GetLimitOrders;
import de.bitsharesmunich.graphenej.api.GetRequiredFees;
import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException; import de.bitsharesmunich.graphenej.errors.RepeatedRequestIdException;
import de.bitsharesmunich.graphenej.errors.MalformedAddressException; import de.bitsharesmunich.graphenej.errors.MalformedAddressException;
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.AssetAmount;
import de.bitsharesmunich.graphenej.UserAccount; import de.bitsharesmunich.graphenej.UserAccount;
import de.bitsharesmunich.graphenej.Address; import de.bitsharesmunich.graphenej.Address;
import de.bitsharesmunich.graphenej.BaseOperation;
import de.bitsharesmunich.graphenej.operations.TransferOperation;
/** /**
* Created by nelson on 6/26/17. * 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_2 = System.getenv("NODE_URL_2");
private String NODE_URL_3 = System.getenv("NODE_URL_3"); private String NODE_URL_3 = System.getenv("NODE_URL_3");
private String NODE_URL_4 = System.getenv("NODE_URL_4"); 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 String ACCOUNT_NAME = System.getenv("ACCOUNT_NAME");
private long BlOCK_TEST_NUMBER = Long.parseLong(System.getenv("BlOCK_TEST_NUMBER")); private long BlOCK_TEST_NUMBER = Long.parseLong(System.getenv("BlOCK_TEST_NUMBER"));
private Asset BTS = new Asset("1.3.0"); private Asset BTS = new Asset("1.3.0");
@ -102,12 +109,13 @@ public class NodeConnectionTest {
*/ */
public void testNodeHopFeature(){ public void testNodeHopFeature(){
nodeConnection = NodeConnection.getInstance(); nodeConnection = NodeConnection.getInstance();
nodeConnection.addNodeUrl(NODE_URL_4); //nodeConnection.addNodeUrl(NODE_URL_4);
//Test adding a "sublist" //Test adding a "sublist"
ArrayList<String> urlList = new ArrayList<String>(){{ ArrayList<String> urlList = new ArrayList<String>(){{
add(NODE_URL_3); add(NODE_URL_3);
add(NODE_URL_3); add(NODE_URL_3);
}}; }};
//nodeConnection.addNodeUrls(urlList);
nodeConnection.addNodeUrl(NODE_URL_1); nodeConnection.addNodeUrl(NODE_URL_1);
nodeConnection.connect("", "", true, mErrorListener); nodeConnection.connect("", "", true, mErrorListener);
@ -129,7 +137,7 @@ public class NodeConnectionTest {
/** /**
* Test for GetAccountBalances Handler. * 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 @Test
@ -140,7 +148,7 @@ public class NodeConnectionTest {
System.out.println("Adding GetAccountBalances here"); System.out.println("Adding GetAccountBalances here");
try{ try{
UserAccount userAccount = new UserAccount(ACCOUNT_ID); UserAccount userAccount = new UserAccount(ACCOUNT_ID_1);
ArrayList<Asset> assetList = new ArrayList<>(); ArrayList<Asset> assetList = new ArrayList<>();
assetList.add(BTS); assetList.add(BTS);
assetList.add(BITDOLAR); assetList.add(BITDOLAR);
@ -162,7 +170,7 @@ public class NodeConnectionTest {
} }
try{ try{
UserAccount userAccount = new UserAccount(ACCOUNT_ID); UserAccount userAccount = new UserAccount(ACCOUNT_ID_1);
System.out.println("Test: Request to all account' assets balance"); System.out.println("Test: Request to all account' assets balance");
nodeConnection.addRequestHandler(new GetAccountBalances(userAccount, null, false, new WitnessResponseListener(){ nodeConnection.addRequestHandler(new GetAccountBalances(userAccount, null, false, new WitnessResponseListener(){
@Override @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<UserAccount> accountList = new ArrayList<UserAccount>(){{
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 @Test
public void testGetAllAssetHoldersRequest(){ public void testGetAllAssetHoldersRequest(){
nodeConnection = NodeConnection.getInstance(); 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 @Test
public void testGetBlockHeaderRequest(){ public void testGetBlockHeaderRequest(){
nodeConnection = NodeConnection.getInstance(); nodeConnection = NodeConnection.getInstance();
@ -297,6 +378,10 @@ public class NodeConnectionTest {
} }
} }
/**
* Test for GetKeyReferences Handler.
*
*/
@Test @Test
public void testGetKeyReferencesRequest(){ public void testGetKeyReferencesRequest(){
nodeConnection = NodeConnection.getInstance(); 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<BaseOperation> 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() { private WitnessResponseListener mErrorListener = new WitnessResponseListener() {