Renaming SUBSCRIPTION_ID to MANUAL_SUBSCRIPTION_ID to better reflect its purpose and using it as the base for subscription ids in the NodeConnection class

This commit is contained in:
Nelson R. Perez 2017-07-03 13:50:23 -05:00
parent 105d4e6d10
commit ec19b62ba7
3 changed files with 33 additions and 17 deletions

View file

@ -40,10 +40,16 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
// Sequence of message ids // Sequence of message ids
public final static int LOGIN_ID = 1; public final static int LOGIN_ID = 1;
public final static int GET_DATABASE_ID = 2; public final static int GET_DATABASE_ID = 2;
public final static int SUBCRIPTION_REQUEST = 3; public final static int SUBSCRIPTION_REQUEST = 3;
// ID of subscription notifications // ID of subscription notifications
public final static int SUBCRIPTION_NOTIFICATION = 4; public final static int SUBSCRIPTION_NOTIFICATION = 4;
/**
* Id attributed to the indivitual 'get_objects' API call required for a fine-grained
* subscription request.
*/
public final static int MANUAL_SUBSCRIPTION_ID = 5;
private SubscriptionResponse.SubscriptionResponseDeserializer mSubscriptionDeserializer; private SubscriptionResponse.SubscriptionResponseDeserializer mSubscriptionDeserializer;
private Gson gson; private Gson gson;
@ -55,11 +61,6 @@ 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<>();
/**
* Id used to separate requests regarding the subscriptions
*/
private final int SUBSCRIPTION_ID = 10;
/** /**
* 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
@ -143,7 +144,7 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
databaseApiId = witnessResponse.result; databaseApiId = witnessResponse.result;
subscribe(); subscribe();
} else if(currentId == SUBCRIPTION_REQUEST){ } else if(currentId == SUBSCRIPTION_REQUEST){
List<SubscriptionListener> subscriptionListeners = mSubscriptionDeserializer.getSubscriptionListeners(); List<SubscriptionListener> subscriptionListeners = mSubscriptionDeserializer.getSubscriptionListeners();
// If we haven't subscribed to all requested subscription channels yet, // If we haven't subscribed to all requested subscription channels yet,
@ -159,7 +160,7 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
} }
payload.add(objects); payload.add(objects);
ApiCall subscribe = new ApiCall(databaseApiId, RPC.GET_OBJECTS, payload, RPC.VERSION, SUBSCRIPTION_ID); ApiCall subscribe = new ApiCall(databaseApiId, RPC.GET_OBJECTS, payload, RPC.VERSION, MANUAL_SUBSCRIPTION_ID);
websocket.sendText(subscribe.toJsonString()); websocket.sendText(subscribe.toJsonString());
subscriptionCounter++; subscriptionCounter++;
}else{ }else{
@ -192,16 +193,19 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
*/ */
private void subscribe(){ private void subscribe(){
ArrayList<Serializable> subscriptionParams = new ArrayList<>(); ArrayList<Serializable> subscriptionParams = new ArrayList<>();
subscriptionParams.add(String.format("%d", SUBCRIPTION_NOTIFICATION)); subscriptionParams.add(String.format("%d", SUBSCRIPTION_NOTIFICATION));
subscriptionParams.add(clearFilter); subscriptionParams.add(clearFilter);
ApiCall getDatabaseId = new ApiCall(databaseApiId, RPC.CALL_SET_SUBSCRIBE_CALLBACK, subscriptionParams, RPC.VERSION, SUBCRIPTION_REQUEST); ApiCall getDatabaseId = new ApiCall(databaseApiId, RPC.CALL_SET_SUBSCRIBE_CALLBACK, subscriptionParams, RPC.VERSION, SUBSCRIPTION_REQUEST);
mWebsocket.sendText(getDatabaseId.toJsonString()); mWebsocket.sendText(getDatabaseId.toJsonString());
currentId = SUBCRIPTION_REQUEST; currentId = SUBSCRIPTION_REQUEST;
} }
/** /**
* Public method used to re-establish a subscription after it was cancelled by a previous * Public method used to re-establish a subscription after it was cancelled by a previous
* call to the {@see #cancelSubscriptions()} method call. * call to the {@see #cancelSubscriptions()} method call.
*
* Please note that you should repeat the registration step for every interested listener, since
* those were probably lost after the previous {@see #cancelSubscriptions()} method call.
*/ */
public void resubscribe(){ public void resubscribe(){
if(mWebsocket.isOpen()){ if(mWebsocket.isOpen()){
@ -212,11 +216,11 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
} }
/** /**
* Method that send a subscription cancellation request to the full node, and also * Method that sends a subscription cancellation request to the full node, and also
* deregisters all subscription and request listeners. * de-registers all subscription and request listeners.
*/ */
public void cancelSubscriptions(){ public void cancelSubscriptions(){
ApiCall unsubscribe = new ApiCall(databaseApiId, RPC.CALL_CANCEL_ALL_SUBSCRIPTIONS, new ArrayList<Serializable>(), RPC.VERSION, SUBCRIPTION_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());
// Clearing all subscription listeners // Clearing all subscription listeners

View file

@ -17,7 +17,7 @@ public class NodeConnection {
private int mUrlIndex; private int mUrlIndex;
private WebsocketWorkerThread mThread; private WebsocketWorkerThread mThread;
private SubscriptionMessagesHub mMessagesHub; private SubscriptionMessagesHub mMessagesHub;
private long requestCounter = SubscriptionMessagesHub.SUBCRIPTION_NOTIFICATION + 1; private long requestCounter = SubscriptionMessagesHub.MANUAL_SUBSCRIPTION_ID + 1;
private static NodeConnection instance; private static NodeConnection instance;

View file

@ -20,11 +20,15 @@ import de.bitsharesmunich.graphenej.models.SubscriptionResponse;
import de.bitsharesmunich.graphenej.models.WitnessResponse; import de.bitsharesmunich.graphenej.models.WitnessResponse;
/** /**
* Created by nelson on 4/25/17. * Class used to encapsulate all tests that relate to the {@see SubscriptionMessagesHub} class.
*/ */
public class SubscriptionMessagesHubTest extends BaseApiTest { public class SubscriptionMessagesHubTest extends BaseApiTest {
private SubscriptionMessagesHub mMessagesHub; private SubscriptionMessagesHub mMessagesHub;
/**
* Error listener
*/
private WitnessResponseListener mErrorListener = new WitnessResponseListener() { private WitnessResponseListener mErrorListener = new WitnessResponseListener() {
@Override @Override
public void onSuccess(WitnessResponse response) { public void onSuccess(WitnessResponse response) {
@ -99,6 +103,12 @@ public class SubscriptionMessagesHubTest extends BaseApiTest {
} }
} }
/**
* This test will register a {@see SubscriptionListener} and wait for an amount equal to MAX_MESSAGES
* of {@see DynamicGlobalProperties} objects to be returned.
*
* The test will be deemed successfull if no errors arise in the meantime.
*/
@Test @Test
public void testGlobalPropertiesDeserializer(){ public void testGlobalPropertiesDeserializer(){
try{ try{
@ -167,6 +177,8 @@ public class SubscriptionMessagesHubTest extends BaseApiTest {
/** /**
* This is a basic test that will only display a count of operations per received broadcasted transactions. * This is a basic test that will only display a count of operations per received broadcasted transactions.
*
* The test will be deemed successfull if we get to receive MAX_MESSAGES transaction objects without errors.
*/ */
@Test @Test
public void testBroadcastedTransactionDeserializer(){ public void testBroadcastedTransactionDeserializer(){