@Override public void put(String topicFilter, SubscribeStore subscribeStore) { if (StrUtil.contains(topicFilter, '#') || StrUtil.contains(topicFilter, '+')) { subscribeWildcardCache.put(topicFilter, subscribeStore.getClientId(), subscribeStore); } else { subscribeNotWildcardCache.put(topicFilter, subscribeStore.getClientId(), subscribeStore); } }
public void processDisConnect(Channel channel, MqttMessage msg) { String clientId = (String) channel.attr(AttributeKey.valueOf("clientId")).get(); SessionStore sessionStore = sessionStoreService.get(clientId); if (sessionStore != null && sessionStore.isCleanSession()) { subscribeStoreService.removeForClient(clientId); dupPublishMessageStoreService.removeByClient(clientId); dupPubRelMessageStoreService.removeByClient(clientId); } LOGGER.debug("DISCONNECT - clientId: {}, cleanSession: {}", clientId, sessionStore.isCleanSession()); sessionStoreService.remove(clientId); channel.close(); }
public void processUnSubscribe(Channel channel, MqttUnsubscribeMessage msg) { List<String> topicFilters = msg.payload().topics(); String clinetId = (String) channel.attr(AttributeKey.valueOf("clientId")).get(); topicFilters.forEach(topicFilter -> { subscribeStoreService.remove(topicFilter, clinetId); LOGGER.debug("UNSUBSCRIBE - clientId: {}, topicFilter: {}", clinetId, topicFilter); }); MqttUnsubAckMessage unsubAckMessage = (MqttUnsubAckMessage) MqttMessageFactory.newMessage( new MqttFixedHeader(MqttMessageType.UNSUBACK, false, MqttQoS.AT_MOST_ONCE, false, 0), MqttMessageIdVariableHeader.from(msg.variableHeader().messageId()), null); channel.writeAndFlush(unsubAckMessage); }
private void sendPublishMessage(String topic, MqttQoS mqttQoS, byte[] messageBytes, boolean retain, boolean dup) { List<SubscribeStore> subscribeStores = subscribeStoreService.search(topic); subscribeStores.forEach(subscribeStore -> { if (sessionStoreService.containsKey(subscribeStore.getClientId())) { MqttQoS respQoS = mqttQoS.value() > subscribeStore.getMqttQoS() ? MqttQoS.valueOf(subscribeStore.getMqttQoS()) : mqttQoS; if (respQoS == MqttQoS.AT_MOST_ONCE) { MqttPublishMessage publishMessage = (MqttPublishMessage) MqttMessageFactory.newMessage( new MqttFixedHeader(MqttMessageType.PUBLISH, dup, respQoS, retain, 0), new MqttPublishVariableHeader(topic, 0), Unpooled.buffer().writeBytes(messageBytes)); LOGGER.debug("PUBLISH - clientId: {}, topic: {}, Qos: {}", subscribeStore.getClientId(), topic, respQoS.value()); ChannelId channelId = channelIdMap.get(sessionStoreService.get(subscribeStore.getClientId()).getChannelId()); if(channelId!=null) { Channel channel = channelGroup.find(channelId); new MqttFixedHeader(MqttMessageType.PUBLISH, dup, respQoS, retain, 0), new MqttPublishVariableHeader(topic, messageId), Unpooled.buffer().writeBytes(messageBytes)); LOGGER.debug("PUBLISH - clientId: {}, topic: {}, Qos: {}, messageId: {}", subscribeStore.getClientId(), topic, respQoS.value(), messageId); DupPublishMessageStore dupPublishMessageStore = new DupPublishMessageStore().setClientId(subscribeStore.getClientId()) .setTopic(topic).setMqttQoS(respQoS.value()).setMessageBytes(messageBytes).setMessageId(messageId); dupPublishMessageStoreService.put(subscribeStore.getClientId(), dupPublishMessageStore); ChannelId channelId = channelIdMap.get(sessionStoreService.get(subscribeStore.getClientId()).getChannelId()); if(channelId!=null) { Channel channel = channelGroup.find(channelId); new MqttFixedHeader(MqttMessageType.PUBLISH, dup, respQoS, retain, 0), new MqttPublishVariableHeader(topic, messageId), Unpooled.buffer().writeBytes(messageBytes)); LOGGER.debug("PUBLISH - clientId: {}, topic: {}, Qos: {}, messageId: {}", subscribeStore.getClientId(), topic, respQoS.value(), messageId); DupPublishMessageStore dupPublishMessageStore = new DupPublishMessageStore().setClientId(subscribeStore.getClientId()) .setTopic(topic).setMqttQoS(respQoS.value()).setMessageBytes(messageBytes).setMessageId(messageId);
private void sendPublishMessage(String clientId, String topic, MqttQoS mqttQoS, byte[] messageBytes, boolean retain, boolean dup) { List<SubscribeStore> subscribeStores = subscribeStoreService.search(topic); subscribeStores.forEach(subscribeStore -> { if (!clientId.equals(subscribeStore.getClientId()) && sessionStoreService.containsKey(subscribeStore.getClientId())) { MqttQoS respQoS = mqttQoS.value() > subscribeStore.getMqttQoS() ? MqttQoS.valueOf(subscribeStore.getMqttQoS()) : mqttQoS; if (respQoS == MqttQoS.AT_MOST_ONCE) { MqttPublishMessage publishMessage = (MqttPublishMessage) MqttMessageFactory.newMessage( new MqttFixedHeader(MqttMessageType.PUBLISH, dup, respQoS, retain, 0), new MqttPublishVariableHeader(topic, 0), ByteBuffer.wrap(messageBytes)); LOGGER.debug("PUBLISH - clientId: {}, topic: {}, Qos: {}", subscribeStore.getClientId(), topic, respQoS.value()); ChannelId channelId = channelIdMap.get(sessionStoreService.get(subscribeStore.getClientId()).getChannelId()); if (channelId != null) { Channel channel = channelGroup.find(channelId); new MqttFixedHeader(MqttMessageType.PUBLISH, dup, respQoS, retain, 0), new MqttPublishVariableHeader(topic, messageId), ByteBuffer.wrap(messageBytes)); LOGGER.debug("PUBLISH - clientId: {}, topic: {}, Qos: {}, messageId: {}", subscribeStore.getClientId(), topic, respQoS.value(), messageId); ChannelId channelId = channelIdMap.get(sessionStoreService.get(subscribeStore.getClientId()).getChannelId()); if (channelId != null) { Channel channel = channelGroup.find(channelId); new MqttFixedHeader(MqttMessageType.PUBLISH, dup, respQoS, retain, 0), new MqttPublishVariableHeader(topic, messageId), ByteBuffer.wrap(messageBytes)); LOGGER.debug("PUBLISH - clientId: {}, topic: {}, Qos: {}, messageId: {}", subscribeStore.getClientId(), topic, respQoS.value(), messageId); ChannelId channelId = channelIdMap.get(sessionStoreService.get(subscribeStore.getClientId()).getChannelId()); if (channelId != null) { Channel channel = channelGroup.find(channelId);
public void processSubscribe(Channel channel, MqttSubscribeMessage msg) { List<MqttTopicSubscription> topicSubscriptions = msg.payload().topicSubscriptions(); if (this.validTopicFilter(topicSubscriptions)) { String clientId = (String) channel.attr(AttributeKey.valueOf("clientId")).get(); List<Integer> mqttQoSList = new ArrayList<Integer>(); topicSubscriptions.forEach(topicSubscription -> { String topicFilter = topicSubscription.topicName(); MqttQoS mqttQoS = topicSubscription.qualityOfService(); SubscribeStore subscribeStore = new SubscribeStore(clientId, topicFilter, mqttQoS.value()); subscribeStoreService.put(topicFilter, subscribeStore); mqttQoSList.add(mqttQoS.value()); LOGGER.debug("SUBSCRIBE - clientId: {}, topFilter: {}, QoS: {}", clientId, topicFilter, mqttQoS.value()); }); MqttSubAckMessage subAckMessage = (MqttSubAckMessage) MqttMessageFactory.newMessage( new MqttFixedHeader(MqttMessageType.SUBACK, false, MqttQoS.AT_MOST_ONCE, false, 0), MqttMessageIdVariableHeader.from(msg.variableHeader().messageId()), new MqttSubAckPayload(mqttQoSList)); channel.writeAndFlush(subAckMessage); // 发布保留消息 topicSubscriptions.forEach(topicSubscription -> { String topicFilter = topicSubscription.topicName(); MqttQoS mqttQoS = topicSubscription.qualityOfService(); this.sendRetainMessage(channel, topicFilter, mqttQoS); }); } else { channel.close(); } }
if (cleanSession) { sessionStoreService.remove(msg.payload().clientIdentifier()); subscribeStoreService.removeForClient(msg.payload().clientIdentifier()); dupPublishMessageStoreService.removeByClient(msg.payload().clientIdentifier()); dupPubRelMessageStoreService.removeByClient(msg.payload().clientIdentifier());