- 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
This commit is contained in:
Nelson R. Perez 2018-07-26 17:13:55 -05:00
parent 364c31c78c
commit 66ce8a4ba4
3 changed files with 77 additions and 15 deletions

View file

@ -1,24 +1,65 @@
package cy.agorise.graphenej.api; 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 class ConnectionStatusUpdate {
public final static String CONNECTED = "Connected"; // Constant used to announce that a connection has been established
public final static String DISCONNECTED = "Disconnected"; 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() { public int getUpdateCode() {
return connectionStatus; return updateCode;
} }
public void setConnectionStatus(String connectionStatus) { public void setUpdateCode(int updateCode) {
this.connectionStatus = connectionStatus; this.updateCode = updateCode;
}
public int getApi() {
return api;
}
public void setApi(int api) {
this.api = api;
} }
} }

View file

@ -89,7 +89,7 @@ public class NetworkService extends Service {
private int mRequestedApis; private int mRequestedApis;
// Variable used to keep track of the currently obtained API accesses // Variable used to keep track of the currently obtained API accesses
private HashMap<Integer, Integer> mApiIds = new HashMap(); private HashMap<Integer, Integer> mApiIds = new HashMap<Integer, Integer>();
private ArrayList<String> mNodeUrls = new ArrayList<>(); private ArrayList<String> mNodeUrls = new ArrayList<>();
@ -217,7 +217,7 @@ public class NetworkService extends Service {
mWebSocket = webSocket; mWebSocket = webSocket;
// Notifying all listeners about the new connection status // 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 we're not yet logged in, we should do it now
if(!isLoggedIn){ if(!isLoggedIn){
@ -251,12 +251,18 @@ public class NetworkService extends Service {
// Storing the "database" api id // Storing the "database" api id
mApiIds.put(ApiAccess.API_DATABASE, apiIdResponse.result); mApiIds.put(ApiAccess.API_DATABASE, apiIdResponse.result);
// Broadcasting result
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.API_UPDATE, ApiAccess.API_DATABASE));
checkNextRequestedApiAccess(); checkNextRequestedApiAccess();
}else if(mLastCall.equals(RPC.CALL_HISTORY)){ }else if(mLastCall.equals(RPC.CALL_HISTORY)){
// Deserializing integer response // Deserializing integer response
Type IntegerJsonResponse = new TypeToken<JsonRpcResponse<Integer>>(){}.getType(); Type IntegerJsonResponse = new TypeToken<JsonRpcResponse<Integer>>(){}.getType();
JsonRpcResponse<Integer> apiIdResponse = gson.fromJson(text, IntegerJsonResponse); JsonRpcResponse<Integer> apiIdResponse = gson.fromJson(text, IntegerJsonResponse);
// Broadcasting result
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.API_UPDATE, ApiAccess.API_HISTORY));
// Storing the "history" api id // Storing the "history" api id
mApiIds.put(ApiAccess.API_HISTORY, apiIdResponse.result); mApiIds.put(ApiAccess.API_HISTORY, apiIdResponse.result);
@ -266,6 +272,9 @@ public class NetworkService extends Service {
Type IntegerJsonResponse = new TypeToken<JsonRpcResponse<Integer>>(){}.getType(); Type IntegerJsonResponse = new TypeToken<JsonRpcResponse<Integer>>(){}.getType();
JsonRpcResponse<Integer> apiIdResponse = gson.fromJson(text, IntegerJsonResponse); JsonRpcResponse<Integer> 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 // Storing the "network_broadcast" api access
mApiIds.put(ApiAccess.API_NETWORK_BROADCAST, apiIdResponse.result); mApiIds.put(ApiAccess.API_NETWORK_BROADCAST, apiIdResponse.result);
@ -383,7 +392,8 @@ public class NetworkService extends Service {
@Override @Override
public void onClosed(WebSocket webSocket, int code, String reason) { public void onClosed(WebSocket webSocket, int code, String reason) {
super.onClosed(webSocket, code, 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; isLoggedIn = false;
} }
@ -406,7 +416,7 @@ public class NetworkService extends Service {
Log.e(TAG,"Response: "+response.message()); Log.e(TAG,"Response: "+response.message());
} }
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED)); RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE));
mSocketIndex++; mSocketIndex++;
if(mSocketIndex > mNodeUrls.size() * 3){ 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;
}
} }

View file

@ -20,7 +20,7 @@ public class ListAssets implements ApiCallable {
/** /**
* Internal constant used to represent the maximum limit of assets retrieved in one call. * 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 String lowerBound;
private int limit; private int limit;