@Test public void testBaseHeader() throws Exception { m_buff = IoBuffer.allocate(14); initBaseHeader(m_buff); m_buff.flip(); m_msgdec.decode(null, m_buff, m_mockProtoDecoder); verifyBaseHeader(m_mockProtoDecoder.getMessage()); }
public MessageDecoderResult decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { ConnectMessage message = new ConnectMessage(); if (decodeCommonHeader(message, in) == NEED_DATA) { return NEED_DATA; } int remainingLength = message.getRemainingLength(); int start = in.position(); if (in.remaining() < 12) { return NEED_DATA; } byte[] encProtoName = new byte[6]; in.skip(2); in.get(encProtoName); String protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { return NOT_OK; } message.setProtocolName(protoName); message.setProcotolVersion(in.get()); byte connFlags = in.get(); boolean cleanSession = ((connFlags & 0x02) >> 1) == 1 ? true : false; boolean willFlag = ((connFlags & 0x04) >> 2) == 1 ? true : false; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { return NOT_OK; } boolean willRetain = ((connFlags & 0x20) >> 5) == 1 ? true : false; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1 ? true : false; boolean userFlag = ((connFlags & 0x80) >> 7) == 1 ? true : false; if (!userFlag && passwordFlag) { return NOT_OK; } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); int keepAlive = Utils.readWord(in); message.setKeepAlive(keepAlive); if (remainingLength == 12) { out.write(message); return OK; } String clientID = Utils.decodeString(in); if (clientID == null) { return NEED_DATA; } message.setClientID(clientID); if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { return NEED_DATA; } message.setWillTopic(willTopic); } if (willFlag) { String willMessage = Utils.decodeString(in); if (willMessage == null) { return NEED_DATA; } message.setWillMessage(willMessage); } int readed = in.position() - start; if (readed == remainingLength) { out.write(message); return OK; } if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { return NEED_DATA; } message.setUsername(userName); } readed = in.position() - start; if (readed == remainingLength) { out.write(message); return OK; } if (passwordFlag) { String password = Utils.decodeString(in); if (password == null) { return NEED_DATA; } message.setPassword(password); } out.write(message); return OK; }
ConnectDecoder extends MqttDecoder { public MessageDecoderResult decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { ConnectMessage message = new ConnectMessage(); if (decodeCommonHeader(message, in) == NEED_DATA) { return NEED_DATA; } int remainingLength = message.getRemainingLength(); int start = in.position(); if (in.remaining() < 12) { return NEED_DATA; } byte[] encProtoName = new byte[6]; in.skip(2); in.get(encProtoName); String protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { return NOT_OK; } message.setProtocolName(protoName); message.setProcotolVersion(in.get()); byte connFlags = in.get(); boolean cleanSession = ((connFlags & 0x02) >> 1) == 1 ? true : false; boolean willFlag = ((connFlags & 0x04) >> 2) == 1 ? true : false; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { return NOT_OK; } boolean willRetain = ((connFlags & 0x20) >> 5) == 1 ? true : false; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1 ? true : false; boolean userFlag = ((connFlags & 0x80) >> 7) == 1 ? true : false; if (!userFlag && passwordFlag) { return NOT_OK; } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); int keepAlive = Utils.readWord(in); message.setKeepAlive(keepAlive); if (remainingLength == 12) { out.write(message); return OK; } String clientID = Utils.decodeString(in); if (clientID == null) { return NEED_DATA; } message.setClientID(clientID); if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { return NEED_DATA; } message.setWillTopic(willTopic); } if (willFlag) { String willMessage = Utils.decodeString(in); if (willMessage == null) { return NEED_DATA; } message.setWillMessage(willMessage); } int readed = in.position() - start; if (readed == remainingLength) { out.write(message); return OK; } if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { return NEED_DATA; } message.setUsername(userName); } readed = in.position() - start; if (readed == remainingLength) { out.write(message); return OK; } if (passwordFlag) { String password = Utils.decodeString(in); if (password == null) { return NEED_DATA; } message.setPassword(password); } out.write(message); return OK; } }
ConnectDecoder extends MqttDecoder { public MessageDecoderResult decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { ConnectMessage message = new ConnectMessage(); if (decodeCommonHeader(message, in) == NEED_DATA) { return NEED_DATA; } int remainingLength = message.getRemainingLength(); int start = in.position(); if (in.remaining() < 12) { return NEED_DATA; } byte[] encProtoName = new byte[6]; in.skip(2); in.get(encProtoName); String protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { return NOT_OK; } message.setProtocolName(protoName); message.setProcotolVersion(in.get()); byte connFlags = in.get(); boolean cleanSession = ((connFlags & 0x02) >> 1) == 1 ? true : false; boolean willFlag = ((connFlags & 0x04) >> 2) == 1 ? true : false; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { return NOT_OK; } boolean willRetain = ((connFlags & 0x20) >> 5) == 1 ? true : false; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1 ? true : false; boolean userFlag = ((connFlags & 0x80) >> 7) == 1 ? true : false; if (!userFlag && passwordFlag) { return NOT_OK; } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); int keepAlive = Utils.readWord(in); message.setKeepAlive(keepAlive); if (remainingLength == 12) { out.write(message); return OK; } String clientID = Utils.decodeString(in); if (clientID == null) { return NEED_DATA; } message.setClientID(clientID); if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { return NEED_DATA; } message.setWillTopic(willTopic); } if (willFlag) { String willMessage = Utils.decodeString(in); if (willMessage == null) { return NEED_DATA; } message.setWillMessage(willMessage); } int readed = in.position() - start; if (readed == remainingLength) { out.write(message); return OK; } if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { return NEED_DATA; } message.setUsername(userName); } readed = in.position() - start; if (readed == remainingLength) { out.write(message); return OK; } if (passwordFlag) { String password = Utils.decodeString(in); if (password == null) { return NEED_DATA; } message.setPassword(password); } out.write(message); return OK; }  }
ConnectDecoder extends MqttDecoder { public MessageDecoderResult decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { ConnectMessage message = new ConnectMessage(); if (decodeCommonHeader(message, in) == NEED_DATA) { return NEED_DATA; } int remainingLength = message.getRemainingLength(); int start = in.position(); if (in.remaining() < 12) { return NEED_DATA; } byte[] encProtoName = new byte[6]; in.skip(2); in.get(encProtoName); String protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { return NOT_OK; } message.setProtocolName(protoName); message.setProcotolVersion(in.get()); byte connFlags = in.get(); boolean cleanSession = ((connFlags & 0x02) >> 1) == 1 ? true : false; boolean willFlag = ((connFlags & 0x04) >> 2) == 1 ? true : false; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { return NOT_OK; } boolean willRetain = ((connFlags & 0x20) >> 5) == 1 ? true : false; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1 ? true : false; boolean userFlag = ((connFlags & 0x80) >> 7) == 1 ? true : false; if (!userFlag && passwordFlag) { return NOT_OK; } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); int keepAlive = Utils.readWord(in); message.setKeepAlive(keepAlive); if (remainingLength == 12) { out.write(message); return OK; } String clientID = Utils.decodeString(in); if (clientID == null) { return NEED_DATA; } message.setClientID(clientID); if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { return NEED_DATA; } message.setWillTopic(willTopic); } if (willFlag) { String willMessage = Utils.decodeString(in); if (willMessage == null) { return NEED_DATA; } message.setWillMessage(willMessage); } int readed = in.position() - start; if (readed == remainingLength) { out.write(message); return OK; } if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { return NEED_DATA; } message.setUsername(userName); } readed = in.position() - start; if (readed == remainingLength) { out.write(message); return OK; } if (passwordFlag) { String password = Utils.decodeString(in); if (password == null) { return NEED_DATA; } message.setPassword(password); } out.write(message); return OK; }  MessageDecoderResult decodable(IoSession session, IoBuffer in); MessageDecoderResult decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out); }
ConnectDecoder extends MqttDecoder { public MessageDecoderResult decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { ConnectMessage message = new ConnectMessage(); if (decodeCommonHeader(message, in) == NEED_DATA) { return NEED_DATA; } int remainingLength = message.getRemainingLength(); int start = in.position(); if (in.remaining() < 12) { return NEED_DATA; } byte[] encProtoName = new byte[6]; in.skip(2); in.get(encProtoName); String protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { return NOT_OK; } message.setProtocolName(protoName); message.setProcotolVersion(in.get()); byte connFlags = in.get(); boolean cleanSession = ((connFlags & 0x02) >> 1) == 1 ? true : false; boolean willFlag = ((connFlags & 0x04) >> 2) == 1 ? true : false; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { return NOT_OK; } boolean willRetain = ((connFlags & 0x20) >> 5) == 1 ? true : false; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1 ? true : false; boolean userFlag = ((connFlags & 0x80) >> 7) == 1 ? true : false; if (!userFlag && passwordFlag) { return NOT_OK; } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); int keepAlive = Utils.readWord(in); message.setKeepAlive(keepAlive); if (remainingLength == 12) { out.write(message); return OK; } String clientID = Utils.decodeString(in); if (clientID == null) { return NEED_DATA; } message.setClientID(clientID); if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { return NEED_DATA; } message.setWillTopic(willTopic); } if (willFlag) { String willMessage = Utils.decodeString(in); if (willMessage == null) { return NEED_DATA; } message.setWillMessage(willMessage); } int readed = in.position() - start; if (readed == remainingLength) { out.write(message); return OK; } if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { return NEED_DATA; } message.setUsername(userName); } readed = in.position() - start; if (readed == remainingLength) { out.write(message); return OK; } if (passwordFlag) { String password = Utils.decodeString(in); if (password == null) { return NEED_DATA; } message.setPassword(password); } out.write(message); return OK; }  MessageDecoderResult decodable(IoSession session, IoBuffer in); MessageDecoderResult decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out);  }