Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica-mqtt
提交
6b1bc029
mica-mqtt
项目概览
mica
/
mica-mqtt
通知
71
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica-mqtt
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6b1bc029
编写于
7月 23, 2021
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
代码完善。
上级
0333b0f3
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
274 addition
and
23 deletion
+274
-23
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServer.java
...ain/java/net/dreamlu/iot/mqtt/core/server/MqttServer.java
+3
-3
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/model/Message.java
.../java/net/dreamlu/iot/mqtt/core/server/model/Message.java
+97
-0
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/model/Subscribe.java
...ava/net/dreamlu/iot/mqtt/core/server/model/Subscribe.java
+8
-8
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/session/IMqttSessionManager.java
...mlu/iot/mqtt/core/server/session/IMqttSessionManager.java
+2
-2
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/session/InMemoryMqttSessionManager.java
.../mqtt/core/server/session/InMemoryMqttSessionManager.java
+10
-10
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/store/IMqttMessageStore.java
...dreamlu/iot/mqtt/core/server/store/IMqttMessageStore.java
+79
-0
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/store/InMemoryMqttMessageStore.java
.../iot/mqtt/core/server/store/InMemoryMqttMessageStore.java
+75
-0
未找到文件。
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServer.java
浏览文件 @
6b1bc029
...
...
@@ -21,7 +21,7 @@ import net.dreamlu.iot.mqtt.codec.MqttPublishMessage;
import
net.dreamlu.iot.mqtt.codec.MqttQoS
;
import
net.dreamlu.iot.mqtt.core.common.MqttPendingPublish
;
import
net.dreamlu.iot.mqtt.core.server.session.IMqttSessionManager
;
import
net.dreamlu.iot.mqtt.core.server.
store.SubscribeStor
e
;
import
net.dreamlu.iot.mqtt.core.server.
model.Subscrib
e
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.tio.core.ChannelContext
;
...
...
@@ -122,12 +122,12 @@ public final class MqttServer {
logger
.
warn
(
"Mqtt publish to clientId:{} ChannelContext is null May be disconnected."
,
clientId
);
return
false
;
}
List
<
Subscribe
Store
>
subscribeList
=
sessionManager
.
searchSubscribe
(
clientId
,
topic
);
List
<
Subscribe
>
subscribeList
=
sessionManager
.
searchSubscribe
(
clientId
,
topic
);
if
(
subscribeList
.
isEmpty
())
{
logger
.
warn
(
"Mqtt publish but clientId:{} subscribeList is empty."
,
clientId
);
return
false
;
}
for
(
Subscribe
Store
subscribe
:
subscribeList
)
{
for
(
Subscribe
subscribe
:
subscribeList
)
{
int
subMqttQoS
=
subscribe
.
getMqttQoS
();
MqttQoS
mqttQoS
=
qos
.
value
()
>
subMqttQoS
?
MqttQoS
.
valueOf
(
subMqttQoS
)
:
qos
;
publish
(
context
,
clientId
,
topic
,
payload
,
mqttQoS
,
retain
);
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/model/Message.java
0 → 100644
浏览文件 @
6b1bc029
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.net.dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
net.dreamlu.iot.mqtt.core.server.model
;
import
net.dreamlu.iot.mqtt.codec.MqttMessageType
;
import
java.io.Serializable
;
import
java.util.Arrays
;
import
java.util.Map
;
/**
* 消息模型,用于存储
*
* @author L.cm
*/
public
class
Message
implements
Serializable
{
private
String
clientId
;
private
int
messageId
;
private
Map
<
String
,
Object
>
headers
;
private
MqttMessageType
messageType
;
private
byte
[]
payload
;
private
long
storeTime
;
public
String
getClientId
()
{
return
clientId
;
}
public
void
setClientId
(
String
clientId
)
{
this
.
clientId
=
clientId
;
}
public
int
getMessageId
()
{
return
messageId
;
}
public
void
setMessageId
(
int
messageId
)
{
this
.
messageId
=
messageId
;
}
public
Map
<
String
,
Object
>
getHeaders
()
{
return
headers
;
}
public
void
setHeaders
(
Map
<
String
,
Object
>
headers
)
{
this
.
headers
=
headers
;
}
public
MqttMessageType
getMessageType
()
{
return
messageType
;
}
public
void
setMessageType
(
MqttMessageType
messageType
)
{
this
.
messageType
=
messageType
;
}
public
byte
[]
getPayload
()
{
return
payload
;
}
public
void
setPayload
(
byte
[]
payload
)
{
this
.
payload
=
payload
;
}
public
long
getStoreTime
()
{
return
storeTime
;
}
public
void
setStoreTime
(
long
storeTime
)
{
this
.
storeTime
=
storeTime
;
}
@Override
public
String
toString
()
{
return
"MessageInfo{"
+
"clientId='"
+
clientId
+
'\''
+
", messageId="
+
messageId
+
", headers="
+
headers
+
", messageType="
+
messageType
+
", payload="
+
Arrays
.
toString
(
payload
)
+
", storeTime="
+
storeTime
+
'}'
;
}
}
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/
store/SubscribeStor
e.java
→
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/
model/Subscrib
e.java
浏览文件 @
6b1bc029
...
...
@@ -14,25 +14,25 @@
* limitations under the License.
*/
package
net.dreamlu.iot.mqtt.core.server.
store
;
package
net.dreamlu.iot.mqtt.core.server.
model
;
import
java.io.Serializable
;
import
java.util.Objects
;
import
java.util.regex.Pattern
;
/**
* 订阅存储
* 订阅
模型,用于
存储
*
* @author L.cm
*/
public
class
Subscribe
Store
implements
Serializable
{
public
class
Subscribe
implements
Serializable
{
private
Pattern
topicRegex
;
private
int
mqttQoS
;
public
Subscribe
Store
()
{
public
Subscribe
()
{
}
public
Subscribe
Store
(
String
topicFilter
,
int
mqttQoS
)
{
public
Subscribe
(
String
topicFilter
,
int
mqttQoS
)
{
this
.
topicRegex
=
Pattern
.
compile
(
topicFilter
.
replace
(
"+"
,
"[^/]+"
).
replace
(
"#"
,
".+"
).
concat
(
"$"
));
this
.
mqttQoS
=
mqttQoS
;
}
...
...
@@ -41,7 +41,7 @@ public class SubscribeStore implements Serializable {
return
topicRegex
;
}
public
Subscribe
Store
setTopicRegex
(
Pattern
topicRegex
)
{
public
Subscribe
setTopicRegex
(
Pattern
topicRegex
)
{
this
.
topicRegex
=
topicRegex
;
return
this
;
}
...
...
@@ -50,7 +50,7 @@ public class SubscribeStore implements Serializable {
return
mqttQoS
;
}
public
Subscribe
Store
setMqttQoS
(
int
mqttQoS
)
{
public
Subscribe
setMqttQoS
(
int
mqttQoS
)
{
this
.
mqttQoS
=
mqttQoS
;
return
this
;
}
...
...
@@ -63,7 +63,7 @@ public class SubscribeStore implements Serializable {
if
(
o
==
null
||
getClass
()
!=
o
.
getClass
())
{
return
false
;
}
Subscribe
Store
that
=
(
SubscribeStor
e
)
o
;
Subscribe
that
=
(
Subscrib
e
)
o
;
return
mqttQoS
==
that
.
mqttQoS
&&
Objects
.
equals
(
topicRegex
,
that
.
topicRegex
);
}
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/session/IMqttSessionManager.java
浏览文件 @
6b1bc029
...
...
@@ -19,7 +19,7 @@ package net.dreamlu.iot.mqtt.core.server.session;
import
net.dreamlu.iot.mqtt.codec.MqttQoS
;
import
net.dreamlu.iot.mqtt.core.common.MqttPendingPublish
;
import
net.dreamlu.iot.mqtt.core.common.MqttPendingQos2Publish
;
import
net.dreamlu.iot.mqtt.core.server.
store.SubscribeStor
e
;
import
net.dreamlu.iot.mqtt.core.server.
model.Subscrib
e
;
import
java.util.List
;
...
...
@@ -54,7 +54,7 @@ public interface IMqttSessionManager {
* @param topicName topicName
* @return 订阅存储列表
*/
List
<
Subscribe
Store
>
searchSubscribe
(
String
clientId
,
String
topicName
);
List
<
Subscribe
>
searchSubscribe
(
String
clientId
,
String
topicName
);
/**
* 添加发布过程存储
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/session/InMemoryMqttSessionManager.java
浏览文件 @
6b1bc029
...
...
@@ -19,7 +19,7 @@ package net.dreamlu.iot.mqtt.core.server.session;
import
net.dreamlu.iot.mqtt.codec.MqttQoS
;
import
net.dreamlu.iot.mqtt.core.common.MqttPendingPublish
;
import
net.dreamlu.iot.mqtt.core.common.MqttPendingQos2Publish
;
import
net.dreamlu.iot.mqtt.core.server.
store.SubscribeStor
e
;
import
net.dreamlu.iot.mqtt.core.server.
model.Subscrib
e
;
import
java.util.*
;
import
java.util.concurrent.ConcurrentHashMap
;
...
...
@@ -39,7 +39,7 @@ public class InMemoryMqttSessionManager implements IMqttSessionManager {
/**
* clientId: {topicFilter: SubscribeStore}
*/
private
final
ConcurrentMap
<
String
,
ConcurrentMap
<
String
,
Subscribe
Store
>>
subscribeStore
=
new
ConcurrentHashMap
<>();
private
final
ConcurrentMap
<
String
,
ConcurrentMap
<
String
,
Subscribe
>>
subscribeStore
=
new
ConcurrentHashMap
<>();
/**
* clientId: {msgId: Object}
*/
...
...
@@ -51,13 +51,13 @@ public class InMemoryMqttSessionManager implements IMqttSessionManager {
@Override
public
void
addSubscribe
(
String
clientId
,
String
topicFilter
,
MqttQoS
mqttQoS
)
{
Map
<
String
,
Subscribe
Store
>
data
=
subscribeStore
.
computeIfAbsent
(
clientId
,
(
key
)
->
new
ConcurrentHashMap
<>(
16
));
data
.
put
(
topicFilter
,
new
Subscribe
Store
(
topicFilter
,
mqttQoS
.
value
()));
Map
<
String
,
Subscribe
>
data
=
subscribeStore
.
computeIfAbsent
(
clientId
,
(
key
)
->
new
ConcurrentHashMap
<>(
16
));
data
.
put
(
topicFilter
,
new
Subscribe
(
topicFilter
,
mqttQoS
.
value
()));
}
@Override
public
void
removeSubscribe
(
String
clientId
,
String
topicFilter
)
{
ConcurrentMap
<
String
,
Subscribe
Store
>
map
=
subscribeStore
.
get
(
clientId
);
ConcurrentMap
<
String
,
Subscribe
>
map
=
subscribeStore
.
get
(
clientId
);
if
(
map
==
null
)
{
return
;
}
...
...
@@ -65,14 +65,14 @@ public class InMemoryMqttSessionManager implements IMqttSessionManager {
}
@Override
public
List
<
Subscribe
Store
>
searchSubscribe
(
String
clientId
,
String
topicName
)
{
List
<
Subscribe
Store
>
list
=
new
ArrayList
<>();
ConcurrentMap
<
String
,
Subscribe
Store
>
map
=
subscribeStore
.
get
(
clientId
);
public
List
<
Subscribe
>
searchSubscribe
(
String
clientId
,
String
topicName
)
{
List
<
Subscribe
>
list
=
new
ArrayList
<>();
ConcurrentMap
<
String
,
Subscribe
>
map
=
subscribeStore
.
get
(
clientId
);
if
(
map
==
null
)
{
return
Collections
.
emptyList
();
}
Collection
<
Subscribe
Store
>
values
=
map
.
values
();
for
(
Subscribe
Store
value
:
values
)
{
Collection
<
Subscribe
>
values
=
map
.
values
();
for
(
Subscribe
value
:
values
)
{
if
(
value
.
getTopicRegex
().
matcher
(
topicName
).
matches
())
{
list
.
add
(
value
);
}
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/store/IMqttMessageStore.java
0 → 100644
浏览文件 @
6b1bc029
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.net.dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
net.dreamlu.iot.mqtt.core.server.store
;
import
net.dreamlu.iot.mqtt.core.server.model.Message
;
import
java.util.Collection
;
/**
* message store
*
* @author L.cm
*/
public
interface
IMqttMessageStore
{
/**
* 存储clientId的遗嘱消息
*
* @param clientId clientId
* @param message message
* @return boolean
*/
boolean
addWillMessage
(
String
clientId
,
Message
message
);
/**
* 清理该clientId的遗嘱消息
*
* @param clientId clientId
* @return boolean
*/
boolean
clearWillMessage
(
String
clientId
);
/**
* 获取will消息
*
* @param clientId clientId
* @return Message
*/
Message
getWillMessage
(
String
clientId
);
/**
* 存储retain消息
*
* @param topic topic
* @param message message
* @return boolean
*/
boolean
addRetainMessage
(
String
topic
,
Message
message
);
/**
* 清理该topic的 retain消息
*
* @param topic topic
* @return boolean
*/
boolean
clearRetainMessage
(
String
topic
);
/**
* 获取所有retain消息
*
* @return Message Collection
*/
Collection
<
Message
>
getAllRetainMsg
();
}
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/store/InMemoryMqttMessageStore.java
0 → 100644
浏览文件 @
6b1bc029
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.net.dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
net.dreamlu.iot.mqtt.core.server.store
;
import
net.dreamlu.iot.mqtt.core.server.model.Message
;
import
java.util.Collection
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentMap
;
/**
* message store
*
* @author L.cm
*/
public
class
InMemoryMqttMessageStore
implements
IMqttMessageStore
{
/**
* 遗嘱消息 clientId: Message
*/
private
final
ConcurrentMap
<
String
,
Message
>
willStore
=
new
ConcurrentHashMap
<>();
/**
* 保持消息 clientId: Message
*/
private
final
ConcurrentMap
<
String
,
Message
>
retainStore
=
new
ConcurrentHashMap
<>();
@Override
public
boolean
addWillMessage
(
String
clientId
,
Message
message
)
{
willStore
.
put
(
clientId
,
message
);
return
true
;
}
@Override
public
boolean
clearWillMessage
(
String
clientId
)
{
willStore
.
remove
(
clientId
);
return
true
;
}
@Override
public
Message
getWillMessage
(
String
clientId
)
{
return
willStore
.
get
(
clientId
);
}
@Override
public
boolean
addRetainMessage
(
String
topic
,
Message
message
)
{
retainStore
.
put
(
topic
,
message
);
return
true
;
}
@Override
public
boolean
clearRetainMessage
(
String
topic
)
{
retainStore
.
remove
(
topic
);
return
true
;
}
@Override
public
Collection
<
Message
>
getAllRetainMsg
()
{
return
retainStore
.
values
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录