Only subscribing to events if the clearFilter variable is true, and also adding support for handlers added before the connection was established

This commit is contained in:
Nelson R. Perez 2017-11-09 17:50:49 -05:00
parent 1e18382451
commit 2d81979c08

View file

@ -59,6 +59,7 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
private int databaseApiId = -1; private int databaseApiId = -1;
private int subscriptionCounter = 0; private int subscriptionCounter = 0;
private HashMap<Long, BaseGrapheneHandler> mHandlerMap = new HashMap<>(); private HashMap<Long, BaseGrapheneHandler> mHandlerMap = new HashMap<>();
private List<BaseGrapheneHandler> pendingHandlerList = new ArrayList<>();
// State variables // State variables
private boolean isUnsubscribing; private boolean isUnsubscribing;
@ -149,7 +150,19 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
WitnessResponse<Integer> witnessResponse = gson.fromJson(message, ApiIdResponse); WitnessResponse<Integer> witnessResponse = gson.fromJson(message, ApiIdResponse);
databaseApiId = witnessResponse.result; databaseApiId = witnessResponse.result;
// Subscribing only if the clearFilter parameter is true
if(clearFilter){
subscribe(); subscribe();
}
// Dispatching the onConnected event to every pending handler
if(pendingHandlerList.size() > 0){
for(BaseGrapheneHandler handler : pendingHandlerList){
handler.setRequestId(++currentId);
dispatchConnectionEvent(handler);
}
pendingHandlerList.clear();
}
} else if(currentId >= SUBSCRIPTION_REQUEST){ } else if(currentId >= SUBSCRIPTION_REQUEST){
List<SubscriptionListener> subscriptionListeners = mSubscriptionDeserializer.getSubscriptionListeners(); List<SubscriptionListener> subscriptionListeners = mSubscriptionDeserializer.getSubscriptionListeners();
@ -264,13 +277,26 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
subscriptionCounter = 0; subscriptionCounter = 0;
} }
public void addRequestHandler(BaseGrapheneHandler handler) throws RepeatedRequestIdException { /**
if(mHandlerMap.get(handler.getRequestId()) != null){ * Adds a handler either to the map of handlers or to a list of pending ones
throw new RepeatedRequestIdException("Already registered handler with id: "+handler.getRequestId()); * @param handler The handler of a given request
* @throws RepeatedRequestIdException
*/
public void addRequestHandler(BaseGrapheneHandler handler) {
if(mWebsocket != null && currentId > SUBSCRIPTION_REQUEST){
handler.setRequestId(++currentId);
mHandlerMap.put(handler.getRequestId(), handler);
dispatchConnectionEvent(handler);
}else{
pendingHandlerList.add(handler);
}
} }
mHandlerMap.put(handler.getRequestId(), handler); /**
* Informing a handler that we have a connection with the full node.
* @param handler Handler that should be notified.
*/
private void dispatchConnectionEvent(BaseGrapheneHandler handler){
try { try {
// Artificially calling the 'onConnected' method of the handler. // Artificially calling the 'onConnected' method of the handler.
// The underlying websocket was already connected, but from the WebSocketAdapter // The underlying websocket was already connected, but from the WebSocketAdapter
@ -279,6 +305,13 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
} catch (Exception e) { } catch (Exception e) {
System.out.println("Exception. Msg: "+e.getMessage()); System.out.println("Exception. Msg: "+e.getMessage());
System.out.println("Exception type: "+e); System.out.println("Exception type: "+e);
for(StackTraceElement el : e.getStackTrace()){
System.out.println(String.format("at %s.%s(%s:%s)",
el.getClassName(),
el.getMethodName(),
el.getFileName(),
el.getLineNumber()));
}
} }
} }
} }