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

develop
Nelson R. Perez 2017-11-09 17:50:49 -05:00
parent 1e18382451
commit 2d81979c08
1 changed files with 39 additions and 6 deletions

View File

@ -59,6 +59,7 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
private int databaseApiId = -1;
private int subscriptionCounter = 0;
private HashMap<Long, BaseGrapheneHandler> mHandlerMap = new HashMap<>();
private List<BaseGrapheneHandler> pendingHandlerList = new ArrayList<>();
// State variables
private boolean isUnsubscribing;
@ -149,7 +150,19 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
WitnessResponse<Integer> witnessResponse = gson.fromJson(message, ApiIdResponse);
databaseApiId = witnessResponse.result;
subscribe();
// Subscribing only if the clearFilter parameter is true
if(clearFilter){
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){
List<SubscriptionListener> subscriptionListeners = mSubscriptionDeserializer.getSubscriptionListeners();
@ -264,13 +277,26 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
subscriptionCounter = 0;
}
public void addRequestHandler(BaseGrapheneHandler handler) throws RepeatedRequestIdException {
if(mHandlerMap.get(handler.getRequestId()) != null){
throw new RepeatedRequestIdException("Already registered handler with id: "+handler.getRequestId());
/**
* Adds a handler either to the map of handlers or to a list of pending ones
* @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 {
// Artificially calling the 'onConnected' method of the handler.
// The underlying websocket was already connected, but from the WebSocketAdapter
@ -279,6 +305,13 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
} catch (Exception e) {
System.out.println("Exception. Msg: "+e.getMessage());
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()));
}
}
}
}