io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader Java Examples

The following examples show how to use io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UnSubscribe.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<MqttMessage> process(MqttMessage req, String clientId, String remoteIp) {
    MqttUnsubscribeMessage msg = (MqttUnsubscribeMessage) req;
    log.info("UNSUBSCRIBE, {}", msg.payload().topics());

    if (msg.payload().topics().isEmpty()) {
        log.error("empty topic, skip it");
        return Optional.empty();
    }

    this.sessionStore.unSubscribe(clientId, msg.payload().topics());

    MqttMessage rsp = MqttMessageFactory.newMessage(new MqttFixedHeader(MqttMessageType.UNSUBACK, false, MqttQoS.AT_LEAST_ONCE, false, ProtocolProcess.fixLengthOfMessageId),
            MqttMessageIdVariableHeader.from(msg.variableHeader().messageId()), null);
    return Optional.of(rsp);
}
 
Example #2
Source File: MqttClientImpl.java    From vertx-mqtt with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the PUBREL message to server
 *
 * @param publishMessageId  identifier of the PUBLISH message to acknowledge
 */
private void publishRelease(int publishMessageId) {

  MqttFixedHeader fixedHeader =
    new MqttFixedHeader(MqttMessageType.PUBREL, false, MqttQoS.AT_LEAST_ONCE, false, 0);

  MqttMessageIdVariableHeader variableHeader =
    MqttMessageIdVariableHeader.from(publishMessageId);

  io.netty.handler.codec.mqtt.MqttMessage pubrel = MqttMessageFactory.newMessage(fixedHeader, variableHeader, null);

  synchronized (this) {
    qos2outbound.put(publishMessageId, pubrel);
  }
  this.write(pubrel);
}
 
Example #3
Source File: ProtocolProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 6 votes vote down vote up
/**
 *  P - B (Qos2)
 * @param channel
 * @param variableHeader
 */
public void processPubRel(Channel channel, MqttMessageIdVariableHeader variableHeader) {
	int messageId = variableHeader.messageId();
	ProcedureMessage info = this.procedureProcess.processPubRel(channel, messageId);

	if (info != null) {
		NettyLog.debug("relInfo:" + info);

		BorkerMessage bMsgInfo = BorkerMessage.builder().sourceClientId(info.getSourceClientId())
				.sourceMsgId(info.getSourceMsgId()).topicName(info.getTopicName()).iQosLevel(info.getIQosLevel())
				.msgBytes(info.getMsgBytes()).retain(false).build();

		List<SubscribeTopicInfo> subscribeClientList = this.topicProcess.search(bMsgInfo.getTopicName());
		this.consumerProcess.sendSubscribMessage(bMsgInfo, subscribeClientList);
	}

	this.sendProcess.sendPubCompMessage(channel, messageId);
}
 
Example #4
Source File: MqttClientImpl.java    From vertx-mqtt with Apache License 2.0 6 votes vote down vote up
/**
 * Sends PUBREC packet to server
 *
 * @param publishMessage a PUBLISH message to acknowledge
 */
private void publishReceived(MqttPublishMessage publishMessage) {

  MqttFixedHeader fixedHeader =
    new MqttFixedHeader(MqttMessageType.PUBREC, false, AT_MOST_ONCE, false, 0);

  MqttMessageIdVariableHeader variableHeader =
    MqttMessageIdVariableHeader.from(publishMessage.messageId());

  io.netty.handler.codec.mqtt.MqttMessage pubrec = MqttMessageFactory.newMessage(fixedHeader, variableHeader, null);

  synchronized (this) {
    qos2inbound.put(publishMessage.messageId(), publishMessage);
  }
  this.write(pubrec);
}
 
Example #5
Source File: MessagePublisher.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
private void sendPubAck(Channel client, Integer packageID) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("发送PubAck消息给客户端");
    }
    try {
        MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.PUBACK, false, MqttQoS.AT_MOST_ONCE, false, 0);
        MqttMessage pubAckMessage = MqttMessageFactory.newMessage(
                mqttFixedHeader,
                MqttMessageIdVariableHeader.from(packageID),
                null);
        client.writeAndFlush(pubAckMessage);
    } catch (Throwable th) {
        LOG.error("Send pubAck error!", th);
        client.close().addListener(CLOSE_ON_FAILURE);
    }
}
 
Example #6
Source File: Publish.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<MqttMessage> process(MqttMessage req, String clientId, String remoteIp) throws BrokerException {
    MqttPublishMessage msg = (MqttPublishMessage) req;
    log.info("PUBLISH, {} Qos: {}", msg.variableHeader().topicName(), msg.fixedHeader().qosLevel());

    switch (msg.fixedHeader().qosLevel()) {
        case AT_MOST_ONCE: {
            this.sessionStore.publishMessage(msg, false);
            return Optional.empty();
        }

        case AT_LEAST_ONCE: {
            boolean result = this.sessionStore.publishMessage(msg, false);
            MqttQoS qos = result ? MqttQoS.AT_LEAST_ONCE : MqttQoS.FAILURE;
            MqttMessage rsp = MqttMessageFactory.newMessage(new MqttFixedHeader(MqttMessageType.PUBACK, false, qos, false, ProtocolProcess.fixLengthOfMessageId),
                    MqttMessageIdVariableHeader.from(msg.variableHeader().packetId()), null);
            return Optional.of(rsp);
        }

        case EXACTLY_ONCE:
        default: {
            log.error("DOT NOT support Qos=2, close");
            throw new BrokerException(ErrorCode.MQTT_NOT_SUPPORT_QOS2);
        }
    }
}
 
Example #7
Source File: MqttProtocolUtil.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
public static MqttSubscribeMessage subscribeMessage(List<MqttTopicSubscription> mqttTopicSubscriptions,
		int messageId) {
	MqttSubscribePayload mqttSubscribePayload = new MqttSubscribePayload(mqttTopicSubscriptions);
	MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.SUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE,
			false, 0);
	MqttMessageIdVariableHeader mqttMessageIdVariableHeader = MqttMessageIdVariableHeader.from(messageId);
	return new MqttSubscribeMessage(mqttFixedHeader, mqttMessageIdVariableHeader, mqttSubscribePayload);
}
 
Example #8
Source File: MqttProtocolHandler.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public void processPubAck(Channel client, MqttPubAckMessage pubAckMessage) {
    MqttMessageIdVariableHeader pubAckVariableMessage = pubAckMessage.variableHeader();
    short packageId = (short) pubAckVariableMessage.messageId();
    String clientId = NettyAttrManager.getAttrClientId(client);

    consumerManager.acknowledge(clientId, packageId);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Received PubAck packageID: {}" + packageId);
    }
}
 
Example #9
Source File: MqttProtocolUtil.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
public static MqttUnsubscribeMessage unSubscribeMessage(List<String> topic, int messageId) {
	MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.UNSUBSCRIBE, false, MqttQoS.AT_MOST_ONCE,
			false, 0x02);
	MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);
	MqttUnsubscribePayload mqttUnsubscribeMessage = new MqttUnsubscribePayload(topic);
	return new MqttUnsubscribeMessage(mqttFixedHeader, variableHeader, mqttUnsubscribeMessage);
}
 
Example #10
Source File: MqttProtocolHandler.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
private void sendUnSubAck(Channel client, int packageID, MqttQoS qoS) {
    MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.UNSUBACK, false, qoS, false, 0);
    MqttMessage unSubAckMessage = MqttMessageFactory.newMessage(
            mqttFixedHeader,
            MqttMessageIdVariableHeader.from(packageID),
            null);
    LOG.info("UNSUBSCRIBE successful, packageID: {}", packageID);
    client.writeAndFlush(unSubAckMessage);
}
 
Example #11
Source File: MqttMessageFactory.java    From lannister with Apache License 2.0 5 votes vote down vote up
public static MqttPubAckMessage puback(int messageId) {
	MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBACK, false, MqttQoS.AT_MOST_ONCE, false,
			2);
	MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);

	return new MqttPubAckMessage(fixedHeader, variableHeader);
}
 
Example #12
Source File: MqttMessageFactory.java    From lannister with Apache License 2.0 5 votes vote down vote up
public static MqttMessage pubrec(int messageId) {
	MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBREC, false, MqttQoS.AT_MOST_ONCE, false,
			2);
	MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);

	return new MqttMessage(fixedHeader, variableHeader);
}
 
Example #13
Source File: MqttMessageFactory.java    From lannister with Apache License 2.0 5 votes vote down vote up
public static MqttMessage pubrel(int messageId) {
	MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBREL, false, MqttQoS.AT_LEAST_ONCE, false,
			2);
	MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);

	return new MqttMessage(fixedHeader, variableHeader);
}
 
Example #14
Source File: MqttMessageFactory.java    From lannister with Apache License 2.0 5 votes vote down vote up
public static MqttMessage pubcomp(int messageId) {
	MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBCOMP, false, MqttQoS.AT_MOST_ONCE, false,
			2);
	MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);

	return new MqttMessage(fixedHeader, variableHeader);
}
 
Example #15
Source File: MqttMessageFactory.java    From lannister with Apache License 2.0 5 votes vote down vote up
public static MqttSubscribeMessage subscribe(int messageId, MqttTopicSubscription... topicSubscriptions) {
	int topicNameSize = 0;
	int topicCount = topicSubscriptions.length;

	for (MqttTopicSubscription item : topicSubscriptions) {
		topicNameSize += item.topicName().getBytes(CharsetUtil.UTF_8).length;
	}

	MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.SUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE,
			false, 2 + topicNameSize + topicCount);
	MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);
	MqttSubscribePayload payload = new MqttSubscribePayload(Lists.newArrayList(topicSubscriptions));

	return new MqttSubscribeMessage(fixedHeader, variableHeader, payload);
}
 
Example #16
Source File: MqttMessageFactory.java    From lannister with Apache License 2.0 5 votes vote down vote up
public static MqttSubAckMessage suback(int messageId, List<Integer> grantedQoSLevels) {
	MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.SUBACK, false, MqttQoS.AT_MOST_ONCE, false,
			2 + grantedQoSLevels.size());
	MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);
	MqttSubAckPayload payload = new MqttSubAckPayload(grantedQoSLevels);

	return new MqttSubAckMessage(fixedHeader, variableHeader, payload);
}
 
Example #17
Source File: MqttMessageFactory.java    From lannister with Apache License 2.0 5 votes vote down vote up
public static MqttUnsubAckMessage unsuback(int messageId) {
	MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.UNSUBACK, false, MqttQoS.AT_MOST_ONCE, false,
			2);
	MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);

	return new MqttUnsubAckMessage(fixedHeader, variableHeader);
}
 
Example #18
Source File: MqttPacketReceiver.java    From lannister with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, MqttMessage msg) throws Exception {
	switch (msg.fixedHeader().messageType()) {
	case PUBLISH:
		if (receiver != null) {
			receiver.messageReceived(Message.newMessage(client.clientId(), (MqttPublishMessage) msg));
		}

		int messageId = ((MqttPublishMessage) msg).variableHeader().messageId();
		if (((MqttPublishMessage) msg).fixedHeader().qosLevel() == MqttQoS.AT_LEAST_ONCE) {
			client.send(MqttMessageFactory.puback(messageId));
		}
		else if (((MqttPublishMessage) msg).fixedHeader().qosLevel() == MqttQoS.EXACTLY_ONCE) {
			client.send(MqttMessageFactory.pubrec(messageId));
		}
		break;

	case CONNACK:
		sharedObject.receivedMessage(msg);

		synchronized (sharedObject.locker()) {
			sharedObject.locker().notify();
		}
		break;

	case PUBREC:
		client.send(MqttMessageFactory.pubrel(((MqttMessageIdVariableHeader) msg.variableHeader()).messageId()));
		break;

	case SUBACK:
	case PUBACK:
	case PUBCOMP:
	default:
		break;
	}
}
 
Example #19
Source File: MQTTProtocolHandler.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
void sendPublishProtocolControlMessage(int messageId, MqttMessageType messageType) {
   MqttQoS qos = (messageType == MqttMessageType.PUBREL) ? MqttQoS.AT_LEAST_ONCE : MqttQoS.AT_MOST_ONCE;
   MqttFixedHeader fixedHeader = new MqttFixedHeader(messageType, false, qos, // Spec requires 01 in header for rel
                                                     false, 0);
   MqttPubAckMessage rel = new MqttPubAckMessage(fixedHeader, MqttMessageIdVariableHeader.from(messageId));
   sendToClient(rel);
}
 
Example #20
Source File: ClientProtocolProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * B - P (Qos1)
 * @param channel
 * @param mqttMessage
 */
public void processPubAck(Channel channel, MqttMessage mqttMessage) {
	MqttMessageIdVariableHeader messageIdVariableHeader = (MqttMessageIdVariableHeader) mqttMessage
			.variableHeader();
	int messageId = messageIdVariableHeader.messageId();

	producerProcess.processPubAck(messageId);
	producerProcess.delMessage(messageId);
}
 
Example #21
Source File: ClientProtocolProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * B- P(Qos2)
 * @param channel
 * @param mqttMessage
 */
public void processPubRec(Channel channel, MqttMessage mqttMessage) {
	MqttMessageIdVariableHeader messageIdVariableHeader = (MqttMessageIdVariableHeader) mqttMessage
			.variableHeader();
	int messageId = messageIdVariableHeader.messageId();

	producerProcess.processPubRec(messageId);
	producerProcess.sendPubRel(messageId);
}
 
Example #22
Source File: ClientProtocolProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * B - P (Qos2)
 * @param channel
 * @param mqttMessage
 */
public void processPubComp(Channel channel, MqttMessage mqttMessage) {
	MqttMessageIdVariableHeader messageIdVariableHeader = (MqttMessageIdVariableHeader) mqttMessage
			.variableHeader();
	int messageId = messageIdVariableHeader.messageId();

	producerProcess.processPubComp(messageId);
	producerProcess.delMessage(messageId);
}
 
Example #23
Source File: ClientProtocolProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * B - S(Qos2)
 * @param channel
 * @param mqttMessage
 */
public void processPubRel(Channel channel, MqttMessage mqttMessage) {
	MqttMessageIdVariableHeader messageIdVariableHeader = (MqttMessageIdVariableHeader) mqttMessage
			.variableHeader();
	int messageId = messageIdVariableHeader.messageId();
	this.consumerProcess.processPubRel(messageId);
	this.consumerProcess.sendPubCompMessage(messageId);
	this.consumerProcess.delMesage(messageId);
}
 
Example #24
Source File: ClientProtocolProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * B - S
 * @param channel
 * @param mqttMessage
 */
public void processUnSubBack(Channel channel, MqttMessage mqttMessage) {
	int messageId;
	if (mqttMessage instanceof MqttUnsubAckMessage) {
		MqttUnsubAckMessage mqttUnsubAckMessage = (MqttUnsubAckMessage) mqttMessage;
		messageId = mqttUnsubAckMessage.variableHeader().messageId();
	} else {
		MqttMessageIdVariableHeader o = (MqttMessageIdVariableHeader) mqttMessage.variableHeader();
		messageId = o.messageId();
		NettyLog.error("not UnsubAckMessage:{}", messageId);
	}
	this.consumerProcess.processUnSubBack(messageId);
}
 
Example #25
Source File: MQTTProtocolHandler.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private int getMessageId(MqttMessage message) {
   return ((MqttMessageIdVariableHeader) message.variableHeader()).messageId();
}
 
Example #26
Source File: MqttProtocolUtil.java    From ext-opensource-netty with Mozilla Public License 2.0 4 votes vote down vote up
public static MqttMessage pubRelMessage(int messageId, boolean isDup) {
	return MqttMessageFactory.newMessage(
			new MqttFixedHeader(MqttMessageType.PUBREL, isDup, MqttQoS.AT_LEAST_ONCE, false, 0),
			MqttMessageIdVariableHeader.from(messageId), null);
}
 
Example #27
Source File: Subscribe.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
private MqttMessage genSubAck(int messageId, List<Integer> mqttQoSList) {
    return MqttMessageFactory.newMessage(new MqttFixedHeader(MqttMessageType.SUBACK, false, MqttQoS.AT_LEAST_ONCE, false, ProtocolProcess.fixLengthOfMessageId + mqttQoSList.size()),
            MqttMessageIdVariableHeader.from(messageId),
            new MqttSubAckPayload(mqttQoSList));
}
 
Example #28
Source File: MqttClientImpl.java    From vertx-mqtt with Apache License 2.0 4 votes vote down vote up
/**
 * Handle the MQTT message received from the remote MQTT server
 *
 * @param msg Incoming Packet
 */
private void handleMessage(ChannelHandlerContext chctx, Object msg) {

  // handling directly native Netty MQTT messages, some of them are translated
  // to the related Vert.x ones for polyglotization
  if (msg instanceof io.netty.handler.codec.mqtt.MqttMessage) {

    io.netty.handler.codec.mqtt.MqttMessage mqttMessage = (io.netty.handler.codec.mqtt.MqttMessage) msg;

    DecoderResult result = mqttMessage.decoderResult();
    if (result.isFailure()) {
      chctx.pipeline().fireExceptionCaught(result.cause());
      return;
    }
    if (!result.isFinished()) {
      chctx.pipeline().fireExceptionCaught(new Exception("Unfinished message"));
      return;
    }

    log.debug(String.format("Incoming packet %s", msg));
    switch (mqttMessage.fixedHeader().messageType()) {

      case CONNACK:

        io.netty.handler.codec.mqtt.MqttConnAckMessage connack = (io.netty.handler.codec.mqtt.MqttConnAckMessage) mqttMessage;

        MqttConnAckMessage mqttConnAckMessage = MqttConnAckMessage.create(
          connack.variableHeader().connectReturnCode(),
          connack.variableHeader().isSessionPresent());
        handleConnack(mqttConnAckMessage);
        break;

      case PUBLISH:

        io.netty.handler.codec.mqtt.MqttPublishMessage publish = (io.netty.handler.codec.mqtt.MqttPublishMessage) mqttMessage;
        ByteBuf newBuf = VertxHandler.safeBuffer(publish.payload(), chctx.alloc());

        MqttPublishMessage mqttPublishMessage = MqttPublishMessage.create(
          publish.variableHeader().packetId(),
          publish.fixedHeader().qosLevel(),
          publish.fixedHeader().isDup(),
          publish.fixedHeader().isRetain(),
          publish.variableHeader().topicName(),
          newBuf);
        handlePublish(mqttPublishMessage);
        break;

      case PUBACK:
        handlePuback(((MqttMessageIdVariableHeader) mqttMessage.variableHeader()).messageId());
        break;

      case PUBREC:
        handlePubrec(((MqttMessageIdVariableHeader) mqttMessage.variableHeader()).messageId());
        break;

      case PUBREL:
        handlePubrel(((MqttMessageIdVariableHeader) mqttMessage.variableHeader()).messageId());
        break;

      case PUBCOMP:
        handlePubcomp(((MqttMessageIdVariableHeader) mqttMessage.variableHeader()).messageId());
        break;

      case SUBACK:

        io.netty.handler.codec.mqtt.MqttSubAckMessage unsuback = (io.netty.handler.codec.mqtt.MqttSubAckMessage) mqttMessage;

        MqttSubAckMessage mqttSubAckMessage = MqttSubAckMessage.create(
          unsuback.variableHeader().messageId(),
          unsuback.payload().grantedQoSLevels());
        handleSuback(mqttSubAckMessage);
        break;

      case UNSUBACK:
        handleUnsuback(((MqttMessageIdVariableHeader) mqttMessage.variableHeader()).messageId());
        break;

      case PINGRESP:
        handlePingresp();
        break;

      default:

        chctx.pipeline().fireExceptionCaught(new Exception("Wrong message type " + msg.getClass().getName()));
        break;
    }

  } else {

    chctx.pipeline().fireExceptionCaught(new Exception("Wrong message type"));
  }
}
 
Example #29
Source File: Utils.java    From cassandana with Apache License 2.0 4 votes vote down vote up
public static int messageId(MqttMessage msg) {
    return ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId();
}
 
Example #30
Source File: ProtocolProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 4 votes vote down vote up
/**
 *  S - B (Qos2)
 * @param channel
 * @param variableHeader
 */
public void processPubRec(Channel channel, MqttMessageIdVariableHeader variableHeader) {
	int messageId = variableHeader.messageId();
	this.consumerProcess.processPubRec(channel, messageId);
	this.sendProcess.sendPubRelMessage(channel, messageId, false);
}