Skip to content

Commit e5ab083

Browse files
authored
Merge pull request #16 from JavaSaBr/feature-broker-11
Implement Mqtt Session Service
2 parents 3d8679c + 5b9465f commit e5ab083

31 files changed

+616
-137
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
JMQTT-Broker is an open source (GPL v3) Java based MQTT Broker with network implementation based on Java.NIO.2
44

55
## Dependencies for building
6-
### java 11+
6+
### java 13+
77
##### Ubuntu:
88
```bash
99
sudo wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add -
1010
sudo add-apt-repository --yes https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/
11-
sudo apt-get install adoptopenjdk-11-hotspot
11+
sudo apt-get install adoptopenjdk-13-hotspot
1212
```
13-
##### Windows: [Windows x64 installer](https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.4%2B11/OpenJDK11U-jdk_x64_windows_hotspot_11.0.4_11.msi)
14-
##### MacOS: [MacOS x64 installer](https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.4%2B11.4/OpenJDK11U-jdk_x64_mac_hotspot_11.0.4_11.pkg)
13+
##### Windows: [Windows x64 installer](https://github.com/AdoptOpenJDK/openjdk13-binaries/releases/download/jdk-13.0.1%2B9/OpenJDK13U-jdk_x64_windows_hotspot_13.0.1_9.msi)
14+
##### MacOS: [MacOS x64 installer](https://github.com/AdoptOpenJDK/openjdk13-binaries/releases/download/jdk-13.0.1%2B9/OpenJDK13U-jdk_x64_mac_hotspot_13.0.1_9.pkg)
1515
### Docker
1616
##### Ubuntu: [installation guide](https://docs.docker.com/install/linux/docker-ce/ubuntu)
1717
##### Windows: [installation guide](https://docs.docker.com/docker-for-windows/install)

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ allprojects {
2020
apply plugin: "groovy"
2121
apply plugin: "org.springframework.boot"
2222

23-
sourceCompatibility = JavaVersion.VERSION_11
24-
targetCompatibility = JavaVersion.VERSION_11
23+
sourceCompatibility = JavaVersion.VERSION_13
24+
targetCompatibility = JavaVersion.VERSION_13
2525

2626
repositories {
2727
jcenter()
@@ -107,7 +107,7 @@ task buildSingleArtifactWithoutTests(type: GradleBuild) {
107107
}
108108

109109
wrapper {
110-
gradleVersion = '5.6.1'
110+
gradleVersion = '6.0'
111111
distributionType = Wrapper.DistributionType.ALL
112112
}
113113

gradle/wrapper/gradle-wrapper.jar

3.01 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.1-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

gradlew

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
99
#
10-
# http://www.apache.org/licenses/LICENSE-2.0
10+
# https://www.apache.org/licenses/LICENSE-2.0
1111
#
1212
# Unless required by applicable law or agreed to in writing, software
1313
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -125,8 +125,8 @@ if $darwin; then
125125
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
126126
fi
127127

128-
# For Cygwin, switch paths to Windows format before running java
129-
if $cygwin ; then
128+
# For Cygwin or MSYS, switch paths to Windows format before running java
129+
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
130130
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
131131
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
132132
JAVACMD=`cygpath --unix "$JAVACMD"`
@@ -154,19 +154,19 @@ if $cygwin ; then
154154
else
155155
eval `echo args$i`="\"$arg\""
156156
fi
157-
i=$((i+1))
157+
i=`expr $i + 1`
158158
done
159159
case $i in
160-
(0) set -- ;;
161-
(1) set -- "$args0" ;;
162-
(2) set -- "$args0" "$args1" ;;
163-
(3) set -- "$args0" "$args1" "$args2" ;;
164-
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
165-
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
166-
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
167-
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
168-
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
169-
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
160+
0) set -- ;;
161+
1) set -- "$args0" ;;
162+
2) set -- "$args0" "$args1" ;;
163+
3) set -- "$args0" "$args1" "$args2" ;;
164+
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
165+
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
166+
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
167+
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
168+
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
169+
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
170170
esac
171171
fi
172172

@@ -175,14 +175,9 @@ save () {
175175
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
176176
echo " "
177177
}
178-
APP_ARGS=$(save "$@")
178+
APP_ARGS=`save "$@"`
179179

180180
# Collect all arguments for the java command, following the shell quoting and substitution rules
181181
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
182182

183-
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
184-
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
185-
cd "$(dirname "$0")"
186-
fi
187-
188183
exec "$JAVACMD" "$@"

gradlew.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@rem you may not use this file except in compliance with the License.
66
@rem You may obtain a copy of the License at
77
@rem
8-
@rem http://www.apache.org/licenses/LICENSE-2.0
8+
@rem https://www.apache.org/licenses/LICENSE-2.0
99
@rem
1010
@rem Unless required by applicable law or agreed to in writing, software
1111
@rem distributed under the License is distributed on an "AS IS" BASIS,

src/main/java/com/ss/mqtt/broker/config/MqttBrokerConfig.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import com.ss.mqtt.broker.model.MqttPropertyConstants;
44
import com.ss.mqtt.broker.model.QoS;
55
import com.ss.mqtt.broker.network.MqttConnection;
6+
import com.ss.mqtt.broker.network.client.MqttClientReleaseHandler;
67
import com.ss.mqtt.broker.network.client.UnsafeMqttClient;
78
import com.ss.mqtt.broker.network.client.impl.DeviceMqttClient;
9+
import com.ss.mqtt.broker.network.client.impl.DeviceMqttClientReleaseHandler;
810
import com.ss.mqtt.broker.network.packet.PacketType;
911
import com.ss.mqtt.broker.network.packet.in.handler.*;
1012
import com.ss.mqtt.broker.service.*;
@@ -20,7 +22,6 @@
2022
import org.jetbrains.annotations.NotNull;
2123
import org.springframework.context.annotation.Bean;
2224
import org.springframework.context.annotation.Configuration;
23-
import org.springframework.context.annotation.PropertySource;
2425
import org.springframework.core.env.Environment;
2526

2627
import java.net.InetSocketAddress;
@@ -48,14 +49,9 @@ private interface ChannelFactory extends
4849
return new DefaultBufferAllocator(networkConfig);
4950
}
5051

51-
@Bean
52-
@NotNull ClientService clientService() {
53-
return new DefaultClientService();
54-
}
55-
5652
@Bean
5753
@NotNull ClientIdRegistry clientIdRegistry() {
58-
return new SimpleClientIdRegistry(
54+
return new InMemoryClientIdRegistry(
5955
env.getProperty(
6056
"client.id.available.chars",
6157
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"
@@ -64,16 +60,20 @@ private interface ChannelFactory extends
6460
);
6561
}
6662

63+
@Bean
64+
@NotNull MqttSessionService mqttSessionService() {
65+
return new InMemoryMqttSessionService(
66+
env.getProperty("sessions.clean.thread.interval", int.class, 60000)
67+
);
68+
}
69+
6770
@Bean
6871
@NotNull CredentialSource credentialSource() {
6972
return new FileCredentialsSource(env.getProperty("credentials.source.file.name", "credentials"));
7073
}
7174

7275
@Bean
73-
@NotNull AuthenticationService authenticationService(
74-
@NotNull CredentialSource credentialSource,
75-
@NotNull ClientIdRegistry clientIdRegistry
76-
) {
76+
@NotNull AuthenticationService authenticationService(@NotNull CredentialSource credentialSource) {
7777
return new SimpleAuthenticationService(
7878
credentialSource,
7979
env.getProperty("authentication.allow.anonymous", boolean.class, false)
@@ -85,31 +85,47 @@ private interface ChannelFactory extends
8585
@NotNull AuthenticationService authenticationService,
8686
@NotNull ClientIdRegistry clientIdRegistry,
8787
@NotNull SubscriptionService subscriptionService,
88-
@NotNull PublishingService publishingService
88+
@NotNull PublishingService publishingService,
89+
@NotNull MqttSessionService mqttSessionService
8990
) {
9091

9192
var handlers = new PacketInHandler[PacketType.INVALID.ordinal()];
92-
handlers[PacketType.CONNECT.ordinal()] = new ConnectInPacketHandler(clientIdRegistry, authenticationService);
93+
handlers[PacketType.CONNECT.ordinal()] = new ConnectInPacketHandler(
94+
clientIdRegistry,
95+
authenticationService,
96+
mqttSessionService
97+
);
9398
handlers[PacketType.SUBSCRIBE.ordinal()] = new SubscribeInPacketHandler(subscriptionService);
9499
handlers[PacketType.UNSUBSCRIBE.ordinal()] = new UnsubscribeInPacketHandler(subscriptionService);
95100
handlers[PacketType.PUBLISH.ordinal()] = new PublishInPacketHandler(publishingService);
101+
handlers[PacketType.DISCONNECT.ordinal()] = new DisconnetInPacketHandler();
96102

97103
return handlers;
98104
}
99105

106+
@Bean
107+
@NotNull MqttClientReleaseHandler deviceMqttClientReleaseHandler(
108+
@NotNull ClientIdRegistry clientIdRegistry,
109+
@NotNull MqttSessionService mqttSessionService
110+
) {
111+
return new DeviceMqttClientReleaseHandler(clientIdRegistry, mqttSessionService);
112+
}
113+
100114
@Bean
101115
@NotNull ServerNetwork<@NotNull MqttConnection> deviceNetwork(
102116
@NotNull ServerNetworkConfig networkConfig,
103117
@NotNull BufferAllocator bufferAllocator,
104118
@NotNull MqttConnectionConfig deviceConnectionConfig,
105-
PacketInHandler @NotNull [] devicePacketHandlers
119+
PacketInHandler @NotNull [] devicePacketHandlers,
120+
@NotNull MqttClientReleaseHandler deviceMqttClientReleaseHandler
106121
) {
107122
return NetworkFactory.newServerNetwork(
108123
networkConfig,
109124
deviceConnectionFactory(
110125
bufferAllocator,
111126
deviceConnectionConfig,
112-
devicePacketHandlers
127+
devicePacketHandlers,
128+
deviceMqttClientReleaseHandler
113129
)
114130
);
115131
}
@@ -212,7 +228,8 @@ private interface ChannelFactory extends
212228
private @NotNull ChannelFactory deviceConnectionFactory(
213229
@NotNull BufferAllocator bufferAllocator,
214230
@NotNull MqttConnectionConfig connectionConfig,
215-
PacketInHandler @NotNull [] packetHandlers
231+
PacketInHandler @NotNull [] packetHandlers,
232+
@NotNull MqttClientReleaseHandler deviceMqttClientReleaseHandler
216233
) {
217234
return (network, channel) -> new MqttConnection(
218235
network,
@@ -221,7 +238,7 @@ private interface ChannelFactory extends
221238
100,
222239
packetHandlers,
223240
connectionConfig,
224-
DeviceMqttClient::new
241+
mqttConnection -> new DeviceMqttClient(mqttConnection, deviceMqttClientReleaseHandler)
225242
);
226243
}
227244
}

src/main/java/com/ss/mqtt/broker/model/MqttPropertyConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public interface MqttPropertyConstants {
4141

4242
int SUBSCRIPTION_ID_NOT_DEFINED = 0;
4343

44-
boolean SESSIONS_ENABLED_DEFAULT = false;
44+
boolean SESSIONS_ENABLED_DEFAULT = true;
4545
boolean KEEP_ALIVE_ENABLED_DEFAULT = false;
4646
boolean RETAIN_AVAILABLE_DEFAULT = false;
4747
boolean WILDCARD_SUBSCRIPTION_AVAILABLE_DEFAULT = false;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.ss.mqtt.broker.model;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
public interface MqttSession {
6+
7+
interface UnsafeMqttSession extends MqttSession {
8+
9+
void setExpirationTime(long expirationTime);
10+
}
11+
12+
@NotNull String getClientId();
13+
14+
/**
15+
* @return the expiration time in ms or -1 if it should not be expired now.
16+
*/
17+
long getExpirationTime();
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.ss.mqtt.broker.model.impl;
2+
3+
import com.ss.mqtt.broker.model.MqttSession.UnsafeMqttSession;
4+
import lombok.Getter;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.Setter;
7+
import lombok.ToString;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
@ToString
11+
@RequiredArgsConstructor
12+
public class DefaultMqttSession implements UnsafeMqttSession {
13+
14+
private final @NotNull String clientId;
15+
16+
private volatile @Getter @Setter long expirationTime = -1;
17+
18+
@Override
19+
public @NotNull String getClientId() {
20+
return clientId;
21+
}
22+
}

0 commit comments

Comments
 (0)