Java Code Examples for io.netty.handler.codec.mqtt.MqttQoS#AT_MOST_ONCE

The following examples show how to use io.netty.handler.codec.mqtt.MqttQoS#AT_MOST_ONCE . 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: 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 2
Source File: SessionTest.java    From lannister with Apache License 2.0 6 votes vote down vote up
@Test
public void testMatches() throws Exception {
	String testTopic = "testTopic/test";
	Session session = new Session("1", "1", 1, 50, true, null);

	TopicSubscription ts0 = new TopicSubscription(session.clientId(), "testTopic/#", MqttQoS.AT_MOST_ONCE);
	TopicSubscription ts1 = new TopicSubscription(session.clientId(), "testTopic/+", MqttQoS.AT_LEAST_ONCE);
	TopicSubscription ts2 = new TopicSubscription(session.clientId(), testTopic, MqttQoS.EXACTLY_ONCE);

	TopicSubscription.NEXUS.put(ts0);
	TopicSubscription.NEXUS.put(ts1);
	TopicSubscription.NEXUS.put(ts2);

	Assert.assertEquals(3, TopicSubscription.NEXUS.topicFiltersOf(session.clientId()).size());

	TopicSubscription target = session.matches(testTopic);

	Assert.assertTrue(target.topicFilter().equals(testTopic));
}
 
Example 3
Source File: MqttServerBadClientTest.java    From vertx-mqtt with Apache License 2.0 5 votes vote down vote up
private MqttMessage createConnectPacket(MqttClientOptions options) {
  MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT,
    false,
    MqttQoS.AT_MOST_ONCE,
    false,
    0);

  MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader(
    PROTOCOL_NAME,
    PROTOCOL_VERSION,
    options.hasUsername(),
    options.hasPassword(),
    options.isWillRetain(),
    options.getWillQoS(),
    options.isWillFlag(),
    options.isCleanSession(),
    options.getKeepAliveTimeSeconds()
  );

  MqttConnectPayload payload = new MqttConnectPayload(
    options.getClientId() == null ? "" : options.getClientId(),
    options.getWillTopic(),
    options.getWillMessage() != null ? options.getWillMessage().getBytes(StandardCharsets.UTF_8) : null,
    options.hasUsername() ? options.getUsername() : null,
    options.hasPassword() ? options.getPassword().getBytes(StandardCharsets.UTF_8) : null
  );

  return MqttMessageFactory.newMessage(fixedHeader, variableHeader, payload);
}
 
Example 4
Source File: ProcedureProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
private boolean getMustStoreRelMessage(MqttQoS qosLevel) {
	boolean bSaveMsg = false;
	if (qosLevel == MqttQoS.AT_MOST_ONCE) {
		bSaveMsg = false;
	} else if (qosLevel == MqttQoS.AT_LEAST_ONCE) {
		bSaveMsg = false;
	} else if (qosLevel == MqttQoS.EXACTLY_ONCE) {
		bSaveMsg = true;
	}
	return bSaveMsg;
}
 
Example 5
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 6
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 7
Source File: MqttPingHandler.java    From smartacus-mqtt-broker with Apache License 2.0 5 votes vote down vote up
private void sendPingReq(Channel channel){
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PINGREQ, false, MqttQoS.AT_MOST_ONCE, false, 0);
    channel.writeAndFlush(new MqttMessage(fixedHeader));

    if(this.pingRespTimeout != null){
        this.pingRespTimeout = channel.eventLoop().schedule(() -> {
            MqttFixedHeader fixedHeader2 = new MqttFixedHeader(MqttMessageType.DISCONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0);
            channel.writeAndFlush(new MqttMessage(fixedHeader2)).addListener(ChannelFutureListener.CLOSE);
            //TODO: what do when the connection is closed ?
        }, this.keepaliveSeconds, TimeUnit.SECONDS);
    }
}
 
Example 8
Source File: MqttMessageFactory.java    From lannister with Apache License 2.0 4 votes vote down vote up
public static MqttMessage pingresp() {
	MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false,
			0);

	return new MqttMessage(fixedHeader);
}
 
Example 9
Source File: MessageUtil.java    From iot-mqtt with Apache License 2.0 4 votes vote down vote up
public static MqttConnAckMessage getConnectAckMessage(MqttConnectReturnCode returnCode,boolean sessionPresent){
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE, false, 0);
    MqttConnAckVariableHeader variableHeade = new MqttConnAckVariableHeader(returnCode,sessionPresent);
    return new MqttConnAckMessage(fixedHeader,variableHeade);
}
 
Example 10
Source File: MqttProtocolHandler.java    From joyqueue with Apache License 2.0 4 votes vote down vote up
public void processSubscribe(Channel client, MqttSubscribeMessage subscribeMessage) {
    List<Integer> resultCodes = new ArrayList<>();
    String clientID = NettyAttrManager.getAttrClientId(client);
    if (connectionManager.isConnected(clientID)) {
        MqttConnection connection = connectionManager.getConnection(clientID);

        if (LOG.isDebugEnabled()) {
            LOG.debug("处理subscribe数据包, clientID: {}, cleanSession: {}", clientID, connection.isCleanSession());
        }

        List<MqttTopicSubscription> topicSubscribes = subscribeMessage.payload().topicSubscriptions();
        LOG.info("Subscribe topics: {}, clientID: {}", topicSubscribes, clientID);

        try {
            if (null != topicSubscribes) {
                Set<MqttSubscription> topicFilters = subscribe(topicSubscribes, clientID, connection.getClientGroupName(), resultCodes);
                MqttSession session = sessionManager.getSession(clientID);
                if (session != null) {
                    for (MqttSubscription subscription : topicFilters) {
                        session.addSubscription(subscription);
                    }
                }
            } else {
                // The payload of a SUBSCRIBE packet MUST contain at least one Topic Filter / QoS pair. A SUBSCRIBE packet with no payload is a protocol violation
                // it MUST close the Network Connection on which it received that Control Packet which caused the protocol violation
                consumerManager.stopConsume(clientID);
                sessionManager.removeSession(clientID);
                connection.getChannel().close().addListener(CLOSE_ON_FAILURE);
                connectionManager.removeConnection(connection);
                client.close().addListener(CLOSE_ON_FAILURE);
            }
        } catch (Exception e) {
            LOG.error("subscribe is error!");
            if (resultCodes.size() < topicSubscribes.size()) {
                int minus = topicSubscribes.size() - resultCodes.size();
                for (; minus > 0; minus--) {
                    resultCodes.add(MqttQoS.FAILURE.value());
                }
            }
        }
    }

    MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.SUBACK, false, MqttQoS.AT_MOST_ONCE, false, 0);
    MqttSubAckMessage subAckMessage = (MqttSubAckMessage) MqttMessageFactory.newMessage(
            mqttFixedHeader,
            MqttMessageIdVariableHeader.from(subscribeMessage.variableHeader().messageId()),
            new MqttSubAckPayload(resultCodes));
    LOG.info("SUBSCRIBE successful, subscribe result: {}", resultCodes);
    client.writeAndFlush(subAckMessage);
}
 
Example 11
Source File: MessageUtil.java    From iot-mqtt with Apache License 2.0 4 votes vote down vote up
public static MqttMessage getPubComMessage(int messageId){
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBCOMP,false,MqttQoS.AT_MOST_ONCE,false,0);
    MqttMessage mqttMessage = new MqttMessage(fixedHeader,MqttMessageIdVariableHeader.from(messageId));
    return mqttMessage;
}
 
Example 12
Source File: MessageUtil.java    From iot-mqtt with Apache License 2.0 4 votes vote down vote up
public static MqttMessage getSubAckMessage(int messageId, List<Integer> qos){
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.SUBACK,false,MqttQoS.AT_MOST_ONCE,false,0);
    MqttMessageIdVariableHeader idVariableHeader = MqttMessageIdVariableHeader.from(messageId);
    MqttSubAckPayload subAckPayload = new MqttSubAckPayload(qos);
    return new MqttSubAckMessage(fixedHeader,idVariableHeader,subAckPayload);
}
 
Example 13
Source File: MessageUtil.java    From iot-mqtt with Apache License 2.0 4 votes vote down vote up
public static MqttMessage getPubRelMessage(int messageId){
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBREL,false,MqttQoS.AT_MOST_ONCE,false,0);
    MqttMessageIdVariableHeader idVariableHeader = MqttMessageIdVariableHeader.from(messageId);
    return new MqttMessage(fixedHeader,idVariableHeader);
}
 
Example 14
Source File: MQTTProtocolHandler.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
void handleUnsubscribe(MqttUnsubscribeMessage message) throws Exception {
   session.getSubscriptionManager().removeSubscriptions(message.payload().topics());
   MqttFixedHeader header = new MqttFixedHeader(MqttMessageType.UNSUBACK, false, MqttQoS.AT_MOST_ONCE, false, 0);
   MqttUnsubAckMessage m = new MqttUnsubAckMessage(header, message.variableHeader());
   sendToClient(m);
}
 
Example 15
Source File: MqttHardwareLoginHandler.java    From blynk-server with GNU General Public License v3.0 4 votes vote down vote up
private static MqttConnAckMessage createConnAckMessage(MqttConnectReturnCode code) {
    MqttFixedHeader mqttFixedHeader =
            new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE, false, 2);
    MqttConnAckVariableHeader mqttConnAckVariableHeader = new MqttConnAckVariableHeader(code, true);
    return new MqttConnAckMessage(mqttFixedHeader, mqttConnAckVariableHeader);
}
 
Example 16
Source File: MQTTProtocolHandler.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
void handlePingreq() {
   MqttMessage pingResp = new MqttMessage(new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false, 0));
   sendToClient(pingResp);
}
 
Example 17
Source File: MqttEndpointImpl.java    From vertx-mqtt with Apache License 2.0 3 votes vote down vote up
public MqttEndpointImpl publishComplete(int publishMessageId) {

    MqttFixedHeader fixedHeader =
      new MqttFixedHeader(MqttMessageType.PUBCOMP, false, MqttQoS.AT_MOST_ONCE, false, 0);
    MqttMessageIdVariableHeader variableHeader =
      MqttMessageIdVariableHeader.from(publishMessageId);

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

    this.write(pubcomp);

    return this;
  }
 
Example 18
Source File: MqttEndpointImpl.java    From vertx-mqtt with Apache License 2.0 3 votes vote down vote up
public MqttEndpointImpl unsubscribeAcknowledge(int unsubscribeMessageId) {

    MqttFixedHeader fixedHeader =
      new MqttFixedHeader(MqttMessageType.UNSUBACK, false, MqttQoS.AT_MOST_ONCE, false, 0);
    MqttMessageIdVariableHeader variableHeader =
      MqttMessageIdVariableHeader.from(unsubscribeMessageId);

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

    this.write(unsuback);

    return this;
  }
 
Example 19
Source File: MqttEndpointImpl.java    From vertx-mqtt with Apache License 2.0 3 votes vote down vote up
public MqttEndpointImpl pong() {

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

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

    this.write(pingresp);

    return this;
  }
 
Example 20
Source File: MqttClientImpl.java    From vertx-mqtt with Apache License 2.0 3 votes vote down vote up
/**
 * See {@link MqttClient#ping()} for more details
 */
@Override
public MqttClient ping() {

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

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

  this.write(pingreq);

  return this;
}