com.google.ipc.invalidation.ticl.ProtocolHandler.ParsedMessage Java Examples

The following examples show how to use com.google.ipc.invalidation.ticl.ProtocolHandler.ParsedMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InvalidationClientCore.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the token in the header of {@code parsedMessage} matches either the
 * client token or nonce of this Ticl (depending on which is non-{@code null}).
 */
private boolean validateToken(ParsedMessage parsedMessage) {
  if (clientToken != null) {
    // Client token case.
    if (!TypedUtil.<Bytes>equals(clientToken, parsedMessage.header.token)) {
      logger.info("Incoming message has bad token: server = %s, client = %s",
          parsedMessage.header.token, clientToken);
      statistics.recordError(ClientErrorType.TOKEN_MISMATCH);
      return false;
    }
    return true;
  } else if (nonce != null) {
    // Nonce case.
    if (!TypedUtil.<Bytes>equals(nonce, parsedMessage.header.token)) {
      statistics.recordError(ClientErrorType.NONCE_MISMATCH);
      logger.info("Rejecting server message with mismatched nonce: Client = %s, Server = %s",
          nonce, parsedMessage.header.token);
      return false;
    } else {
      logger.info("Accepting server message with matching nonce: %s", nonce);
      return true;
    }
  }
  // Neither token nor nonce; ignore message.
  logger.warning("Neither token nor nonce was set in validateToken: %s, %s", clientToken, nonce);
  return false;
}
 
Example #2
Source File: InvalidationClientCore.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns whether the token in the header of {@code parsedMessage} matches either the
 * client token or nonce of this Ticl (depending on which is non-{@code null}).
 */
private boolean validateToken(ParsedMessage parsedMessage) {
  if (clientToken != null) {
    // Client token case.
    if (!TypedUtil.<ByteString>equals(clientToken, parsedMessage.header.token)) {
      logger.info("Incoming message has bad token: server = %s, client = %s",
          CommonProtoStrings2.toLazyCompactString(parsedMessage.header.token),
          CommonProtoStrings2.toLazyCompactString(clientToken));
      statistics.recordError(ClientErrorType.TOKEN_MISMATCH);
      return false;
    }
    return true;
  } else if (nonce != null) {
    // Nonce case.
    if (!TypedUtil.<ByteString>equals(nonce, parsedMessage.header.token)) {
      statistics.recordError(ClientErrorType.NONCE_MISMATCH);
      logger.info("Rejecting server message with mismatched nonce: Client = %s, Server = %s",
          CommonProtoStrings2.toLazyCompactString(nonce),
          CommonProtoStrings2.toLazyCompactString(parsedMessage.header.token));
      return false;
    } else {
      logger.info("Accepting server message with matching nonce: %s",
          CommonProtoStrings2.toLazyCompactString(nonce));
      return true;
    }
  }
  // Neither token nor nonce; ignore message.
  logger.warning("Neither token nor nonce was set in validateToken: %s, %s", clientToken, nonce);
  return false;
}
 
Example #3
Source File: InvalidationClientCore.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns whether the token in the header of {@code parsedMessage} matches either the
 * client token or nonce of this Ticl (depending on which is non-{@code null}).
 */
private boolean validateToken(ParsedMessage parsedMessage) {
  if (clientToken != null) {
    // Client token case.
    if (!TypedUtil.<ByteString>equals(clientToken, parsedMessage.header.token)) {
      logger.info("Incoming message has bad token: server = %s, client = %s",
          CommonProtoStrings2.toLazyCompactString(parsedMessage.header.token),
          CommonProtoStrings2.toLazyCompactString(clientToken));
      statistics.recordError(ClientErrorType.TOKEN_MISMATCH);
      return false;
    }
    return true;
  } else if (nonce != null) {
    // Nonce case.
    if (!TypedUtil.<ByteString>equals(nonce, parsedMessage.header.token)) {
      statistics.recordError(ClientErrorType.NONCE_MISMATCH);
      logger.info("Rejecting server message with mismatched nonce: Client = %s, Server = %s",
          CommonProtoStrings2.toLazyCompactString(nonce),
          CommonProtoStrings2.toLazyCompactString(parsedMessage.header.token));
      return false;
    } else {
      logger.info("Accepting server message with matching nonce: %s",
          CommonProtoStrings2.toLazyCompactString(nonce));
      return true;
    }
  }
  // Neither token nor nonce; ignore message.
  logger.warning("Neither token nor nonce was set in validateToken: %s, %s", clientToken, nonce);
  return false;
}
 
Example #4
Source File: InvalidationClientCore.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Handles an {@code incomingMessage} from the data center. If it is valid and addressed to
 * this client, dispatches to methods to handle sub-parts of the message; if not, drops the
 * message.
 */
void handleIncomingMessage(byte[] incomingMessage) {
  Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
  statistics.recordReceivedMessage(ReceivedMessageType.TOTAL);
  ParsedMessage parsedMessage = protocolHandler.handleIncomingMessage(incomingMessage);
  if (parsedMessage == null) {
    // Invalid message.
    return;
  }

  // Ensure we have either a matching token or a matching nonce.
  if (!validateToken(parsedMessage)) {
    return;
  }

  // Handle a token-control message, if present.
  if (parsedMessage.tokenControlMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.TOKEN_CONTROL);
    handleTokenChanged(parsedMessage.header.token,
        parsedMessage.tokenControlMessage.hasNewToken() ?
            parsedMessage.tokenControlMessage.getNewToken() : null);
  }

  // We might have lost our token or failed to acquire one. Ensure that we do not proceed in
  // either case.
  if (clientToken == null) {
    return;
  }

  // First, handle the message header.
  handleIncomingHeader(parsedMessage.header);

  // Then, handle any work remaining in the message.
  if (parsedMessage.invalidationMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.INVALIDATION);
    handleInvalidations(parsedMessage.invalidationMessage.getInvalidation());
  }
  if (parsedMessage.registrationStatusMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.REGISTRATION_STATUS);
    handleRegistrationStatus(parsedMessage.registrationStatusMessage.getRegistrationStatus());
  }
  if (parsedMessage.registrationSyncRequestMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.REGISTRATION_SYNC_REQUEST);
    handleRegistrationSyncRequest();
  }
  if (parsedMessage.infoRequestMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.INFO_REQUEST);
    handleInfoMessage(parsedMessage.infoRequestMessage.getInfoType());
  }
  if (parsedMessage.errorMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.ERROR);
    handleErrorMessage(parsedMessage.header, parsedMessage.errorMessage.getCode(),
        parsedMessage.errorMessage.getDescription());
  }
}
 
Example #5
Source File: InvalidationClientCore.java    From android-chromium with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Handles an {@code incomingMessage} from the data center. If it is valid and addressed to
 * this client, dispatches to methods to handle sub-parts of the message; if not, drops the
 * message.
 */
void handleIncomingMessage(byte[] incomingMessage) {
  Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
  statistics.recordReceivedMessage(ReceivedMessageType.TOTAL);
  ParsedMessage parsedMessage = protocolHandler.handleIncomingMessage(incomingMessage);
  if (parsedMessage == null) {
    // Invalid message.
    return;
  }

  // Ensure we have either a matching token or a matching nonce.
  if (!validateToken(parsedMessage)) {
    return;
  }

  // Handle a token-control message, if present.
  if (parsedMessage.tokenControlMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.TOKEN_CONTROL);
    handleTokenChanged(parsedMessage.header.token,
        parsedMessage.tokenControlMessage.hasNewToken() ?
            parsedMessage.tokenControlMessage.getNewToken() : null);
  }

  // We might have lost our token or failed to acquire one. Ensure that we do not proceed in
  // either case.
  if (clientToken == null) {
    return;
  }

  // First, handle the message header.
  handleIncomingHeader(parsedMessage.header);

  // Then, handle any work remaining in the message.
  if (parsedMessage.invalidationMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.INVALIDATION);
    handleInvalidations(parsedMessage.invalidationMessage.getInvalidationList());
  }
  if (parsedMessage.registrationStatusMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.REGISTRATION_STATUS);
    handleRegistrationStatus(parsedMessage.registrationStatusMessage.getRegistrationStatusList());
  }
  if (parsedMessage.registrationSyncRequestMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.REGISTRATION_SYNC_REQUEST);
    handleRegistrationSyncRequest();
  }
  if (parsedMessage.infoRequestMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.INFO_REQUEST);
    handleInfoMessage(parsedMessage.infoRequestMessage.getInfoTypeList());
  }
  if (parsedMessage.errorMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.ERROR);
    handleErrorMessage(parsedMessage.header, parsedMessage.errorMessage.getCode(),
        parsedMessage.errorMessage.getDescription());
  }
}
 
Example #6
Source File: InvalidationClientCore.java    From android-chromium with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Handles an {@code incomingMessage} from the data center. If it is valid and addressed to
 * this client, dispatches to methods to handle sub-parts of the message; if not, drops the
 * message.
 */
void handleIncomingMessage(byte[] incomingMessage) {
  Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
  statistics.recordReceivedMessage(ReceivedMessageType.TOTAL);
  ParsedMessage parsedMessage = protocolHandler.handleIncomingMessage(incomingMessage);
  if (parsedMessage == null) {
    // Invalid message.
    return;
  }

  // Ensure we have either a matching token or a matching nonce.
  if (!validateToken(parsedMessage)) {
    return;
  }

  // Handle a token-control message, if present.
  if (parsedMessage.tokenControlMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.TOKEN_CONTROL);
    handleTokenChanged(parsedMessage.header.token,
        parsedMessage.tokenControlMessage.hasNewToken() ?
            parsedMessage.tokenControlMessage.getNewToken() : null);
  }

  // We might have lost our token or failed to acquire one. Ensure that we do not proceed in
  // either case.
  if (clientToken == null) {
    return;
  }

  // First, handle the message header.
  handleIncomingHeader(parsedMessage.header);

  // Then, handle any work remaining in the message.
  if (parsedMessage.invalidationMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.INVALIDATION);
    handleInvalidations(parsedMessage.invalidationMessage.getInvalidationList());
  }
  if (parsedMessage.registrationStatusMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.REGISTRATION_STATUS);
    handleRegistrationStatus(parsedMessage.registrationStatusMessage.getRegistrationStatusList());
  }
  if (parsedMessage.registrationSyncRequestMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.REGISTRATION_SYNC_REQUEST);
    handleRegistrationSyncRequest();
  }
  if (parsedMessage.infoRequestMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.INFO_REQUEST);
    handleInfoMessage(parsedMessage.infoRequestMessage.getInfoTypeList());
  }
  if (parsedMessage.errorMessage != null) {
    statistics.recordReceivedMessage(ReceivedMessageType.ERROR);
    handleErrorMessage(parsedMessage.header, parsedMessage.errorMessage.getCode(),
        parsedMessage.errorMessage.getDescription());
  }
}