Java Code Examples for io.netty.handler.codec.mqtt.MqttQoS#AT_LEAST_ONCE
The following examples show how to use
io.netty.handler.codec.mqtt.MqttQoS#AT_LEAST_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: Publish.java From WeEvent with Apache License 2.0 | 6 votes |
@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 2
Source File: SessionTest.java From lannister with Apache License 2.0 | 6 votes |
@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: CommandSubscriptionTest.java From hono with Eclipse Public License 2.0 | 6 votes |
/** * Verifies subscription pattern with auth device differing device id in topic. * This represents a scenario where a gateway subscribes on behalf of a device. */ @Test public void testSubscriptionAuthWithDifferentDeviceId() { final String gatewayManagedDeviceId = "gatewayManagedDevice"; final MqttTopicSubscription mqttTopicSubscription = new MqttTopicSubscriptionImpl( getCommandEndpoint() + "//" + gatewayManagedDeviceId + "/req/#", MqttQoS.AT_LEAST_ONCE); final CommandSubscription subscription = CommandSubscription.fromTopic(mqttTopicSubscription, device, "testMqttClient"); assertThat(subscription).isNotNull(); assertThat(subscription.getTenant()).isEqualTo(device.getTenantId()); assertThat(subscription.getDeviceId()).isEqualTo(gatewayManagedDeviceId); assertThat(subscription.getAuthenticatedDeviceId()).isEqualTo(device.getDeviceId()); assertThat(subscription.getTopic()).isEqualTo(getCommandEndpoint() + "//" + gatewayManagedDeviceId + "/req/#"); assertThat(subscription.getQos()).isEqualTo(MqttQoS.AT_LEAST_ONCE); assertThat(subscription.getClientId()).isEqualTo("testMqttClient"); assertThat(subscription.isGatewaySubscriptionForSpecificDevice()).isEqualTo(true); }
Example 4
Source File: MqttContext.java From hono with Eclipse Public License 2.0 | 5 votes |
/** * Checks if the message has been published using QoS 1. * * @return {@code true} if the message has been published using QoS 1. */ public boolean isAtLeastOnce() { if (message == null) { return false; } else { return MqttQoS.AT_LEAST_ONCE == message.qosLevel(); } }
Example 5
Source File: MQTTProtocolHandler.java From activemq-artemis with Apache License 2.0 | 5 votes |
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 6
Source File: MqttPacketReceiver.java From lannister with Apache License 2.0 | 5 votes |
@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 7
Source File: MqttMessageFactory.java From lannister with Apache License 2.0 | 5 votes |
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 8
Source File: MqttMessageFactory.java From lannister with Apache License 2.0 | 5 votes |
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 9
Source File: CommandSubscriptionTest.java From hono with Eclipse Public License 2.0 | 5 votes |
/** * Verifies subscription pattern with authenticated device, correct pattern and valid qos. */ @Test public void testSubscriptionAuthWithQoS() { final MqttTopicSubscription mqttTopicSubscription = new MqttTopicSubscriptionImpl( getCommandEndpoint() + "/+/+/req/#", MqttQoS.AT_LEAST_ONCE); final CommandSubscription subscription = CommandSubscription.fromTopic(mqttTopicSubscription, device, "testMqttClient"); assertThat(subscription).isNotNull(); assertThat(subscription.getTenant()).isEqualTo(device.getTenantId()); assertThat(subscription.getDeviceId()).isEqualTo(device.getDeviceId()); assertThat(subscription.getTopic()).isEqualTo(getCommandEndpoint() + "/+/+/req/#"); assertThat(subscription.getQos()).isEqualTo(MqttQoS.AT_LEAST_ONCE); assertThat(subscription.getClientId()).isEqualTo("testMqttClient"); }
Example 10
Source File: VertxMqttConnection.java From jetlinks-community with Apache License 2.0 | 5 votes |
@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 11
Source File: JsonMqttAdaptor.java From iotplatform with Apache License 2.0 | 5 votes |
private MqttPublishMessage createMqttPublishMsg(DeviceSessionCtx ctx, String topic, JsonElement json) { MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, false, MqttQoS.AT_LEAST_ONCE, false, 0); MqttPublishVariableHeader header = new MqttPublishVariableHeader(topic, ctx.nextMsgId()); ByteBuf payload = ALLOCATOR.buffer(); payload.writeBytes(GSON.toJson(json).getBytes(UTF8)); return new MqttPublishMessage(mqttFixedHeader, header, payload); }
Example 12
Source File: MqttDeviceAwareSessionContext.java From Groza with Apache License 2.0 | 5 votes |
public MqttQoS getQoSForTopic(String topic) { List<Integer> qosList = mqttQoSMap.entrySet() .stream() .filter(entry -> entry.getKey().matches(topic)) .map(Map.Entry::getValue) .collect(Collectors.toList()); if (!qosList.isEmpty()) { return MqttQoS.valueOf(qosList.get(0)); } else { return MqttQoS.AT_LEAST_ONCE; } }
Example 13
Source File: MqttProtocolUtil.java From ext-opensource-netty with Mozilla Public License 2.0 | 5 votes |
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 14
Source File: ConsumerProcess.java From ext-opensource-netty with Mozilla Public License 2.0 | 5 votes |
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 15
Source File: ConsumerProcess.java From ext-opensource-netty with Mozilla Public License 2.0 | 5 votes |
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 16
Source File: ProcedureProcess.java From ext-opensource-netty with Mozilla Public License 2.0 | 5 votes |
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 17
Source File: Connect.java From WeEvent with Apache License 2.0 | 5 votes |
public MqttMessage processConnect(MqttConnectMessage msg, SessionContext sessionData) { MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_LEAST_ONCE, false, 0); String clientId = sessionData.getClientId(); if (StringUtils.isBlank(clientId)) { log.error("clientId is empty, reject"); return MqttMessageFactory.newMessage(fixedHeader, new MqttConnAckVariableHeader(MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED, false), null); } // verify userName and password String username = msg.payload().userName(); String password = msg.payload().passwordInBytes() == null ? null : new String(msg.payload().passwordInBytes(), StandardCharsets.UTF_8); if (!this.authService.verifyUserName(username, password)) { log.error("verify account failed, reject"); return MqttMessageFactory.newMessage(fixedHeader, new MqttConnAckVariableHeader(MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD, false), null); } if (this.sessionStore.existSession(clientId)) { log.info("exist client id, force to delete the older"); this.sessionStore.removeSession(clientId); } // store new session this.sessionStore.addSession(clientId, sessionData); log.info("MQTT connected, clientId: {}", clientId); return MqttMessageFactory.newMessage(fixedHeader, new MqttConnAckVariableHeader(MqttConnectReturnCode.CONNECTION_ACCEPTED, false), null); }
Example 18
Source File: ProtocolProcess.java From ext-opensource-netty with Mozilla Public License 2.0 | 4 votes |
/** * 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); } }
Example 19
Source File: MqttEndpointImpl.java From vertx-mqtt with Apache License 2.0 | 3 votes |
public MqttEndpointImpl 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); this.write(pubrel); return this; }
Example 20
Source File: MqttServerBadClientTest.java From vertx-mqtt with Apache License 2.0 | 3 votes |
private MqttPublishMessage createPublishMessage() { MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, false, MqttQoS.AT_LEAST_ONCE, true, 0); MqttPublishVariableHeader mqttPublishVariableHeader = new MqttPublishVariableHeader(MQTT_TOPIC, 1); ByteBuf payload = ALLOCATOR.buffer(); payload.writeBytes(MQTT_MESSAGE.getBytes(CharsetUtil.UTF_8)); return new MqttPublishMessage(mqttFixedHeader, mqttPublishVariableHeader, payload); }