public static MqttMessage getMqttPubrecMessage(MqttPublishMessage message) { MqttFixedHeader fixedHeader = new MqttFixedHeader( MqttMessageType.PUBREC, message.fixedHeader().isDup(), message.fixedHeader().qosLevel(), message.fixedHeader().isRetain(), message.fixedHeader().remainingLength() ); return new MqttMessage(fixedHeader); }
@Override public MqttPublishMessage replace(ByteBuf content) { return new MqttPublishMessage(fixedHeader(), variableHeader(), content); }
public static MqttPubAckMessage getMqttPubackMessage(MqttPublishMessage message) { MqttFixedHeader fixedHeader = new MqttFixedHeader( MqttMessageType.PUBACK, message.fixedHeader().isDup(), message.fixedHeader().qosLevel(), message.fixedHeader().isRetain(), message.fixedHeader().remainingLength() ); return new MqttPubAckMessage(fixedHeader, MqttMessageIdVariableHeader.from(message.variableHeader().packetId())); }
@Override public void retain(Topic topic, MqttPublishMessage msg) { final ByteBuf payload = msg.content(); byte[] rawPayload = new byte[payload.readableBytes()]; payload.getBytes(0, rawPayload); final RetainedMessage toStore = new RetainedMessage(msg.fixedHeader().qosLevel(), rawPayload); storage.put(topic, toStore); }
@Override public void retain(Topic topic, MqttPublishMessage msg) { final ByteBuf payload = msg.content(); byte[] rawPayload = new byte[payload.readableBytes()]; payload.getBytes(0, rawPayload); final RetainedMessage toStore = new RetainedMessage(msg.fixedHeader().qosLevel(), rawPayload); queueMap.put(topic, toStore); }
private static ByteBuf encodePublishMessage( ByteBufAllocator byteBufAllocator, MqttPublishMessage message) { MqttFixedHeader mqttFixedHeader = message.fixedHeader(); MqttPublishVariableHeader variableHeader = message.variableHeader(); ByteBuf payload = message.payload().duplicate(); String topicName = variableHeader.topicName(); byte[] topicNameBytes = encodeStringUtf8(topicName); int variableHeaderBufferSize = 2 + topicNameBytes.length + (mqttFixedHeader.qosLevel().value() > 0 ? 2 : 0); int payloadBufferSize = payload.readableBytes(); int variablePartSize = variableHeaderBufferSize + payloadBufferSize; int fixedHeaderBufferSize = 1 + getVariableLengthInt(variablePartSize); ByteBuf buf = byteBufAllocator.buffer(fixedHeaderBufferSize + variablePartSize); buf.writeByte(getFixedHeaderByte1(mqttFixedHeader)); writeVariableLengthInt(buf, variablePartSize); buf.writeShort(topicNameBytes.length); buf.writeBytes(topicNameBytes); if (mqttFixedHeader.qosLevel().value() > 0) { buf.writeShort(variableHeader.messageId()); } buf.writeBytes(payload); return buf; }
void sendPublish(MqttPublishMessage publishMsg) { final int packetId = publishMsg.variableHeader().packetId(); final String topicName = publishMsg.variableHeader().topicName(); final String clientId = getClientId(); MqttQoS qos = publishMsg.fixedHeader().qosLevel(); if (LOG.isTraceEnabled()) { LOG.trace("Sending PUBLISH({}) message. MessageId={}, CId={}, topic={}, payload={}", qos, packetId, clientId, topicName, DebugUtils.payload2Str(publishMsg.payload())); } else { LOG.debug("Sending PUBLISH({}) message. MessageId={}, CId={}, topic={}", qos, packetId, clientId, topicName); } sendIfWritableElseDrop(publishMsg); }
/** * Intended usage is only for embedded versions of the broker, where the hosting application * want to use the broker to send a publish message. Like normal external publish message but * with some changes to avoid security check, and the handshake phases for Qos1 and Qos2. It * also doesn't notifyTopicPublished because using internally the owner should already know * where it's publishing. * * @param msg * the message to publish */ public void internalPublish(MqttPublishMessage msg) { final MqttQoS qos = msg.fixedHeader().qosLevel(); final Topic topic = new Topic(msg.variableHeader().topicName()); final ByteBuf payload = msg.payload(); LOG.info("Sending internal PUBLISH message Topic={}, qos={}", topic, qos); publish2Subscribers(payload, topic, qos); if (!msg.fixedHeader().isRetain()) { return; } if (qos == AT_MOST_ONCE || msg.payload().readableBytes() == 0) { // QoS == 0 && retain => clean old retained retainedRepository.cleanRetained(topic); return; } retainedRepository.retain(topic, msg); }
@Override public void handleMessage(Message message) { Client client = message.getClient(); MqttPublishMessage publishMessage = (MqttPublishMessage) message.getPayload(); messageStore.put(message); int qos = MessageUtil.actualQos(publishMessage.fixedHeader().qosLevel().value()); if (qos == MqttQoS.AT_LEAST_ONCE.value()) { MqttPubAckMessage pubAckMessage = MessageUtil.getMqttPubackMessage(publishMessage); client.getCtx().writeAndFlush(pubAckMessage); } else if (qos == MqttQoS.EXACTLY_ONCE.value()) { MqttMessage pubrecMessage = MessageUtil.getMqttPubrecMessage(publishMessage); client.getCtx().writeAndFlush(pubrecMessage); } }
static void verifyReceiveRetainedPublish(EmbeddedChannel embeddedChannel, String expectedTopic, String expectedContent, MqttQoS expectedQos) { MqttPublishMessage receivedPublish = embeddedChannel.flushOutbound().readOutbound(); assertEquals(receivedPublish.fixedHeader().qosLevel(), expectedQos); assertPublishIsCorrect(expectedTopic, expectedContent, receivedPublish); assertTrue("MUST be retained publish", receivedPublish.fixedHeader().isRetain()); }
/** * First phase of a publish QoS2 protocol, sent by publisher to the broker. Publish to all interested * subscribers. */ void receivedPublishQos2(MQTTConnection connection, MqttPublishMessage mqttPublishMessage, String username) { LOG.trace("Processing PUBREL message on connection: {}", connection); final Topic topic = new Topic(mqttPublishMessage.variableHeader().topicName()); final ByteBuf payload = mqttPublishMessage.payload(); final String clientId = connection.getClientId(); if (!authorizator.canWrite(topic, username, clientId)) { LOG.error("MQTT client is not authorized to publish on topic. CId={}, topic: {}", clientId, topic); return; } publish2Subscribers(payload, topic, EXACTLY_ONCE); final boolean retained = mqttPublishMessage.fixedHeader().isRetain(); if (retained) { if (!payload.isReadable()) { retainedRepository.cleanRetained(topic); } else { // before wasn't stored retainedRepository.retain(topic, mqttPublishMessage); } } String clientID = connection.getClientId(); interceptor.notifyTopicPublished(mqttPublishMessage, clientID, username); }
new MqttFixedHeader( MqttMessageType.PUBLISH, publishMessage.fixedHeader().isDup(), MqttQoS.valueOf(MessageUtil.actualQos(publishMessage.fixedHeader().qosLevel().value())), publishMessage.fixedHeader().isRetain(), publishMessage.fixedHeader().remainingLength() ), new MqttPublishVariableHeader(
static void verifyReceiveRetainedPublish(EmbeddedChannel embeddedChannel, String expectedTopic, String expectedContent) { MqttPublishMessage receivedPublish = embeddedChannel.readOutbound(); assertPublishIsCorrect(expectedTopic, expectedContent, receivedPublish); assertTrue("MUST be retained publish", receivedPublish.fixedHeader().isRetain()); }
void processPublish(MqttPublishMessage msg) { final MqttQoS qos = msg.fixedHeader().qosLevel(); final String username = NettyUtils.userName(channel); final String topicName = msg.variableHeader().topicName(); msg.variableHeader().packetId(), qos); ByteBuf payload = msg.payload(); final boolean retain = msg.fixedHeader().isRetain(); final Topic topic = new Topic(topicName); if (!topic.isValid()) {
static void verifyPublishIsReceived(EmbeddedChannel embCh, MqttQoS expectedQos, String expectedPayload) { final MqttPublishMessage publishReceived = embCh.flushOutbound().readOutbound(); final String payloadMessage = DebugUtils.payload2Str(publishReceived.payload()); assertEquals("Sent and received payload must be identical", expectedPayload, payloadMessage); assertEquals("Expected QoS don't match", expectedQos, publishReceived.fixedHeader().qosLevel()); }
@Override public MqttPublishMessage replace(ByteBuf content) { return new MqttPublishMessage(fixedHeader(), variableHeader(), content); }
@Override public MqttPublishMessage replace(ByteBuf content) { return new MqttPublishMessage(fixedHeader(), variableHeader(), content); }
public static IMessagesStore.StoredMessage asStoredMessage(MqttPublishMessage msg) { // TODO ugly, too much array copy ByteBuf payload = msg.payload(); byte[] payloadContent = readBytesAndRewind(payload); IMessagesStore.StoredMessage stored = new IMessagesStore.StoredMessage(payloadContent, msg.fixedHeader().qosLevel(), msg.variableHeader().topicName()); stored.setRetained(msg.fixedHeader().isRetain()); return stored; }
public static Message<MqttPublishVariableHeader, MqttPublishPayload> fromMqttMessage(MqttPublishMessage msg, MqttVersion version, String clientId, String userName, String brokerId) { // forge bytes payload byte[] bytes = new byte[0]; if (msg.payload() != null && msg.payload().readableBytes() > 0) { ByteBuf buf = msg.payload().duplicate(); bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); } return new Message<>(msg.fixedHeader(), new MqttAdditionalHeader(version, clientId, userName, brokerId), msg.variableHeader(), new MqttPublishPayload(bytes)); }
@Test public void testPublishMessage() throws Exception { final MqttPublishMessage message = createPublishMessage(); ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message); final List<Object> out = new LinkedList<>(); mqttDecoder.decode(ctx, byteBuf, out); assertEquals("Expected one object bout got " + out.size(), 1, out.size()); final MqttPublishMessage decodedMessage = (MqttPublishMessage) out.get(0); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validatePublishVariableHeader(message.variableHeader(), decodedMessage.variableHeader()); validatePublishPayload(message.payload(), decodedMessage.payload()); }