Esempio n. 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());
    }
  }
 @Override
 public String fromObject(Object obj, String resultCode, Writer stream) throws IOException {
   ResponseBean response = (ResponseBean) obj;
   if (response.getData() != null) {
     Message message = (Message) response.getData();
     stream.write(message.toString());
   }
   return null;
 }
 @Override
 public String getValue(Message msg) {
   return (String)
       msg.getField(
           ProtoBufFieldProcessor.getFieldDescriptor(
               AppClass.getDefaultInstance(), AppClass.ID_FIELD_NUMBER));
 }
  /**
   * Handle the receipt of the OOB response.
   *
   * @param msg the out-of-band response
   * @return true the message processed, null if none processed.
   */
  public Message receiveOobResponse(OobResponse msg) {
    if (msg.getCorrelationId() != correlationId) {
      // only possible with race condition on client reset and re-use when a server message
      // comes back
      log.info(
          "Correlation mismatch client "
              + correlationId
              + " OobResponse "
              + msg.getCorrelationId());

      return null; // don't process the server message anymore in this case.
    }
    if (onOobResponsePrototype != null && onOobResponseFunction != null) {
      Message onMsg = null;
      try {
        onMsg = onOobResponsePrototype.newBuilderForType().mergeFrom(msg.getMessageBytes()).build();

        onOobResponseFunction.run(onMsg);

        return onMsg;
      } catch (InvalidProtocolBufferException e) {
        String errorMessage = "Invalid OobResponse Protobuf for correlationId " + correlationId;

        log.warn(errorMessage, e);
      }

    } else {
      if (log.isDebugEnabled()) {
        log.debug("No onOobResponseCallbackFunction registered for correlationId " + correlationId);
      }
    }
    return null;
  }
Esempio n. 5
0
 @Override
 public Message callBlockingMethod(
     @NotNull Descriptors.MethodDescriptor method,
     @Nullable RpcController controller,
     @NotNull Message request,
     @NotNull Message responsePrototype)
     throws ServiceException {
   try {
     final HttpUriRequest httpRequest = createRequest(method, request);
     final HttpResponse response = client.execute(httpRequest);
     if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
       try (final InputStream stream = response.getEntity().getContent()) {
         final Header encoding = response.getFirstHeader(HttpHeaders.CONTENT_ENCODING);
         final Charset charset;
         if (encoding != null && Charset.isSupported(encoding.getValue())) {
           charset = Charset.forName(encoding.getValue());
         } else {
           charset = StandardCharsets.UTF_8;
         }
         return format.read(responsePrototype.toBuilder(), stream, charset).build();
       }
     }
     return null;
   } catch (Exception e) {
     throw new ServiceException(e);
   }
 }
 @Override
 ChannelBuffer serialize(Message header) {
   assert header.isInitialized();
   final GetMasterRegistrationRequestPB.Builder builder =
       GetMasterRegistrationRequestPB.newBuilder();
   return toChannelBuffer(header, builder.build());
 }
 @Override
 ChannelBuffer serialize(Message header) {
   assert header.isInitialized();
   final IsAlterTableDoneRequestPB.Builder builder = IsAlterTableDoneRequestPB.newBuilder();
   TableIdentifierPB tableID = TableIdentifierPB.newBuilder().setTableName(name).build();
   builder.setTable(tableID);
   return toChannelBuffer(header, builder.build());
 }
Esempio n. 8
0
 public static void sendMessage(
     OutputStream out, Protocol.UserAction.Action action, Message message) throws IOException {
   Protocol.UserAction.newBuilder()
       .setAction(action)
       .setBody(message.toByteString())
       .build()
       .writeDelimitedTo(out);
 }
 @Override
 public long getSize(
     final Message m,
     final Class<?> type,
     final Type genericType,
     final Annotation[] annotations,
     final MediaType mediaType) {
   return m.getSerializedSize();
 }
 @Override
 public void writeTo(
     final Message m,
     final Class<?> type,
     final Type genericType,
     final Annotation[] annotations,
     final MediaType mediaType,
     final MultivaluedMap<String, Object> httpHeaders,
     final OutputStream entityStream)
     throws IOException {
   entityStream.write(m.toByteArray());
 }
 /** Get request protobuf for the RPC method. */
 private Message getRequestProto(SocketRpcProtos.Request rpcRequest, Message requestPrototype)
     throws RpcException {
   Message.Builder builder;
   try {
     builder = requestPrototype.newBuilderForType().mergeFrom(rpcRequest.getRequestProto());
     if (!builder.isInitialized()) {
       throw new RpcException(ErrorReason.BAD_REQUEST_PROTO, "Invalid request proto", null);
     }
   } catch (InvalidProtocolBufferException e) {
     throw new RpcException(ErrorReason.BAD_REQUEST_PROTO, "Invalid request proto", e);
   }
   return builder.build();
 }
 /** Create RPC response protobuf from method invocation results. */
 private SocketRpcProtos.Response createRpcResponse(
     Message response, boolean callbackInvoked, SocketRpcController socketController) {
   Builder responseBuilder = SocketRpcProtos.Response.newBuilder();
   if (response != null) {
     responseBuilder.setCallback(true).setResponseProto(response.toByteString());
   } else {
     // Set whether callback was called (in case of async)
     responseBuilder.setCallback(callbackInvoked);
   }
   if (socketController.failed()) {
     responseBuilder.setError(socketController.errorText());
     responseBuilder.setErrorReason(ErrorReason.RPC_FAILED);
   }
   return responseBuilder.build();
 }
Esempio n. 13
0
 public void onReceive(WirePayload payload) throws ServiceException {
   try {
     Response response = payload.getResponse();
     if (response.hasError()) {
       System.out.println("has error: ");
       System.out.println(response.getError().getStacktrace());
       controller.setFailed(response.getError().getMessage());
       return;
     }
     Message result = responsePrototype.getParserForType().parseFrom(response.getResult());
     callback.run(result);
   } catch (Exception e) {
     controller.setFailed(e.getMessage());
   }
 }
Esempio n. 14
0
 /**
  * Decodes protobuf messages of given type from buffer. If not enough data has been presented
  * delegates to the {@link CumulativeProtocolDecoder} base class to read more data from the wire.
  *
  * <p>It uses instance of internal {@link SizeContext} class to calculate size of buffer expected
  * by the given type of message. The size of every message that arrives on the wire is specified
  * by the prepending varint value.
  *
  * @param session The session used to store internal {@link SizeContext}.
  * @param in The buffer used to read messages if contains enough data.
  * @param out The output for messages decoded from data provided.
  * @see ProtobufEncoder
  * @see SizeContext
  */
 @Override
 protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
     throws Exception {
   SizeContext ctx = SizeContext.get(session, in);
   if (ctx.hasEnoughData(in)) {
     try {
       Message.Builder builder = prototype.newBuilderForType();
       ctx.getInputStream(in).readMessage(builder, extentions);
       out.write(builder.build());
       return true;
     } finally {
       ctx.shiftPositionAndReset(session, in);
     }
   }
   return false;
 }
Esempio n. 15
0
  public void callMethod(
      MethodDescriptor method,
      RpcController controller,
      Message request,
      Message responsePrototype,
      RpcCallback<Message> done) {
    ClientRpcController rpcController = (ClientRpcController) controller;

    int correlationId = getNextCorrelationId();
    rpcController.setCorrelationId(correlationId);

    PendingClientCallState state =
        new PendingClientCallState(rpcController, method, responsePrototype, request, done);
    registerPendingRequest(correlationId, state);

    RpcRequest rpcRequest =
        RpcRequest.newBuilder()
            .setCorrelationId(correlationId)
            .setServiceIdentifier(state.getServiceIdentifier())
            .setMethodIdentifier(state.getMethodIdentifier())
            .setRequestBytes(request.toByteString())
            .build();

    if (channel.isConnected()) {
      if (LOGGER.isDebugEnabled())
        //				LOGGER.debug("Sending ["+rpcRequest.getCorrelationId()+"]RpcRequest.");
        LOGGER.debug(
            "Sending [" + rpcRequest.getCorrelationId() + "]RpcRequest. signature {}",
            state.getMethodDesc().getFullName());
      WirePayload payload = WirePayload.newBuilder().setRpcRequest(rpcRequest).build();
      channel.write(payload);
    } else {
      LOGGER.error("Sending [" + rpcRequest.getCorrelationId() + "]RpcRequest. FAILURE");
      String errorMessage = "Session [" + channel + "]closed";
      RpcError rpcError =
          RpcError.newBuilder()
              .setCorrelationId(correlationId)
              .setErrorMessage(errorMessage)
              .build();
      error(rpcError);
      doLog(state, rpcError, errorMessage);
    }
  }
Esempio n. 16
0
 protected void doLog(PendingClientCallState state, Message response, String errorMessage) {
   if (rpcLogger != null) {
     RpcPayloadInfo reqInfo =
         RpcPayloadInfo.newBuilder()
             .setSize(state.getRequest().getSerializedSize())
             .setTs(state.getStartTimestamp())
             .build();
     RpcPayloadInfo resInfo =
         RpcPayloadInfo.newBuilder()
             .setSize(response.getSerializedSize())
             .setTs(System.currentTimeMillis())
             .build();
     rpcLogger.logCall(
         clientInfo,
         serverInfo,
         state.getMethodDesc().getFullName(),
         state.getRequest(),
         response,
         errorMessage,
         state.getController().getCorrelationId(),
         reqInfo,
         resInfo);
   }
 }
Esempio n. 17
0
  @Override
  public Integer apply(Pair<RequestHeader, Message> headerAndParam) {
    RequestHeader header = headerAndParam.getFirst();
    String methodName = header.getMethodName();
    Integer priorityByAnnotation = annotatedQos.get(methodName);
    if (priorityByAnnotation != null) {
      return priorityByAnnotation;
    }

    Message param = headerAndParam.getSecond();
    if (param == null) {
      return HConstants.NORMAL_QOS;
    }
    if (methodName.equalsIgnoreCase("multi") && param instanceof MultiRequest) {
      // The multi call has its priority set in the header.  All calls should work this way but
      // only this one has been converted so far.  No priority == NORMAL_QOS.
      return header.hasPriority() ? header.getPriority() : HConstants.NORMAL_QOS;
    }
    String cls = param.getClass().getName();
    Class<? extends Message> rpcArgClass = argumentToClassMap.get(cls);
    RegionSpecifier regionSpecifier = null;
    // check whether the request has reference to meta region or now.
    try {
      // Check if the param has a region specifier; the pb methods are hasRegion and getRegion if
      // hasRegion returns true.  Not all listed methods have region specifier each time.  For
      // example, the ScanRequest has it on setup but thereafter relies on the scannerid rather than
      // send the region over every time.
      Method hasRegion = methodMap.get("hasRegion").get(rpcArgClass);
      if (hasRegion != null && (Boolean) hasRegion.invoke(param, (Object[]) null)) {
        Method getRegion = methodMap.get("getRegion").get(rpcArgClass);
        regionSpecifier = (RegionSpecifier) getRegion.invoke(param, (Object[]) null);
        HRegion region = hRegionServer.getRegion(regionSpecifier);
        if (region.getRegionInfo().isMetaTable()) {
          if (LOG.isTraceEnabled()) {
            LOG.trace("High priority because region=" + region.getRegionNameAsString());
          }
          return HConstants.HIGH_QOS;
        }
      }
    } catch (Exception ex) {
      // Not good throwing an exception out of here, a runtime anyways.  Let the query go into the
      // server and have it throw the exception if still an issue.  Just mark it normal priority.
      if (LOG.isTraceEnabled()) LOG.trace("Marking normal priority after getting exception=" + ex);
      return HConstants.NORMAL_QOS;
    }

    if (methodName.equals("scan")) { // scanner methods...
      ScanRequest request = (ScanRequest) param;
      if (!request.hasScannerId()) {
        return HConstants.NORMAL_QOS;
      }
      RegionScanner scanner = hRegionServer.getScanner(request.getScannerId());
      if (scanner != null && scanner.getRegionInfo().isMetaRegion()) {
        if (LOG.isTraceEnabled()) {
          // Scanner requests are small in size so TextFormat version should not overwhelm log.
          LOG.trace("High priority scanner request " + TextFormat.shortDebugString(request));
        }
        return HConstants.HIGH_QOS;
      }
    }
    return HConstants.NORMAL_QOS;
  }
Esempio n. 18
0
 private static void send(final DataOutputStream out, final Op opcode, final Message proto)
     throws IOException {
   op(out, opcode);
   proto.writeDelimitedTo(out);
   out.flush();
 }
 public static boolean hasFieldByName(Message message, String name) {
   return message.getDescriptorForType().findFieldByName(name) != null;
 }
Esempio n. 20
0
 @Subscribe
 public void logEvent(Message message) throws IOException {
   LOG.info(message.toString());
 }
Esempio n. 21
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);
      }
    }
Esempio n. 22
0
 public static Short getDescriptor(Message message) {
   return typeDescriptorMap.get(message.getClass());
 }
 public static Object getFieldByName(Message message, String name) {
   return message.getField(message.getDescriptorForType().findFieldByName(name));
 }
 public static Type getTypeByName(Message message, String name) {
   return message.getDescriptorForType().findFieldByName(name).getType();
 }
 public static Message addField(Message m, String name, Object value) {
   Message.Builder builder = m.toBuilder();
   setFieldByName(builder, name, value);
   return builder.build();
 }
 public byte[] serializeToByteArray(Message msg) {
   return msg.toByteArray();
 }
  @Test
  public void testRoleAttackWithoutFeedback() throws Exception {

    UserManager manager = UserManager.getInstance();
    manager.removeUser("test001");
    manager.removeUser("test002");

    User user1 = manager.createDefaultUser();
    user1.set_id(new UserId("test001"));
    // user1.setSessionKey(SessionKey.createSessionKeyFromRandomString());
    user1.setUsername("test001");
    for (int i = 0; i < 1; i++) {
      user1.addTool(makeBuffTool(i));
    }
    user1.setBag(makeBag(user1, 3));

    User user2 = manager.createDefaultUser();
    user2.set_id(new UserId("test002"));
    // user2.setSessionKey(SessionKey.createSessionKeyFromRandomString());
    user2.setUsername("test002");
    for (int i = 0; i < 1; i++) {
      user2.addTool(makeBuffTool(i));
    }
    user2.setBag(makeBag(user2, 3));

    manager.saveUser(user1, true);
    manager.saveUserBag(user1, true);
    manager.saveUser(user2, true);
    manager.saveUserBag(user2, true);

    SessionManager sessionManager = new SessionManager();

    IoSession session1 = createNiceMock(IoSession.class);
    sessionManager.registerSession(session1, user1);

    IoSession session2 = createNiceMock(IoSession.class);
    sessionManager.registerSession(session2, user2);

    BattleManager battleManager = BattleManager.getInstance();
    battleManager.clearAllBattles();

    // Create two single room
    RoomManager roomManager = RoomManager.getInstance();
    roomManager.assignRoom(user1, RoomType.SINGLE_ROOM);
    roomManager.assignRoom(user2, RoomType.SINGLE_ROOM);
    // User1 and User2 press "ready start"
    roomManager.readyStart(user1.getSessionKey(), true);
    roomManager.readyStart(user2.getSessionKey(), true);

    Collection<Battle> battles = battleManager.findAllBattles();

    // Thread.sleep(Long.MAX_VALUE);
    assertEquals(1, battles.size());

    Battle battle = battles.iterator().next();
    // Change the battleMap
    ArrayList<BattleBitSetMap> battleMaps = BattleDataLoader4Bitmap.getBattleMapList();
    BattleBitSetMap map03 = null;
    for (BattleBitSetMap bm : battleMaps) {
      if (bm.getMapId().equals("0")) {
        map03 = bm;
        break;
      }
    }
    assertNotNull(map03);
    TestUtil.setPrivateFieldValue("battleMap", battle, map03);

    HashMap<SessionKey, BattleUser> battleUserMap = battle.getBattleUserMap();
    assertEquals(2, battleUserMap.size());

    BattleUser bUser1 = battleUserMap.get(user1.getSessionKey());
    BattleUser bUser2 = battleUserMap.get(user2.getSessionKey());

    assertEquals(user1.getSessionKey(), bUser1.getUserSessionKey());
    assertEquals(user1.getBlood(), bUser1.getBlood());
    assertEquals(user1.getTkew(), bUser1.getThew());

    Jedis jedis = JedisFactory.getJedis();

    // User1 and User2 are stage ready.
    // Test roundStart
    final ArrayList<Message> msgList = new ArrayList<Message>();
    MessageQueue queue = createMock(MessageQueue.class);
    queue.sessionWrite(anyObject(SessionKey.class), anyObject());
    expectLastCall()
        .andAnswer(
            new IAnswer<Object>() {
              public Object answer() throws Throwable {
                Object msg = getCurrentArguments()[1];
                msgList.add((Message) msg);
                return null;
              }
            })
        .anyTimes();
    TestUtil.setPrivateFieldValue("messageQueue", GameContext.getInstance(), queue);
    replay(queue);

    battleManager.stageReady(user1.getSessionKey());
    battleManager.stageReady(user2.getSessionKey());

    // System.out.println(msgList);
    // Check if roundStart is called
    assertEquals(2, msgList.size());
    BseRoundStart roundStart = (BseRoundStart) msgList.get(0);
    assertNotNull(roundStart.getUserId(0));
    assertNotNull(roundStart.getUserId(1));
    // userPos: (990, 37), angle: 93, power: 83, dir: 1
    // Role Attack
    msgList.clear();
    BceRoleAttack.Builder bceRoleAttack = BceRoleAttack.newBuilder();
    bceRoleAttack.setAngle(150000);
    bceRoleAttack.setAtkAngle(bceRoleAttack.getAngle());
    bceRoleAttack.setDirection(RoleDirection.RIGHT.ordinal());
    bceRoleAttack.setPower(30);
    int userx = 1380;
    int usery = 697;
    bceRoleAttack.setUserx(userx);
    bceRoleAttack.setUsery(usery);
    bUser1.setPosX(userx);
    bUser1.setPosY(usery);
    bUser2.setPosX(userx);
    bUser2.setPosY(usery);

    battleManager.roleAttack(bUser1.getUserSessionKey(), bceRoleAttack.build());

    // System.out.println(msgList);
    // 2 roleAttack messages, 2 roundStart messages.
    long timeOut = System.currentTimeMillis() + 10000;
    Message xinqi = null;
    while (System.currentTimeMillis() < timeOut) {
      int size = msgList.size();
      for (int i = 0; i < size; i++) {
        Object xm = msgList.get(i);
        if (xm instanceof BseAskRoundOver) {
          xinqi = (Message) xm;
          break;
        }
      }
      Thread.sleep(200);
    }
    assertTrue(xinqi != null);

    verify(queue);

    // Clean system.
    jedis.del(battle.getBattleSessionKey().toString());
    jedis.del(user1.getSessionKey().toString());
    jedis.del(user2.getSessionKey().toString());
  }
 public static boolean isFieldSetByName(Message message, String name) {
   return message.hasField(message.getDescriptorForType().findFieldByName(name));
 }
 public static Text toText(Message message) {
   return new Text(message.toByteArray());
 }