Introducing a couple of private variables in order to keep track of the subscription status

This commit is contained in:
Nelson R. Perez 2017-07-03 20:05:59 -05:00
parent ec19b62ba7
commit 50fc2d805e

View file

@ -61,6 +61,10 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
private int subscriptionCounter = 0; private int subscriptionCounter = 0;
private HashMap<Long, BaseGrapheneHandler> mHandlerMap = new HashMap<>(); private HashMap<Long, BaseGrapheneHandler> mHandlerMap = new HashMap<>();
// State variables
private boolean isUnsubscribing;
private boolean isSubscribed;
/** /**
* Constructor used to create a subscription message hub that will call the set_subscribe_callback * Constructor used to create a subscription message hub that will call the set_subscribe_callback
* API with the clear_filter parameter set to false, meaning that it will only receive automatic updates * API with the clear_filter parameter set to false, meaning that it will only receive automatic updates
@ -147,6 +151,10 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
} else if(currentId == SUBSCRIPTION_REQUEST){ } else if(currentId == SUBSCRIPTION_REQUEST){
List<SubscriptionListener> subscriptionListeners = mSubscriptionDeserializer.getSubscriptionListeners(); List<SubscriptionListener> subscriptionListeners = mSubscriptionDeserializer.getSubscriptionListeners();
if(!isUnsubscribing){
isSubscribed = true;
}
// If we haven't subscribed to all requested subscription channels yet, // If we haven't subscribed to all requested subscription channels yet,
// just send one more subscription // just send one more subscription
if(subscriptionListeners != null && if(subscriptionListeners != null &&
@ -192,6 +200,8 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
* Private method that sends a subscription request to the full node * Private method that sends a subscription request to the full node
*/ */
private void subscribe(){ private void subscribe(){
isUnsubscribing = false;
ArrayList<Serializable> subscriptionParams = new ArrayList<>(); ArrayList<Serializable> subscriptionParams = new ArrayList<>();
subscriptionParams.add(String.format("%d", SUBSCRIPTION_NOTIFICATION)); subscriptionParams.add(String.format("%d", SUBSCRIPTION_NOTIFICATION));
subscriptionParams.add(clearFilter); subscriptionParams.add(clearFilter);
@ -220,6 +230,9 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
* de-registers all subscription and request listeners. * de-registers all subscription and request listeners.
*/ */
public void cancelSubscriptions(){ public void cancelSubscriptions(){
isSubscribed = false;
isUnsubscribing = true;
ApiCall unsubscribe = new ApiCall(databaseApiId, RPC.CALL_CANCEL_ALL_SUBSCRIPTIONS, new ArrayList<Serializable>(), RPC.VERSION, SUBSCRIPTION_REQUEST); ApiCall unsubscribe = new ApiCall(databaseApiId, RPC.CALL_CANCEL_ALL_SUBSCRIPTIONS, new ArrayList<Serializable>(), RPC.VERSION, SUBSCRIPTION_REQUEST);
mWebsocket.sendText(unsubscribe.toJsonString()); mWebsocket.sendText(unsubscribe.toJsonString());
@ -230,6 +243,14 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
mHandlerMap.clear(); mHandlerMap.clear();
} }
/**
* Method used to check the current state of the connection.
* @return: True if the websocket is open and there is an active subscription, false otherwise.
*/
public boolean isSubscribed(){
return this.mWebsocket.isOpen() && isSubscribed;
}
/** /**
* Method used to reset all internal variables. * Method used to reset all internal variables.
*/ */