public static WsPacket decodeJson(String stringResult) { try { JSONObject jsObj = JSONObject.fromObject(stringResult); String wsOpCode = jsObj.getString(WSOPCODE); if (wsOpCode == null) { if (WSManager.log != null) { WSManager.log.warn("数据为:" + stringResult + ",无wsOpCode"); } return null; } if (!WSManager.wsOpCodeMap.containsKey(wsOpCode)) { if (WSManager.log != null) { WSManager.log.warn("wsOpCode为:" + wsOpCode + "无对应解析,请及时解决"); } return null; } Class<?> className = WSManager.wsOpCodeMap.get(wsOpCode); Method buildM = className.getDeclaredMethod("newBuilder"); AbstractMessage.Builder<?> builder = (Builder<?>) buildM.invoke(null); Message data = PacketUtils.jsonToProtoBuf(stringResult, builder); WsPacket wsPacket = new WsPacket(wsOpCode, data); return wsPacket; } catch (Exception e) { if (WSManager.log != null) { WSManager.log.error("json转换成protobuf异常", e); } return null; } }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // int占4个字节 int messageId = in.readInt(); // 消息体的长度(假设长度为20) byte[] bytes = new byte[in.readableBytes()]; in.readBytes(bytes); // 4+20 =24 AbstractMessage message = msgPool.getMessage(messageId); if (message == null) { LOGGER.error("未注册的消息id{}", messageId); return; } out.add(message.getParserForType().parseFrom(bytes)); }
@Override protected void encode(ChannelHandlerContext ctx, AbstractMessage msg, ByteBuf out) throws Exception { int messageId = msgPool.getMessageId(msg); if (messageId == 0) { LOGGER.error("编码到未知的消息{}", messageId); } byte[] bytes = msg.toByteArray(); int length = Integer.BYTES + bytes.length; boolean writeAble = out.isWritable(length); if (!writeAble) { LOGGER.error("消息过大,编码失败 {} -> {}", messageId, length); return; } // int->4 out.writeInt(messageId); // ->20(假设) out.writeBytes(bytes); }
private static void requireAllFieldsExcept(AbstractMessage message, int... fieldNumbersNotRequired) { Collection<Descriptors.FieldDescriptor> required = new ArrayList<>(message.getDescriptorForType().getFields()); Collection<Descriptors.FieldDescriptor> actual = message.getAllFields().keySet(); required.removeAll(actual); if(fieldNumbersNotRequired != null) { for(int fieldNumber : fieldNumbersNotRequired) { required.remove(message.getDescriptorForType().findFieldByNumber(fieldNumber)); } } if(!required.isEmpty()) { Collection<String> names = new ArrayList<>(required.size()); for(Descriptors.FieldDescriptor desc : required) { names.add(desc.getName()); } throw new ProtobufReadException(message.getDescriptorForType().getFullName(), "Missing required fields: " + names.toString()); } }
public static <T extends /*@NonNull*/ AbstractMessage> List<T> parseDelimitedFrom( @Nullable ByteBuffer byteBuf, Parser<T> parser) throws IOException { if (byteBuf == null) { return ImmutableList.of(); } SizeLimitBypassingParser<T> sizeLimitBypassingParser = new SizeLimitBypassingParser<>(parser); List<T> messages = Lists.newArrayList(); try (InputStream input = new ByteBufferInputStream(byteBuf)) { T message; while ((message = sizeLimitBypassingParser.parseDelimitedFrom(input)) != null) { messages.add(message); } } return messages; }
@Override public void send(final MessageController messageController, final AbstractMessage message) { final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); try { final Map<String, Collection<String>> headers = buildHeaders(messageController); final AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.preparePost(url); requestBuilder.setHeaders(headers); requestBuilder.setBody(messagePayload(messageController, message)); final Future<Response> f = requestBuilder.execute(); final Response r = f.get(); final String responseBody = r.getResponseBody(); if (!"OK".equals(responseBody)) { LOGGER.warn("Non-OK response received for HTTP transport client request."); } } catch (final IOException | ExecutionException | InterruptedException e) { LOGGER.error("Could not execute HTTP request.", e); } }
@Override public void send(final MessageController messageController, final AbstractMessage message) { sendCount.increment(); final ServiceProto.ContentType contentType = getContentType(messageController); if (contentType == null) { sendFailureCount.increment(); throw new RuntimeException("Could not get content type of message."); } try { final byte[] messageBytes = rawMessageBytes(contentType, message); final AMQP.BasicProperties basicProperties = buildBasicProperties(messageController, contentType); channel.basicPublish( transportRef.getExchange(), "fanout".equals(transportRef.getType()) ? "" : transportRef.getRoutingKey(), basicProperties, messageBytes); } catch (final Exception e) { sendFailureCount.increment(); LOGGER.error("Exception caught publishing message:", e); } }
@Override public Future<Message> sendAndReceive( final ServiceProto.ServiceRef destination, final AbstractMessage message, final Class messageClass, final Optional<Expiration> expirationOptional) { final SettableFuture<Message> resultsFuture = SettableFuture.create(); final byte[] messageId = MessageUtils.randomMessageId(24); final MessageController controller = new DefaultMessageController( serviceRef, destination, ContentTypes.fromClass(messageClass), Optional.of(messageId), Optional.<byte[]> absent(), expirationOptional.isPresent() ? Optional.of(expirationOptional.get().getExpiration()) : Optional .<DateTime> absent()); resultsFutures.putIfAbsent(Arrays.hashCode(messageId), new Pair<>(resultsFuture, DateTime.now())); serviceRegistry.sendMessage(controller, message); return resultsFuture; }
public NettyRpcProto.RpcRequest build() { NettyRpcProto.RpcRequest result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
public NettyRpcProto.RpcResponse build() { NettyRpcProto.RpcResponse result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
public NettyRpcProto.RpcCancelRequest build() { NettyRpcProto.RpcCancelRequest result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
public NettyRpcProto.RpcContainer build() { NettyRpcProto.RpcContainer result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
/** * 将字符串转换成HttpPacket * * @param stringResult * 字符串 * @param isServer * 是不是服务器一般传true * @param httpPacket * 消息包 * @return */ public static boolean decodeJson(String stringResult, boolean isServer, HttpPacket httpPacket) { try { // 转换成json获取hOpCode,如果没有看看头消息有没有 JSONObject jsObj = JSONObject.fromObject(stringResult); String hOpCode; if (jsObj.containsKey(AllowParam.HOPCODE)) { hOpCode = jsObj.getString(AllowParam.HOPCODE); } else if (httpPacket.hSession.headParam.hOpCode != null && !httpPacket.hSession.headParam.hOpCode.equals("")) { hOpCode = httpPacket.hSession.headParam.hOpCode; } else { return false; } // 是否设定相应解析 if (!HttpManager.hOpCodeMap.containsKey(hOpCode)) { if (HttpConfig.log != null) { HttpConfig.log.warn("hOpCode为:" + hOpCode + "无对应解析,请及时解决"); } return false; } // 解析 Class<?>[] classNames = HttpManager.hOpCodeMap.get(hOpCode); Class<?> className; if (isServer) { className = classNames[0]; } else { className = classNames[1]; } Method buildM = className.getDeclaredMethod("newBuilder"); AbstractMessage.Builder<?> builder = (Builder<?>) buildM.invoke(null); Message data = PacketUtils.jsonToProtoBuf(stringResult, builder); if (data == null) { return false; } // 设置hOpCode和消息体 httpPacket.sethOpCode(hOpCode); httpPacket.setData(data); return true; } catch (Exception e) { if (HttpConfig.log != null) { HttpConfig.log.error("json转换成protobuf异常", e); } return false; } }
@SuppressWarnings("unchecked") @Override public void consume(AbstractMessage msg, Channel channel) { //将消息分发到指定的队列中,该队列有可能在同一个进程,也有可能不在同一个进程 int queueId = 1; IProcessor processor = processors.get(queueId); if (processor == null) { log.error("找不到可用的消息处理器[{}]", queueId); return; } Session session = AttributeUtil.get(channel, SessionKey.SESSION); if (session == null) { return; } AbstractHandler handler = msgPool.getHandler(msg.getClass().getName()); handler.setMessage(msg); handler.setParam(session); log.debug("收到消息:" + msg); processor.process(handler); }
public Row build() { Row result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
public HColumn build() { HColumn result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
@SuppressWarnings("unchecked") @Override public void consume(AbstractMessage msg, Channel channel) { //将消息分发到指定的队列中,该队列有可能在同一个进程,也有可能不在同一个进程 int queueId = 1; IProcessor processor = processors.get(queueId); if (processor == null) { LOGGER.error("找不到可用的消息处理器[{}]", queueId); return; } Session session = AttributeUtil.get(channel, SessionKey.SESSION); if (session == null) { return; } AbstractHandler handler = msgPool.getHandler(msg.getClass().getName()); handler.setMessage(msg); handler.setParam(session); LOGGER.debug("收到消息:" + msg); processor.process(handler); }
public static void sendMsg(AbstractMessage msg, long id) { Session session = SessionManager.getInstance().getSession(id); if (session == null) { return; } session.sendMessage(msg); }
public static void sendMsgToRids(AbstractMessage msg, Collection<Long> rids) { for (Long rid : rids) { if (rid != null) { sendMsg(msg, rid); } } }
public static void sendMsgToRids(AbstractMessage msg, Collection<Long> rids, Long exceptRoleId) { for (Long rid : rids) { if (rid != null && (!rid.equals(exceptRoleId))) { sendMsg(msg, rid); } } }
public DataUnitDef.ColumnValue build() { DataUnitDef.ColumnValue result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
public DataUnitDef.ColumnMeta build() { DataUnitDef.ColumnMeta result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
public DataUnitDef.RowData build() { DataUnitDef.RowData result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
public DataUnitDef.TableData build() { DataUnitDef.TableData result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
public DataUnitDef.DataSet build() { DataUnitDef.DataSet result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
public AddressBookProtos.Person build() { AddressBookProtos.Person result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
public AddressBookProtos.AddressBook build() { AddressBookProtos.AddressBook result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
@Test public void find_closest_superclass_column_type() { final ColumnTypeRegistry<?> registry = ColumnTypeRegistry.newBuilder() .put(GeneratedMessageV3.class, new GeneratedMessageType()) .put(AbstractMessage.class, new AbstractMessageType()) .build(); final EntityColumn column = mockProperty(Any.class); final ColumnType type = registry.get(column); assertNotNull(type); assertThat(type, instanceOf(GeneratedMessageType.class)); }
public static ByteBuffer toByteBuffer(List<? extends AbstractMessage> messages) throws IOException { // TODO optimize byte copying ByteArrayOutputStream output = new ByteArrayOutputStream(); for (AbstractMessage message : messages) { message.writeDelimitedTo(output); } return ByteBuffer.wrap(output.toByteArray()); }
private static byte /*@Nullable*/ [] toByteArray(List<? extends AbstractMessage> messages) throws IOException { if (messages.isEmpty()) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (AbstractMessage message : messages) { message.writeDelimitedTo(baos); } return baos.toByteArray(); }
public long writeMessage(final AbstractMessage message, String type) throws IOException { return write(type, new Copier() { @Override public void copyTo(OutputStream writer) throws IOException { message.writeTo(writer); } }); }
public long writeMessages(final List<? extends AbstractMessage> messages, String type) throws IOException { return write(type, new Copier() { @Override public void copyTo(OutputStream writer) throws IOException { for (AbstractMessage message : messages) { message.writeDelimitedTo(writer); } } }); }
public <T extends /*@NonNull*/ AbstractMessage> /*@Nullable*/ T readMessage(long cappedId, Parser<T> parser) throws IOException { boolean overwritten; boolean inTheFuture; synchronized (lock) { overwritten = out.isOverwritten(cappedId); inTheFuture = cappedId >= out.getCurrIndex(); } if (overwritten) { return null; } if (inTheFuture) { // this can happen when the glowroot folder is copied for analysis without shutting down // the JVM and glowroot.capped.db is copied first, then new data is written to // glowroot.capped.db and the new capped ids are written to glowroot.h2.db and then // glowroot.h2.db is copied with capped ids that do not exist in the copied // glowroot.capped.db return null; } // it's important to wrap CappedBlockInputStream in a BufferedInputStream to prevent // lots of small reads from the underlying RandomAccessFile final int bufferSize = 32768; InputStream input = newLZFInputStream( new BufferedInputStream(new CappedBlockInputStream(cappedId), bufferSize)); try { return parser.parseFrom(input); } catch (Exception e) { if (!out.isOverwritten(cappedId)) { logger.error(e.getMessage(), e); } return null; } finally { input.close(); } }
public PhoneProtos.Phone build() { PhoneProtos.Phone result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
public Entity.PlayerBlob build() { Entity.PlayerBlob result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
private Entity.PlayerBlob buildParsed() throws com.google.protobuf.InvalidProtocolBufferException { Entity.PlayerBlob result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result).asInvalidProtocolBufferException(); } return result; }
public Entity.PlayerBagStoreBlob build() { Entity.PlayerBagStoreBlob result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
private Entity.PlayerBagStoreBlob buildParsed() throws com.google.protobuf.InvalidProtocolBufferException { Entity.PlayerBagStoreBlob result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result).asInvalidProtocolBufferException(); } return result; }
public Entity.PlayerEventSysStoreBlob build() { Entity.PlayerEventSysStoreBlob result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result); } return result; }
private Entity.PlayerEventSysStoreBlob buildParsed() throws com.google.protobuf.InvalidProtocolBufferException { Entity.PlayerEventSysStoreBlob result = buildPartial(); if (!result.isInitialized()) { throw AbstractMessage.Builder.newUninitializedMessageException(result).asInvalidProtocolBufferException(); } return result; }