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

The following examples show how to use io.netty.handler.codec.mqtt.MqttQoS#EXACTLY_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: 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 2
Source File: VertxMqttConnection.java    From jetlinks-community with Apache License 2.0 5 votes vote down vote up
@Override
public void acknowledge() {
    if (acknowledged) {
        return;
    }
    acknowledged = true;
    if (message.qosLevel() == MqttQoS.AT_LEAST_ONCE) {
        log.debug("PUBACK QoS1 mqtt[{}] message[{}]", getClientId(), message.messageId());
        endpoint.publishAcknowledge(message.messageId());
    } else if (message.qosLevel() == MqttQoS.EXACTLY_ONCE) {
        log.debug("PUBREC QoS2 mqtt[{}] message[{}]", getClientId(), message.messageId());
        endpoint.publishReceived(message.messageId());
    }
}
 
Example 3
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 4
Source File: ConsumerProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
private boolean getMustStorePubMessage(MqttQoS respQoS) {
	boolean bSaveMsg = false;
	if (respQoS == MqttQoS.AT_MOST_ONCE) {
		bSaveMsg = false;
	} else if (respQoS == MqttQoS.AT_LEAST_ONCE) {
		bSaveMsg = true;
	} else if (respQoS == MqttQoS.EXACTLY_ONCE) {
		bSaveMsg = false;
		;
	}
	return bSaveMsg;
}
 
Example 5
Source File: ConsumerProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
private int getMustSendMessageId(MqttQoS qosLevel, String clientId) {
	int msgId = 0;
	if (qosLevel == MqttQoS.AT_MOST_ONCE) {
		msgId = 0;
	} else if (qosLevel == MqttQoS.AT_LEAST_ONCE) {
		msgId = messageIdService.getNextMessageId(clientId);
	} else if (qosLevel == MqttQoS.EXACTLY_ONCE) {
		msgId = messageIdService.getNextMessageId(clientId);
	}
	return msgId;
}
 
Example 6
Source File: ClientProtocolProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * B - S(Qos0, Qos1, Qos2)
 * @param channel
 * @param mqttMessage
 */
public void processPublish(Channel channel, MqttPublishMessage mqttMessage) {
	MqttFixedHeader mqttFixedHeader = mqttMessage.fixedHeader();
	MqttPublishVariableHeader mqttPublishVariableHeader = mqttMessage.variableHeader();
	ByteBuf payload = mqttMessage.payload();
	String topciName = mqttPublishVariableHeader.topicName();
	MqttQoS qosLevel = mqttFixedHeader.qosLevel();
	int messageId = mqttPublishVariableHeader.packetId();
	byte[] bytes = ByteBufUtil.copyByteBuf(payload); 

	MessageData recviceMessage = MessageData.builder().topic(topciName).qos(qosLevel.value())
			.messageId(messageId).payload(bytes)
			.status(MessageStatus.PUB)
			.dup(mqttFixedHeader.isDup())
			.retained(mqttFixedHeader.isRetain()).build();

	if (qosLevel == MqttQoS.EXACTLY_ONCE) {
		this.consumerProcess.saveMesage(recviceMessage);
	}

	this.consumerProcess.processPublish(recviceMessage);

	switch (qosLevel) {
	case AT_MOST_ONCE:
		break;
	case AT_LEAST_ONCE:
		this.consumerProcess.sendPubAckMessage(messageId);
		break;
	case EXACTLY_ONCE:
		this.consumerProcess.sendPubRecMessage(messageId);
		break;
	default:
		break;
	}
}
 
Example 7
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 8
Source File: ProtocolProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 4 votes vote down vote up
/**
 *  P - B (Qos0,Qos1,Qos2)
 * @param channel
 * @param msg
 */
public void processPublish(Channel channel, MqttPublishMessage msg) {
	String clientId = NettyUtil.getClientId(channel);
	MqttQoS qosLevel = msg.fixedHeader().qosLevel();
	int packetId = msg.variableHeader().packetId();
	String topicName = msg.variableHeader().topicName();
	
	if (!topicProcess.checkVaildTopic(topicName)) {
		NettyLog.debug("In Vaild Topic: {}", topicName);
		return ;
	}
	
	boolean isRetain = msg.fixedHeader().isRetain();
	byte[] msgBytes = ByteBufUtil.copyByteBuf(msg.payload());

	BorkerMessage bMsgInfo = BorkerMessage.builder().sourceClientId(clientId).sourceMsgId(packetId)
			.topicName(topicName).iQosLevel(qosLevel.value()).msgBytes(msgBytes).retain(isRetain).build();

	NettyLog.debug("processPublish: {}", bMsgInfo);

	List<SubscribeTopicInfo> subscribeClientList = this.topicProcess.search(bMsgInfo.getTopicName());

	this.procedureProcess.processPublish(channel, bMsgInfo);
	
	if (qosLevel != MqttQoS.EXACTLY_ONCE) {
		this.consumerProcess.sendSubscribMessage(bMsgInfo, subscribeClientList);
	}

	if (qosLevel == MqttQoS.AT_MOST_ONCE) {
	} else if (qosLevel == MqttQoS.AT_LEAST_ONCE) {
		this.sendProcess.sendPubAckMessage(channel, packetId);
	} else if (qosLevel == MqttQoS.EXACTLY_ONCE) {
		this.sendProcess.sendPubRecMessage(channel, packetId);
	} else {
	}

	// retain=1, 保留消息
	if (isRetain) {
		topicProcess.publicRetainMessage(bMsgInfo);
	}
	
	if (pubishMessageLister != null) {
		pubishMessageLister.processMessage(bMsgInfo);
	}
}