@Test public void testBugMissedMessageIDEncoding() throws Exception{ String topic = "/topic"; PublishMessage msg = new PublishMessage(); msg.setQos(QOSType.MOST_ONE); msg.setTopicName(topic); byte[] payload = "Test my payload".getBytes(); msg.setPayload(payload); m_encoder.encode(null, msg, m_mockProtoEncoder); int size = m_mockProtoEncoder.getBuffer().remaining(); assertEquals(10+payload.length, size); assertEquals(0x30, m_mockProtoEncoder.getBuffer().get()); assertEquals(23, m_mockProtoEncoder.getBuffer().get()); }
public void encode(IoSession session, PublishMessage message, ProtocolEncoderOutput out) throws Exception { if (message.getQos() == QOSType.RESERVED) { throw new IllegalArgumentException("Found a message with RESERVED Qos"); } if (message.getTopicName() == null || message.getTopicName().isEmpty()) { throw new IllegalArgumentException("Found a message with empty or null topic name"); } IoBuffer variableHeaderBuff = IoBuffer.allocate(2).setAutoExpand(true); variableHeaderBuff.put(Utils.encodeString(message.getTopicName())); if (message.getQos() == QOSType.LEAST_ONE || message.getQos() == QOSType.EXACTLY_ONCE ) { if (message.getMessageID() == null) { throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted"); } Utils.writeWord(variableHeaderBuff, message.getMessageID()); } variableHeaderBuff.put(message.getPayload()); variableHeaderBuff.flip(); int variableHeaderSize = variableHeaderBuff.remaining(); byte flags = Utils.encodeFlags(message); IoBuffer buff = IoBuffer.allocate(2 + variableHeaderSize).setAutoExpand(true);; buff.put((byte) (AbstractMessage.PUBLISH << 4 | flags)); buff.put(Utils.encodeRemainingLength(variableHeaderSize)); buff.put(variableHeaderBuff).flip(); out.write(buff); }
PublishEncoder implements MessageEncoder<PublishMessage> { public void encode(IoSession session, PublishMessage message, ProtocolEncoderOutput out) throws Exception { if (message.getQos() == QOSType.RESERVED) { throw new IllegalArgumentException("Found a message with RESERVED Qos"); } if (message.getTopicName() == null || message.getTopicName().isEmpty()) { throw new IllegalArgumentException("Found a message with empty or null topic name"); } IoBuffer variableHeaderBuff = IoBuffer.allocate(2).setAutoExpand(true); variableHeaderBuff.put(Utils.encodeString(message.getTopicName())); if (message.getQos() == QOSType.LEAST_ONE || message.getQos() == QOSType.EXACTLY_ONCE ) { if (message.getMessageID() == null) { throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted"); } Utils.writeWord(variableHeaderBuff, message.getMessageID()); } variableHeaderBuff.put(message.getPayload()); variableHeaderBuff.flip(); int variableHeaderSize = variableHeaderBuff.remaining(); byte flags = Utils.encodeFlags(message); IoBuffer buff = IoBuffer.allocate(2 + variableHeaderSize).setAutoExpand(true);; buff.put((byte) (AbstractMessage.PUBLISH << 4 | flags)); buff.put(Utils.encodeRemainingLength(variableHeaderSize)); buff.put(variableHeaderBuff).flip(); out.write(buff); } }
PublishEncoder implements MessageEncoder<PublishMessage> { public void encode(IoSession session, PublishMessage message, ProtocolEncoderOutput out) throws Exception { if (message.getQos() == QOSType.RESERVED) { throw new IllegalArgumentException("Found a message with RESERVED Qos"); } if (message.getTopicName() == null || message.getTopicName().isEmpty()) { throw new IllegalArgumentException("Found a message with empty or null topic name"); } IoBuffer variableHeaderBuff = IoBuffer.allocate(2).setAutoExpand(true); variableHeaderBuff.put(Utils.encodeString(message.getTopicName())); if (message.getQos() == QOSType.LEAST_ONE || message.getQos() == QOSType.EXACTLY_ONCE ) { if (message.getMessageID() == null) { throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted"); } Utils.writeWord(variableHeaderBuff, message.getMessageID()); } variableHeaderBuff.put(message.getPayload()); variableHeaderBuff.flip(); int variableHeaderSize = variableHeaderBuff.remaining(); byte flags = Utils.encodeFlags(message); IoBuffer buff = IoBuffer.allocate(2 + variableHeaderSize).setAutoExpand(true);; buff.put((byte) (AbstractMessage.PUBLISH << 4 | flags)); buff.put(Utils.encodeRemainingLength(variableHeaderSize)); buff.put(variableHeaderBuff).flip(); out.write(buff); }  }
PublishEncoder implements MessageEncoder<PublishMessage> { public void encode(IoSession session, PublishMessage message, ProtocolEncoderOutput out) throws Exception { if (message.getQos() == QOSType.RESERVED) { throw new IllegalArgumentException("Found a message with RESERVED Qos"); } if (message.getTopicName() == null || message.getTopicName().isEmpty()) { throw new IllegalArgumentException("Found a message with empty or null topic name"); } IoBuffer variableHeaderBuff = IoBuffer.allocate(2).setAutoExpand(true); variableHeaderBuff.put(Utils.encodeString(message.getTopicName())); if (message.getQos() == QOSType.LEAST_ONE || message.getQos() == QOSType.EXACTLY_ONCE ) { if (message.getMessageID() == null) { throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted"); } Utils.writeWord(variableHeaderBuff, message.getMessageID()); } variableHeaderBuff.put(message.getPayload()); variableHeaderBuff.flip(); int variableHeaderSize = variableHeaderBuff.remaining(); byte flags = Utils.encodeFlags(message); IoBuffer buff = IoBuffer.allocate(2 + variableHeaderSize).setAutoExpand(true);; buff.put((byte) (AbstractMessage.PUBLISH << 4 | flags)); buff.put(Utils.encodeRemainingLength(variableHeaderSize)); buff.put(variableHeaderBuff).flip(); out.write(buff); }  void encode(IoSession session, PublishMessage message, ProtocolEncoderOutput out); }
PublishEncoder implements MessageEncoder<PublishMessage> { public void encode(IoSession session, PublishMessage message, ProtocolEncoderOutput out) throws Exception { if (message.getQos() == QOSType.RESERVED) { throw new IllegalArgumentException("Found a message with RESERVED Qos"); } if (message.getTopicName() == null || message.getTopicName().isEmpty()) { throw new IllegalArgumentException("Found a message with empty or null topic name"); } IoBuffer variableHeaderBuff = IoBuffer.allocate(2).setAutoExpand(true); variableHeaderBuff.put(Utils.encodeString(message.getTopicName())); if (message.getQos() == QOSType.LEAST_ONE || message.getQos() == QOSType.EXACTLY_ONCE ) { if (message.getMessageID() == null) { throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted"); } Utils.writeWord(variableHeaderBuff, message.getMessageID()); } variableHeaderBuff.put(message.getPayload()); variableHeaderBuff.flip(); int variableHeaderSize = variableHeaderBuff.remaining(); byte flags = Utils.encodeFlags(message); IoBuffer buff = IoBuffer.allocate(2 + variableHeaderSize).setAutoExpand(true);; buff.put((byte) (AbstractMessage.PUBLISH << 4 | flags)); buff.put(Utils.encodeRemainingLength(variableHeaderSize)); buff.put(variableHeaderBuff).flip(); out.write(buff); }  void encode(IoSession session, PublishMessage message, ProtocolEncoderOutput out);  }