From 66ce8a4ba42e795b82fb443cc53465c3135f1976 Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Thu, 26 Jul 2018 17:13:55 -0500 Subject: [PATCH] - Modified the ConnectionStatusUpdate class in order to allow it to send more than just connection/disconnection events - Allowing the NetworkService to be queried about specific API ids --- .../graphenej/api/ConnectionStatusUpdate.java | 61 ++++++++++++++++--- .../graphenej/api/android/NetworkService.java | 29 +++++++-- .../graphenej/api/calls/ListAssets.java | 2 +- 3 files changed, 77 insertions(+), 15 deletions(-) diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/ConnectionStatusUpdate.java b/graphenej/src/main/java/cy/agorise/graphenej/api/ConnectionStatusUpdate.java index b58b1bd..15c5a82 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/api/ConnectionStatusUpdate.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/ConnectionStatusUpdate.java @@ -1,24 +1,65 @@ package cy.agorise.graphenej.api; /** - * Class used to send connection status updates + * Class used to send connection status updates. + * + * Connection status updates can be any of the following: + * - {@link ConnectionStatusUpdate#CONNECTED} + * - {@link ConnectionStatusUpdate#AUTHENTICATED} + * - {@link ConnectionStatusUpdate#API_UPDATE} + * - {@link ConnectionStatusUpdate#DISCONNECTED} + * + * This is specified by the field called {@link #updateCode}. + * + * If the updateCode is ConnectionStatusUpdate#API_UPDATE another extra field called + * {@link #api} is used to specify which api we're getting access to. */ public class ConnectionStatusUpdate { - public final static String CONNECTED = "Connected"; - public final static String DISCONNECTED = "Disconnected"; + // Constant used to announce that a connection has been established + public final static int CONNECTED = 0; + // Constant used to announce a successful authentication + public final static int AUTHENTICATED = 1; + // Constant used to announce an api update + public final static int API_UPDATE = 2; + // Constant used to announce a disconnection event + public final static int DISCONNECTED = 3; - private String connectionStatus; + /** + * The update code is the general purpose of the update message. Can be any of the following: + * - {@link ConnectionStatusUpdate#CONNECTED} + * - {@link ConnectionStatusUpdate#AUTHENTICATED} + * - {@link ConnectionStatusUpdate#API_UPDATE} + * - {@link ConnectionStatusUpdate#DISCONNECTED} + */ + private int updateCode; - public ConnectionStatusUpdate(String status){ - this.connectionStatus = status; + /** + * This field is used in case the updateCode is {@link ConnectionStatusUpdate#API_UPDATE} and + * it serves to specify which API we're getting access to. + * + * It can be any of the fields defined in {@link ApiAccess} + */ + private int api; + + public ConnectionStatusUpdate(int updateCode, int api){ + this.updateCode = updateCode; + this.api = api; } - public String getConnectionStatus() { - return connectionStatus; + public int getUpdateCode() { + return updateCode; } - public void setConnectionStatus(String connectionStatus) { - this.connectionStatus = connectionStatus; + public void setUpdateCode(int updateCode) { + this.updateCode = updateCode; + } + + public int getApi() { + return api; + } + + public void setApi(int api) { + this.api = api; } } 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 8fd4355..f9dadf9 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 @@ -89,7 +89,7 @@ public class NetworkService extends Service { private int mRequestedApis; // Variable used to keep track of the currently obtained API accesses - private HashMap mApiIds = new HashMap(); + private HashMap mApiIds = new HashMap(); private ArrayList mNodeUrls = new ArrayList<>(); @@ -217,7 +217,7 @@ public class NetworkService extends Service { mWebSocket = webSocket; // Notifying all listeners about the new connection status - RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.CONNECTED)); + RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.CONNECTED, ApiAccess.API_NONE)); // If we're not yet logged in, we should do it now if(!isLoggedIn){ @@ -251,12 +251,18 @@ public class NetworkService extends Service { // Storing the "database" api id mApiIds.put(ApiAccess.API_DATABASE, apiIdResponse.result); + // Broadcasting result + RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.API_UPDATE, ApiAccess.API_DATABASE)); + checkNextRequestedApiAccess(); }else if(mLastCall.equals(RPC.CALL_HISTORY)){ // Deserializing integer response Type IntegerJsonResponse = new TypeToken>(){}.getType(); JsonRpcResponse apiIdResponse = gson.fromJson(text, IntegerJsonResponse); + // Broadcasting result + RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.API_UPDATE, ApiAccess.API_HISTORY)); + // Storing the "history" api id mApiIds.put(ApiAccess.API_HISTORY, apiIdResponse.result); @@ -266,6 +272,9 @@ public class NetworkService extends Service { Type IntegerJsonResponse = new TypeToken>(){}.getType(); JsonRpcResponse apiIdResponse = gson.fromJson(text, IntegerJsonResponse); + // Broadcasting result + RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.API_UPDATE, ApiAccess.API_NETWORK_BROADCAST)); + // Storing the "network_broadcast" api access mApiIds.put(ApiAccess.API_NETWORK_BROADCAST, apiIdResponse.result); @@ -383,7 +392,8 @@ public class NetworkService extends Service { @Override public void onClosed(WebSocket webSocket, int code, String reason) { super.onClosed(webSocket, code, reason); - RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED)); + Log.d(TAG,"onClosed"); + RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE)); isLoggedIn = false; } @@ -406,7 +416,7 @@ public class NetworkService extends Service { Log.e(TAG,"Response: "+response.message()); } - RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED)); + RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE)); mSocketIndex++; if(mSocketIndex > mNodeUrls.size() * 3){ @@ -417,4 +427,15 @@ public class NetworkService extends Service { } } }; + + /** + * Method used to check whether or not the network service is connected to a node that + * offers a specific API. + * + * @param whichApi The API we want to use. + * @return True if the node has got that API enabled, false otherwise + */ + public boolean hasApiId(int whichApi){ + return mApiIds.get(whichApi) != null; + } } diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/calls/ListAssets.java b/graphenej/src/main/java/cy/agorise/graphenej/api/calls/ListAssets.java index 570fb60..f7b7688 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/api/calls/ListAssets.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/calls/ListAssets.java @@ -20,7 +20,7 @@ public class ListAssets implements ApiCallable { /** * Internal constant used to represent the maximum limit of assets retrieved in one call. */ - private final int MAX_BATCH_SIZE = 100; + public static final int MAX_BATCH_SIZE = 100; private String lowerBound; private int limit;