-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from JavaSaBr/feature-broker-23
Feature broker 23: Shared subscriptions
- Loading branch information
Showing
46 changed files
with
856 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6-milestone-1-all.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
src/main/java/com/ss/mqtt/broker/config/MqttNetworkConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/ss/mqtt/broker/handler/packet/in/SubscribeInPacketHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,37 @@ | ||
package com.ss.mqtt.broker.handler.packet.in; | ||
|
||
import static com.ss.mqtt.broker.model.reason.code.SubscribeAckReasonCode.SHARED_SUBSCRIPTIONS_NOT_SUPPORTED; | ||
import static com.ss.mqtt.broker.model.reason.code.SubscribeAckReasonCode.WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED; | ||
import static java.lang.Byte.toUnsignedInt; | ||
import com.ss.mqtt.broker.model.reason.code.DisconnectReasonCode; | ||
import com.ss.mqtt.broker.model.reason.code.SubscribeAckReasonCode; | ||
import com.ss.mqtt.broker.network.client.MqttClient.UnsafeMqttClient; | ||
import com.ss.mqtt.broker.network.packet.in.SubscribeInPacket; | ||
import com.ss.mqtt.broker.service.SubscriptionService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Set; | ||
|
||
@RequiredArgsConstructor | ||
public class SubscribeInPacketHandler extends AbstractPacketHandler<UnsafeMqttClient, SubscribeInPacket> { | ||
|
||
private final static Set<SubscribeAckReasonCode> INVALID_ACK_CODE = Set.of( | ||
SHARED_SUBSCRIPTIONS_NOT_SUPPORTED, | ||
WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED | ||
); | ||
|
||
private final @NotNull SubscriptionService subscriptionService; | ||
|
||
@Override | ||
protected void handleImpl(@NotNull UnsafeMqttClient client, @NotNull SubscribeInPacket packet) { | ||
var ackReasonCodes = subscriptionService.subscribe(client, packet.getTopicFilters()); | ||
client.send(client.getPacketOutFactory().newSubscribeAck(packet.getPacketId(), ackReasonCodes)); | ||
var reason = ackReasonCodes.findAny(INVALID_ACK_CODE::contains); | ||
if (reason != null) { | ||
var disconnectReasonCode = DisconnectReasonCode.of(toUnsignedInt(reason.getValue())); | ||
var disconnect = client.getPacketOutFactory().newDisconnect(client, disconnectReasonCode); | ||
client.sendWithFeedback(disconnect).thenAccept(result -> client.getConnection().close()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/ss/mqtt/broker/model/SharedSubscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.ss.mqtt.broker.model; | ||
|
||
import com.ss.mqtt.broker.model.topic.SharedTopicFilter; | ||
import com.ss.mqtt.broker.network.client.MqttClient; | ||
import com.ss.rlib.common.util.array.Array; | ||
import com.ss.rlib.common.util.array.ConcurrentArray; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class SharedSubscriber implements Subscriber { | ||
|
||
private static @NotNull SingleSubscriber next(@NotNull Array<SingleSubscriber> subscribers, int current) { | ||
return subscribers.get(current % subscribers.size()); | ||
} | ||
|
||
private final @NotNull SharedTopicFilter topicFilter; | ||
private final @NotNull ConcurrentArray<SingleSubscriber> subscribers; | ||
private final @NotNull AtomicInteger current; | ||
|
||
public SharedSubscriber(@NotNull SubscribeTopicFilter topic) { | ||
subscribers = ConcurrentArray.ofType(Subscriber.class); | ||
current = new AtomicInteger(0); | ||
topicFilter = (SharedTopicFilter) topic.getTopicFilter(); | ||
} | ||
|
||
public @NotNull SingleSubscriber getSubscriber() { | ||
//noinspection ConstantConditions | ||
return subscribers.getInReadLock(current.getAndIncrement(), SharedSubscriber::next); | ||
} | ||
|
||
public void addSubscriber(@NotNull SingleSubscriber client) { | ||
subscribers.runInWriteLock(client, Array::add); | ||
} | ||
|
||
public boolean removeSubscriber(@NotNull MqttClient client) { | ||
return subscribers.removeIfConvertedInWriteLock(client, SingleSubscriber::getMqttClient, Objects::equals); | ||
} | ||
|
||
public int size() { | ||
//noinspection ConstantConditions | ||
return subscribers.getInReadLock(Collection::size); | ||
} | ||
|
||
public @NotNull String getGroup() { | ||
return topicFilter.getGroup(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/ss/mqtt/broker/model/SingleSubscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.ss.mqtt.broker.model; | ||
|
||
import com.ss.mqtt.broker.network.client.MqttClient; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.ToString; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
@ToString | ||
@EqualsAndHashCode(of = "mqttClient") | ||
@RequiredArgsConstructor | ||
public class SingleSubscriber implements Subscriber { | ||
|
||
private final @Getter @NotNull MqttClient mqttClient; | ||
private final @NotNull SubscribeTopicFilter subscribe; | ||
|
||
public @NotNull QoS getQos() { | ||
return subscribe.getQos(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,3 @@ | ||
package com.ss.mqtt.broker.model; | ||
|
||
import com.ss.mqtt.broker.model.topic.TopicFilter; | ||
import com.ss.mqtt.broker.network.client.MqttClient; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
@ToString | ||
@EqualsAndHashCode(of = "mqttClient") | ||
public class Subscriber { | ||
|
||
private final @Getter @NotNull MqttClient mqttClient; | ||
private final @NotNull SubscribeTopicFilter subscribeTopicFilter; | ||
|
||
/** | ||
* Creates subscriber | ||
* | ||
* @param mqttClient MQTT client which will become a subscriber | ||
* @param topicFilter topic filter that MQTT client subscribes to | ||
*/ | ||
public Subscriber(@NotNull MqttClient mqttClient, @NotNull SubscribeTopicFilter topicFilter) { | ||
this.mqttClient = mqttClient; | ||
this.subscribeTopicFilter = topicFilter; | ||
} | ||
|
||
public @NotNull QoS getQos() { | ||
return subscribeTopicFilter.getQos(); | ||
} | ||
|
||
public @NotNull TopicFilter getTopicFilter() { | ||
return subscribeTopicFilter.getTopicFilter(); | ||
} | ||
|
||
} | ||
public interface Subscriber {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.