CNode addSubscription(Subscription newSubscription) { // if already contains one with same topic and same client, keep that with higher QoS if (subscriptions.contains(newSubscription)) { final Subscription existing = subscriptions.stream() .filter(s -> s.equals(newSubscription)) .findFirst().get(); if (existing.getRequestedQos().value() < newSubscription.getRequestedQos().value()) { subscriptions.remove(existing); subscriptions.add(new Subscription(newSubscription)); } } else { this.subscriptions.add(new Subscription(newSubscription)); } return this; }
private static Result<MqttSubAckPayload> decodeSubackPayload( ByteBuf buffer, int bytesRemainingInVariablePart) { final List<Integer> grantedQos = new ArrayList<Integer>(); int numberOfBytesConsumed = 0; while (numberOfBytesConsumed < bytesRemainingInVariablePart) { int qos = buffer.readUnsignedByte(); if (qos != MqttQoS.FAILURE.value()) { qos &= 0x03; } numberOfBytesConsumed++; grantedQos.add(qos); } return new Result<MqttSubAckPayload>(new MqttSubAckPayload(grantedQos), numberOfBytesConsumed); }
@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); } }
/** * Create the SUBACK response from a list of topicFilters */ private MqttSubAckMessage doAckMessageFromValidateFilters(List<MqttTopicSubscription> topicFilters, int messageId) { List<Integer> grantedQoSLevels = new ArrayList<>(); for (MqttTopicSubscription req : topicFilters) { grantedQoSLevels.add(req.qualityOfService().value()); } MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.SUBACK, false, AT_MOST_ONCE, false, 0); MqttSubAckPayload payload = new MqttSubAckPayload(grantedQoSLevels); return new MqttSubAckMessage(fixedHeader, from(messageId), payload); }
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; }
private static int getFixedHeaderByte1(MqttFixedHeader header) { int ret = 0; ret |= header.messageType().value() << 4; if (header.isDup()) { ret |= 0x08; } ret |= header.qosLevel().value() << 1; if (header.isRetain()) { ret |= 0x01; } return ret; }
private void verifyFailureQos(MqttSubAckMessage subAckMsg) { List<Integer> grantedQoSes = subAckMsg.payload().grantedQoSLevels(); assertEquals(1, grantedQoSes.size()); assertTrue(grantedQoSes.contains(MqttQoS.FAILURE.value())); }
buf.writeShort(topicNameBytes.length); buf.writeBytes(topicNameBytes, 0, topicNameBytes.length); buf.writeByte(topic.qualityOfService().value());
public MqttConnectMessage build() { MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0); MqttConnectVariableHeader mqttConnectVariableHeader = new MqttConnectVariableHeader( version.protocolName(), version.protocolLevel(), hasUser, hasPassword, willRetain, willQos.value(), willFlag, cleanSession, keepAliveSecs); MqttConnectPayload mqttConnectPayload = new MqttConnectPayload(clientId, willTopic, willMessage, username, password); return new MqttConnectMessage(mqttFixedHeader, mqttConnectVariableHeader, mqttConnectPayload); } }
private static Result<MqttPublishVariableHeader> decodePublishVariableHeader( ByteBuf buffer, MqttFixedHeader mqttFixedHeader) { final Result<String> decodedTopic = decodeString(buffer); if (!isValidPublishTopicName(decodedTopic.value)) { throw new DecoderException("invalid publish topic name: " + decodedTopic.value + " (contains wildcards)"); } int numberOfBytesConsumed = decodedTopic.numberOfBytesConsumed; int messageId = -1; if (mqttFixedHeader.qosLevel().value() > 0) { final Result<Integer> decodedMessageId = decodeMessageId(buffer); messageId = decodedMessageId.value; numberOfBytesConsumed += decodedMessageId.numberOfBytesConsumed; } final MqttPublishVariableHeader mqttPublishVariableHeader = new MqttPublishVariableHeader(decodedTopic.value, messageId); return new Result<MqttPublishVariableHeader>(mqttPublishVariableHeader, numberOfBytesConsumed); }
static byte encodeFlags(MqttMessage message) { byte flags = 0; if (message.fixedHeader().isDup()) { flags |= (byte) 0x08; } if (message.fixedHeader().isRetain()) { flags |= (byte) 0x01; } flags |= (byte) ((message.fixedHeader().qosLevel().value() & 0x03) << 1); return flags; }
private void subscribe(MqttQoS topic, String newsTopic, MQTTConnection connection) { MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(topic, newsTopic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, this.connection); MqttSubAckMessage subAck = ((EmbeddedChannel) this.connection.channel).readOutbound(); assertEquals(topic.value(), (int) subAck.payload().grantedQoSLevels().get(0)); }
private void subscribe(MqttQoS topic, String newsTopic, MQTTConnection connection) { MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(topic, newsTopic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, this.connection); MqttSubAckMessage subAck = ((EmbeddedChannel) this.connection.channel).readOutbound(); assertEquals(topic.value(), (int) subAck.payload().grantedQoSLevels().get(0)); }
protected void subscribe(EmbeddedChannel channel, String topic, MqttQoS desiredQos) { MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, FAKE_CLIENT_ID, null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = NettyUtils.clientID(channel); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchWithoutQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); }
protected void subscribe(MQTTConnection connection, String topic, MqttQoS desiredQos) { EmbeddedChannel channel = (EmbeddedChannel) connection.channel; MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = connection.getClientId(); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchWithoutQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); }
protected void subscribe(MQTTConnection connection, String topic, MqttQoS desiredQos) { EmbeddedChannel channel = (EmbeddedChannel) connection.channel; MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = connection.getClientId(); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchWithoutQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); }
protected void subscribe(MQTTConnection connection, String topic, MqttQoS desiredQos) { EmbeddedChannel channel = (EmbeddedChannel) connection.channel; MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = connection.getClientId(); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchWithoutQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); }
protected void subscribe(MQTTConnection connection, String topic, MqttQoS desiredQos) { EmbeddedChannel channel = (EmbeddedChannel) connection.channel; MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = connection.getClientId(); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); //assertTrue(matchedSubscriptions.size() >=1); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); // assertTrue(matchedSubscriptions.contains(expectedSubscription)); }