├── logo.png ├── .travis.yml ├── doc └── img │ ├── gzh.jpg │ ├── logo.png │ ├── broker.jpg │ └── launch.jpg ├── out └── production │ └── ttiot-broker │ └── META-INF │ └── ttiot-broker.kotlin_module ├── ttiot-common ├── ttiot-common-core │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── link │ │ │ └── ttiot │ │ │ └── common │ │ │ └── core │ │ │ ├── constant │ │ │ ├── enums │ │ │ │ ├── ThreadPoolEnum.java │ │ │ │ └── MessageState.java │ │ │ ├── AppProtocalMqttConstant.java │ │ │ ├── ProtocalConstant.java │ │ │ └── CommonConstant.java │ │ │ ├── bean │ │ │ └── KeyFiledBean.java │ │ │ ├── exception │ │ │ ├── ProtocalException.java │ │ │ └── ConnectException.java │ │ │ ├── provider │ │ │ └── bean │ │ │ │ ├── SimpleBeanProvider.java │ │ │ │ └── SingtonBeanProvider.java │ │ │ ├── thread │ │ │ ├── ThreadProvider.java │ │ │ └── ThreadPoolProvider.java │ │ │ ├── ssl │ │ │ └── SslBuilder.java │ │ │ ├── security │ │ │ └── IdGenerator.java │ │ │ ├── function │ │ │ └── FunctionApi.java │ │ │ └── channel │ │ │ └── ChannelUtils.java │ └── pom.xml ├── ttiot-common-config │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── banner.txt │ │ │ └── java │ │ │ └── link │ │ │ └── ttiot │ │ │ └── common │ │ │ └── config │ │ │ └── ConfigurationFileReader.java │ └── pom.xml ├── pom.xml ├── ttiot-common-ioc │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── link │ │ └── ttiot │ │ └── common │ │ └── ioc │ │ ├── vo │ │ ├── MqttPayload.java │ │ ├── HttpRequest.java │ │ └── HttpRet.java │ │ ├── core │ │ ├── HttpHandler.java │ │ ├── RuleHandler.java │ │ ├── ApplicationListener.java │ │ ├── ApplicationEvent.java │ │ └── AbstractApplicationListener.java │ │ ├── annotation │ │ ├── DefaultListener.java │ │ ├── Inject.java │ │ ├── Http.java │ │ ├── Ruler.java │ │ ├── Eventor.java │ │ └── Listener.java │ │ └── multicaster │ │ ├── Multicaster.java │ │ └── ListenerRetriever.java ├── ttiot-common-redis │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── link │ │ │ └── ttiot │ │ │ └── common │ │ │ └── redis │ │ │ ├── KeyNamePolicy.java │ │ │ └── serializer │ │ │ └── Serializer.java │ └── pom.xml └── ttiot-common-context │ ├── src │ └── main │ │ └── java │ │ └── link │ │ └── ttiot │ │ └── common │ │ └── context │ │ ├── entity │ │ ├── Tenant.java │ │ ├── Device.java │ │ ├── Subscribe.java │ │ └── MessageStore.java │ │ ├── protocal │ │ ├── mqtt │ │ │ ├── MqttApplicationListener.java │ │ │ └── MqttApplicationEvent.java │ │ ├── ProtocolApplicationListener.java │ │ └── ProtocolApplicationEvent.java │ │ ├── exception │ │ └── ExceptionHandlerAdapter.java │ │ ├── service │ │ ├── DeviceService.java │ │ ├── SessionService.java │ │ ├── SubscribeService.java │ │ └── MessageStoreService.java │ │ ├── Session.java │ │ └── ChannelGroup.java │ └── pom.xml ├── ttiot-example ├── Dockerfile ├── src │ └── main │ │ ├── resources │ │ └── TTIotBootstrap.yml │ │ └── java │ │ └── link │ │ └── ttiot │ │ └── example │ │ └── server │ │ ├── listener │ │ ├── DbDemo.java │ │ └── LoginLogListener.java │ │ ├── exception │ │ └── ExceptionHandlerDemo.java │ │ ├── ServerExampleLauncher.java │ │ ├── rule │ │ └── ChatRuleHandler.java │ │ └── http │ │ └── DemoHttpHandler.java └── pom.xml ├── docker-compose.yml ├── ttiot-broker ├── src │ ├── main │ │ ├── resources │ │ │ └── TTIotBootstrap.yml │ │ └── java │ │ │ ├── link │ │ │ └── ttiot │ │ │ │ └── broker │ │ │ │ ├── handler │ │ │ │ ├── http │ │ │ │ │ ├── Method.java │ │ │ │ │ ├── HttpMqttRequestVo.java │ │ │ │ │ ├── HttpPacket.java │ │ │ │ │ ├── KeyValue.java │ │ │ │ │ ├── MqttHttpHandler.java │ │ │ │ │ ├── HttpRequestHandler.java │ │ │ │ │ └── RequestLine.java │ │ │ │ ├── websocket │ │ │ │ │ └── MqttWebSocketCodec.java │ │ │ │ ├── listener │ │ │ │ │ ├── connect │ │ │ │ │ │ ├── MqttIdentifierRejectedExceptionListener.java │ │ │ │ │ │ ├── MqttUnacceptableProtocolVersionExceptionListener.java │ │ │ │ │ │ ├── MqttDisConnectListener.java │ │ │ │ │ │ └── MqttConnectBackListener.java │ │ │ │ │ ├── decode │ │ │ │ │ │ ├── MqttDecoderSucceedDefaultListener.java │ │ │ │ │ │ └── MqttDecodeErrorDefaultListener.java │ │ │ │ │ ├── ping │ │ │ │ │ │ └── MqttPingListener.java │ │ │ │ │ ├── publish │ │ │ │ │ │ ├── ack │ │ │ │ │ │ │ └── MqttPubAckListener.java │ │ │ │ │ │ ├── comp │ │ │ │ │ │ │ └── MqttPubCompListener.java │ │ │ │ │ │ ├── rel │ │ │ │ │ │ │ └── MqttPubRelListener.java │ │ │ │ │ │ ├── rec │ │ │ │ │ │ │ └── MqttPubRecListener.java │ │ │ │ │ │ └── MqttUnconfirmedPublishListener.java │ │ │ │ │ ├── subscribe │ │ │ │ │ │ └── MqttUnSubscribeListener.java │ │ │ │ │ ├── ack │ │ │ │ │ │ └── MqttAckListener.java │ │ │ │ │ └── lwt │ │ │ │ │ │ └── MqttLwtListener.java │ │ │ │ ├── rule │ │ │ │ │ └── RuleDispatcher.java │ │ │ │ ├── ExceptionHandler.java │ │ │ │ └── MqttDecoderHandler.java │ │ │ │ ├── exception │ │ │ │ └── ServerExceptionHandler.java │ │ │ │ └── eventor │ │ │ │ ├── publish │ │ │ │ ├── MqttUnconfirmedPublishEvent.java │ │ │ │ ├── ack │ │ │ │ │ └── MqttPubAckEvent.java │ │ │ │ ├── rec │ │ │ │ │ └── MqttPubRecEvent.java │ │ │ │ ├── comp │ │ │ │ │ └── MqttPubCompEvent.java │ │ │ │ ├── rel │ │ │ │ │ └── MqttPubRelEvent.java │ │ │ │ ├── MqttPublishEvent.java │ │ │ │ ├── MqttPublishDevEvent.java │ │ │ │ └── MqttPublishTopicEvent.java │ │ │ │ ├── ack │ │ │ │ └── MqttAckEvent.java │ │ │ │ ├── lwt │ │ │ │ └── MqttLwtEvent.java │ │ │ │ ├── ping │ │ │ │ └── MqttPingEvent.java │ │ │ │ ├── connect │ │ │ │ ├── MqttDisConnectEvent.java │ │ │ │ ├── MqttUnacceptableProtocolVersionExceptionEvent.java │ │ │ │ ├── MqttIdentifierRejectedExceptionEvent.java │ │ │ │ ├── MqttConnectEvent.java │ │ │ │ └── MqttConnectBackEvent.java │ │ │ │ ├── decode │ │ │ │ ├── MqttDecoderFailEvent.java │ │ │ │ └── MqttDecoderSuccessEvent.java │ │ │ │ ├── http │ │ │ │ └── HttpRequestEvent.java │ │ │ │ └── subscribe │ │ │ │ ├── MqttSubscribeEvent.java │ │ │ │ └── MqttUnSubscribeEvent.java │ │ │ └── Bootstrap.java │ └── test │ │ └── java │ │ ├── IdGenerateTest.java │ │ └── DbTest.java └── pom.xml ├── .gitignore └── ttiot-client-java ├── src └── main │ └── java │ └── link │ └── ttiot │ └── client │ └── java │ ├── exception │ └── ClientExceptionHandler.java │ ├── Bootstrap.java │ ├── eventor │ ├── lwt │ │ └── MqttLwtEvent.java │ └── decode │ │ ├── MqttDecoderFailEvent.java │ │ └── MqttDecoderSuccessEvent.java │ └── handler │ └── MqttDecoderHandler.java └── pom.xml /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shijun90/TTIot/HEAD/logo.png -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | script: true -------------------------------------------------------------------------------- /doc/img/gzh.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shijun90/TTIot/HEAD/doc/img/gzh.jpg -------------------------------------------------------------------------------- /doc/img/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shijun90/TTIot/HEAD/doc/img/logo.png -------------------------------------------------------------------------------- /out/production/ttiot-broker/META-INF/ttiot-broker.kotlin_module: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /doc/img/broker.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shijun90/TTIot/HEAD/doc/img/broker.jpg -------------------------------------------------------------------------------- /doc/img/launch.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shijun90/TTIot/HEAD/doc/img/launch.jpg -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/constant/enums/ThreadPoolEnum.java: -------------------------------------------------------------------------------- 1 | package link.ttiot.common.core.constant.enums; 2 | 3 | public enum ThreadPoolEnum { 4 | fixedThread,singleThread,cachedThread 5 | } 6 | -------------------------------------------------------------------------------- /ttiot-example/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM anapsix/alpine-java:8_server-jre_unlimited 2 | 3 | MAINTAINER conttononline@outlook.com 4 | 5 | RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 6 | 7 | RUN mkdir -p /ttiot-example 8 | 9 | WORKDIR /ttiot-example 10 | 11 | EXPOSE 8729 12 | 13 | ADD ./ttiot-example/target/ttiot-example.jar ./ 14 | 15 | CMD java -Djava.security.egd=file:/dev/./urandom -jar ttiot-example.jar -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | ttiot-redis: 4 | image: redis:5.0 5 | command: redis-server --requirepass TTIot 6 | ports: 7 | - 6379:6379 8 | restart: always 9 | container_name: ttiot-redis 10 | hostname: ttiot-redis 11 | 12 | ttiot-example: 13 | build: 14 | context: ./ 15 | dockerfile: ./ttiot-example/Dockerfile 16 | restart: always 17 | ports: 18 | - 8729:8729 19 | container_name: ttiot-example 20 | hostname: ttiot-example 21 | image: ttiot-example -------------------------------------------------------------------------------- /ttiot-broker/src/main/resources/TTIotBootstrap.yml: -------------------------------------------------------------------------------- 1 | TTiot: 2 | port: 8729 3 | host: 0.0.0.0 4 | heartbeatTimeout: 10 5 | netty: 6 | bossGroupCount: -1 7 | workerGroupCount: -1 8 | leakDetectorLevel: DISABLED 9 | maxPayloadSize: 6553600 10 | ssl: 11 | enabled: false 12 | certificateType: 13 | certificatePath: 14 | certificatePassword: 15 | redis: 16 | host: ttiot-redis 17 | port: 6379 18 | timeout: 2000 19 | connectionTimeout: 2000 20 | soTimeout: 2000 21 | password: TTIot 22 | database: 0 23 | ssl: false 24 | maxIdle: 8 25 | minIdle: 0 26 | maxTotal: 8 27 | maxWaitMillis: -1 28 | 29 | -------------------------------------------------------------------------------- /ttiot-example/src/main/resources/TTIotBootstrap.yml: -------------------------------------------------------------------------------- 1 | TTiot: 2 | port: 8729 3 | host: 0.0.0.0 4 | heartbeatTimeout: 10 5 | netty: 6 | bossGroupCount: -1 7 | workerGroupCount: -1 8 | leakDetectorLevel: DISABLED 9 | maxPayloadSize: 65536 10 | ssl: 11 | enabled: true 12 | certificateType: PKCS12 13 | certificatePath: 14 | certificatePassword: 15 | redis: 16 | host: ttiot-redis 17 | port: 6379 18 | timeout: 2000 19 | connectionTimeout: 2000 20 | soTimeout: 2000 21 | password: TTIot 22 | database: 0 23 | ssl: false 24 | maxIdle: 8 25 | minIdle: 0 26 | maxTotal: 8 27 | maxWaitMillis: -1 28 | 29 | 30 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/http/Method.java: -------------------------------------------------------------------------------- 1 | package link.ttiot.broker.handler.http; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * @author wchao 7 | * 2017年6月28日 下午2:23:16 8 | */ 9 | public enum Method { 10 | GET("GET"), POST("POST"), HEAD("HEAD"), PUT("PUT"), TRACE("TRACE"), OPTIONS("OPTIONS"), PATCH("PATCH"); 11 | public static Method from(String method) { 12 | Method[] values = Method.values(); 13 | for (Method v : values) { 14 | if (Objects.equals(v.value, method)) { 15 | return v; 16 | } 17 | } 18 | return GET; 19 | } 20 | 21 | String value; 22 | 23 | private Method(String value) { 24 | this.value = value; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-config/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | .___________.___________. __ ______ .___________. 4 | | | || | / __ \ | | 5 | `---| |----`---| |----`| | | | | | `---| |----` 6 | | | | | | | | | | | | | 7 | | | | | | | | `--' | | | 8 | |__| |__| |__| \______/ |__| 9 | 10 | Copyright © www.ttiot.link 11 | VERSION: {} 12 | PORT: {} 13 | BOSSGROUP: {} 14 | WORKERGROUP: {} 15 | HEARTBEATTIMEOUT: {} 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### gradle ### 2 | .gradle 3 | /build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | 6 | ### STS ### 7 | .settings/ 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | bin/ 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | rebel.xml 22 | 23 | ### NetBeans ### 24 | nbproject/private/ 25 | build/ 26 | nbbuild/ 27 | dist/ 28 | nbdist/ 29 | .nb-gradle/ 30 | 31 | ### maven ### 32 | target/ 33 | *.war 34 | *.ear 35 | *.zip 36 | *.tar 37 | *.tar.gz 38 | 39 | ### logs #### 40 | /logs/ 41 | *.log 42 | 43 | ### temp ignore ### 44 | *.cache 45 | *.diff 46 | *.patch 47 | *.tmp 48 | *.java~ 49 | *.properties~ 50 | *.xml~ 51 | 52 | ### system ignore ### 53 | .DS_Store 54 | Thumbs.db 55 | Servers 56 | .metadata 57 | upload 58 | gen_code 59 | 60 | ### node ### 61 | node_modules 62 | -------------------------------------------------------------------------------- /ttiot-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | ttiot 7 | link.ttiot 8 | 2.0.0 9 | 10 | 4.0.0 11 | pom 12 | 13 | ttiot-common-core 14 | ttiot-common-redis 15 | ttiot-common-ioc 16 | ttiot-common-config 17 | ttiot-common-context 18 | 19 | ttiot-common 20 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/Bootstrap.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | 18 | import link.ttiot.broker.start.ServerLauncher; 19 | 20 | /** 21 | * @author: shijun 22 | * @date: 2019-04-11 23 | * @description: 24 | */ 25 | public class Bootstrap { 26 | 27 | public static void main(String[] args){ 28 | new ServerLauncher().launch(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | ttiot-common 7 | link.ttiot 8 | 2.0.0 9 | 10 | 4.0.0 11 | jar 12 | ttiot-common-ioc 13 | 14 | 15 | link.ttiot 16 | ttiot-common-core 17 | 18 | 19 | 20 | 21 | 22 | org.apache.maven.plugins 23 | maven-compiler-plugin 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/constant/enums/MessageState.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.constant.enums; 18 | 19 | /** 20 | * @author: shijun 21 | * @date: 2019-05-04 22 | * @description: 存储的消息状态 23 | */ 24 | public enum MessageState { 25 | PUBLISH, //等待REC,这类消息需要重新发送 26 | PUBREL, //等待COMPLETE,重发消息标识 27 | } 28 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/exception/ServerExceptionHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.exception; 18 | 19 | import link.ttiot.common.context.exception.ExceptionHandlerAdapter; 20 | 21 | /** 22 | * @author: shijun 23 | * @date: 2020-02-27 24 | * @description: 25 | */ 26 | public class ServerExceptionHandler extends ExceptionHandlerAdapter { 27 | } 28 | -------------------------------------------------------------------------------- /ttiot-client-java/src/main/java/link/ttiot/client/java/exception/ClientExceptionHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.client.java.exception; 18 | 19 | import link.ttiot.common.context.exception.ExceptionHandlerAdapter; 20 | 21 | /** 22 | * @author: shijun 23 | * @date: 2020-02-27 24 | * @description: 25 | */ 26 | public class ClientExceptionHandler extends ExceptionHandlerAdapter { 27 | } 28 | -------------------------------------------------------------------------------- /ttiot-client-java/src/main/java/link/ttiot/client/java/Bootstrap.java: -------------------------------------------------------------------------------- 1 | package link.ttiot.client.java; /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | 18 | import link.ttiot.client.java.start.ClientLauncher; 19 | 20 | /** 21 | * @author: shijun 22 | * @date: 2019-04-11 23 | * @description: 24 | */ 25 | public class Bootstrap { 26 | 27 | public static void main(String[] args){ 28 | new ClientLauncher().launch(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/vo/MqttPayload.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.vo; 18 | 19 | import lombok.Data; 20 | import lombok.ToString; 21 | 22 | /** 23 | * @author: shijun 24 | * @date: 2020-03-09 25 | * @description: 26 | */ 27 | @Data 28 | @ToString 29 | public class MqttPayload { 30 | 31 | private String rule; 32 | 33 | private String content; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/bean/KeyFiledBean.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.bean; 18 | 19 | import lombok.AllArgsConstructor; 20 | import lombok.Data; 21 | 22 | /** 23 | * @author: shijun 24 | * @date: 2019-04-22 25 | * @description: 26 | */ 27 | @AllArgsConstructor 28 | @Data 29 | public class KeyFiledBean { 30 | 31 | private String key; 32 | private String filed; 33 | } 34 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/core/HttpHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.core; 18 | 19 | import link.ttiot.common.ioc.vo.HttpRequest; 20 | import link.ttiot.common.ioc.vo.HttpRet; 21 | 22 | 23 | /** 24 | * @author: shijun 25 | * @date: 2019-04-15 26 | * @description: 27 | */ 28 | public interface HttpHandler{ 29 | 30 | public HttpRet handler(HttpRequest param); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/annotation/DefaultListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.annotation; 18 | 19 | import java.lang.annotation.*; 20 | 21 | /** 22 | * @author: shijun 23 | * @date: 2019-04-15 24 | * @description: 25 | */ 26 | @Documented 27 | @Inherited 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Target(ElementType.TYPE) 30 | public @interface DefaultListener { 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/annotation/Inject.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.annotation; 18 | 19 | import java.lang.annotation.*; 20 | 21 | /** 22 | * @author: shijun 23 | * @date: 2019-04-28 24 | * @description: 25 | */ 26 | @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Documented 29 | public @interface Inject { 30 | 31 | } 32 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/annotation/Http.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.annotation; 18 | 19 | import java.lang.annotation.*; 20 | 21 | /** 22 | * @author: shijun 23 | * @date: 2019-04-15 24 | * @description: 25 | */ 26 | @Documented 27 | @Inherited 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Target(ElementType.TYPE) 30 | public @interface Http { 31 | 32 | String uri() default ""; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/annotation/Ruler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.annotation; 18 | 19 | import java.lang.annotation.*; 20 | 21 | /** 22 | * @author: shijun 23 | * @date: 2019-04-15 24 | * @description: 25 | */ 26 | @Documented 27 | @Inherited 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Target(ElementType.TYPE) 30 | public @interface Ruler { 31 | 32 | String name() default ""; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/core/RuleHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.core; 18 | 19 | import link.ttiot.common.ioc.vo.MqttPayload; 20 | 21 | import java.util.EventListener; 22 | 23 | /** 24 | * @author: shijun 25 | * @date: 2019-04-15 26 | * @description: 27 | */ 28 | public interface RuleHandler extends EventListener { 29 | 30 | public void handler(MqttPayload mqttPayloadVo); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/multicaster/Multicaster.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.multicaster; 18 | 19 | import link.ttiot.common.ioc.core.ApplicationEvent; 20 | 21 | /** 22 | * @author: shijun 23 | * @date: 2019-05-05 24 | * @description: 25 | */ 26 | public interface Multicaster { 27 | 28 | void multicastEventProcess(ApplicationEvent event); 29 | 30 | void multicastEvent(final ApplicationEvent event); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/vo/HttpRequest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.vo; 18 | 19 | import lombok.AllArgsConstructor; 20 | import lombok.Data; 21 | 22 | /** 23 | * @author: shijun 24 | * @date: 2020-03-26 25 | * @description: 26 | */ 27 | @AllArgsConstructor 28 | @Data 29 | public class HttpRequest { 30 | private String userName; 31 | private String password; 32 | private String body; 33 | private String tenantId; 34 | } 35 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-config/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | ttiot-common 7 | link.ttiot 8 | 2.0.0 9 | 10 | 4.0.0 11 | ttiot-common-config 12 | jar 13 | 14 | 15 | link.ttiot 16 | ttiot-common-core 17 | 18 | 19 | org.yaml 20 | snakeyaml 21 | 22 | 23 | 24 | 25 | 26 | org.apache.maven.plugins 27 | maven-compiler-plugin 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/http/HttpMqttRequestVo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.http; 18 | 19 | import link.ttiot.common.ioc.vo.MqttPayload; 20 | import lombok.Data; 21 | 22 | /** 23 | * @author: shijun 24 | * @date: 2020-03-09 25 | * @description: 26 | */ 27 | @Data 28 | public class HttpMqttRequestVo { 29 | 30 | private String topic; 31 | 32 | private int mqttQos; 33 | 34 | private Boolean retain; 35 | 36 | private MqttPayload payload; 37 | } 38 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/annotation/Eventor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.annotation; 18 | 19 | import java.lang.annotation.*; 20 | 21 | /** 22 | * @author: shijun 23 | * @date: 2019-04-15 24 | * @description: 25 | */ 26 | @Documented 27 | @Inherited 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Target(ElementType.TYPE) 30 | public @interface Eventor { 31 | 32 | String value() default ""; 33 | 34 | boolean auth() default true; 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/annotation/Listener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.annotation; 18 | 19 | import java.lang.annotation.*; 20 | 21 | /** 22 | * @author: shijun 23 | * @date: 2019-04-15 24 | * @description: 25 | */ 26 | @Documented 27 | @Inherited 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Target(ElementType.TYPE) 30 | public @interface Listener { 31 | 32 | boolean replace() default false; 33 | 34 | boolean asynchronous() default false; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-redis/src/main/java/link/ttiot/common/redis/KeyNamePolicy.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.redis; 18 | 19 | /** 20 | * @author: shijun 21 | * @date: 2019-04-19 22 | * @description: 23 | */ 24 | public interface KeyNamePolicy { 25 | 26 | KeyNamePolicy defaultKeyNamePolicy = new KeyNamePolicy() { 27 | @Override 28 | public String getKeyName(Object key) { 29 | return key.toString(); 30 | } 31 | }; 32 | 33 | String getKeyName(Object var1); 34 | } 35 | -------------------------------------------------------------------------------- /ttiot-example/src/main/java/link/ttiot/example/server/listener/DbDemo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.example.server.listener; 18 | 19 | import cn.hutool.core.lang.Console; 20 | import lombok.experimental.UtilityClass; 21 | 22 | /** 23 | * @author: shijun 24 | * @date: 2019-05-06 25 | * @description: 26 | */ 27 | @UtilityClass 28 | public class DbDemo { 29 | 30 | 31 | public void saveLogin(Long time,String devName,String tenant){ 32 | Console.print("储存信息:"+"租户:"+tenant+"设备编号:"+devName+"在"+time+"登陆"); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/entity/Tenant.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.entity; 18 | 19 | import lombok.AllArgsConstructor; 20 | import lombok.Data; 21 | 22 | import java.io.Serializable; 23 | 24 | /** 25 | * @author: shijun 26 | * @date: 2019-04-19 27 | * @description: 28 | */ 29 | @Data 30 | @AllArgsConstructor 31 | public class Tenant implements Serializable { 32 | 33 | private static final long serialVersionUID = 1L; 34 | 35 | private String tenantId; 36 | private String tenantName; 37 | } 38 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-redis/src/main/java/link/ttiot/common/redis/serializer/Serializer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.redis.serializer; 18 | 19 | /** 20 | * @author: shijun 21 | * @date: 2019-04-19 22 | * @description: 23 | */ 24 | public interface Serializer { 25 | 26 | byte[] keyToBytes(String var1); 27 | 28 | String keyFromBytes(byte[] var1); 29 | 30 | byte[] fieldToBytes(Object var1); 31 | 32 | Object fieldFromBytes(byte[] var1); 33 | 34 | byte[] valueToBytes(Object var1); 35 | 36 | Object valueFromBytes(byte[] var1); 37 | } 38 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/core/ApplicationListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.core; 18 | 19 | 20 | 21 | import link.ttiot.common.ioc.core.ApplicationEvent; 22 | 23 | import java.util.EventListener; 24 | 25 | /** 26 | * @author: shijun 27 | * @date: 2019-04-13 28 | * @description: 29 | */ 30 | 31 | public interface ApplicationListener extends EventListener { 32 | 33 | void onApplicationEvent(E var1); 34 | 35 | boolean isDefault(); 36 | 37 | boolean isAsynchronous(); 38 | } 39 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/http/HttpPacket.java: -------------------------------------------------------------------------------- 1 | package link.ttiot.broker.handler.http; 2 | 3 | 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | 8 | public class HttpPacket { 9 | 10 | 11 | private String headerString; 12 | 13 | protected Map headers = new HashMap<>(); 14 | 15 | public HttpPacket() { 16 | 17 | } 18 | 19 | public void addHeader(String key, String value) { 20 | headers.put(key, value); 21 | } 22 | 23 | public void addHeaders(Map headers) { 24 | if (headers != null) { 25 | this.headers.putAll(headers); 26 | } 27 | } 28 | 29 | public String getHeader(String key) { 30 | return headers.get(key); 31 | } 32 | 33 | /** 34 | * @return the headers 35 | */ 36 | public Map getHeaders() { 37 | return headers; 38 | } 39 | 40 | public String getHeaderString() { 41 | return headerString; 42 | } 43 | 44 | public void removeHeader(String key, String value) { 45 | headers.remove(key); 46 | } 47 | 48 | /** 49 | * @param headers the headers to set 50 | */ 51 | public void setHeaders(Map headers) { 52 | this.headers = headers; 53 | } 54 | 55 | public void setHeaderString(String headerString) { 56 | this.headerString = headerString; 57 | } 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/publish/MqttUnconfirmedPublishEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.publish; 18 | 19 | 20 | import link.ttiot.common.ioc.core.ApplicationEvent; 21 | 22 | /** 23 | * @author: shijun 24 | * @date: 2019-05-05 25 | * @description: 26 | */ 27 | public class MqttUnconfirmedPublishEvent extends ApplicationEvent { 28 | 29 | public MqttUnconfirmedPublishEvent(Object source) { 30 | super(source); 31 | } 32 | 33 | @Override 34 | public String getSource() { 35 | return (String) super.getSource(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/entity/Device.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.entity; 18 | 19 | import lombok.AllArgsConstructor; 20 | import lombok.Data; 21 | 22 | import java.io.Serializable; 23 | 24 | /** 25 | * @author: shijun 26 | * @date: 2019-04-19 27 | * @description: 28 | */ 29 | @Data 30 | @AllArgsConstructor 31 | public class Device implements Serializable { 32 | 33 | private static final long serialVersionUID = 1L; 34 | private String clientId; 35 | private String clientPassword; 36 | private String tenantId; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /ttiot-example/src/main/java/link/ttiot/example/server/exception/ExceptionHandlerDemo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.example.server.exception; 18 | 19 | import cn.hutool.core.lang.Console; 20 | import link.ttiot.broker.exception.ServerExceptionHandler; 21 | 22 | 23 | /** 24 | * @author: shijun 25 | * @date: 2019-05-07 26 | * @description: 27 | */ 28 | public class ExceptionHandlerDemo extends ServerExceptionHandler { 29 | 30 | @Override 31 | public void uncaughtException(Thread t, Throwable e) { 32 | super.uncaughtException(t, e); 33 | Console.print("my exception handler"); 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /ttiot-client-java/src/main/java/link/ttiot/client/java/eventor/lwt/MqttLwtEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.client.java.eventor.lwt; 18 | 19 | import link.ttiot.common.ioc.annotation.Eventor; 20 | import link.ttiot.common.ioc.core.ApplicationEvent; 21 | 22 | 23 | /** 24 | * @author: shijun 25 | * @date: 2019-04-15 26 | * @description:遗嘱事件 27 | */ 28 | @Eventor 29 | public class MqttLwtEvent extends ApplicationEvent { 30 | 31 | public MqttLwtEvent(String clientId) { 32 | super(clientId); 33 | } 34 | 35 | @Override 36 | public String getSource() { 37 | return (String) source; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/protocal/mqtt/MqttApplicationListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.protocal.mqtt; 18 | 19 | 20 | import link.ttiot.common.context.protocal.ProtocolApplicationListener; 21 | 22 | /** 23 | * @author: shijun 24 | * @date: 2019-04-15 25 | * @description: 26 | */ 27 | public abstract class MqttApplicationListener extends ProtocolApplicationListener { 28 | 29 | public MqttApplicationListener() { 30 | super(); 31 | } 32 | 33 | @Override 34 | public void onApplicationEvent(E var1) { 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/entity/Subscribe.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.entity; 18 | 19 | import lombok.AllArgsConstructor; 20 | import lombok.Data; 21 | 22 | import java.io.Serializable; 23 | 24 | /** 25 | * @author: shijun 26 | * @date: 2019-04-19 27 | * @description: 28 | */ 29 | @Data 30 | @AllArgsConstructor 31 | public class Subscribe implements Serializable { 32 | 33 | private static final long serialVersionUID = 1L; 34 | 35 | private String topicFilter; 36 | 37 | private String devName; 38 | 39 | private int mqttQoS; 40 | 41 | private String clientId; 42 | } 43 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/ack/MqttAckEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.ack; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 22 | import link.ttiot.common.ioc.annotation.Eventor; 23 | 24 | 25 | /** 26 | * @author: shijun 27 | * @date: 2019-04-15 28 | * @description: 29 | */ 30 | @Eventor 31 | public class MqttAckEvent extends MqttApplicationEvent { 32 | 33 | public MqttAckEvent(MqttMessage msg, ChannelHandlerContext context) { 34 | super(msg,context); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/core/ApplicationEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.core; 18 | 19 | 20 | import java.util.EventObject; 21 | 22 | /** 23 | * @author: shijun 24 | * @date: 2019-04-13 25 | * @description: 26 | */ 27 | 28 | public abstract class ApplicationEvent extends EventObject { 29 | 30 | private static final long serialVersionUID = 1L; 31 | 32 | private final long timestamp = System.currentTimeMillis(); 33 | 34 | public ApplicationEvent(Object source) { 35 | super(source); 36 | } 37 | 38 | public final long getTimestamp() { 39 | return this.timestamp; 40 | } 41 | } -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | ttiot-common 7 | link.ttiot 8 | 2.0.0 9 | 10 | 4.0.0 11 | ttiot-common-context 12 | jar 13 | 14 | 15 | link.ttiot 16 | ttiot-common-core 17 | 18 | 19 | link.ttiot 20 | ttiot-common-config 21 | 22 | 23 | link.ttiot 24 | ttiot-common-ioc 25 | 26 | 27 | 28 | 29 | 30 | org.apache.maven.plugins 31 | maven-compiler-plugin 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | ttiot-common 7 | link.ttiot 8 | 2.0.0 9 | 10 | 2.0.0 11 | 4.0.0 12 | jar 13 | ttiot-common-redis 14 | 15 | 16 | link.ttiot 17 | ttiot-common-core 18 | 19 | 20 | redis.clients 21 | jedis 22 | 23 | 24 | link.ttiot 25 | ttiot-common-config 26 | 27 | 28 | 29 | 30 | 31 | org.apache.maven.plugins 32 | maven-compiler-plugin 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/lwt/MqttLwtEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.lwt; 18 | 19 | import io.netty.handler.codec.mqtt.MqttQoS; 20 | import link.ttiot.common.ioc.annotation.Eventor; 21 | import link.ttiot.common.ioc.core.ApplicationEvent; 22 | import lombok.Getter; 23 | 24 | 25 | /** 26 | * @author: shijun 27 | * @date: 2019-04-15 28 | * @description:遗嘱事件 29 | */ 30 | @Eventor 31 | public class MqttLwtEvent extends ApplicationEvent { 32 | 33 | public MqttLwtEvent(String clientId) { 34 | super(clientId); 35 | } 36 | 37 | @Override 38 | public String getSource() { 39 | return (String) source; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/exception/ProtocalException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.exception; 18 | 19 | import io.netty.channel.Channel; 20 | import lombok.Getter; 21 | 22 | /** 23 | * @author: shijun 24 | * @date: 2019-05-07 25 | * @description: 26 | */ 27 | public class ProtocalException extends RuntimeException { 28 | 29 | @Getter 30 | private Channel channel; 31 | 32 | public ProtocalException(Channel channel){ 33 | this.channel=channel; 34 | } 35 | 36 | public ProtocalException( String msg, Throwable throwable, Channel channel) { 37 | super(msg, throwable); 38 | this.channel=channel; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/provider/bean/SimpleBeanProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.provider.bean; 18 | 19 | import cn.hutool.core.exceptions.ExceptionUtil; 20 | import cn.hutool.core.exceptions.UtilException; 21 | import lombok.experimental.UtilityClass; 22 | 23 | /** 24 | * @author: shijun 25 | * @date: 2019-04-14 26 | * @description: 27 | */ 28 | @UtilityClass 29 | public class SimpleBeanProvider { 30 | 31 | public T produce(Class type) { 32 | try { 33 | return type.newInstance(); 34 | } catch (Throwable e) { 35 | throw ExceptionUtil.wrap(e, UtilException.class); 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /ttiot-example/src/main/java/link/ttiot/example/server/ServerExampleLauncher.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.example.server; 18 | 19 | 20 | import link.ttiot.broker.db.redis.RedisDbHelper; 21 | import link.ttiot.broker.start.ServerLauncher; 22 | import link.ttiot.common.redis.RedisSourceProvider; 23 | import link.ttiot.example.server.exception.ExceptionHandlerDemo; 24 | 25 | /** 26 | * @author: shijun 27 | * @date: 2019-05-05 28 | * @description: 29 | */ 30 | public class ServerExampleLauncher { 31 | 32 | public static void main(String[] args) { 33 | new ServerLauncher().dbHelper(new RedisDbHelper(new RedisSourceProvider())).exceptionHandler(new ExceptionHandlerDemo()).launch(); 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/exception/ExceptionHandlerAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.exception; 18 | 19 | 20 | import link.ttiot.common.context.Context; 21 | import link.ttiot.common.core.channel.ChannelUtils; 22 | import link.ttiot.common.core.exception.ConnectException; 23 | import lombok.extern.slf4j.Slf4j; 24 | 25 | /** 26 | * @author: shijun 27 | * @date: 2019-04-24 28 | * @description: 29 | */ 30 | @Slf4j 31 | public abstract class ExceptionHandlerAdapter implements Thread.UncaughtExceptionHandler { 32 | 33 | /** 34 | * 连接中异常 35 | * @param t 36 | * @param e 37 | */ 38 | @Override 39 | public void uncaughtException(Thread t, Throwable e) { 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /ttiot-example/src/main/java/link/ttiot/example/server/listener/LoginLogListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.example.server.listener; 18 | 19 | import link.ttiot.broker.eventor.connect.MqttConnectEvent; 20 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 21 | import link.ttiot.common.ioc.annotation.Listener; 22 | 23 | /** 24 | * @author: shijun 25 | * @date: 2019-05-06 26 | * @description: 27 | */ 28 | @Listener(asynchronous = true) 29 | public class LoginLogListener extends MqttApplicationListener { 30 | 31 | @Override 32 | public void onApplicationEvent(MqttConnectEvent mqttConnectEvent) { 33 | DbDemo.saveLogin(mqttConnectEvent.getTimestamp(), mqttConnectEvent.getDevName(), mqttConnectEvent.getTenantId()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /ttiot-example/src/main/java/link/ttiot/example/server/rule/ChatRuleHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.example.server.rule; 18 | 19 | import link.ttiot.common.context.service.DeviceService; 20 | import link.ttiot.common.ioc.annotation.Inject; 21 | import link.ttiot.common.ioc.annotation.Ruler; 22 | import link.ttiot.common.ioc.core.RuleHandler; 23 | import link.ttiot.common.ioc.vo.MqttPayload; 24 | 25 | /** 26 | * @author: shijun 27 | * @date: 2020-03-19 28 | * @description: 29 | */ 30 | @Ruler(name = "chat") 31 | public class ChatRuleHandler implements RuleHandler { 32 | 33 | @Inject 34 | private DeviceService deviceService; 35 | 36 | @Override 37 | public void handler(MqttPayload mqttPayloadVo) { 38 | System.out.println(mqttPayloadVo); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/ping/MqttPingEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.ping; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 22 | import link.ttiot.common.core.constant.AppProtocalMqttConstant; 23 | import link.ttiot.common.ioc.annotation.Eventor; 24 | 25 | 26 | /** 27 | * @author: shijun 28 | * @date: 2019-04-15 29 | * @description: 30 | */ 31 | @Eventor(value = AppProtocalMqttConstant.PINGREQ,auth = true) 32 | public class MqttPingEvent extends MqttApplicationEvent { 33 | 34 | public MqttPingEvent(MqttMessage msg, ChannelHandlerContext context) { 35 | super(msg, context); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/publish/ack/MqttPubAckEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.publish.ack; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 22 | import link.ttiot.common.core.constant.AppProtocalMqttConstant; 23 | import link.ttiot.common.ioc.annotation.Eventor; 24 | 25 | 26 | /** 27 | * @author: shijun 28 | * @date: 2019-04-15 29 | * @description: 30 | */ 31 | @Eventor(value = AppProtocalMqttConstant.PUBACK) 32 | public class MqttPubAckEvent extends MqttApplicationEvent { 33 | 34 | public MqttPubAckEvent(MqttMessage msg, ChannelHandlerContext context) { 35 | super(msg,context); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/publish/rec/MqttPubRecEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.publish.rec; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 22 | import link.ttiot.common.core.constant.AppProtocalMqttConstant; 23 | import link.ttiot.common.ioc.annotation.Eventor; 24 | 25 | 26 | /** 27 | * @author: shijun 28 | * @date: 2019-04-15 29 | * @description: 30 | */ 31 | @Eventor(value = AppProtocalMqttConstant.PUBREC) 32 | public class MqttPubRecEvent extends MqttApplicationEvent { 33 | 34 | public MqttPubRecEvent(MqttMessage msg, ChannelHandlerContext context) { 35 | super(msg,context); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/connect/MqttDisConnectEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.connect; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 22 | import link.ttiot.common.core.constant.AppProtocalMqttConstant; 23 | import link.ttiot.common.ioc.annotation.Eventor; 24 | 25 | 26 | /** 27 | * @author: shijun 28 | * @date: 2019-04-15 29 | * @description: 30 | */ 31 | @Eventor(value = AppProtocalMqttConstant.DISCONNECT) 32 | public class MqttDisConnectEvent extends MqttApplicationEvent { 33 | 34 | public MqttDisConnectEvent(MqttMessage msg, ChannelHandlerContext context) { 35 | super(msg,context); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/decode/MqttDecoderFailEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.decode; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 22 | import link.ttiot.common.ioc.annotation.Eventor; 23 | 24 | /** 25 | * @author: shijun 26 | * @date: 2019-04-15 27 | * @description: 28 | */ 29 | @Eventor(auth = false) 30 | public class MqttDecoderFailEvent extends MqttApplicationEvent { 31 | 32 | public MqttDecoderFailEvent(MqttMessage msg,ChannelHandlerContext source) { 33 | super(msg,source); 34 | } 35 | @Override 36 | public MqttMessage getSource() { 37 | return (MqttMessage)source; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/publish/comp/MqttPubCompEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.publish.comp; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 22 | import link.ttiot.common.core.constant.AppProtocalMqttConstant; 23 | import link.ttiot.common.ioc.annotation.Eventor; 24 | 25 | 26 | /** 27 | * @author: shijun 28 | * @date: 2019-04-15 29 | * @description: 30 | */ 31 | @Eventor(value = AppProtocalMqttConstant.PUBCOMP) 32 | public class MqttPubCompEvent extends MqttApplicationEvent { 33 | 34 | public MqttPubCompEvent(MqttMessage msg, ChannelHandlerContext context) { 35 | super(msg,context); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/publish/rel/MqttPubRelEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.publish.rel; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 22 | import link.ttiot.common.core.constant.AppProtocalMqttConstant; 23 | import link.ttiot.common.ioc.annotation.Eventor; 24 | 25 | 26 | /** 27 | * @author: shijun 28 | * @date: 2019-04-15 29 | * @description: 收到recive消息之后回复 30 | */ 31 | @Eventor(value = AppProtocalMqttConstant.PUBREL) 32 | public class MqttPubRelEvent extends MqttApplicationEvent { 33 | 34 | public MqttPubRelEvent(MqttMessage msg, ChannelHandlerContext context) { 35 | super(msg,context); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/protocal/mqtt/MqttApplicationEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.protocal.mqtt; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.ProtocolApplicationEvent; 22 | 23 | /** 24 | * @author: shijun 25 | * @date: 2019-04-15 26 | * @description: 27 | */ 28 | public abstract class MqttApplicationEvent extends ProtocolApplicationEvent { 29 | 30 | public MqttApplicationEvent(MqttMessage mqttMessage, ChannelHandlerContext context) { 31 | super(mqttMessage,context); 32 | } 33 | 34 | @Override 35 | public MqttMessage getSource() { 36 | return (MqttMessage) source; 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/decode/MqttDecoderSuccessEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.decode; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 22 | import link.ttiot.common.ioc.annotation.Eventor; 23 | 24 | /** 25 | * @author: shijun 26 | * @date: 2019-04-15 27 | * @description: 28 | */ 29 | @Eventor(auth = false) 30 | public class MqttDecoderSuccessEvent extends MqttApplicationEvent { 31 | 32 | public MqttDecoderSuccessEvent( MqttMessage msg,ChannelHandlerContext context) { 33 | super(msg,context); 34 | } 35 | @Override 36 | public MqttMessage getSource() { 37 | return (MqttMessage)source; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /ttiot-client-java/src/main/java/link/ttiot/client/java/eventor/decode/MqttDecoderFailEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.client.java.eventor.decode; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 22 | import link.ttiot.common.ioc.annotation.Eventor; 23 | 24 | /** 25 | * @author: shijun 26 | * @date: 2019-04-15 27 | * @description: 28 | */ 29 | @Eventor(auth = false) 30 | public class MqttDecoderFailEvent extends MqttApplicationEvent { 31 | 32 | public MqttDecoderFailEvent(MqttMessage msg,ChannelHandlerContext source) { 33 | super(msg,source); 34 | } 35 | @Override 36 | public MqttMessage getSource() { 37 | return (MqttMessage)source; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /ttiot-client-java/src/main/java/link/ttiot/client/java/eventor/decode/MqttDecoderSuccessEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.client.java.eventor.decode; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 22 | import link.ttiot.common.ioc.annotation.Eventor; 23 | 24 | /** 25 | * @author: shijun 26 | * @date: 2019-04-15 27 | * @description: 28 | */ 29 | @Eventor(auth = false) 30 | public class MqttDecoderSuccessEvent extends MqttApplicationEvent { 31 | 32 | public MqttDecoderSuccessEvent( MqttMessage msg,ChannelHandlerContext context) { 33 | super(msg,context); 34 | } 35 | @Override 36 | public MqttMessage getSource() { 37 | return (MqttMessage)source; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /ttiot-example/src/main/java/link/ttiot/example/server/http/DemoHttpHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.example.server.http; 18 | 19 | import link.ttiot.common.context.service.DeviceService; 20 | import link.ttiot.common.ioc.annotation.Http; 21 | import link.ttiot.common.ioc.annotation.Inject; 22 | import link.ttiot.common.ioc.core.HttpHandler; 23 | import link.ttiot.common.ioc.vo.HttpRequest; 24 | import link.ttiot.common.ioc.vo.HttpRet; 25 | 26 | /** 27 | * @author: shijun 28 | * @date: 2020-03-25 29 | * @description: 30 | */ 31 | @Http(uri = "/demo") 32 | public class DemoHttpHandler implements HttpHandler { 33 | 34 | @Inject 35 | private DeviceService deviceService; 36 | 37 | 38 | @Override 39 | public HttpRet handler(HttpRequest param) { 40 | 41 | return HttpRet.success(param); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/service/DeviceService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.service; 18 | 19 | import link.ttiot.common.context.db.DbHelper; 20 | import link.ttiot.common.ioc.annotation.Inject; 21 | import lombok.Getter; 22 | 23 | import java.util.Optional; 24 | 25 | /** 26 | * @author: shijun 27 | * @date: 2019-04-22 28 | * @description: 29 | */ 30 | public class DeviceService{ 31 | 32 | @Getter 33 | @Inject 34 | private DbHelper dbHelper; 35 | 36 | public boolean login(String tenantId, String userName, String password) { 37 | return Optional.ofNullable(dbHelper.getDev(tenantId, userName)). 38 | map(u -> u.getClientPassword() 39 | .equals(password)) 40 | .orElse(false); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/constant/AppProtocalMqttConstant.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.constant; 18 | 19 | /** 20 | * @author: shijun 21 | * @date: 2019-04-16 22 | * @description: 23 | */ 24 | public interface AppProtocalMqttConstant { 25 | 26 | String CONNECT="CONNECT"; 27 | String CONNACK="CONNACK"; 28 | String PUBLISH="PUBLISH"; 29 | String PUBACK="PUBACK"; 30 | String PUBREC="PUBREC"; 31 | String PUBREL="PUBREL"; 32 | String PUBCOMP="PUBCOMP"; 33 | String SUBSCRIBE="SUBSCRIBE"; 34 | String SUBACK="SUBACK"; 35 | String UNSUBSCRIBE="UNSUBSCRIBE"; 36 | String UNSUBACK="UNSUBACK"; 37 | String PINGREQ="PINGREQ"; 38 | String PINGRESP="PINGRESP"; 39 | String DISCONNECT="DISCONNECT"; 40 | String HTTPREQUEST="httpRequest"; 41 | 42 | } 43 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/service/SessionService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.service; 18 | import link.ttiot.common.context.Session; 19 | import link.ttiot.common.context.db.DbHelper; 20 | import link.ttiot.common.ioc.annotation.Inject; 21 | import lombok.Getter; 22 | 23 | /** 24 | * @author: shijun 25 | * @date: 2019-05-03 26 | * @description: 27 | */ 28 | public class SessionService { 29 | 30 | @Getter 31 | @Inject 32 | private DbHelper dbHelper; 33 | 34 | public void save(Session ttIotSession) { 35 | dbHelper.saveSession(ttIotSession); 36 | } 37 | 38 | public Session get(String clientId) { 39 | return dbHelper.getSession(clientId); 40 | } 41 | 42 | public void delete(String clientId) { 43 | dbHelper.deleteSession(clientId); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/http/KeyValue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.http; 18 | 19 | /** 20 | * @author: shijun 21 | * @date: 2020-03-03 22 | * @description: 23 | */ 24 | public class KeyValue { 25 | private String key; 26 | private String value; 27 | 28 | /** 29 | * @return the key 30 | */ 31 | public String getKey() { 32 | return key; 33 | } 34 | 35 | /** 36 | * @return the value 37 | */ 38 | public String getValue() { 39 | return value; 40 | } 41 | 42 | /** 43 | * @param key the key to set 44 | */ 45 | public void setKey(String key) { 46 | this.key = key; 47 | } 48 | 49 | /** 50 | * @param value the value to set 51 | */ 52 | public void setValue(String value) { 53 | this.value = value; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/protocal/ProtocolApplicationListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.protocal; 18 | 19 | import link.ttiot.common.context.Context; 20 | import link.ttiot.common.ioc.core.AbstractApplicationListener; 21 | import link.ttiot.common.ioc.core.ApplicationEvent; 22 | 23 | /** 24 | * @author: shijun 25 | * @date: 2019-04-15 26 | * @description: 27 | */ 28 | public abstract class ProtocolApplicationListener extends AbstractApplicationListener { 29 | 30 | public ProtocolApplicationListener() { 31 | super(); 32 | } 33 | 34 | public void publishEvent(ApplicationEvent event) { 35 | getTTIotContext().publishEvent(event); 36 | } 37 | 38 | public Context getTTIotContext() { 39 | return Context.me(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/connect/MqttUnacceptableProtocolVersionExceptionEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.connect; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 22 | import link.ttiot.common.ioc.annotation.Eventor; 23 | 24 | /** 25 | * @author: shijun 26 | * @date: 2019-04-15 27 | * @description: 28 | */ 29 | @Eventor(auth = false) 30 | public class MqttUnacceptableProtocolVersionExceptionEvent extends MqttApplicationEvent { 31 | public MqttUnacceptableProtocolVersionExceptionEvent( MqttMessage msg,ChannelHandlerContext context) { 32 | super(msg,context); 33 | } 34 | @Override 35 | public MqttMessage getSource() { 36 | return (MqttMessage)source; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/core/AbstractApplicationListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.core; 18 | 19 | import lombok.Getter; 20 | import lombok.Setter; 21 | 22 | /** 23 | * @author: shijun 24 | * @date: 2019-04-15 25 | * @description: 26 | */ 27 | public abstract class AbstractApplicationListener implements ApplicationListener { 28 | 29 | private boolean isDefault; 30 | 31 | private boolean isAsynchronous; 32 | 33 | @Override 34 | public boolean isDefault() { 35 | return isDefault; 36 | } 37 | 38 | @Override 39 | public boolean isAsynchronous() { 40 | return isAsynchronous; 41 | } 42 | 43 | public AbstractApplicationListener() { 44 | } 45 | 46 | @Override 47 | public void onApplicationEvent(E var1) { 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/constant/ProtocalConstant.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.constant; 18 | 19 | import io.netty.util.AttributeKey; 20 | 21 | import java.util.List; 22 | import java.util.Set; 23 | 24 | /** 25 | * @author: shijun 26 | * @date: 2019-04-22 27 | * @description: 28 | */ 29 | public interface ProtocalConstant { 30 | 31 | String SEPARATOR = "-"; 32 | String TERMINATOR = ";"; 33 | int CLIENT_IDENTIFY_LENGTH = 21; 34 | char SEPARATORTOPIC_CHAR = '/'; 35 | String SEPARATORTOPIC_STRING = "/"; 36 | String SEPARATORTOPIC_SINGLE = "+"; 37 | String SEPARATORTOPIC_GLOB = "#"; 38 | AttributeKey LOGIN = AttributeKey.valueOf("LOGIN"); 39 | AttributeKey CLIENTID = AttributeKey.valueOf("CLIENTID"); 40 | AttributeKey> RECEIVE = AttributeKey.valueOf("RECEIVE"); 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/Session.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context; 18 | 19 | import io.netty.handler.codec.mqtt.MqttPublishMessage; 20 | import link.ttiot.common.core.security.ClientIdReader; 21 | import lombok.AllArgsConstructor; 22 | import lombok.Data; 23 | import lombok.NoArgsConstructor; 24 | 25 | import java.io.Serializable; 26 | 27 | /** 28 | * @author: shijun 29 | * @date: 2019-05-02 30 | * @description: 31 | */ 32 | @Data 33 | @AllArgsConstructor 34 | @NoArgsConstructor 35 | public class Session implements Serializable { 36 | 37 | private static final long serialVersionUID = 1L; 38 | 39 | private String clientId; 40 | 41 | private boolean cleanSession; 42 | 43 | private MqttPublishMessage willMessage; 44 | 45 | public String getTenantId() { 46 | return ClientIdReader.tenantId(this.clientId); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/connect/MqttIdentifierRejectedExceptionEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.connect; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttConnectMessage; 21 | import io.netty.handler.codec.mqtt.MqttMessage; 22 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 23 | import link.ttiot.common.ioc.annotation.Eventor; 24 | 25 | /** 26 | * @author: shijun 27 | * @date: 2019-04-15 28 | * @description: 29 | */ 30 | @Eventor(auth = false) 31 | public class MqttIdentifierRejectedExceptionEvent extends MqttApplicationEvent { 32 | public MqttIdentifierRejectedExceptionEvent( MqttMessage msg,ChannelHandlerContext context) { 33 | super(msg,context); 34 | } 35 | 36 | @Override 37 | public MqttConnectMessage getSource() { 38 | return (MqttConnectMessage)source; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/http/HttpRequestEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.http; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.http.FullHttpRequest; 21 | import io.netty.handler.codec.mqtt.MqttMessage; 22 | import link.ttiot.common.context.protocal.ProtocolApplicationEvent; 23 | import link.ttiot.common.core.constant.AppProtocalMqttConstant; 24 | import link.ttiot.common.ioc.annotation.Eventor; 25 | 26 | /** 27 | * @author: shijun 28 | * @date: 2020-03-17 29 | * @description: 30 | */ 31 | @Eventor(value = AppProtocalMqttConstant.HTTPREQUEST,auth = false) 32 | public class HttpRequestEvent extends ProtocolApplicationEvent { 33 | 34 | public HttpRequestEvent(Object msg, ChannelHandlerContext context) { 35 | super(msg, context); 36 | } 37 | 38 | @Override 39 | public FullHttpRequest getSource() { 40 | return (FullHttpRequest) source; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/publish/MqttPublishEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.publish; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import io.netty.handler.codec.mqtt.MqttPublishMessage; 22 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 23 | import link.ttiot.common.core.constant.AppProtocalMqttConstant; 24 | import link.ttiot.common.ioc.annotation.Eventor; 25 | 26 | 27 | /** 28 | * @author: shijun 29 | * @date: 2019-04-15 30 | * @description: 31 | */ 32 | @Eventor(value = AppProtocalMqttConstant.PUBLISH) 33 | public class MqttPublishEvent extends MqttApplicationEvent { 34 | 35 | public MqttPublishEvent(MqttMessage msg,ChannelHandlerContext context) { 36 | super(msg,context); 37 | } 38 | 39 | @Override 40 | public MqttPublishMessage getSource() { 41 | return (MqttPublishMessage) source; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/connect/MqttConnectEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.connect; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttConnectMessage; 21 | import io.netty.handler.codec.mqtt.MqttMessage; 22 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 23 | import link.ttiot.common.core.constant.AppProtocalMqttConstant; 24 | import link.ttiot.common.ioc.annotation.Eventor; 25 | 26 | 27 | /** 28 | * @author: shijun 29 | * @date: 2019-04-15 30 | * @description: 31 | */ 32 | @Eventor(value = AppProtocalMqttConstant.CONNECT,auth = false) 33 | public class MqttConnectEvent extends MqttApplicationEvent { 34 | 35 | public MqttConnectEvent(MqttMessage msg,ChannelHandlerContext context) { 36 | super(msg, context); 37 | } 38 | 39 | @Override 40 | public MqttConnectMessage getSource() { 41 | return (MqttConnectMessage)source; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/subscribe/MqttSubscribeEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.subscribe; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import io.netty.handler.codec.mqtt.MqttSubscribeMessage; 22 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 23 | import link.ttiot.common.core.constant.AppProtocalMqttConstant; 24 | import link.ttiot.common.ioc.annotation.Eventor; 25 | 26 | 27 | /** 28 | * @author: shijun 29 | * @date: 2019-04-15 30 | * @description: 31 | */ 32 | @Eventor(value = AppProtocalMqttConstant.SUBSCRIBE) 33 | public class MqttSubscribeEvent extends MqttApplicationEvent { 34 | 35 | public MqttSubscribeEvent(MqttMessage msg,ChannelHandlerContext context) { 36 | super(msg,context); 37 | } 38 | 39 | @Override 40 | public MqttSubscribeMessage getSource() { 41 | return (MqttSubscribeMessage) source; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/websocket/MqttWebSocketCodec.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.websocket; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import io.netty.channel.ChannelHandlerContext; 21 | import io.netty.handler.codec.MessageToMessageCodec; 22 | import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | * @author: shijun 28 | * @date: 2020-03-04 29 | * @description: 30 | */ 31 | public class MqttWebSocketCodec extends MessageToMessageCodec { 32 | 33 | @Override 34 | protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List out) throws Exception { 35 | out.add(new BinaryWebSocketFrame(msg.retain())); 36 | } 37 | 38 | 39 | 40 | @Override 41 | protected void decode(ChannelHandlerContext ctx, BinaryWebSocketFrame msg, List out) throws Exception { 42 | out.add(msg.retain().content()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/exception/ConnectException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.exception; 18 | 19 | import io.netty.channel.Channel; 20 | import io.netty.handler.codec.mqtt.MqttConnectReturnCode; 21 | 22 | /** 23 | * @author: shijun 24 | * @date: 2019-04-24 25 | * @description: 26 | */ 27 | public class ConnectException extends ProtocalException { 28 | 29 | private static final long serialVersionUID = 1L; 30 | 31 | private MqttConnectReturnCode returnCode; 32 | 33 | public ConnectException(MqttConnectReturnCode status,Channel channel) { 34 | super(channel); 35 | this.returnCode = status; 36 | } 37 | 38 | public ConnectException(MqttConnectReturnCode status, String msg, Throwable throwable,Channel channel) { 39 | super(msg, throwable,channel); 40 | this.returnCode = status; 41 | } 42 | 43 | public MqttConnectReturnCode getStatus() { 44 | return this.returnCode; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/subscribe/MqttUnSubscribeEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.subscribe; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttMessage; 21 | import io.netty.handler.codec.mqtt.MqttUnsubscribeMessage; 22 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 23 | import link.ttiot.common.core.constant.AppProtocalMqttConstant; 24 | import link.ttiot.common.ioc.annotation.Eventor; 25 | 26 | 27 | /** 28 | * @author: shijun 29 | * @date: 2019-04-15 30 | * @description: 31 | */ 32 | @Eventor(value = AppProtocalMqttConstant.UNSUBSCRIBE) 33 | public class MqttUnSubscribeEvent extends MqttApplicationEvent { 34 | 35 | public MqttUnSubscribeEvent(MqttMessage msg, ChannelHandlerContext context) { 36 | super(msg,context); 37 | } 38 | 39 | @Override 40 | public MqttUnsubscribeMessage getSource() { 41 | return (MqttUnsubscribeMessage) source; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/connect/MqttIdentifierRejectedExceptionListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.connect; 18 | 19 | import link.ttiot.broker.eventor.connect.MqttIdentifierRejectedExceptionEvent; 20 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 21 | import link.ttiot.common.ioc.annotation.DefaultListener; 22 | import lombok.extern.slf4j.Slf4j; 23 | 24 | /** 25 | * @author: shijun 26 | * @date: 2019-04-15 27 | * @description: 28 | */ 29 | @Slf4j 30 | @DefaultListener 31 | public class MqttIdentifierRejectedExceptionListener extends MqttApplicationListener { 32 | 33 | public MqttIdentifierRejectedExceptionListener() { 34 | super(); 35 | } 36 | 37 | @Override 38 | public void onApplicationEvent(MqttIdentifierRejectedExceptionEvent rejectedExceptionEvent) { 39 | rejectedExceptionEvent.getChannelHandlerContext().channel().close(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/rule/RuleDispatcher.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.rule; 18 | 19 | import cn.hutool.json.JSONUtil; 20 | import link.ttiot.broker.eventor.publish.MqttPublishTopicEvent; 21 | import link.ttiot.common.context.Context; 22 | import link.ttiot.common.ioc.annotation.Listener; 23 | import link.ttiot.common.ioc.core.AbstractApplicationListener; 24 | import link.ttiot.common.ioc.vo.MqttPayload; 25 | 26 | /** 27 | * @author: shijun 28 | * @date: 2020-03-19 29 | * @description: 30 | */ 31 | @Listener(asynchronous = true) 32 | public class RuleDispatcher extends AbstractApplicationListener { 33 | 34 | @Override 35 | public void onApplicationEvent(MqttPublishTopicEvent mqttConnectEvent) { 36 | try { 37 | MqttPayload vo = JSONUtil.toBean(new String(mqttConnectEvent.getBytes()), MqttPayload.class); 38 | Context.me().multicastRuleHandler(vo.getRule(),vo); 39 | }catch (Exception e){ 40 | return; 41 | } 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/connect/MqttUnacceptableProtocolVersionExceptionListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.connect; 18 | 19 | import link.ttiot.broker.eventor.connect.MqttUnacceptableProtocolVersionExceptionEvent; 20 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 21 | import link.ttiot.common.ioc.annotation.DefaultListener; 22 | import lombok.extern.slf4j.Slf4j; 23 | 24 | /** 25 | * @author: shijun 26 | * @date: 2019-04-15 27 | * @description: 28 | */ 29 | @Slf4j 30 | @DefaultListener 31 | public class MqttUnacceptableProtocolVersionExceptionListener extends MqttApplicationListener { 32 | 33 | public MqttUnacceptableProtocolVersionExceptionListener() { 34 | super(); 35 | } 36 | 37 | @Override 38 | public void onApplicationEvent(MqttUnacceptableProtocolVersionExceptionEvent versionExceptionEvent) { 39 | versionExceptionEvent.getChannelHandlerContext().channel().close(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/thread/ThreadProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.thread; 18 | import lombok.extern.slf4j.Slf4j; 19 | 20 | import java.util.Optional; 21 | import java.util.concurrent.ThreadFactory; 22 | 23 | /** 24 | * @author: shijun 25 | * @date: 2019-05-07 26 | * @description: 27 | */ 28 | @Slf4j 29 | public class ThreadProvider implements ThreadFactory { 30 | 31 | private Thread.UncaughtExceptionHandler uncaughtExceptionHandler; 32 | 33 | public ThreadProvider(Thread.UncaughtExceptionHandler uncaughtExceptionHandler){ 34 | this.uncaughtExceptionHandler=uncaughtExceptionHandler; 35 | } 36 | 37 | @Override 38 | public Thread newThread(Runnable r) { 39 | return Optional.ofNullable(uncaughtExceptionHandler).map(u->{ 40 | log.info("Task named {} begin",r.getClass()); 41 | Thread thread=new Thread(r); 42 | thread.setUncaughtExceptionHandler(u); 43 | return thread; 44 | }).orElse(new Thread(r)); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/entity/MessageStore.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.entity; 18 | 19 | import io.netty.handler.codec.mqtt.MqttQoS; 20 | import link.ttiot.common.core.constant.enums.MessageState; 21 | import lombok.AllArgsConstructor; 22 | import lombok.Data; 23 | 24 | import java.io.Serializable; 25 | 26 | /** 27 | * @author: shijun 28 | * @date: 2019-04-19 29 | * @description: 30 | */ 31 | @Data 32 | @AllArgsConstructor 33 | public class MessageStore implements Serializable { 34 | 35 | private static final long serialVersionUID = 1L; 36 | 37 | private MessageState messageState; 38 | 39 | private String clientId; 40 | 41 | private String topic; 42 | 43 | private MqttQoS mqttQoS; 44 | 45 | private int packetId; 46 | 47 | public MessageStore( String topic, MqttQoS mqttQoS, byte[] messageBytes) { 48 | this.topic = topic; 49 | this.mqttQoS = mqttQoS; 50 | this.messageBytes = messageBytes; 51 | } 52 | 53 | private byte[] messageBytes; 54 | 55 | } 56 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/publish/MqttPublishDevEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.publish; 18 | 19 | import io.netty.handler.codec.mqtt.MqttQoS; 20 | import link.ttiot.common.ioc.annotation.Eventor; 21 | import link.ttiot.common.ioc.core.ApplicationEvent; 22 | import lombok.Getter; 23 | 24 | 25 | /** 26 | * @author: shijun 27 | * @date: 2019-04-15 28 | * @description: 29 | */ 30 | @Eventor 31 | public class MqttPublishDevEvent extends ApplicationEvent { 32 | 33 | @Getter 34 | private String topic; 35 | @Getter 36 | private byte[] byteBuf; 37 | @Getter 38 | private MqttQoS qoS; 39 | @Getter 40 | private String tenantId; 41 | 42 | public MqttPublishDevEvent(String clientId, String topic, byte[] byteBuf, MqttQoS qoS,String tenantId) { 43 | super(clientId); 44 | this.topic=topic; 45 | this.byteBuf=byteBuf; 46 | this.qoS=qoS; 47 | this.tenantId=tenantId; 48 | } 49 | 50 | @Override 51 | public String getSource() { 52 | return (String) source; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/publish/MqttPublishTopicEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.publish; 18 | 19 | import io.netty.handler.codec.mqtt.MqttQoS; 20 | import link.ttiot.common.ioc.annotation.Eventor; 21 | import link.ttiot.common.ioc.core.ApplicationEvent; 22 | import lombok.Getter; 23 | 24 | 25 | /** 26 | * @author: shijun 27 | * @date: 2019-04-15 28 | * @description: 29 | */ 30 | @Eventor 31 | public class MqttPublishTopicEvent extends ApplicationEvent { 32 | 33 | @Getter 34 | private MqttQoS mqttQoS; 35 | 36 | @Getter 37 | private byte[] bytes; 38 | 39 | @Getter 40 | private String tenantId; 41 | 42 | @Getter 43 | private boolean retain; 44 | 45 | 46 | public MqttPublishTopicEvent(String topic,MqttQoS mqttQoS,byte[] bytes,String tenantId,boolean isRetain) { 47 | super(topic); 48 | this.mqttQoS=mqttQoS; 49 | this.bytes=bytes; 50 | this.tenantId=tenantId; 51 | this.retain=isRetain; 52 | } 53 | 54 | @Override 55 | public String getSource() { 56 | return (String) source; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/eventor/connect/MqttConnectBackEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.eventor.connect; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | import io.netty.handler.codec.mqtt.MqttConnectMessage; 21 | import io.netty.handler.codec.mqtt.MqttConnectReturnCode; 22 | import io.netty.handler.codec.mqtt.MqttMessage; 23 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationEvent; 24 | import link.ttiot.common.ioc.annotation.Eventor; 25 | import lombok.Getter; 26 | 27 | 28 | /** 29 | * @author: shijun 30 | * @date: 2019-04-15 31 | * @description: 32 | */ 33 | @Eventor(auth = false) 34 | public class MqttConnectBackEvent extends MqttApplicationEvent { 35 | 36 | @Getter 37 | private MqttConnectReturnCode mqttConnectReturnCode; 38 | 39 | public MqttConnectBackEvent(MqttMessage msg,ChannelHandlerContext context,MqttConnectReturnCode connectReturnCode) { 40 | super(msg, context); 41 | this.mqttConnectReturnCode=connectReturnCode; 42 | } 43 | 44 | @Override 45 | public MqttConnectMessage getSource() { 46 | return (MqttConnectMessage)source; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/decode/MqttDecoderSucceedDefaultListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.decode; 18 | 19 | import link.ttiot.broker.eventor.decode.MqttDecoderSuccessEvent; 20 | import link.ttiot.common.context.Context; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 22 | import link.ttiot.common.ioc.annotation.DefaultListener; 23 | import lombok.extern.slf4j.Slf4j; 24 | 25 | /** 26 | * @author: shijun 27 | * @date: 2019-04-12 28 | * @description: 29 | */ 30 | @Slf4j 31 | @DefaultListener 32 | public class MqttDecoderSucceedDefaultListener extends MqttApplicationListener { 33 | 34 | public MqttDecoderSucceedDefaultListener() { 35 | super(); 36 | } 37 | 38 | @Override 39 | public void onApplicationEvent(MqttDecoderSuccessEvent successEvent) { 40 | String type=successEvent.getSource().fixedHeader().messageType().toString(); 41 | log.info("The event type is {}",type); 42 | publishEvent(Context.me().mqttEventorProduce(type,successEvent.getChannelHandlerContext(),successEvent.getSource())); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /ttiot-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | ttiot 7 | link.ttiot 8 | 2.0.0 9 | 10 | 4.0.0 11 | ttiot-example 12 | jar 13 | 14 | 15 | link.ttiot 16 | ttiot-broker 17 | 18 | 19 | 20 | 21 | ${project.artifactId} 22 | 23 | 24 | org.apache.maven.plugins 25 | maven-shade-plugin 26 | 2.4.1 27 | 28 | 29 | package 30 | 31 | shade 32 | 33 | 34 | 35 | 36 | link.ttiot.example.server.ExampleLauncher 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | ttiot-common 7 | link.ttiot 8 | 2.0.0 9 | 10 | 4.0.0 11 | jar 12 | ttiot-common-core 13 | 14 | 15 | org.apache.commons 16 | commons-pool2 17 | 18 | 19 | cn.hutool 20 | hutool-all 21 | 22 | 23 | org.projectlombok 24 | lombok 25 | 26 | 27 | ch.qos.logback 28 | logback-classic 29 | 30 | 31 | org.slf4j 32 | slf4j-api 33 | 34 | 35 | junit 36 | junit 37 | 38 | 39 | io.netty 40 | netty-all 41 | 42 | 43 | 44 | 45 | 46 | org.apache.maven.plugins 47 | maven-compiler-plugin 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/vo/HttpRet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.vo; 18 | 19 | import cn.hutool.json.JSONUtil; 20 | import lombok.AllArgsConstructor; 21 | import lombok.Data; 22 | 23 | /** 24 | * @author: shijun 25 | * @date: 2020-03-17 26 | * @description: 27 | */ 28 | @AllArgsConstructor 29 | @Data 30 | public class HttpRet { 31 | 32 | private static int ERROR=500; 33 | private static int SUCCESS=0; 34 | private static String SUCCESS_DF_MSG="success"; 35 | private static String ERROR_MSG_NOTFUND="404 not fund"; 36 | 37 | private Integer code; 38 | 39 | private Object data; 40 | 41 | private String msg; 42 | 43 | public static HttpRet error(String msg){ 44 | return new HttpRet(ERROR,null,msg); 45 | } 46 | 47 | public static HttpRet errorForNotFund(){ 48 | return new HttpRet(ERROR,null,ERROR_MSG_NOTFUND); 49 | } 50 | 51 | public boolean isSuccess(){ 52 | return this.code==0; 53 | } 54 | 55 | public static HttpRet success(Object data){ 56 | return new HttpRet(SUCCESS,data,SUCCESS_DF_MSG); 57 | } 58 | 59 | public String toJson(){ 60 | return JSONUtil.toJsonPrettyStr(this); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/ExceptionHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler; 18 | 19 | 20 | import link.ttiot.broker.eventor.lwt.MqttLwtEvent; 21 | import link.ttiot.common.context.Context; 22 | import link.ttiot.common.context.exception.ExceptionHandlerAdapter; 23 | import link.ttiot.common.core.channel.ChannelUtils; 24 | import link.ttiot.common.core.exception.ConnectException; 25 | import lombok.extern.slf4j.Slf4j; 26 | 27 | /** 28 | * @author: shijun 29 | * @date: 2019-04-24 30 | * @description: 31 | */ 32 | @Slf4j 33 | public class ExceptionHandler extends ExceptionHandlerAdapter { 34 | 35 | /** 36 | * 连接中异常 37 | * @param t 38 | * @param e 39 | */ 40 | @Override 41 | public void uncaughtException(Thread t, Throwable e) { 42 | if (e instanceof ConnectException){ 43 | log.error("connect error the reason is {}",e.getMessage()); 44 | String clientId=ChannelUtils.clientId(((ConnectException) e).getChannel()); 45 | if (clientId!=null){ 46 | Context.me().publishEvent(new MqttLwtEvent(clientId)); 47 | } 48 | ((ConnectException) e).getChannel().close(); 49 | } 50 | e.printStackTrace(); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-config/src/main/java/link/ttiot/common/config/ConfigurationFileReader.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.config; 18 | 19 | import cn.hutool.core.io.resource.ClassPathResource; 20 | import link.ttiot.common.core.constant.CommonConstant; 21 | import lombok.Cleanup; 22 | import lombok.NonNull; 23 | import lombok.SneakyThrows; 24 | import lombok.extern.slf4j.Slf4j; 25 | import org.yaml.snakeyaml.Yaml; 26 | 27 | import java.io.File; 28 | import java.io.FileInputStream; 29 | import java.io.FileNotFoundException; 30 | import java.io.IOException; 31 | 32 | /** 33 | * @author: shijun 34 | * @date: 2019-04-11 35 | * @description: 36 | */ 37 | @Slf4j 38 | public class ConfigurationFileReader { 39 | 40 | 41 | @SneakyThrows({FileNotFoundException.class,IOException.class}) 42 | public static T loaderYml(@NonNull File configFileSrc, Class c){ 43 | @Cleanup FileInputStream in = null; 44 | Yaml yaml = new Yaml(); 45 | in = new FileInputStream(configFileSrc); 46 | return yaml.loadAs(in, c); 47 | } 48 | 49 | @SneakyThrows 50 | public static T loaderBaseYml(Class c) { 51 | ClassPathResource resource = new ClassPathResource(CommonConstant.BASE_CONTEXT_CONFIG_FILE); 52 | return loaderYml(resource.getFile(), c); 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/ping/MqttPingListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.ping; 18 | 19 | import link.ttiot.broker.eventor.ping.MqttPingEvent; 20 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 21 | import link.ttiot.common.context.service.SubscribeService; 22 | import link.ttiot.common.core.function.FunctionApi; 23 | import link.ttiot.common.core.provider.message.MessageProvider; 24 | import link.ttiot.common.ioc.annotation.DefaultListener; 25 | import link.ttiot.common.ioc.annotation.Inject; 26 | import lombok.extern.slf4j.Slf4j; 27 | 28 | 29 | /** 30 | * @author: shijun 31 | * @date: 2019-04-12 32 | * @description: 33 | */ 34 | @Slf4j 35 | @DefaultListener 36 | public class MqttPingListener extends MqttApplicationListener implements FunctionApi { 37 | 38 | @Inject 39 | private SubscribeService subscribeService; 40 | 41 | public MqttPingListener() { 42 | super(); 43 | } 44 | 45 | @Override 46 | public void onApplicationEvent(MqttPingEvent mqttPingEvent) { 47 | 48 | log.info("receive:【{}】 heartbeat ",mqttPingEvent.getChannel().remoteAddress().toString()); 49 | 50 | predicateDo(mqttPingEvent.getChannel(), u -> u.isWritable(), u -> u.writeAndFlush(MessageProvider.pingMessageProduce())); 51 | 52 | } 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/constant/CommonConstant.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.constant; 18 | 19 | /** 20 | * @author: shijun 21 | * @date: 2019-04-11 22 | * @description: 23 | */ 24 | public interface CommonConstant { 25 | String BASE_CONTEXT_CONFIG_FILE = "TTIotBootstrap.yml"; 26 | String BASE_VERSION = "2.0.0"; 27 | String BASE_BANNER_CONFIG_FILE = "banner.txt"; 28 | String MQTT_DECODER="mqttDecoder"; 29 | String MQTT_ENCODE="mqttEncode"; 30 | String PROTOCOL_ADAPTIVE_HANDLER="protocolAdaptiveHandler"; 31 | String MQTT_HANDLER="mqttHander"; 32 | String MQTT_WEBSOCKET_CODEC="mqttWebSocketCodec"; 33 | String HTTP_CHUNKED_WRITER="httpChunkedWriter"; 34 | String HTTP_CODER="httpCoder"; 35 | String HTTP_COMPRESSOR="compressor"; 36 | String HTTP_AGGREGATOR="httpAggregator"; 37 | String HTTP_REQUEST_HANDLER="httpRequestHandler"; 38 | String MQTT_WEBSOCKET_SUBPROTOCOLS="server, mqttv3.1, mqttv3.1.1"; 39 | String MQTT_WEBSOCKET_PROTOCOL="protocol"; 40 | String HEARTBEAT_HANDLER="heartbeatHander"; 41 | String FIELD_DEFAULT="isDefault"; 42 | String FIELD_ASYNCHRONOUS="isAsynchronous"; 43 | String EVENTLOOP_KEY="io.netty.eventLoopThreads"; 44 | String WEBSOCKET_PATH="/mqtt"; 45 | int HTTP_MAXCONTENT_LENGTH=1048576000; 46 | int MAX_PAYLOAD_SIZE=6553600; 47 | 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/decode/MqttDecodeErrorDefaultListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.decode; 18 | 19 | import io.netty.handler.codec.mqtt.MqttUnacceptableProtocolVersionException; 20 | import link.ttiot.broker.eventor.connect.MqttIdentifierRejectedExceptionEvent; 21 | import link.ttiot.broker.eventor.connect.MqttUnacceptableProtocolVersionExceptionEvent; 22 | import link.ttiot.broker.eventor.decode.MqttDecoderFailEvent; 23 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 24 | import link.ttiot.common.ioc.annotation.DefaultListener; 25 | import lombok.extern.slf4j.Slf4j; 26 | 27 | /** 28 | * @author: shijun 29 | * @date: 2019-04-15 30 | * @description: 31 | */ 32 | @Slf4j 33 | @DefaultListener 34 | public class MqttDecodeErrorDefaultListener extends MqttApplicationListener { 35 | 36 | public MqttDecodeErrorDefaultListener() { 37 | super(); 38 | } 39 | 40 | @Override 41 | public void onApplicationEvent(MqttDecoderFailEvent failEvent) { 42 | publishEvent(failEvent.getSource().decoderResult().cause() instanceof MqttUnacceptableProtocolVersionException ? new MqttUnacceptableProtocolVersionExceptionEvent(failEvent.getSource(),failEvent.getChannelHandlerContext()):new MqttIdentifierRejectedExceptionEvent(failEvent.getSource(),failEvent.getChannelHandlerContext())); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/ssl/SslBuilder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.ssl; 18 | 19 | import lombok.extern.slf4j.Slf4j; 20 | 21 | import javax.net.ssl.KeyManagerFactory; 22 | import javax.net.ssl.SSLContext; 23 | import java.io.FileInputStream; 24 | import java.io.InputStream; 25 | import java.security.KeyStore; 26 | 27 | /** 28 | * @author: shijun 29 | * @date: 2020-05-19 30 | * @description: 31 | */ 32 | @Slf4j 33 | public class SslBuilder { 34 | 35 | private static String sslContextType="ssl"; 36 | 37 | public SSLContext createSslContext(String KeyStoreType,String path,String sslPassword) { 38 | try { 39 | //JKS \PKCS12-.pfx 40 | KeyStore ks=KeyStore.getInstance(KeyStoreType); 41 | // path 42 | InputStream ksInputStream=new FileInputStream(path); 43 | ks.load(ksInputStream,sslPassword.toCharArray()); 44 | KeyManagerFactory kmf=KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 45 | kmf.init(ks,sslPassword.toCharArray()); 46 | SSLContext sslContext=SSLContext.getInstance(sslContextType); 47 | sslContext.init(kmf.getKeyManagers(),null,null); 48 | return sslContext; 49 | } catch (Exception e) { 50 | log.error(e.getMessage(),e); 51 | return null; 52 | } 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/publish/ack/MqttPubAckListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.publish.ack; 18 | 19 | import io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader; 20 | import link.ttiot.broker.eventor.publish.ack.MqttPubAckEvent; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 22 | import link.ttiot.common.context.service.MessageStoreService; 23 | import link.ttiot.common.core.function.FunctionApi; 24 | import link.ttiot.common.ioc.annotation.DefaultListener; 25 | import link.ttiot.common.ioc.annotation.Inject; 26 | import lombok.extern.slf4j.Slf4j; 27 | 28 | 29 | /** 30 | * @author: shijun 31 | * @date: 2019-04-12 32 | * @description: 33 | */ 34 | @Slf4j 35 | @DefaultListener 36 | public class MqttPubAckListener extends MqttApplicationListener implements FunctionApi { 37 | 38 | @Inject 39 | private MessageStoreService messageStoreService; 40 | 41 | public MqttPubAckListener() { 42 | super(); 43 | } 44 | 45 | @Override 46 | public void onApplicationEvent(MqttPubAckEvent mqttPubAckEvent) { 47 | 48 | MqttMessageIdVariableHeader mqttMessageIdVariableHeader = (MqttMessageIdVariableHeader)mqttPubAckEvent.getSource().variableHeader(); 49 | 50 | messageStoreService.delete(mqttMessageIdVariableHeader.messageId(),mqttPubAckEvent.getClientId()); 51 | 52 | } 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/publish/comp/MqttPubCompListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.publish.comp; 18 | 19 | import io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader; 20 | import link.ttiot.broker.eventor.publish.comp.MqttPubCompEvent; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 22 | import link.ttiot.common.context.service.MessageStoreService; 23 | import link.ttiot.common.core.function.FunctionApi; 24 | import link.ttiot.common.ioc.annotation.DefaultListener; 25 | import link.ttiot.common.ioc.annotation.Inject; 26 | import lombok.extern.slf4j.Slf4j; 27 | 28 | 29 | /** 30 | * @author: shijun 31 | * @date: 2019-04-12 32 | * @description: 33 | */ 34 | @Slf4j 35 | @DefaultListener 36 | public class MqttPubCompListener extends MqttApplicationListener implements FunctionApi { 37 | 38 | @Inject 39 | private MessageStoreService messageStoreService; 40 | 41 | public MqttPubCompListener() { 42 | super(); 43 | } 44 | 45 | @Override 46 | public void onApplicationEvent(MqttPubCompEvent mqttPubCompEvent) { 47 | 48 | MqttMessageIdVariableHeader mqttMessageIdVariableHeader = (MqttMessageIdVariableHeader)mqttPubCompEvent.getSource().variableHeader(); 49 | 50 | messageStoreService.delete(mqttMessageIdVariableHeader.messageId(),mqttPubCompEvent.getClientId()); 51 | 52 | } 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/http/MqttHttpHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.http; 18 | 19 | import cn.hutool.json.JSONUtil; 20 | import io.netty.handler.codec.mqtt.MqttQoS; 21 | import link.ttiot.broker.eventor.publish.MqttPublishTopicEvent; 22 | import link.ttiot.common.context.Context; 23 | import link.ttiot.common.ioc.annotation.Http; 24 | import link.ttiot.common.ioc.core.HttpHandler; 25 | import link.ttiot.common.ioc.vo.HttpRequest; 26 | import link.ttiot.common.ioc.vo.HttpRet; 27 | 28 | /** 29 | * @author: shijun 30 | * @date: 2020-03-26 31 | * @description: 32 | */ 33 | @Http(uri = "/mqtt") 34 | public class MqttHttpHandler implements HttpHandler { 35 | 36 | private static String INVALID_MQTTQOS = "mqttqos must be >0 and <1"; 37 | 38 | @Override 39 | public HttpRet handler(HttpRequest param) { 40 | HttpMqttRequestVo vo = JSONUtil.toBean(param.getBody(), HttpMqttRequestVo.class); 41 | if (MqttQoS.AT_MOST_ONCE.value() <= vo.getMqttQos() && vo.getMqttQos() <= MqttQoS.AT_LEAST_ONCE.value()) { 42 | byte[] bytes = JSONUtil.toJsonPrettyStr(vo.getPayload()).getBytes(); 43 | Context.me().publishEvent(new MqttPublishTopicEvent(vo.getTopic(), MqttQoS.valueOf(vo.getMqttQos()), bytes, 44 | param.getTenantId(), vo.getRetain())); 45 | return HttpRet.success(null); 46 | } else { 47 | return HttpRet.error(INVALID_MQTTQOS); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/security/IdGenerator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.security; 18 | 19 | import cn.hutool.core.lang.ObjectId; 20 | import link.ttiot.common.core.constant.ProtocalConstant; 21 | import link.ttiot.common.core.function.FunctionApi; 22 | 23 | import java.util.Optional; 24 | 25 | /** 26 | * @author: shijun 27 | * @date: 2019-04-22 28 | * @description: 29 | */ 30 | public class IdGenerator implements FunctionApi, ProtocalConstant { 31 | 32 | private static IdGenerator ttIotIdGenerator; 33 | 34 | private IdGenerator() { 35 | } 36 | 37 | public static IdGenerator me() { 38 | if (ttIotIdGenerator == null) { 39 | ttIotIdGenerator = new IdGenerator(); 40 | } 41 | return ttIotIdGenerator; 42 | } 43 | 44 | public String productClientId(String tenantId, String devName) { 45 | return predicateThrowException(tenantId + SEPARATOR + devName, u -> u.length() < CLIENT_IDENTIFY_LENGTH, new RuntimeException()); 46 | 47 | } 48 | 49 | public String productTenantId( ) { 50 | return ObjectId.next(); 51 | } 52 | 53 | public String productDevNameId(String devName) { 54 | return Optional.ofNullable(devName).orElseGet(() -> ObjectId.next().substring(15,20)); 55 | } 56 | 57 | public String productDevPasswordId(String devPassword) { 58 | return Optional.ofNullable(devPassword).orElseGet(() -> ObjectId.next().substring(15,20)); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/protocal/ProtocolApplicationEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.protocal; 18 | 19 | import io.netty.channel.Channel; 20 | import io.netty.channel.ChannelHandlerContext; 21 | import link.ttiot.common.core.channel.ChannelUtils; 22 | import link.ttiot.common.core.security.ClientIdReader; 23 | import link.ttiot.common.ioc.core.ApplicationEvent; 24 | import lombok.Getter; 25 | import lombok.Setter; 26 | 27 | import java.util.Optional; 28 | 29 | /** 30 | * @author: shijun 31 | * @date: 2019-04-15 32 | * @description: 33 | */ 34 | public abstract class ProtocolApplicationEvent extends ApplicationEvent { 35 | 36 | @Getter 37 | public ChannelHandlerContext channelHandlerContext; 38 | @Getter 39 | @Setter 40 | private Channel channel; 41 | 42 | public ProtocolApplicationEvent(Object msg, ChannelHandlerContext context) { 43 | super(msg); 44 | this.channel = context.channel(); 45 | channelHandlerContext = context; 46 | } 47 | 48 | public String getClientId() { 49 | return Optional.ofNullable(channel).map(u -> ChannelUtils.clientId(u)).orElse(null); 50 | } 51 | 52 | public String getTenantId() { 53 | return Optional.ofNullable(getClientId()).map(u -> ClientIdReader.tenantId(u)).orElse(null); 54 | } 55 | 56 | public String getDevName() { 57 | return Optional.ofNullable(getClientId()).map(u -> ClientIdReader.devName(u)).orElse(null); 58 | } 59 | 60 | 61 | } 62 | -------------------------------------------------------------------------------- /ttiot-broker/src/test/java/IdGenerateTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | import cn.hutool.core.lang.Console; 18 | import link.ttiot.broker.db.redis.RedisDbHelper; 19 | import link.ttiot.common.context.db.DbHelper; 20 | import link.ttiot.common.context.entity.Device; 21 | import link.ttiot.common.context.entity.Tenant; 22 | import link.ttiot.common.core.security.IdGenerator; 23 | import link.ttiot.common.redis.RedisSourceProvider; 24 | import org.junit.Before; 25 | import org.junit.Test; 26 | 27 | 28 | /** 29 | * @author: shijun 30 | * @date: 2019-04-23 31 | * @description: 32 | */ 33 | public class IdGenerateTest { 34 | 35 | DbHelper dbHelper; 36 | 37 | private static String tenantId="TT"; 38 | 39 | /** 40 | * 数据源 41 | */ 42 | @Before 43 | public void data() { 44 | dbHelper = new RedisDbHelper(new RedisSourceProvider()); 45 | } 46 | 47 | @Test 48 | public void tenant(){ 49 | Tenant tenant = new Tenant(tenantId, "shijun'tt"); 50 | dbHelper.saveTenant(tenant); 51 | } 52 | 53 | @Test 54 | public void dev() { 55 | String devName= IdGenerator.me().productDevNameId(null); 56 | String passwordId=IdGenerator.me().productDevPasswordId(null); 57 | String clientId=IdGenerator.me().productClientId(tenantId,devName); 58 | Console.print("devname:"+devName); 59 | Console.print("devpassword:"+passwordId); 60 | Console.print("clientId:"+clientId); 61 | Device dev2 = new Device(devName, passwordId,tenantId ); 62 | dbHelper.saveDev(dev2); 63 | } 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /ttiot-broker/src/test/java/DbTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | import link.ttiot.broker.db.redis.RedisDbHelper; 18 | import link.ttiot.common.context.db.DbHelper; 19 | import link.ttiot.common.context.entity.Device; 20 | import link.ttiot.common.context.entity.Tenant; 21 | import link.ttiot.common.redis.RedisSourceProvider; 22 | import org.junit.Before; 23 | import org.junit.Test; 24 | 25 | import java.util.Map; 26 | 27 | /** 28 | * @author: shijun 29 | * @date: 2019-04-19 30 | * @description: 31 | */ 32 | public class DbTest { 33 | 34 | DbHelper dbHelper; 35 | Tenant tenant; 36 | Device dev; 37 | 38 | @Before 39 | public void before() { 40 | /** 41 | * 数据源插件 42 | */ 43 | dbHelper = new RedisDbHelper(new RedisSourceProvider()); 44 | tenant = new Tenant("TTIot", "TTIot"); 45 | dev = new Device("TTIot_dev", "password", "TTIot"); 46 | } 47 | 48 | @Test 49 | public void save() { 50 | /** 51 | * 保存租户 52 | */ 53 | Boolean saveTenant1 = dbHelper.saveTenant(tenant); 54 | Tenant getTenant3 = dbHelper.getTenant("TTIot"); 55 | Map map = dbHelper.listTenant(); 56 | /** 57 | * 保存设备 58 | */ 59 | Boolean saveDev1 = dbHelper.saveDev(dev); 60 | Map listDev = dbHelper.listDevByTenantAndState("TTIot"); 61 | 62 | } 63 | 64 | @Test 65 | public void delete() { 66 | dbHelper.deleteTenant("TTIot"); 67 | Boolean deleteTenant = dbHelper.deleteTenant("TTIot"); 68 | } 69 | 70 | 71 | 72 | } 73 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/http/HttpRequestHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.http; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import io.netty.buffer.Unpooled; 21 | import io.netty.channel.ChannelFutureListener; 22 | import io.netty.channel.ChannelHandler; 23 | import io.netty.channel.ChannelHandlerContext; 24 | import io.netty.channel.ChannelInboundHandlerAdapter; 25 | import io.netty.handler.codec.http.*; 26 | import io.netty.util.CharsetUtil; 27 | import link.ttiot.broker.eventor.decode.MqttDecoderFailEvent; 28 | import link.ttiot.broker.eventor.decode.MqttDecoderSuccessEvent; 29 | import link.ttiot.broker.eventor.http.HttpRequestEvent; 30 | import link.ttiot.common.context.Context; 31 | import link.ttiot.common.context.service.DeviceService; 32 | import link.ttiot.common.core.constant.CommonConstant; 33 | import link.ttiot.common.core.function.FunctionApi; 34 | import link.ttiot.common.ioc.annotation.DefaultListener; 35 | import link.ttiot.common.ioc.annotation.Inject; 36 | import lombok.extern.slf4j.Slf4j; 37 | 38 | 39 | /** 40 | * @author: shijun 41 | * @date: 2020-02-28 42 | * @description: 43 | */ 44 | @ChannelHandler.Sharable 45 | @Slf4j 46 | public class HttpRequestHandler extends ChannelInboundHandlerAdapter implements FunctionApi { 47 | 48 | @Override 49 | public void channelRead(ChannelHandlerContext ctx, Object msg) { 50 | 51 | predicateDoElse(msg,u->!(u instanceof FullHttpRequest),u->ctx.close(),u->{ 52 | Context.me().publishEvent(new HttpRequestEvent(u,ctx)); 53 | }); 54 | 55 | } 56 | 57 | 58 | 59 | } -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/subscribe/MqttUnSubscribeListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.subscribe; 18 | 19 | 20 | import link.ttiot.broker.eventor.ack.MqttAckEvent; 21 | import link.ttiot.broker.eventor.subscribe.MqttUnSubscribeEvent; 22 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 23 | import link.ttiot.common.context.service.SubscribeService; 24 | import link.ttiot.common.core.function.FunctionApi; 25 | import link.ttiot.common.ioc.annotation.DefaultListener; 26 | import link.ttiot.common.ioc.annotation.Inject; 27 | import lombok.extern.slf4j.Slf4j; 28 | 29 | 30 | /** 31 | * @author: shijun 32 | * @date: 2019-04-12 33 | * @description: 34 | */ 35 | @Slf4j 36 | @DefaultListener 37 | public class MqttUnSubscribeListener extends MqttApplicationListener implements FunctionApi { 38 | 39 | @Inject 40 | private SubscribeService subscribeService; 41 | 42 | public MqttUnSubscribeListener() { 43 | super(); 44 | } 45 | 46 | @Override 47 | public void onApplicationEvent(MqttUnSubscribeEvent mqttUnSubscribeEvent) { 48 | 49 | mqttUnSubscribeEvent.getSource().payload().topics().forEach(u -> { 50 | 51 | log.info("The topic【{}】was dropped", u); 52 | 53 | subscribeService.delete(mqttUnSubscribeEvent.getTenantId(),u,mqttUnSubscribeEvent.getDevName()); 54 | 55 | publishEvent(new MqttAckEvent(mqttUnSubscribeEvent.getSource(),mqttUnSubscribeEvent.getChannelHandlerContext())); 56 | 57 | }); 58 | } 59 | 60 | 61 | } 62 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/function/FunctionApi.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.function; 18 | 19 | import java.util.function.Consumer; 20 | import java.util.function.Predicate; 21 | 22 | /** 23 | * @author: shijun 24 | * @date: 2019-04-22 25 | * @description: 26 | */ 27 | public interface FunctionApi { 28 | 29 | 30 | default void predicateDoElse(T t, Predicate predicate, Consumer consumer, Consumer consumer2) { 31 | if (t != null) { 32 | if (predicate.test(t)) { 33 | consumer.accept(t); 34 | } else { 35 | consumer2.accept(t); 36 | } 37 | } 38 | } 39 | 40 | default void predicateDo(T t, Predicate predicate, Consumer consumer) { 41 | if (t != null) { 42 | if (predicate.test(t)) { 43 | consumer.accept(t); 44 | } 45 | } 46 | } 47 | 48 | default T predicateThrowException(T t, Predicate predicate,RuntimeException e){ 49 | if (t != null) { 50 | if (!predicate.test(t)) { 51 | throw e; 52 | }else{ 53 | return t; 54 | } 55 | } 56 | return null; 57 | } 58 | 59 | 60 | default void predicateDoElseThrowException(T t, Predicate predicate, Consumer consumer,RuntimeException e){ 61 | if (t != null) { 62 | if (!predicate.test(t)) { 63 | throw e; 64 | }else{ 65 | consumer.accept(t); 66 | } 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/publish/rel/MqttPubRelListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.publish.rel; 18 | 19 | import io.netty.handler.codec.mqtt.*; 20 | import link.ttiot.broker.eventor.ack.MqttAckEvent; 21 | import link.ttiot.broker.eventor.publish.rel.MqttPubRelEvent; 22 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 23 | import link.ttiot.common.core.channel.ChannelUtils; 24 | import link.ttiot.common.core.function.FunctionApi; 25 | import link.ttiot.common.core.provider.message.MessageProvider; 26 | import link.ttiot.common.ioc.annotation.DefaultListener; 27 | import lombok.extern.slf4j.Slf4j; 28 | 29 | 30 | /** 31 | * @author: shijun 32 | * @date: 2019-04-12 33 | * @description: 34 | */ 35 | @Slf4j 36 | @DefaultListener 37 | public class MqttPubRelListener extends MqttApplicationListener implements FunctionApi { 38 | 39 | public MqttPubRelListener() { 40 | super(); 41 | } 42 | 43 | @Override 44 | public void onApplicationEvent(MqttPubRelEvent mqttPubRelEvent) { 45 | 46 | MqttMessageIdVariableHeader mqttMessageIdVariableHeader = (MqttMessageIdVariableHeader)mqttPubRelEvent.getSource().variableHeader(); 47 | //移除packetId 48 | ChannelUtils.removeReceive(mqttPubRelEvent.getChannel(), mqttMessageIdVariableHeader.messageId()); 49 | MqttMessage compMessage = MessageProvider.pubAckProduce(MqttMessageType.PUBCOMP , mqttMessageIdVariableHeader.messageId(),0); 50 | publishEvent(new MqttAckEvent(compMessage, mqttPubRelEvent.getChannelHandlerContext())); 51 | } 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /ttiot-client-java/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | ttiot 7 | link.ttiot 8 | 2.0.0 9 | 10 | 4.0.0 11 | ttiot-client-java 12 | jar 13 | Client 14 | 15 | 16 | 17 | org.apache.maven.plugins 18 | maven-compiler-plugin 19 | 20 | 8 21 | 8 22 | 23 | 24 | 25 | 26 | 27 | 28 | com.google.code.gson 29 | gson 30 | 31 | 32 | 33 | link.ttiot 34 | ttiot-common-core 35 | 36 | 37 | link.ttiot 38 | ttiot-common-config 39 | 40 | 41 | link.ttiot 42 | ttiot-common-ioc 43 | 44 | 45 | link.ttiot 46 | ttiot-common-redis 47 | 48 | 49 | link.ttiot 50 | ttiot-common-context 51 | 52 | 53 | junit 54 | junit 55 | 56 | 57 | link.ttiot 58 | ttiot-common-context 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/service/SubscribeService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.service; 18 | 19 | import link.ttiot.common.context.db.DbHelper; 20 | import link.ttiot.common.context.entity.Subscribe; 21 | import link.ttiot.common.core.security.ClientIdReader; 22 | import link.ttiot.common.ioc.annotation.Inject; 23 | import lombok.Getter; 24 | 25 | import java.util.List; 26 | 27 | /** 28 | * @author: shijun 29 | * @date: 2019-04-28 30 | * @description: 31 | */ 32 | public class SubscribeService { 33 | 34 | @Getter 35 | @Inject 36 | private DbHelper dbHelper; 37 | 38 | public void put(String tenantId, Subscribe subscribe) { 39 | dbHelper.saveSubscribe(tenantId, subscribe); 40 | dbHelper.saveDevTopic(tenantId, subscribe); 41 | } 42 | 43 | public List search(String tenantId, String topic) { 44 | return dbHelper.searchTopic(tenantId, topic); 45 | } 46 | 47 | 48 | public void delete(String tenantId, String topic, String devName) { 49 | dbHelper.deleteSubscribe(tenantId, topic, devName); 50 | dbHelper.deleteDevTopic(tenantId, topic, devName); 51 | } 52 | 53 | 54 | public void delete(String clientId) { 55 | //先删除topic里面的dev关系 56 | dbHelper.listSubscribeByDev(ClientIdReader.tenantId(clientId), ClientIdReader.devName(clientId)).stream().forEach(u -> { 57 | dbHelper.deleteSubscribe(ClientIdReader.tenantId(clientId), u, ClientIdReader.devName(clientId)); 58 | }); 59 | //再删除dev里面的topic关系 60 | dbHelper.deleteDevTopicByClientId(clientId); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/http/RequestLine.java: -------------------------------------------------------------------------------- 1 | package link.ttiot.broker.handler.http; 2 | 3 | /** 4 | * @author wchao 5 | * 2017年6月28日 下午2:20:32 6 | */ 7 | public class RequestLine { 8 | private Method method; 9 | private String path; 10 | private String initPath; 11 | private String query; 12 | private String pathAndQuery; 13 | private String protocol; 14 | private String version; 15 | private String line; 16 | 17 | /** 18 | * @return the line 19 | */ 20 | public String getLine() { 21 | return line; 22 | } 23 | 24 | /** 25 | * @return the method 26 | */ 27 | public Method getMethod() { 28 | return method; 29 | } 30 | 31 | /** 32 | * @return the path 33 | */ 34 | public String getPath() { 35 | return path; 36 | } 37 | 38 | public String getPathAndQuery() { 39 | return pathAndQuery; 40 | } 41 | 42 | /** 43 | * @return the query 44 | */ 45 | public String getQuery() { 46 | return query; 47 | } 48 | 49 | /** 50 | * @return the version 51 | */ 52 | public String getVersion() { 53 | return version; 54 | } 55 | 56 | /** 57 | * @param line the line to set 58 | */ 59 | public void setLine(String line) { 60 | this.line = line; 61 | } 62 | 63 | /** 64 | * @param method the method to set 65 | */ 66 | public void setMethod(Method method) { 67 | this.method = method; 68 | } 69 | 70 | /** 71 | * @param path the path to set 72 | */ 73 | public void setPath(String path) { 74 | this.path = path; 75 | } 76 | 77 | public void setPathAndQuery(String pathAndQuery) { 78 | this.pathAndQuery = pathAndQuery; 79 | } 80 | 81 | /** 82 | * @param query the query to set 83 | */ 84 | public void setQuery(String query) { 85 | this.query = query; 86 | } 87 | 88 | /** 89 | * @param version the version to set 90 | */ 91 | public void setVersion(String version) { 92 | this.version = version; 93 | } 94 | 95 | /** 96 | * @return the protocol 97 | */ 98 | public String getProtocol() { 99 | return protocol; 100 | } 101 | 102 | /** 103 | * @param protocol the protocol to set 104 | */ 105 | public void setProtocol(String protocol) { 106 | this.protocol = protocol; 107 | } 108 | 109 | public String getInitPath() { 110 | return initPath; 111 | } 112 | 113 | public void setInitPath(String initPath) { 114 | this.initPath = initPath; 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/channel/ChannelUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.channel; 18 | 19 | import io.netty.channel.Channel; 20 | import io.netty.util.Attribute; 21 | import io.netty.util.AttributeKey; 22 | import link.ttiot.common.core.constant.ProtocalConstant; 23 | import lombok.experimental.UtilityClass; 24 | 25 | import java.util.Set; 26 | 27 | 28 | /** 29 | * @author: shijun 30 | * @date: 2019-04-26 31 | * @description: 32 | */ 33 | @UtilityClass 34 | public class ChannelUtils { 35 | 36 | public Boolean login() { 37 | return false; 38 | } 39 | 40 | public Attribute getAttributeKey(Channel channel, AttributeKey attributeKey) { 41 | return channel.attr(attributeKey); 42 | } 43 | 44 | public boolean ifLogin(Channel channel) { 45 | //活跃状态并且通过认证 46 | return channel != null && channel.isActive() && getAttributeKey(channel, ProtocalConstant.LOGIN).get(); 47 | } 48 | 49 | public String clientId(Channel channel) { 50 | return getAttributeKey(channel, ProtocalConstant.CLIENTID).get(); 51 | } 52 | 53 | 54 | public void addReceive(Channel channel, int receiveId) { 55 | Set set = getAttributeKey(channel, ProtocalConstant.RECEIVE).get(); 56 | set.add(receiveId); 57 | } 58 | 59 | public void removeReceive(Channel channel, int receiveId) { 60 | Set set = getAttributeKey(channel, ProtocalConstant.RECEIVE).get(); 61 | set.remove(receiveId); 62 | } 63 | 64 | public boolean containReceive(Channel channel, int receiveId) { 65 | Set set = getAttributeKey(channel, ProtocalConstant.RECEIVE).get(); 66 | return set.contains(receiveId); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /ttiot-broker/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | ttiot 7 | link.ttiot 8 | 2.0.0 9 | 10 | 4.0.0 11 | ttiot-broker 12 | jar 13 | Broker 14 | 15 | 16 | 17 | org.apache.maven.plugins 18 | maven-compiler-plugin 19 | 20 | 8 21 | 8 22 | 23 | 24 | 25 | 26 | 27 | 28 | com.google.code.gson 29 | gson 30 | 31 | 32 | 33 | link.ttiot 34 | ttiot-common-core 35 | 36 | 37 | link.ttiot 38 | ttiot-common-config 39 | 40 | 41 | link.ttiot 42 | ttiot-common-ioc 43 | 44 | 45 | link.ttiot 46 | ttiot-common-redis 47 | 48 | 49 | link.ttiot 50 | ttiot-common-context 51 | 52 | 53 | junit 54 | junit 55 | 56 | 57 | org.apache.commons 58 | commons-lang3 59 | 60 | 61 | link.ttiot 62 | ttiot-common-context 63 | 64 | 65 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/ack/MqttAckListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.ack; 18 | 19 | import io.netty.handler.codec.mqtt.*; 20 | import link.ttiot.broker.eventor.ack.MqttAckEvent; 21 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 22 | import link.ttiot.common.core.function.FunctionApi; 23 | import link.ttiot.common.core.provider.message.MessageProvider; 24 | import link.ttiot.common.ioc.annotation.DefaultListener; 25 | import lombok.extern.slf4j.Slf4j; 26 | 27 | import java.util.Optional; 28 | 29 | 30 | /** 31 | * @author: shijun 32 | * @date: 2019-04-12 33 | * @description: 34 | */ 35 | @Slf4j 36 | @DefaultListener 37 | public class MqttAckListener extends MqttApplicationListener implements FunctionApi { 38 | 39 | public MqttAckListener() { 40 | super(); 41 | } 42 | 43 | @Override 44 | public void onApplicationEvent(MqttAckEvent mqttAckEvent) { 45 | MqttMessage mqttMessage = mqttAckEvent.getSource(); 46 | Object msg=null; 47 | if (mqttMessage instanceof MqttSubscribeMessage){ 48 | msg= MessageProvider.subAckProduce((MqttSubscribeMessage) mqttMessage); 49 | }else if (mqttMessage instanceof MqttUnsubscribeMessage){ 50 | msg= MessageProvider.unSubAckProduce((MqttUnsubscribeMessage) mqttMessage); 51 | }else if (mqttMessage instanceof MqttPublishMessage){ 52 | msg= MessageProvider.pubAckProduce(mqttMessage.fixedHeader().messageType(),((MqttPublishMessage) mqttMessage).variableHeader().packetId(),0x02); 53 | }else if (mqttMessage instanceof MqttMessage){ 54 | msg=mqttMessage; 55 | } 56 | Optional.ofNullable(msg).map(u->mqttAckEvent.getChannel().writeAndFlush(u)); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/connect/MqttDisConnectListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.connect; 18 | 19 | import link.ttiot.broker.eventor.connect.MqttDisConnectEvent; 20 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 21 | import link.ttiot.common.context.service.MessageStoreService; 22 | import link.ttiot.common.context.service.SessionService; 23 | import link.ttiot.common.context.service.SubscribeService; 24 | import link.ttiot.common.ioc.annotation.DefaultListener; 25 | import link.ttiot.common.ioc.annotation.Inject; 26 | import lombok.extern.slf4j.Slf4j; 27 | 28 | 29 | 30 | /** 31 | * @author: shijun 32 | * @date: 2019-04-12 33 | * @description: 34 | */ 35 | @Slf4j 36 | @DefaultListener 37 | public class MqttDisConnectListener extends MqttApplicationListener { 38 | 39 | @Inject 40 | private SessionService sessionService; 41 | 42 | @Inject 43 | private SubscribeService subscribeService; 44 | 45 | @Inject 46 | private MessageStoreService messageStoreService; 47 | 48 | public MqttDisConnectListener() { 49 | super(); 50 | } 51 | 52 | @Override 53 | public void onApplicationEvent(MqttDisConnectEvent mqttDisConnectEvent) { 54 | 55 | log.info("The client requests the link to close , clientId: {}", mqttDisConnectEvent.getClientId()); 56 | sessionService.delete(mqttDisConnectEvent.getClientId()); 57 | 58 | getTTIotContext().removeChannelByContext(mqttDisConnectEvent.getClientId(),sessionService,()->{ 59 | subscribeService.delete(mqttDisConnectEvent.getClientId()); 60 | messageStoreService.deleteByClientId(mqttDisConnectEvent.getClientId()); 61 | }); 62 | mqttDisConnectEvent.getChannel().close(); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/service/MessageStoreService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context.service; 18 | 19 | 20 | import link.ttiot.common.context.db.DbHelper; 21 | import link.ttiot.common.context.entity.MessageStore; 22 | import link.ttiot.common.ioc.annotation.Inject; 23 | import lombok.Getter; 24 | 25 | import java.util.Map; 26 | 27 | /** 28 | * @author: shijun 29 | * @date: 2019-05-03 30 | * @description: 31 | */ 32 | public class MessageStoreService { 33 | 34 | @Getter 35 | @Inject 36 | private DbHelper dbHelper; 37 | 38 | public boolean saveAndFlushRetain(String tenantId, MessageStore messageStore) { 39 | return dbHelper.saveRetainMessage(tenantId, messageStore.getTopic(), messageStore); 40 | } 41 | 42 | public MessageStore getRetain(String tenantId, String topic) { 43 | return dbHelper.getRetainMessage(tenantId, topic); 44 | } 45 | 46 | public boolean deleteRetain(String tenantId, String topic) { 47 | return dbHelper.deleteRetainMessage(tenantId, topic); 48 | } 49 | 50 | public boolean save(String clientId, MessageStore messageStore) { 51 | return dbHelper.saveMessage(messageStore.getPacketId(), clientId, messageStore); 52 | } 53 | 54 | public MessageStore get(int packetId, String clientId) { 55 | return dbHelper.getMessage(packetId, clientId); 56 | } 57 | 58 | public boolean delete(int packetId, String clientId) { 59 | return dbHelper.deleteMessage(packetId, clientId); 60 | } 61 | 62 | public boolean deleteByClientId(String clientId) { 63 | return dbHelper.deleteMessageByClient(clientId); 64 | } 65 | 66 | public int incrPacketId() { 67 | return (int) dbHelper.incrPacketId(); 68 | } 69 | 70 | public Map getByClientId(String clientId) { 71 | 72 | return dbHelper.listMessageByClient(clientId); 73 | 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/provider/bean/SingtonBeanProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.provider.bean; 18 | 19 | import cn.hutool.core.exceptions.ExceptionUtil; 20 | import cn.hutool.core.exceptions.UtilException; 21 | import lombok.experimental.UtilityClass; 22 | 23 | import java.lang.reflect.Constructor; 24 | import java.lang.reflect.InvocationTargetException; 25 | import java.util.concurrent.ConcurrentHashMap; 26 | import java.util.concurrent.ConcurrentMap; 27 | 28 | /** 29 | * @author: shijun 30 | * @date: 2019-04-14 31 | * @description: 32 | */ 33 | @UtilityClass 34 | public class SingtonBeanProvider{ 35 | 36 | private static final ConcurrentMap map = new ConcurrentHashMap(); 37 | 38 | public T produce(Class type) { 39 | Object ob = map.get(type); 40 | try { 41 | if (ob == null) { 42 | synchronized (map) { 43 | ob = type.newInstance(); 44 | map.put(type, ob); 45 | } 46 | } 47 | } catch (Throwable e){ 48 | throw ExceptionUtil.wrap(e, UtilException.class); 49 | } 50 | return (T) ob; 51 | } 52 | 53 | 54 | public T protocalProduce(Class type,Boolean b) { 55 | Object ob = map.get(type); 56 | try { 57 | if (ob == null) { 58 | synchronized (map) { 59 | Constructor cons = type.getConstructor(boolean.class); 60 | ob = cons.newInstance(b); 61 | map.put(type, ob); 62 | } 63 | } 64 | } catch (Throwable e){ 65 | throw ExceptionUtil.wrap(e, UtilException.class); 66 | } 67 | return (T) ob; 68 | } 69 | 70 | public void remove(Class type) { 71 | map.remove(type); 72 | } 73 | 74 | public void display(T t) { } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/publish/rec/MqttPubRecListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.publish.rec; 18 | 19 | import io.netty.handler.codec.mqtt.*; 20 | 21 | import link.ttiot.broker.eventor.ack.MqttAckEvent; 22 | import link.ttiot.broker.eventor.publish.rec.MqttPubRecEvent; 23 | import link.ttiot.common.context.entity.MessageStore; 24 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 25 | import link.ttiot.common.context.service.MessageStoreService; 26 | import link.ttiot.common.core.constant.enums.MessageState; 27 | import link.ttiot.common.core.function.FunctionApi; 28 | import link.ttiot.common.core.provider.message.MessageProvider; 29 | import link.ttiot.common.ioc.annotation.DefaultListener; 30 | import link.ttiot.common.ioc.annotation.Inject; 31 | import lombok.extern.slf4j.Slf4j; 32 | 33 | 34 | /** 35 | * @author: shijun 36 | * @date: 2019-04-12 37 | * @description: 38 | */ 39 | @Slf4j 40 | @DefaultListener 41 | public class MqttPubRecListener extends MqttApplicationListener implements FunctionApi { 42 | 43 | @Inject 44 | private MessageStoreService messageStoreService; 45 | 46 | public MqttPubRecListener() { 47 | super(); 48 | } 49 | 50 | @Override 51 | public void onApplicationEvent(MqttPubRecEvent mqttPubRecEvent) { 52 | 53 | MqttMessageIdVariableHeader mqttMessageIdVariableHeader = (MqttMessageIdVariableHeader) mqttPubRecEvent.getSource().variableHeader(); 54 | MessageStore messageStore = messageStoreService.get(mqttMessageIdVariableHeader.messageId(), mqttPubRecEvent.getClientId()); 55 | messageStore.setMessageState(MessageState.PUBREL); 56 | messageStoreService.save(mqttPubRecEvent.getClientId(), messageStore); 57 | MqttMessage mqttMessage = MessageProvider.pubAckProduce(MqttMessageType.PUBREL, mqttMessageIdVariableHeader.messageId(), 0); 58 | publishEvent(new MqttAckEvent(mqttMessage, mqttPubRecEvent.getChannelHandlerContext())); 59 | } 60 | 61 | 62 | } 63 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-context/src/main/java/link/ttiot/common/context/ChannelGroup.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.context; 18 | 19 | import io.netty.channel.Channel; 20 | import io.netty.channel.ChannelId; 21 | import io.netty.channel.group.DefaultChannelGroup; 22 | import io.netty.util.concurrent.EventExecutor; 23 | import link.ttiot.common.context.service.SessionService; 24 | import link.ttiot.common.core.constant.ProtocalConstant; 25 | 26 | import java.util.HashSet; 27 | import java.util.Map; 28 | import java.util.Optional; 29 | import java.util.concurrent.ConcurrentHashMap; 30 | 31 | /** 32 | * @author: shijun 33 | * @date: 2019-05-03 34 | * @description: 35 | */ 36 | public class ChannelGroup extends DefaultChannelGroup { 37 | 38 | private Map ChannelIdMap = new ConcurrentHashMap<>(); 39 | 40 | /** 41 | * 上线 42 | * 43 | * @return 44 | */ 45 | public boolean add(String clientId, Channel channel) { 46 | channel.attr(ProtocalConstant.LOGIN).set(true); 47 | channel.attr(ProtocalConstant.CLIENTID).set(clientId); 48 | channel.attr(ProtocalConstant.RECEIVE).set(new HashSet<>()); 49 | this.ChannelIdMap.put(clientId, channel.id()); 50 | return super.add(channel); 51 | } 52 | 53 | public Channel find(String clientId) { 54 | return Optional.ofNullable(ChannelIdMap.get(clientId)).map(u -> super.find(u)).orElse(null); 55 | } 56 | 57 | /** 58 | * 下线 59 | * 60 | * @return 61 | */ 62 | public boolean remove(String clientId, SessionService sessionService, Runnable runnable) { 63 | 64 | return Optional.ofNullable(sessionService.get(clientId)).map(u -> { 65 | if (u.isCleanSession()) { 66 | runnable.run(); 67 | sessionService.delete(clientId); 68 | } 69 | return super.remove(find(clientId)); 70 | }).orElse(false); 71 | } 72 | 73 | public ChannelGroup(EventExecutor executor) { 74 | super(executor); 75 | } 76 | 77 | 78 | } 79 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/connect/MqttConnectBackListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.connect; 18 | 19 | import io.netty.channel.Channel; 20 | import io.netty.handler.codec.mqtt.MqttConnAckMessage; 21 | import io.netty.handler.codec.mqtt.MqttConnectMessage; 22 | import link.ttiot.broker.eventor.connect.MqttConnectBackEvent; 23 | import link.ttiot.common.context.protocal.mqtt.MqttApplicationListener; 24 | import link.ttiot.common.core.provider.message.MessageProvider; 25 | import link.ttiot.common.ioc.annotation.DefaultListener; 26 | import lombok.extern.slf4j.Slf4j; 27 | 28 | 29 | /** 30 | * @author: shijun 31 | * @date: 2019-04-12 32 | * @description: 33 | */ 34 | @Slf4j 35 | @DefaultListener 36 | public class MqttConnectBackListener extends MqttApplicationListener { 37 | 38 | public MqttConnectBackListener() { 39 | super(); 40 | } 41 | 42 | @Override 43 | public void onApplicationEvent(MqttConnectBackEvent mqttConnectBackEvent) { 44 | MqttConnectMessage mqttConnectMessage= mqttConnectBackEvent.getSource(); 45 | Channel channel=mqttConnectBackEvent.getChannelHandlerContext().channel(); 46 | MqttConnAckMessage mqttConnAckMessage=null; 47 | switch (mqttConnectBackEvent.getMqttConnectReturnCode()){ 48 | case CONNECTION_ACCEPTED: 49 | mqttConnAckMessage= MessageProvider.connectBackproduce( mqttConnectBackEvent.getMqttConnectReturnCode(), !mqttConnectMessage.variableHeader().isCleanSession(),mqttConnectMessage.fixedHeader().isDup() , mqttConnectMessage.fixedHeader().isRetain()); 50 | break; 51 | case CONNECTION_REFUSED_IDENTIFIER_REJECTED: 52 | mqttConnAckMessage= MessageProvider.connectBackproduce( mqttConnectBackEvent.getMqttConnectReturnCode(), true,false ,false); 53 | break; 54 | case CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD: 55 | mqttConnAckMessage= MessageProvider.connectBackproduce( mqttConnectBackEvent.getMqttConnectReturnCode(), true,false ,false); 56 | return; 57 | } 58 | channel.writeAndFlush(mqttConnAckMessage); 59 | 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/MqttDecoderHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler; 18 | 19 | import io.netty.channel.ChannelHandler; 20 | import io.netty.channel.ChannelHandlerContext; 21 | import io.netty.channel.SimpleChannelInboundHandler; 22 | import io.netty.handler.codec.mqtt.*; 23 | import io.netty.handler.timeout.IdleState; 24 | import io.netty.handler.timeout.IdleStateEvent; 25 | import link.ttiot.broker.eventor.decode.MqttDecoderFailEvent; 26 | import link.ttiot.broker.eventor.decode.MqttDecoderSuccessEvent; 27 | import link.ttiot.broker.eventor.lwt.MqttLwtEvent; 28 | import link.ttiot.common.context.Context; 29 | import link.ttiot.common.core.channel.ChannelUtils; 30 | import link.ttiot.common.core.function.FunctionApi; 31 | import lombok.extern.slf4j.Slf4j; 32 | 33 | import java.util.Optional; 34 | 35 | 36 | /** 37 | * @author: shijun 38 | * @date: 2019-04-12 39 | * @description: 40 | */ 41 | @ChannelHandler.Sharable 42 | @Slf4j 43 | public class MqttDecoderHandler extends SimpleChannelInboundHandler implements FunctionApi { 44 | 45 | @Override 46 | protected void channelRead0(ChannelHandlerContext ctx, MqttMessage msg) { 47 | 48 | Context.me().publishEvent(msg.decoderResult().isFailure() ? new MqttDecoderFailEvent(msg, ctx) : new MqttDecoderSuccessEvent(msg, ctx)); 49 | 50 | } 51 | 52 | @Override 53 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 54 | log.debug("The current thread is raising an exception"); 55 | } 56 | 57 | @Override 58 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { 59 | if (evt instanceof IdleStateEvent) { 60 | IdleStateEvent idleStateEvent = (IdleStateEvent) evt; 61 | predicateDo(ctx.channel(), u -> idleStateEvent.state() == IdleState.ALL_IDLE, u -> { 62 | Optional.ofNullable(ChannelUtils.clientId(u)).map(clientId -> { 63 | Context.me().publishEvent(new MqttLwtEvent(clientId)); 64 | return true; 65 | }).orElse(false); 66 | ctx.close(); 67 | }); 68 | } 69 | } 70 | 71 | 72 | 73 | @Override 74 | public void channelInactive(ChannelHandlerContext ctx) throws Exception { 75 | super.channelInactive(ctx); 76 | } 77 | 78 | 79 | } 80 | -------------------------------------------------------------------------------- /ttiot-client-java/src/main/java/link/ttiot/client/java/handler/MqttDecoderHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.client.java.handler; 18 | 19 | import io.netty.channel.ChannelHandler; 20 | import io.netty.channel.ChannelHandlerContext; 21 | import io.netty.channel.SimpleChannelInboundHandler; 22 | import io.netty.handler.codec.mqtt.MqttMessage; 23 | import io.netty.handler.timeout.IdleState; 24 | import io.netty.handler.timeout.IdleStateEvent; 25 | import link.ttiot.client.java.eventor.decode.MqttDecoderFailEvent; 26 | import link.ttiot.client.java.eventor.decode.MqttDecoderSuccessEvent; 27 | import link.ttiot.client.java.eventor.lwt.MqttLwtEvent; 28 | import link.ttiot.common.context.Context; 29 | import link.ttiot.common.core.channel.ChannelUtils; 30 | import link.ttiot.common.core.function.FunctionApi; 31 | import lombok.extern.slf4j.Slf4j; 32 | 33 | import java.util.Optional; 34 | 35 | 36 | /** 37 | * @author: shijun 38 | * @date: 2019-04-12 39 | * @description: 40 | */ 41 | @ChannelHandler.Sharable 42 | @Slf4j 43 | public class MqttDecoderHandler extends SimpleChannelInboundHandler implements FunctionApi { 44 | 45 | @Override 46 | protected void channelRead0(ChannelHandlerContext ctx, MqttMessage msg) { 47 | Context.me().publishEvent(msg.decoderResult().isFailure() ? new MqttDecoderFailEvent(msg, ctx) : new MqttDecoderSuccessEvent(msg, ctx)); 48 | } 49 | 50 | @Override 51 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 52 | log.debug("The current thread is raising an exception"); 53 | } 54 | 55 | @Override 56 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { 57 | if (evt instanceof IdleStateEvent) { 58 | IdleStateEvent idleStateEvent = (IdleStateEvent) evt; 59 | predicateDo(ctx.channel(), u -> idleStateEvent.state() == IdleState.ALL_IDLE, u -> { 60 | Optional.ofNullable(ChannelUtils.clientId(u)).map(clientId -> { 61 | Context.me().publishEvent(new MqttLwtEvent(clientId)); 62 | return true; 63 | }).orElse(false); 64 | ctx.close(); 65 | }); 66 | } 67 | } 68 | 69 | @Override 70 | public void channelInactive(ChannelHandlerContext ctx) throws Exception { 71 | super.channelInactive(ctx); 72 | } 73 | 74 | 75 | } 76 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-core/src/main/java/link/ttiot/common/core/thread/ThreadPoolProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.core.thread; 18 | 19 | import cn.hutool.core.exceptions.DependencyException; 20 | import cn.hutool.core.exceptions.ExceptionUtil; 21 | import cn.hutool.core.exceptions.UtilException; 22 | import cn.hutool.core.thread.ThreadUtil; 23 | import link.ttiot.common.core.constant.enums.ThreadPoolEnum; 24 | import lombok.experimental.UtilityClass; 25 | 26 | import java.lang.reflect.Constructor; 27 | import java.util.Optional; 28 | import java.util.concurrent.ConcurrentHashMap; 29 | import java.util.concurrent.ConcurrentMap; 30 | import java.util.concurrent.ExecutorService; 31 | 32 | /** 33 | * @author: shijun 34 | * @date: 2019-04-16 35 | * @description: 36 | */ 37 | 38 | @UtilityClass 39 | public class ThreadPoolProvider { 40 | 41 | private static final ConcurrentMap map = new ConcurrentHashMap<>(); 42 | 43 | private static ThreadPoolEnum defaultThreadPoolEnum; 44 | 45 | public ThreadPool getDefault() { 46 | return Optional.ofNullable(defaultThreadPoolEnum).map(u -> map.get(u)).orElseThrow(() -> new UtilException("Component does not exist")); 47 | } 48 | 49 | public ThreadPool get(ThreadPoolEnum type) { 50 | ThreadPool ob = map.get(type); 51 | if (ob == null) { 52 | ob = register(type, null, false,null); 53 | } 54 | return ob; 55 | } 56 | 57 | public ThreadPool register(ThreadPoolEnum type, Integer corePoolSize, boolean isDefault,Thread.UncaughtExceptionHandler uncaughtExceptionHandler) { 58 | ThreadPool ob = map.get(type); 59 | if (ob == null) { 60 | synchronized (map) { 61 | try { 62 | Constructor cons = ThreadPool.class.getConstructor(ThreadPoolEnum.class, Integer.class,Thread.UncaughtExceptionHandler.class); 63 | ob = (ThreadPool) cons.newInstance(type, corePoolSize,uncaughtExceptionHandler); 64 | if (isDefault) { 65 | defaultThreadPoolEnum = type; 66 | } 67 | map.put(type, ob); 68 | } catch (Throwable e) { 69 | throw ExceptionUtil.wrap(e, DependencyException.class); 70 | } 71 | } 72 | } 73 | return ob; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /ttiot-common/ttiot-common-ioc/src/main/java/link/ttiot/common/ioc/multicaster/ListenerRetriever.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.common.ioc.multicaster; 18 | 19 | import link.ttiot.common.ioc.core.ApplicationListener; 20 | import link.ttiot.common.ioc.core.HttpHandler; 21 | import link.ttiot.common.ioc.core.RuleHandler; 22 | import link.ttiot.common.ioc.vo.MqttPayload; 23 | import lombok.Getter; 24 | import lombok.extern.slf4j.Slf4j; 25 | 26 | import java.util.HashMap; 27 | import java.util.HashSet; 28 | import java.util.Set; 29 | 30 | /** 31 | * @author: shijun 32 | * @date: 2019-04-13 33 | * @description: 34 | */ 35 | @Slf4j 36 | public class ListenerRetriever { 37 | 38 | @Getter 39 | public final Set> applicationListeners; 40 | 41 | @Getter 42 | public final Set> defaultApplicationListeners; 43 | 44 | @Getter 45 | public final HashMap> ruleHanlders; 46 | 47 | @Getter 48 | public final HashMap httpHandlers; 49 | 50 | public ListenerRetriever() { 51 | applicationListeners = new HashSet<>(); 52 | defaultApplicationListeners = new HashSet<>(); 53 | ruleHanlders = new HashMap<>(); 54 | httpHandlers=new HashMap<>(); 55 | } 56 | 57 | public void addApplicationListener(boolean isdefault, ApplicationListener applicationListener) { 58 | log.info("Components {} are added in TTIOC, defualt value is : {}", applicationListener.getClass(), isdefault); 59 | if (applicationListener != null) { 60 | if (isdefault) { 61 | defaultApplicationListeners.add(applicationListener); 62 | } else { 63 | applicationListeners.add(applicationListener); 64 | } 65 | } 66 | } 67 | 68 | public void addRuleHanlder(String name, RuleHandler ruleHandler) { 69 | if (ruleHandler != null) { 70 | log.info("RuleHandler {} are added in TTIOC, name is : {}", ruleHandler, name); 71 | ruleHanlders.put(name, ruleHandler); 72 | } 73 | } 74 | 75 | public void addHttpHanlder(String name,HttpHandler httpHandler) { 76 | if (httpHandler != null) { 77 | log.info("HttpHandler {} are added in TTIOC, name is : {}", httpHandler, name); 78 | httpHandlers.put(name, httpHandler); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/lwt/MqttLwtListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.lwt; 18 | 19 | import io.netty.buffer.ByteBuf; 20 | import io.netty.buffer.ByteBufUtil; 21 | import io.netty.handler.codec.mqtt.MqttFixedHeader; 22 | import io.netty.handler.codec.mqtt.MqttPublishVariableHeader; 23 | import link.ttiot.common.context.Context; 24 | import link.ttiot.common.context.service.SessionService; 25 | import link.ttiot.common.ioc.core.AbstractApplicationListener; 26 | import link.ttiot.broker.eventor.lwt.MqttLwtEvent; 27 | import link.ttiot.broker.eventor.publish.MqttPublishTopicEvent; 28 | import link.ttiot.common.core.function.FunctionApi; 29 | import link.ttiot.common.ioc.annotation.DefaultListener; 30 | import link.ttiot.common.ioc.annotation.Inject; 31 | import link.ttiot.common.ioc.core.ApplicationEvent; 32 | import lombok.extern.slf4j.Slf4j; 33 | 34 | import java.util.Optional; 35 | 36 | 37 | /** 38 | * @author: shijun 39 | * @date: 2019-04-12 40 | * @description: 41 | */ 42 | @Slf4j 43 | @DefaultListener 44 | public class MqttLwtListener extends AbstractApplicationListener implements FunctionApi { 45 | 46 | @Inject 47 | private SessionService sessionService; 48 | 49 | public MqttLwtListener() { 50 | super(); 51 | } 52 | 53 | @Override 54 | public void onApplicationEvent(MqttLwtEvent mqttLwtEvent) { 55 | Optional.ofNullable(sessionService.get(mqttLwtEvent.getSource())).map(session->{ 56 | return Optional.ofNullable(session.getWillMessage()).map(willMessage->{ 57 | MqttFixedHeader mqttFixedHeader = willMessage.fixedHeader(); 58 | MqttPublishVariableHeader mqttPublishVariableHeader = willMessage.variableHeader(); 59 | ByteBuf payload = willMessage.payload(); 60 | byte[] bytes = ByteBufUtil.getBytes(payload); 61 | publishEvent(new MqttPublishTopicEvent(mqttPublishVariableHeader.topicName(), mqttFixedHeader.qosLevel(), bytes, 62 | session.getTenantId(),mqttFixedHeader.isRetain())); 63 | return true; 64 | }).orElse(false); 65 | }).orElse(null); 66 | } 67 | 68 | public void publishEvent(ApplicationEvent event) { 69 | getTTIotContext().publishEvent(event); 70 | } 71 | 72 | public Context getTTIotContext() { 73 | return Context.me(); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /ttiot-broker/src/main/java/link/ttiot/broker/handler/listener/publish/MqttUnconfirmedPublishListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright(c) link.ttiot & shijun All rights reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * Author: shijun (conttononline@outlook.com) 15 | */ 16 | 17 | package link.ttiot.broker.handler.listener.publish; 18 | 19 | 20 | import io.netty.handler.codec.mqtt.*; 21 | import link.ttiot.common.context.Context; 22 | import link.ttiot.common.context.service.MessageStoreService; 23 | import link.ttiot.common.ioc.core.AbstractApplicationListener; 24 | import link.ttiot.broker.eventor.publish.MqttUnconfirmedPublishEvent; 25 | import link.ttiot.common.core.channel.ChannelUtils; 26 | import link.ttiot.common.core.constant.enums.MessageState; 27 | import link.ttiot.common.core.function.FunctionApi; 28 | import link.ttiot.common.core.provider.message.MessageProvider; 29 | import link.ttiot.common.ioc.annotation.DefaultListener; 30 | import link.ttiot.common.ioc.annotation.Inject; 31 | import lombok.extern.slf4j.Slf4j; 32 | 33 | import java.util.Optional; 34 | 35 | 36 | /** 37 | * @author: shijun 38 | * @date: 2019-04-12 39 | * @description: 40 | */ 41 | @Slf4j 42 | @DefaultListener 43 | public class MqttUnconfirmedPublishListener extends AbstractApplicationListener implements FunctionApi { 44 | 45 | @Inject 46 | private MessageStoreService messageStoreService; 47 | 48 | public MqttUnconfirmedPublishListener() { 49 | super(); 50 | } 51 | 52 | @Override 53 | public void onApplicationEvent(MqttUnconfirmedPublishEvent mqttPublishEvent) { 54 | 55 | String clientId=mqttPublishEvent.getSource(); 56 | 57 | Optional.ofNullable(getTTIotContext().getChannelByContext(clientId)).map(u->{ 58 | if (ChannelUtils.ifLogin(u)){ 59 | messageStoreService.getByClientId(clientId).forEach((k,v)->{ 60 | MqttMessage mqttMessage=null; 61 | if (v.getMessageState()== MessageState.PUBLISH){ 62 | mqttMessage= MessageProvider.publishMessageProduce(v.getTopic(),v.getMessageBytes(),Integer.parseInt(k),v.getMqttQoS(),false); 63 | }else if (v.getMessageState()== MessageState.PUBREL){ 64 | mqttMessage= MessageProvider.pubAckProduce(MqttMessageType.PUBREL,Integer.parseInt(k),0); 65 | } 66 | u.writeAndFlush(mqttMessage); 67 | }); 68 | } 69 | return true; 70 | }); 71 | 72 | } 73 | 74 | public Context getTTIotContext() { 75 | return Context.me(); 76 | } 77 | } 78 | --------------------------------------------------------------------------------