/** * Return true if the message's key matches this key matcher. */ public boolean matches(KeyedMessage other) { return matches(other.getKey()); }
/** * Unregister a previously registered keyed message listener. * * @param message The message with the key this listener was previously * registered to receive. * @param listener The listener to remove. */ public void removeListener(KeyedMessage message, VehicleMessage.Listener listener) { removeListener(message.getKey(), listener); }
public static ExactKeyMatcher buildExactMatcher(final KeyedMessage keyed) { return buildExactMatcher(keyed.getKey()); }
/** * Register to receive a callback when a message with same key as the given * KeyedMessage is received. * * @param keyedMessage A message with the key you want to receive updates * for - the response to a command typically has the same key as the * request, so you can use the outgoing message's KeyedMessage to * register to receive a response. * @param listener An listener instance to receive the callback. */ public void addListener(KeyedMessage keyedMessage, VehicleMessage.Listener listener) { addListener(keyedMessage.getKey(), listener); }
@Override public MessageKey getKey() { if(super.getKey() == null) { HashMap<String, Object> key = new HashMap<>(); key.put(CanMessage.BUS_KEY, getBusId()); key.put(CanMessage.ID_KEY, getId()); key.put(MODE_KEY, getMode()); key.put(PID_KEY, getPid()); setKey(new MessageKey(key)); } return super.getKey(); }
public void add(KeyedMessage message) { ThreadPreconditions.checkOnMainThread(); MessageKey key = message.getKey(); boolean dataSetChanged = false; if(mMessages.containsKey(key)) { mMessages.put(key, message); // Already exists in values, just need to update it KeyMatcher exactMatcher = ExactKeyMatcher.buildExactMatcher(key); for(int i = 0; i < mValues.size(); i++) { KeyedMessage existingMessage = ((KeyedMessage)mValues.get(i)); if(exactMatcher.matches(existingMessage.getKey())) { mValues.set(i, message); dataSetChanged |= shouldRefreshView(message, existingMessage); break; } } } else { // Need to recreate values list because positions will be shifted mMessages.put(key, message); mValues = new ArrayList<VehicleMessage>(mMessages.values()); Collections.sort(mValues); dataSetChanged = true; } if(dataSetChanged) { notifyDataSetChanged(); } }
mKeyedMessages.put(keyedMessage.getKey(), keyedMessage);
/** * Send a message to the VehicleInterface and register the given listener to * receive the first response matching the message's key. * * This function is non-blocking. * * The listener is unregistered after the first response is received. If you * need to accept multiple responses for the same request, you must manually * register your own listener to control its lifecycle. * * @param message The desired message to send to the vehicle. * @param listener The message listener that should receive a callback when * a response matching the outgoing message's key is received from the * VI. */ public void request(KeyedMessage message, VehicleMessage.Listener listener) { // Register the listener as non-persistent, so it is deleted after // receiving the first response mNotifier.register(ExactKeyMatcher.buildExactMatcher(message.getKey()), listener, false); send(message); }
@Test public void getNamed() { String name = "foo"; String value = "value"; KeyedMessage message = new SimpleVehicleMessage(name, value); pipeline.receive(message); SimpleVehicleMessage retreived = (SimpleVehicleMessage) pipeline.get( message.getKey()); assertThat(retreived, notNullValue()); assertThat((String)retreived.getValue(), equalTo(value)); }
@Test public void testSerialize() throws DataSinkException { KeyedMessage measurement = new SimpleVehicleMessage(measurementId, value); measurement.asKeyedMessage().getKey(); String serializedMessage = JsonFormatter.serialize(measurement); assertTrue(!serializedMessage.contains("mKey")); }