Adjusted code in order to broadcast internally responses with empty results, which were skipped before

This commit is contained in:
Nelson R. Perez 2018-08-31 18:11:57 -05:00
parent 7e2ef7b705
commit f8326093a2

View file

@ -256,19 +256,27 @@ public class NetworkService extends Service {
public void onMessage(WebSocket webSocket, String text) { public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text); super.onMessage(webSocket, text);
Log.v(TAG,"<- "+text); Log.v(TAG,"<- "+text);
JsonRpcResponse<?> response = gson.fromJson(text, JsonRpcResponse.class); JsonRpcNotification notification = gson.fromJson(text, JsonRpcNotification.class);
// We will only handle messages that relate to the login and API accesses here. if(notification.method != null){
// If we are dealing with a notification
handleJsonRpcNotification(notification);
}else{
// If we are dealing with a response
JsonRpcResponse<?> response = gson.fromJson(text, JsonRpcResponse.class);
if(response.result != null){ if(response.result != null){
// Handling initial handshake with the full node (authentication and API access checks)
if(response.result instanceof Double || response.result instanceof Boolean){ if(response.result instanceof Double || response.result instanceof Boolean){
if(mLastCall.equals(RPC.CALL_LOGIN)){ switch (mLastCall) {
case RPC.CALL_LOGIN:
isLoggedIn = true; isLoggedIn = true;
// Broadcasting result // Broadcasting result
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.AUTHENTICATED, ApiAccess.API_NONE)); RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.AUTHENTICATED, ApiAccess.API_NONE));
checkNextRequestedApiAccess(); checkNextRequestedApiAccess();
}else if(mLastCall.equals(RPC.CALL_DATABASE)){ break;
case RPC.CALL_DATABASE: {
// 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);
@ -280,7 +288,9 @@ public class NetworkService extends Service {
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.API_UPDATE, ApiAccess.API_DATABASE)); RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.API_UPDATE, ApiAccess.API_DATABASE));
checkNextRequestedApiAccess(); checkNextRequestedApiAccess();
}else if(mLastCall.equals(RPC.CALL_HISTORY)){ break;
}
case 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);
@ -292,7 +302,9 @@ public class NetworkService extends Service {
mApiIds.put(ApiAccess.API_HISTORY, apiIdResponse.result); mApiIds.put(ApiAccess.API_HISTORY, apiIdResponse.result);
checkNextRequestedApiAccess(); checkNextRequestedApiAccess();
}else if(mLastCall.equals(RPC.CALL_NETWORK_BROADCAST)){ break;
}
case RPC.CALL_NETWORK_BROADCAST:
// 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);
@ -305,23 +317,16 @@ public class NetworkService extends Service {
// All calls have been handled at this point // All calls have been handled at this point
mLastCall = ""; mLastCall = "";
break;
}
} }
} }
// Properly de-serialize all other fields and broadcasts to the event bus
handleJsonRpcResponse(response, text);
}else{
// If no 'result' field was found, this incoming message probably corresponds to a
// JSON-RPC notification message, which should have a 'method' field with the string
// 'notice' as its value
JsonRpcNotification notification = gson.fromJson(text, JsonRpcNotification.class);
if(notification.method != null && notification.method.equals("notice")){
handleJsonRpcNotification(notification);
}else{
if(response.error != null && response.error.message != null){ if(response.error != null && response.error.message != null){
// We could not make sense of this incoming message, just log a warning // We could not make sense of this incoming message, just log a warning
Log.w(TAG,"Error.Msg: "+response.error.message); Log.w(TAG,"Error.Msg: "+response.error.message);
} }
} // Properly de-serialize all other fields and broadcasts to the event bus
handleJsonRpcResponse(response, text);
} }
} }
@ -392,7 +397,6 @@ public class NetworkService extends Service {
if(parsedResponse == null){ if(parsedResponse == null){
parsedResponse = response; parsedResponse = response;
} }
// Broadcasting the parsed response to all interested listeners // Broadcasting the parsed response to all interested listeners
RxBus.getBusInstance().send(parsedResponse); RxBus.getBusInstance().send(parsedResponse);
} }