diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java index 94e3b02..dcc2bf3 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java @@ -351,10 +351,9 @@ public class NetworkService extends Service { } /** - * Public method that can be called from classes that bind to the service and find out that - * for any reason want the service to connect to a different node. + * Used to close the current connection and cause the service to attempt a reconnection. */ - public void removeCurrentNodeAndReconnect() { + public void reconnectNode() { mWebSocket.close(GOING_AWAY_STATUS, null); } @@ -669,10 +668,8 @@ public class NetworkService extends Service { @Override public void onClosed(WebSocket webSocket, int code, String reason) { super.onClosed(webSocket, code, reason); - Log.d(TAG,"onClosed"); - - if (code == GOING_AWAY_STATUS) - handleWebSocketDisconnection(true, true); + if(code == GOING_AWAY_STATUS) + handleWebSocketDisconnection(true, false); else handleWebSocketDisconnection(false, false); } @@ -691,7 +688,7 @@ public class NetworkService extends Service { Log.e(TAG,"Response: "+response.message()); } - handleWebSocketDisconnection(true, false); + handleWebSocketDisconnection(true, true); } /** @@ -699,9 +696,10 @@ public class NetworkService extends Service { * potentially tries to reconnect to another one. * * @param tryReconnection States if a reconnection to other node should be tried. - * @param removeSelectedNode States if the current node should be removed from the nodes list. + * @param penalizeNode Whether or not to penalize the current node with a very high latency reading. */ - private void handleWebSocketDisconnection(boolean tryReconnection, boolean removeSelectedNode) { + private void handleWebSocketDisconnection(boolean tryReconnection, boolean penalizeNode) { + Log.d(TAG,"handleWebSocketDisconnection. try reconnection: " + tryReconnection + ", penalizeNode: " + penalizeNode); RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE)); isLoggedIn = false; @@ -716,6 +714,13 @@ public class NetworkService extends Service { // Updating the selected node's 'connected' status on the NodeLatencyVerifier instance if(nodeLatencyVerifier != null) nodeLatencyVerifier.updateActiveNodeInformation(mSelectedNode); + + if (penalizeNode){ + // Adding a very high latency value to this node in order to prevent + // us from getting it again + mSelectedNode.addLatencyValue(Long.MAX_VALUE); + nodeProvider.updateNode(mSelectedNode); + } } if(tryReconnection) { @@ -723,20 +728,6 @@ public class NetworkService extends Service { mCurrentId = 0; mApiIds.clear(); - if (removeSelectedNode && mSelectedNode != null) { - // Remove node from node provider so that it is not returned for following connections - nodeProvider.removeNode(mSelectedNode); - - // Remove node from nodeLatencyVerifier, so that it publishes its removal - nodeLatencyVerifier.removeNode(mSelectedNode); - // Avoid crash #133 - } else if (mSelectedNode != null){ - // Adding a very high latency value to this node in order to prevent - // us from getting it again - mSelectedNode.addLatencyValue(Long.MAX_VALUE); - nodeProvider.updateNode(mSelectedNode); - } - RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE)); if (nodeProvider.getBestNode() == null) { @@ -746,7 +737,6 @@ public class NetworkService extends Service { mHandler.postDelayed(new Runnable() { @Override public void run() { - Log.d(TAG, "Retrying"); connect(); } }, DEFAULT_RETRY_DELAY); diff --git a/sample/src/main/java/cy/agorise/labs/sample/CallsActivity.java b/sample/src/main/java/cy/agorise/labs/sample/CallsActivity.java index 086321b..00e679b 100644 --- a/sample/src/main/java/cy/agorise/labs/sample/CallsActivity.java +++ b/sample/src/main/java/cy/agorise/labs/sample/CallsActivity.java @@ -25,7 +25,7 @@ import io.reactivex.functions.Consumer; public class CallsActivity extends AppCompatActivity { private final String TAG = this.getClass().getName(); - private static final String REMOVE_CURRENT_NODE = "remove_current_node"; + private static final String RECONNECT_NODE = "reconnect_node"; private static final String TEST_BRAINKEY_DERIVATION = "test_brainkey_derivation"; @BindView(R.id.call_list) @@ -82,7 +82,7 @@ public class CallsActivity extends AppCompatActivity { RPC.CALL_GET_ACCOUNT_BALANCES, RPC.CALL_BROADCAST_TRANSACTION, RPC.CALL_GET_TRANSACTION, - REMOVE_CURRENT_NODE, + RECONNECT_NODE, TEST_BRAINKEY_DERIVATION }; @@ -104,7 +104,7 @@ public class CallsActivity extends AppCompatActivity { Intent intent; if(selectedCall.equals(RPC.CALL_SET_SUBSCRIBE_CALLBACK)){ intent = new Intent(CallsActivity.this, SubscriptionActivity.class); - } else if (selectedCall.equals(REMOVE_CURRENT_NODE)){ + } else if (selectedCall.equals(RECONNECT_NODE)){ intent = new Intent(CallsActivity.this, RemoveNodeActivity.class); } else if (selectedCall.equals(TEST_BRAINKEY_DERIVATION)){ intent = new Intent(CallsActivity.this, BrainkeyActivity.class); diff --git a/sample/src/main/java/cy/agorise/labs/sample/RemoveNodeActivity.java b/sample/src/main/java/cy/agorise/labs/sample/RemoveNodeActivity.java index 2ab9954..db7f861 100644 --- a/sample/src/main/java/cy/agorise/labs/sample/RemoveNodeActivity.java +++ b/sample/src/main/java/cy/agorise/labs/sample/RemoveNodeActivity.java @@ -68,9 +68,9 @@ public class RemoveNodeActivity extends AppCompatActivity implements ServiceConn rvNodes.setAdapter(nodesAdapter); } - @OnClick(R.id.btnRemoveCurrentNode) + @OnClick(R.id.btnReconnectNode) public void removeCurrentNode() { - mNetworkService.removeCurrentNodeAndReconnect(); + mNetworkService.reconnectNode(); } @Override diff --git a/sample/src/main/java/cy/agorise/labs/sample/SampleApplication.java b/sample/src/main/java/cy/agorise/labs/sample/SampleApplication.java index 7127a33..4039e61 100644 --- a/sample/src/main/java/cy/agorise/labs/sample/SampleApplication.java +++ b/sample/src/main/java/cy/agorise/labs/sample/SampleApplication.java @@ -25,7 +25,9 @@ public class SampleApplication extends Application { String[] nodeURLs = new String[]{ "wss://bitshares.openledger.info/ws", "wss://us.nodes.bitshares.ws", - "wss://eu.nodes.bitshares.ws" + "wss://eu.nodes.bitshares.ws", + "wss://citadel.li/node", + "wss://api.bts.mobi/ws" }; List nodeList = Arrays.asList(nodeURLs); String nodes = join(nodeList, ","); diff --git a/sample/src/main/res/layout/activity_remove_node.xml b/sample/src/main/res/layout/activity_remove_node.xml index a1cb381..2a9af76 100644 --- a/sample/src/main/res/layout/activity_remove_node.xml +++ b/sample/src/main/res/layout/activity_remove_node.xml @@ -10,14 +10,14 @@ android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" - app:layout_constraintBottom_toTopOf="@+id/btnRemoveCurrentNode" + app:layout_constraintBottom_toTopOf="@+id/btnReconnectNode" tools:listitem="@layout/item_node"/>