Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: add support to publish message to specific clients #797

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions broker/src/main/java/io/moquette/broker/PostOffice.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public RouteResult ifFailed(Runnable action) {

private static final Logger LOG = LoggerFactory.getLogger(PostOffice.class);

private static final Set<String> NO_FILTER = new HashSet<>();
private static final Set<String> NO_FILTER = Collections.unmodifiableSet(new HashSet<>());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't change the semantics of NO_FILTER this time, but I think NO_FILTER should be unmodifiable.


private final Authorizator authorizator;
private final ISubscriptionsDirectory subscriptions;
Expand Down Expand Up @@ -701,12 +701,15 @@ static MqttQoS lowerQosToTheSubscriptionDesired(Subscription sub, MqttQoS qos) {
* the result of the enqueuing operation to session loops.
*/
public RoutingResults internalPublish(MqttPublishMessage msg) {
return internalPublish(msg,NO_FILTER);
}
public RoutingResults internalPublish(MqttPublishMessage msg,Set<String> clientIds) {
final MqttQoS qos = msg.fixedHeader().qosLevel();
final Topic topic = new Topic(msg.variableHeader().topicName());
final ByteBuf payload = msg.payload();
LOG.info("Sending internal PUBLISH message Topic={}, qos={}", topic, qos);

final RoutingResults publishResult = publish2Subscribers(payload, topic, qos);
final RoutingResults publishResult = publish2Subscribers(payload, topic, qos,clientIds);
LOG.trace("after routed publishes: {}", publishResult);

if (!msg.fixedHeader().isRetain()) {
Expand Down
19 changes: 19 additions & 0 deletions broker/src/main/java/io/moquette/broker/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.util.List;
import java.util.Properties;
import java.util.UUID;
import java.util.HashSet;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

Expand Down Expand Up @@ -567,6 +568,24 @@ public RoutingResults internalPublish(MqttPublishMessage msg, final String clien
msg.payload().release();
return routingResults;
}
public RoutingResults publishToClient(MqttPublishMessage msg, final String clientId) {
return publishToClients(msg,Collections.singletonList(clientId));
}
public RoutingResults publishToClients(MqttPublishMessage msg, final Collection<String> clientIds) {
final int messageID = msg.variableHeader().packetId();
if (!initialized) {
LOG.error("Moquette is not started, message cannot be published. CId: [{}], messageId: {}",
String.join(",",clientIds),
messageID);
throw new IllegalStateException("Can't publish on a integration is not yet started");
}
LOG.trace("publishing to message CId: [{}], messageId: {}", String.join(",",clientIds), messageID);
final RoutingResults routingResults = clientIds.isEmpty()?
dispatcher.internalPublish(msg):
dispatcher.internalPublish(msg,new HashSet<>(clientIds));
msg.payload().release();
return routingResults;
}

public void stopServer() {
LOG.info("Unbinding integration from the configured ports");
Expand Down