Adding node hopping capabilities to the NodeConnection class along with some tests
This commit is contained in:
parent
69ccbe8cd9
commit
3e96bbc9b3
3 changed files with 47 additions and 31 deletions
|
@ -67,8 +67,7 @@ public class NodeConnection {
|
||||||
* @param urlList: List of URLs of the nodes
|
* @param urlList: List of URLs of the nodes
|
||||||
*/
|
*/
|
||||||
public void addNodeUrls(List<String> urlList){
|
public void addNodeUrls(List<String> urlList){
|
||||||
List<String> newList = new ArrayList<String>(mUrlList);
|
mUrlList.addAll(urlList);
|
||||||
newList.addAll(urlList);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -91,7 +90,7 @@ public class NodeConnection {
|
||||||
@Override
|
@Override
|
||||||
public void onError(BaseResponse.Error error) {
|
public void onError(BaseResponse.Error error) {
|
||||||
System.out.println("NodeConnect Error. Msg: "+error);
|
System.out.println("NodeConnect Error. Msg: "+error);
|
||||||
|
mUrlIndex++;
|
||||||
connect(mUser, mPassword, mSubscribe, mErrorListener);
|
connect(mUser, mPassword, mSubscribe, mErrorListener);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -114,18 +113,23 @@ public class NodeConnection {
|
||||||
* about the failure of the desired broadcast operation.
|
* about the failure of the desired broadcast operation.
|
||||||
*/
|
*/
|
||||||
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(mUrlList.size() > 0){
|
||||||
|
if(mUrlIndex < mUrlList.size()){
|
||||||
|
System.out.println("Connecting to: "+ this.mUrlList.get(mUrlIndex));
|
||||||
mUser = user;
|
mUser = user;
|
||||||
mPassword = password;
|
mPassword = password;
|
||||||
mSubscribe = subscribe;
|
mSubscribe = subscribe;
|
||||||
System.out.println("Connecting to: "+ this.mUrlList.get(mUrlIndex));
|
|
||||||
mErrorListener = errorListener;
|
mErrorListener = errorListener;
|
||||||
mThread = new WebsocketWorkerThread(this.mUrlList.get(mUrlIndex), mInternalErrorListener);
|
mThread = new WebsocketWorkerThread(this.mUrlList.get(mUrlIndex), mInternalErrorListener);
|
||||||
mUrlIndex = mUrlIndex + 1 % this.mUrlList.size();
|
|
||||||
|
|
||||||
mMessagesHub = new SubscriptionMessagesHub(user, password, subscribe, mInternalErrorListener);
|
mMessagesHub = new SubscriptionMessagesHub(user, password, subscribe, mInternalErrorListener);
|
||||||
mThread.addListener(mMessagesHub);
|
mThread.addListener(mMessagesHub);
|
||||||
mThread.start();
|
mThread.start();
|
||||||
|
}else{
|
||||||
|
errorListener.onError(new BaseResponse.Error("Can't connect, ran out of URLs!"));
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
errorListener.onError(new BaseResponse.Error("Can't connect, missing URL"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,7 @@ public class WebsocketWorkerThread extends Thread {
|
||||||
* about the failure of the connection.
|
* about the failure of the connection.
|
||||||
*/
|
*/
|
||||||
public WebsocketWorkerThread(String url, NodeErrorListener errorListener){
|
public WebsocketWorkerThread(String url, NodeErrorListener errorListener){
|
||||||
|
mErrorListener = errorListener;
|
||||||
try {
|
try {
|
||||||
WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(TIMEOUT);
|
WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(TIMEOUT);
|
||||||
|
|
||||||
|
@ -74,13 +75,18 @@ public class WebsocketWorkerThread extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
mWebSocket = factory.createSocket(url);
|
mWebSocket = factory.createSocket(url);
|
||||||
mErrorListener = errorListener;
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("IOException. Msg: "+e.getMessage());
|
System.out.println("IOException. Msg: "+e.getMessage());
|
||||||
|
mErrorListener.onError(new BaseResponse.Error(e.getMessage()));
|
||||||
} catch(NullPointerException e){
|
} catch(NullPointerException e){
|
||||||
System.out.println("NullPointerException at WebsocketWorkerThreas. Msg: "+e.getMessage());
|
System.out.println("NullPointerException at WebsocketWorkerThreas. Msg: "+e.getMessage());
|
||||||
|
mErrorListener.onError(new BaseResponse.Error(e.getMessage()));
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
System.out.println("NoSuchAlgorithmException. Msg: "+e.getMessage());
|
System.out.println("NoSuchAlgorithmException. Msg: "+e.getMessage());
|
||||||
|
mErrorListener.onError(new BaseResponse.Error(e.getMessage()));
|
||||||
|
} catch(IllegalArgumentException e){
|
||||||
|
System.out.println("IllegalArgumentException. Msg: "+e.getMessage());
|
||||||
|
mErrorListener.onError(new BaseResponse.Error(e.getMessage()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,17 +58,20 @@ public class NodeConnectionTest {
|
||||||
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 TEST_ACCOUNT_BRAIN_KEY = System.getenv("TEST_ACCOUNT_BRAIN_KEY");
|
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_1 = "1.2.140994";
|
||||||
private String ACCOUNT_ID_2 = System.getenv("ACCOUNT_ID_2");
|
private String ACCOUNT_ID_2 = "1.2.138632";
|
||||||
private String ACCOUNT_NAME = System.getenv("ACCOUNT_NAME");
|
private String ACCOUNT_NAME = "bilthon-7";
|
||||||
private long BlOCK_TEST_NUMBER = Long.parseLong(System.getenv("BlOCK_TEST_NUMBER"));
|
private long BlOCK_TEST_NUMBER = 11000000;
|
||||||
private Asset BTS = new Asset("1.3.0");
|
private Asset BTS = new Asset("1.3.0");
|
||||||
private Asset BLOCKPAY = new Asset("1.3.1072");
|
private Asset BLOCKPAY = new Asset("1.3.1072");
|
||||||
private Asset BITDOLAR = new Asset("1.3.121"); //USD Smartcoin
|
private Asset BITDOLAR = new Asset("1.3.121"); //USD Smartcoin
|
||||||
private Asset BITEURO = new Asset("1.3.120"); //EUR Smartcoin
|
private Asset BITEURO = new Asset("1.3.120"); //EUR Smartcoin
|
||||||
private NodeConnection nodeConnection;
|
private NodeConnection nodeConnection;
|
||||||
|
|
||||||
private TimerTask scheduleTask = new TimerTask() {
|
/**
|
||||||
|
* Sample task to be scheduled
|
||||||
|
*/
|
||||||
|
private TimerTask getAccountsTask = new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
System.out.println("Adding request here");
|
System.out.println("Adding request here");
|
||||||
|
@ -91,6 +94,9 @@ public class NodeConnectionTest {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Task that will release the worker thread, effectively terminating this test
|
||||||
|
*/
|
||||||
private TimerTask releaseTask = new TimerTask() {
|
private TimerTask releaseTask = new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
@ -103,12 +109,13 @@ public class NodeConnectionTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNodeConnection(){
|
public void testNodeConnection(){
|
||||||
|
System.out.println("** Testing simple node connection **");
|
||||||
nodeConnection = NodeConnection.getInstance();
|
nodeConnection = NodeConnection.getInstance();
|
||||||
nodeConnection.addNodeUrl(NODE_URL_1);
|
nodeConnection.addNodeUrl(NODE_URL_1);
|
||||||
nodeConnection.connect("", "", true, mErrorListener);
|
nodeConnection.connect("", "", true, mErrorListener);
|
||||||
|
|
||||||
Timer timer = new Timer();
|
Timer timer = new Timer();
|
||||||
timer.schedule(scheduleTask, 5000);
|
timer.schedule(getAccountsTask, 5000);
|
||||||
timer.schedule(releaseTask, 30000);
|
timer.schedule(releaseTask, 30000);
|
||||||
|
|
||||||
try{
|
try{
|
||||||
|
@ -121,30 +128,29 @@ public class NodeConnectionTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
/**
|
/**
|
||||||
* Test for NodeConnection's addNodeUrl and addNodeUrls working together.
|
* 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
|
* Need to setup the NODE_URL_2 env to work. The first hard-coded URL is wrong and will
|
||||||
* websockets URL just to test the hop.
|
* fail. The NodeConnection instance should recover and try the next one in the list.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@Test
|
||||||
public void testNodeHopFeature(){
|
public void testNodeHopFeature(){
|
||||||
|
System.out.println("** Testing node hopping **");
|
||||||
nodeConnection = NodeConnection.getInstance();
|
nodeConnection = NodeConnection.getInstance();
|
||||||
//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("wss://eu.openledger.info/wrong");
|
||||||
add(NODE_URL_3);
|
add(NODE_URL_2);
|
||||||
}};
|
}};
|
||||||
//nodeConnection.addNodeUrls(urlList);
|
nodeConnection.addNodeUrls(urlList);
|
||||||
nodeConnection.addNodeUrl(NODE_URL_1);
|
|
||||||
|
|
||||||
nodeConnection.connect("", "", true, mErrorListener);
|
nodeConnection.connect("", "", true, mErrorListener);
|
||||||
|
|
||||||
Timer timer = new Timer();
|
Timer timer = new Timer();
|
||||||
timer.schedule(scheduleTask, 5000);
|
timer.schedule(releaseTask, 15000);
|
||||||
timer.schedule(releaseTask, 30000);
|
|
||||||
|
|
||||||
try{
|
try{
|
||||||
// Holding this thread while we get update notifications
|
// Holding this thread while we get update notifications
|
||||||
|
|
Loading…
Reference in a new issue