示例#1
0
  public long extractTimestampMillis(String topic, final byte[] bytes) throws IOException {
    if (timestampFieldPath != null) {
      com.google.protobuf.Message decodedMessage = protobufUtil.decodeMessage(topic, bytes);
      int i = 0;
      for (; i < timestampFieldPath.length - 1; ++i) {
        decodedMessage =
            (com.google.protobuf.Message)
                decodedMessage.getField(
                    decodedMessage.getDescriptorForType().findFieldByName(timestampFieldPath[i]));
      }
      Object timestampObject =
          decodedMessage.getField(
              decodedMessage.getDescriptorForType().findFieldByName(timestampFieldPath[i]));
      if (timestampObject instanceof com.google.protobuf.Timestamp) {
        return Timestamps.toMillis((com.google.protobuf.Timestamp) timestampObject);
      } else {
        return toMillis((Long) timestampObject);
      }
    } else {
      // Assume that the timestamp field is the first field, is required,
      // and is a uint64.

      CodedInputStream input = CodedInputStream.newInstance(bytes);
      // Don't really care about the tag, but need to read it to get, to
      // the payload.
      input.readTag();
      return toMillis(input.readUInt64());
    }
  }
示例#2
0
    @Override
    public void message(final int sequenceNo, Message message) {
      final String messageName = "/" + message.getClass().getSimpleName();
      final Timer profilingTimer = Timing.startRequest(messageName);
      if (message instanceof Rpc.CancelRpc) {
        final ServerRpcController controller = activeRpcs.get(sequenceNo);
        if (controller == null) {
          throw new IllegalStateException("Trying to cancel an RPC that is not active!");
        } else {
          LOG.info("Cancelling open RPC " + sequenceNo);
          controller.cancel();
        }
      } else if (message instanceof ProtocolAuthenticate) {
        // Workaround for bug: http://codereview.waveprotocol.org/224001/

        // When we get this message, either the connection will not be logged in
        // (loggedInUser == null) or the connection will have been authenticated
        // via cookies
        // (in which case loggedInUser must match the authenticated user, and
        // this message has no
        // effect).

        ProtocolAuthenticate authMessage = (ProtocolAuthenticate) message;
        ParticipantId authenticatedAs = authenticate(authMessage.getToken());

        Preconditions.checkArgument(authenticatedAs != null, "Auth token invalid");
        Preconditions.checkState(
            loggedInUser == null || loggedInUser.equals(authenticatedAs),
            "Session already authenticated as a different user");

        loggedInUser = authenticatedAs;
        LOG.info("Session authenticated as " + loggedInUser);
        sendMessage(sequenceNo, ProtocolAuthenticationResult.getDefaultInstance());
      } else if (provider.registeredServices.containsKey(message.getDescriptorForType())) {
        if (activeRpcs.containsKey(sequenceNo)) {
          throw new IllegalStateException(
              "Can't invoke a new RPC with a sequence number already in use.");
        } else {
          final RegisteredServiceMethod serviceMethod =
              provider.registeredServices.get(message.getDescriptorForType());

          // Create the internal ServerRpcController used to invoke the call.
          final ServerRpcController controller =
              new ServerRpcControllerImpl(
                  message,
                  serviceMethod.service,
                  serviceMethod.method,
                  loggedInUser,
                  new RpcCallback<Message>() {
                    @Override
                    public synchronized void run(Message message) {
                      if (message instanceof Rpc.RpcFinished
                          || !serviceMethod.method.getOptions().getExtension(Rpc.isStreamingRpc)) {
                        // This RPC is over - remove it from the map.
                        boolean failed =
                            message instanceof Rpc.RpcFinished
                                ? ((Rpc.RpcFinished) message).getFailed()
                                : false;
                        LOG.fine("RPC " + sequenceNo + " is now finished, failed = " + failed);
                        if (failed) {
                          LOG.info("error = " + ((Rpc.RpcFinished) message).getErrorText());
                        }
                        activeRpcs.remove(sequenceNo);
                      }
                      sendMessage(sequenceNo, message);
                      if (profilingTimer != null) {
                        Timing.stop(profilingTimer);
                      }
                    }
                  });

          // Kick off a new thread specific to this RPC.
          activeRpcs.put(sequenceNo, controller);
          provider.threadPool.execute(controller);
        }
      } else {
        // Sent a message type we understand, but don't expect - erronous case!
        throw new IllegalStateException(
            "Got expected but unknown message  (" + message + ") for sequence: " + sequenceNo);
      }
    }
 public static Type getTypeByName(Message message, String name) {
   return message.getDescriptorForType().findFieldByName(name).getType();
 }
 public static boolean hasFieldByName(Message message, String name) {
   return message.getDescriptorForType().findFieldByName(name) != null;
 }
 public static Object getFieldByName(Message message, String name) {
   return message.getField(message.getDescriptorForType().findFieldByName(name));
 }
 public static boolean isFieldSetByName(Message message, String name) {
   return message.hasField(message.getDescriptorForType().findFieldByName(name));
 }