Loading services/core/java/com/android/server/location/contexthub/ContextHubEndpointBroker.java +6 −4 Original line number Diff line number Diff line Loading @@ -171,7 +171,8 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub } } public boolean isInMessageHistory(HubMessage message) { public boolean isInReliableMessageHistory(HubMessage message) { if (!message.isResponseRequired()) return false; // Clean up the history Iterator<Map.Entry<Integer, Long>> iterator = mRxMessageHistoryMap.entrySet().iterator(); Loading @@ -188,7 +189,8 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub return mRxMessageHistoryMap.containsKey(message.getMessageSequenceNumber()); } public void addMessageToHistory(HubMessage message) { public void addReliableMessageToHistory(HubMessage message) { if (!message.isResponseRequired()) return; if (mRxMessageHistoryMap.containsKey(message.getMessageSequenceNumber())) { long value = mRxMessageHistoryMap.get(message.getMessageSequenceNumber()); Log.w( Loading Loading @@ -623,7 +625,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub return ErrorCode.PERMANENT_ERROR; } HubEndpointInfo remote = mSessionMap.get(sessionId).getRemoteEndpointInfo(); if (mSessionMap.get(sessionId).isInMessageHistory(message)) { if (mSessionMap.get(sessionId).isInReliableMessageHistory(message)) { Log.e(TAG, "Dropping duplicate message: " + message); return ErrorCode.TRANSIENT_ERROR; } Loading @@ -648,7 +650,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub boolean success = invokeCallback((consumer) -> consumer.onMessageReceived(sessionId, message)); if (success) { mSessionMap.get(sessionId).addMessageToHistory(message); mSessionMap.get(sessionId).addReliableMessageToHistory(message); } return success ? ErrorCode.OK : ErrorCode.TRANSIENT_ERROR; } Loading services/tests/servicestests/src/com/android/server/location/contexthub/ContextHubEndpointTest.java +22 −0 Original line number Diff line number Diff line Loading @@ -80,6 +80,10 @@ public class ContextHubEndpointTest { new HubMessage.Builder(SAMPLE_MESSAGE_TYPE, new byte[] {1, 2, 3, 4, 5}) .setResponseRequired(true) .build(); private static final HubMessage SAMPLE_UNRELIABLE_MESSAGE = new HubMessage.Builder(SAMPLE_MESSAGE_TYPE, new byte[] {1, 2, 3, 4, 5}) .setResponseRequired(false) .build(); private ContextHubClientManager mClientManager; private ContextHubEndpointManager mEndpointManager; Loading Loading @@ -260,6 +264,24 @@ public class ContextHubEndpointTest { unregisterExampleEndpoint(endpoint); } @Test public void testUnreliableMessage() throws RemoteException { IContextHubEndpoint endpoint = registerExampleEndpoint(); int sessionId = openTestSession(endpoint); mEndpointManager.onMessageReceived(sessionId, SAMPLE_UNRELIABLE_MESSAGE); ArgumentCaptor<HubMessage> messageCaptor = ArgumentCaptor.forClass(HubMessage.class); verify(mMockCallback).onMessageReceived(eq(sessionId), messageCaptor.capture()); assertThat(messageCaptor.getValue()).isEqualTo(SAMPLE_UNRELIABLE_MESSAGE); // Confirm we can send another message mEndpointManager.onMessageReceived(sessionId, SAMPLE_UNRELIABLE_MESSAGE); verify(mMockCallback, times(2)).onMessageReceived(eq(sessionId), messageCaptor.capture()); assertThat(messageCaptor.getValue()).isEqualTo(SAMPLE_UNRELIABLE_MESSAGE); unregisterExampleEndpoint(endpoint); } /** A helper method to create a session and validates reliable message sending. */ private void testMessageTransactionInternal( IContextHubEndpoint endpoint, boolean deliverMessageStatus) throws RemoteException { Loading Loading
services/core/java/com/android/server/location/contexthub/ContextHubEndpointBroker.java +6 −4 Original line number Diff line number Diff line Loading @@ -171,7 +171,8 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub } } public boolean isInMessageHistory(HubMessage message) { public boolean isInReliableMessageHistory(HubMessage message) { if (!message.isResponseRequired()) return false; // Clean up the history Iterator<Map.Entry<Integer, Long>> iterator = mRxMessageHistoryMap.entrySet().iterator(); Loading @@ -188,7 +189,8 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub return mRxMessageHistoryMap.containsKey(message.getMessageSequenceNumber()); } public void addMessageToHistory(HubMessage message) { public void addReliableMessageToHistory(HubMessage message) { if (!message.isResponseRequired()) return; if (mRxMessageHistoryMap.containsKey(message.getMessageSequenceNumber())) { long value = mRxMessageHistoryMap.get(message.getMessageSequenceNumber()); Log.w( Loading Loading @@ -623,7 +625,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub return ErrorCode.PERMANENT_ERROR; } HubEndpointInfo remote = mSessionMap.get(sessionId).getRemoteEndpointInfo(); if (mSessionMap.get(sessionId).isInMessageHistory(message)) { if (mSessionMap.get(sessionId).isInReliableMessageHistory(message)) { Log.e(TAG, "Dropping duplicate message: " + message); return ErrorCode.TRANSIENT_ERROR; } Loading @@ -648,7 +650,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub boolean success = invokeCallback((consumer) -> consumer.onMessageReceived(sessionId, message)); if (success) { mSessionMap.get(sessionId).addMessageToHistory(message); mSessionMap.get(sessionId).addReliableMessageToHistory(message); } return success ? ErrorCode.OK : ErrorCode.TRANSIENT_ERROR; } Loading
services/tests/servicestests/src/com/android/server/location/contexthub/ContextHubEndpointTest.java +22 −0 Original line number Diff line number Diff line Loading @@ -80,6 +80,10 @@ public class ContextHubEndpointTest { new HubMessage.Builder(SAMPLE_MESSAGE_TYPE, new byte[] {1, 2, 3, 4, 5}) .setResponseRequired(true) .build(); private static final HubMessage SAMPLE_UNRELIABLE_MESSAGE = new HubMessage.Builder(SAMPLE_MESSAGE_TYPE, new byte[] {1, 2, 3, 4, 5}) .setResponseRequired(false) .build(); private ContextHubClientManager mClientManager; private ContextHubEndpointManager mEndpointManager; Loading Loading @@ -260,6 +264,24 @@ public class ContextHubEndpointTest { unregisterExampleEndpoint(endpoint); } @Test public void testUnreliableMessage() throws RemoteException { IContextHubEndpoint endpoint = registerExampleEndpoint(); int sessionId = openTestSession(endpoint); mEndpointManager.onMessageReceived(sessionId, SAMPLE_UNRELIABLE_MESSAGE); ArgumentCaptor<HubMessage> messageCaptor = ArgumentCaptor.forClass(HubMessage.class); verify(mMockCallback).onMessageReceived(eq(sessionId), messageCaptor.capture()); assertThat(messageCaptor.getValue()).isEqualTo(SAMPLE_UNRELIABLE_MESSAGE); // Confirm we can send another message mEndpointManager.onMessageReceived(sessionId, SAMPLE_UNRELIABLE_MESSAGE); verify(mMockCallback, times(2)).onMessageReceived(eq(sessionId), messageCaptor.capture()); assertThat(messageCaptor.getValue()).isEqualTo(SAMPLE_UNRELIABLE_MESSAGE); unregisterExampleEndpoint(endpoint); } /** A helper method to create a session and validates reliable message sending. */ private void testMessageTransactionInternal( IContextHubEndpoint endpoint, boolean deliverMessageStatus) throws RemoteException { Loading