33import com .ss .mqtt .broker .model .MqttPropertyConstants ;
44import com .ss .mqtt .broker .model .QoS ;
55import com .ss .mqtt .broker .network .MqttConnection ;
6- import com .ss .mqtt .broker .network .packet .in .MqttReadablePacket ;
7- import com .ss .mqtt .broker .network .packet .out .MqttWritablePacket ;
6+ import com .ss .mqtt .broker .network .client .UnsafeMqttClient ;
7+ import com .ss .mqtt .broker .network .client .impl .DeviceMqttClient ;
8+ import com .ss .mqtt .broker .network .packet .PacketType ;
9+ import com .ss .mqtt .broker .network .packet .in .handler .*;
10+ import com .ss .mqtt .broker .service .ClientIdRegistry ;
811import com .ss .mqtt .broker .service .ClientService ;
912import com .ss .mqtt .broker .service .PublishingService ;
1013import com .ss .mqtt .broker .service .SubscriptionService ;
11- import com .ss .mqtt .broker .service .impl .DefaultClientService ;
12- import com .ss .mqtt .broker .service .impl .SimplePublishingService ;
13- import com .ss .mqtt .broker .service .impl .SimpleSubscriptionService ;
14- import com .ss .mqtt .broker .service .impl .SimpleSubscriptions ;
14+ import com .ss .mqtt .broker .service .impl .*;
1515import com .ss .rlib .network .*;
1616import com .ss .rlib .network .impl .DefaultBufferAllocator ;
1717import com .ss .rlib .network .server .ServerNetwork ;
@@ -52,29 +52,62 @@ private interface ChannelFactory extends
5252 return new DefaultClientService ();
5353 }
5454
55+ @ NotNull
56+ @ Bean ClientIdRegistry clientIdRegistry () {
57+ return new SimpleClientIdRegistry (
58+ env .getProperty (
59+ "client.id.available.chars" ,
60+ "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"
61+ ),
62+ env .getProperty ("client.id.max.length" , int .class , 36 )
63+ );
64+ }
65+
5566 @ Bean
56- @ NotNull Network <? extends Connection <MqttReadablePacket , MqttWritablePacket >> network (
57- @ NotNull ServerNetworkConfig networkConfig ,
58- @ NotNull BufferAllocator bufferAllocator ,
59- @ NotNull Consumer <MqttConnection > mqttConnectionConsumer ,
60- @ NotNull MqttConnectionConfig connectionConfig ,
67+ PacketInHandler @ NotNull [] devicePacketHandlers (
68+ @ NotNull ClientIdRegistry clientIdRegistry ,
6169 @ NotNull SubscriptionService subscriptionService ,
6270 @ NotNull PublishingService publishingService
6371 ) {
64- ServerNetwork <MqttConnection > serverNetwork = NetworkFactory .newServerNetwork (
72+
73+ var handlers = new PacketInHandler [PacketType .INVALID .ordinal ()];
74+ handlers [PacketType .CONNECT .ordinal ()] = new ConnectInPacketHandler (clientIdRegistry );
75+ handlers [PacketType .SUBSCRIBE .ordinal ()] = new SubscribeInPacketHandler (subscriptionService );
76+ handlers [PacketType .UNSUBSCRIBE .ordinal ()] = new UnsubscribeInPacketHandler (subscriptionService );
77+ handlers [PacketType .PUBLISH .ordinal ()] = new PublishInPacketHandler (publishingService );
78+
79+ return handlers ;
80+ }
81+
82+ @ Bean
83+ @ NotNull ServerNetwork <@ NotNull MqttConnection > deviceNetwork (
84+ @ NotNull ServerNetworkConfig networkConfig ,
85+ @ NotNull BufferAllocator bufferAllocator ,
86+ @ NotNull MqttConnectionConfig deviceConnectionConfig ,
87+ PacketInHandler @ NotNull [] devicePacketHandlers
88+ ) {
89+ return NetworkFactory .newServerNetwork (
6590 networkConfig ,
66- networkChannelFactory (
91+ deviceConnectionFactory (
6792 bufferAllocator ,
68- connectionConfig ,
69- subscriptionService ,
70- publishingService
93+ deviceConnectionConfig ,
94+ devicePacketHandlers
7195 )
7296 );
97+ }
7398
74- serverNetwork .start (new InetSocketAddress ("localhost" , 1883 ));
75- serverNetwork .onAccept (mqttConnectionConsumer );
99+ @ Bean
100+ @ NotNull InetSocketAddress deviceNetworkAddress (
101+ @ NotNull ServerNetwork <@ NotNull MqttConnection > deviceNetwork ,
102+ @ NotNull Consumer <@ NotNull MqttConnection > mqttConnectionConsumer
103+ ) {
104+
105+ var address = new InetSocketAddress ("localhost" , 1883 );
106+
107+ deviceNetwork .start (address );
108+ deviceNetwork .onAccept (mqttConnectionConsumer );
76109
77- return serverNetwork ;
110+ return address ;
78111 }
79112
80113 @ Bean
@@ -88,16 +121,16 @@ private interface ChannelFactory extends
88121 }
89122
90123 @ Bean
91- @ NotNull Consumer <MqttConnection > mqttConnectionConsumer (@ NotNull ClientService clientService ) {
124+ @ NotNull Consumer <@ NotNull MqttConnection > mqttConnectionConsumer () {
92125 return mqttConnection -> {
93126 log .info ("Accepted connection: {}" , mqttConnection );
94- var client = mqttConnection .getClient ();
127+ var client = ( UnsafeMqttClient ) mqttConnection .getClient ();
95128 mqttConnection .onReceive ((conn , packet ) -> client .handle (packet ));
96129 };
97130 }
98131
99132 @ Bean
100- @ NotNull MqttConnectionConfig mqttConnectionConfig () {
133+ @ NotNull MqttConnectionConfig deviceConnectionConfig () {
101134 return new MqttConnectionConfig (
102135 QoS .of (env .getProperty ("mqtt.connection.max.qos" , int .class , 2 )),
103136 env .getProperty (
@@ -128,21 +161,19 @@ private interface ChannelFactory extends
128161 );
129162 }
130163
131- private @ NotNull ChannelFactory networkChannelFactory (
164+ private @ NotNull ChannelFactory deviceConnectionFactory (
132165 @ NotNull BufferAllocator bufferAllocator ,
133166 @ NotNull MqttConnectionConfig connectionConfig ,
134- @ NotNull SubscriptionService subscriptionService ,
135- @ NotNull PublishingService publishingService
167+ PacketInHandler @ NotNull [] packetHandlers
136168 ) {
137169 return (network , channel ) -> new MqttConnection (
138170 network ,
139171 channel ,
140- NetworkCryptor .NULL ,
141172 bufferAllocator ,
142173 100 ,
143- subscriptionService ,
144- publishingService ,
145- connectionConfig
174+ packetHandlers ,
175+ connectionConfig ,
176+ DeviceMqttClient :: new
146177 );
147178 }
148179}
0 commit comments