@@ -95,10 +95,10 @@ private abstract static class PipelineInitializer {
95
95
96
96
private static final Logger LOG = LoggerFactory .getLogger (NewNettyAcceptor .class );
97
97
98
- EventLoopGroup m_bossGroup ;
99
- EventLoopGroup m_workerGroup ;
100
- BytesMetricsCollector m_bytesMetricsCollector = new BytesMetricsCollector ();
101
- MessageMetricsCollector m_metricsCollector = new MessageMetricsCollector ();
98
+ private EventLoopGroup bossGroup ;
99
+ private EventLoopGroup workerGroup ;
100
+ private BytesMetricsCollector bytesMetricsCollector = new BytesMetricsCollector ();
101
+ private MessageMetricsCollector metricsCollector = new MessageMetricsCollector ();
102
102
private Optional <? extends ChannelInboundHandler > metrics ;
103
103
private Optional <? extends ChannelInboundHandler > errorsCather ;
104
104
@@ -125,13 +125,13 @@ public void initialize(NewNettyMQTTHandler mqttHandler, IConfig props, ISslConte
125
125
boolean epoll = props .boolProp (BrokerConstants .NETTY_EPOLL_PROPERTY_NAME , false );
126
126
if (epoll ) {
127
127
LOG .info ("Netty is using Epoll" );
128
- m_bossGroup = new EpollEventLoopGroup ();
129
- m_workerGroup = new EpollEventLoopGroup ();
128
+ bossGroup = new EpollEventLoopGroup ();
129
+ workerGroup = new EpollEventLoopGroup ();
130
130
channelClass = EpollServerSocketChannel .class ;
131
131
} else {
132
132
LOG .info ("Netty is using NIO" );
133
- m_bossGroup = new NioEventLoopGroup ();
134
- m_workerGroup = new NioEventLoopGroup ();
133
+ bossGroup = new NioEventLoopGroup ();
134
+ workerGroup = new NioEventLoopGroup ();
135
135
channelClass = NioServerSocketChannel .class ;
136
136
}
137
137
@@ -154,9 +154,7 @@ public void initialize(NewNettyMQTTHandler mqttHandler, IConfig props, ISslConte
154
154
}
155
155
initializePlainTCPTransport (mqttHandler , props );
156
156
initializeWebSocketTransport (mqttHandler , props );
157
- String sslTcpPortProp = props .getProperty (BrokerConstants .SSL_PORT_PROPERTY_NAME );
158
- String wssPortProp = props .getProperty (BrokerConstants .WSS_PORT_PROPERTY_NAME );
159
- if (sslTcpPortProp != null || wssPortProp != null ) {
157
+ if (securityPortsConfigured (props )) {
160
158
SslContext sslContext = sslCtxCreator .initSSLContext ();
161
159
if (sslContext == null ) {
162
160
LOG .error ("Can't initialize SSLHandler layer! Exiting, check your configuration of jks" );
@@ -167,15 +165,21 @@ public void initialize(NewNettyMQTTHandler mqttHandler, IConfig props, ISslConte
167
165
}
168
166
}
169
167
170
- private void initFactory (String host , int port , String protocol , final PipelineInitializer pipeliner ) {
168
+ private boolean securityPortsConfigured (IConfig props ) {
169
+ String sslTcpPortProp = props .getProperty (BrokerConstants .SSL_PORT_PROPERTY_NAME );
170
+ String wssPortProp = props .getProperty (BrokerConstants .WSS_PORT_PROPERTY_NAME );
171
+ return sslTcpPortProp != null || wssPortProp != null ;
172
+ }
173
+
174
+ private void initFactory (String host , int port , String protocol , final PipelineInitializer pipelieInitializer ) {
171
175
LOG .debug ("Initializing server. Protocol={}" , protocol );
172
176
ServerBootstrap b = new ServerBootstrap ();
173
- b .group (m_bossGroup , m_workerGroup ).channel (channelClass )
177
+ b .group (bossGroup , workerGroup ).channel (channelClass )
174
178
.childHandler (new ChannelInitializer <SocketChannel >() {
175
179
176
180
@ Override
177
181
public void initChannel (SocketChannel ch ) throws Exception {
178
- pipeliner .init (ch );
182
+ pipelieInitializer .init (ch );
179
183
}
180
184
})
181
185
.option (ChannelOption .SO_BACKLOG , nettySoBacklog )
@@ -200,7 +204,7 @@ private void initializePlainTCPTransport(NewNettyMQTTHandler handler, IConfig pr
200
204
String tcpPortProp = props .getProperty (PORT_PROPERTY_NAME , DISABLED_PORT_BIND );
201
205
if (DISABLED_PORT_BIND .equals (tcpPortProp )) {
202
206
LOG .info ("Property {} has been set to {}. TCP MQTT will be disabled" , BrokerConstants .PORT_PROPERTY_NAME ,
203
- DISABLED_PORT_BIND );
207
+ DISABLED_PORT_BIND );
204
208
return ;
205
209
}
206
210
int port = Integer .parseInt (tcpPortProp );
@@ -209,33 +213,38 @@ private void initializePlainTCPTransport(NewNettyMQTTHandler handler, IConfig pr
209
213
@ Override
210
214
void init (SocketChannel channel ) {
211
215
ChannelPipeline pipeline = channel .pipeline ();
212
- pipeline .addFirst ("idleStateHandler" , new IdleStateHandler (nettyChannelTimeoutSeconds , 0 , 0 ));
213
- pipeline .addAfter ("idleStateHandler" , "idleEventHandler" , timeoutHandler );
214
- // pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
215
- if (errorsCather .isPresent ()) {
216
- pipeline .addLast ("bugsnagCatcher" , errorsCather .get ());
217
- }
218
- pipeline .addFirst ("bytemetrics" , new BytesMetricsHandler (m_bytesMetricsCollector ));
219
- pipeline .addLast ("autoflush" , new AutoFlushHandler (1 , TimeUnit .SECONDS ));
220
- pipeline .addLast ("decoder" , new MqttDecoder (maxBytesInMessage ));
221
- pipeline .addLast ("encoder" , MqttEncoder .INSTANCE );
222
- pipeline .addLast ("metrics" , new MessageMetricsHandler (m_metricsCollector ));
223
- pipeline .addLast ("messageLogger" , new MQTTMessageLogger ());
224
- if (metrics .isPresent ()) {
225
- pipeline .addLast ("wizardMetrics" , metrics .get ());
226
- }
227
- pipeline .addLast ("handler" , handler );
216
+ configureMQTTPipeline (pipeline , timeoutHandler , handler );
228
217
}
229
218
});
230
219
}
231
220
221
+ private void configureMQTTPipeline (ChannelPipeline pipeline , MoquetteIdleTimeoutHandler timeoutHandler ,
222
+ NewNettyMQTTHandler handler ) {
223
+ pipeline .addFirst ("idleStateHandler" , new IdleStateHandler (nettyChannelTimeoutSeconds , 0 , 0 ));
224
+ pipeline .addAfter ("idleStateHandler" , "idleEventHandler" , timeoutHandler );
225
+ // pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
226
+ if (errorsCather .isPresent ()) {
227
+ pipeline .addLast ("bugsnagCatcher" , errorsCather .get ());
228
+ }
229
+ pipeline .addFirst ("bytemetrics" , new BytesMetricsHandler (bytesMetricsCollector ));
230
+ pipeline .addLast ("autoflush" , new AutoFlushHandler (1 , TimeUnit .SECONDS ));
231
+ pipeline .addLast ("decoder" , new MqttDecoder (maxBytesInMessage ));
232
+ pipeline .addLast ("encoder" , MqttEncoder .INSTANCE );
233
+ pipeline .addLast ("metrics" , new MessageMetricsHandler (metricsCollector ));
234
+ pipeline .addLast ("messageLogger" , new MQTTMessageLogger ());
235
+ if (metrics .isPresent ()) {
236
+ pipeline .addLast ("wizardMetrics" , metrics .get ());
237
+ }
238
+ pipeline .addLast ("handler" , handler );
239
+ }
240
+
232
241
private void initializeWebSocketTransport (final NewNettyMQTTHandler handler , IConfig props ) {
233
242
LOG .debug ("Configuring Websocket MQTT transport" );
234
243
String webSocketPortProp = props .getProperty (WEB_SOCKET_PORT_PROPERTY_NAME , DISABLED_PORT_BIND );
235
244
if (DISABLED_PORT_BIND .equals (webSocketPortProp )) {
236
245
// Do nothing no WebSocket configured
237
246
LOG .info ("Property {} has been setted to {}. Websocket MQTT will be disabled" ,
238
- BrokerConstants .WEB_SOCKET_PORT_PROPERTY_NAME , DISABLED_PORT_BIND );
247
+ BrokerConstants .WEB_SOCKET_PORT_PROPERTY_NAME , DISABLED_PORT_BIND );
239
248
return ;
240
249
}
241
250
int port = Integer .parseInt (webSocketPortProp );
@@ -254,15 +263,7 @@ void init(SocketChannel channel) {
254
263
new WebSocketServerProtocolHandler ("/mqtt" , MQTT_SUBPROTOCOL_CSV_LIST ));
255
264
pipeline .addLast ("ws2bytebufDecoder" , new WebSocketFrameToByteBufDecoder ());
256
265
pipeline .addLast ("bytebuf2wsEncoder" , new ByteBufToWebSocketFrameEncoder ());
257
- pipeline .addFirst ("idleStateHandler" , new IdleStateHandler (nettyChannelTimeoutSeconds , 0 , 0 ));
258
- pipeline .addAfter ("idleStateHandler" , "idleEventHandler" , timeoutHandler );
259
- pipeline .addFirst ("bytemetrics" , new BytesMetricsHandler (m_bytesMetricsCollector ));
260
- pipeline .addLast ("autoflush" , new AutoFlushHandler (1 , TimeUnit .SECONDS ));
261
- pipeline .addLast ("decoder" , new MqttDecoder (maxBytesInMessage ));
262
- pipeline .addLast ("encoder" , MqttEncoder .INSTANCE );
263
- pipeline .addLast ("metrics" , new MessageMetricsHandler (m_metricsCollector ));
264
- pipeline .addLast ("messageLogger" , new MQTTMessageLogger ());
265
- pipeline .addLast ("handler" , handler );
266
+ configureMQTTPipeline (pipeline , timeoutHandler , handler );
266
267
}
267
268
});
268
269
}
@@ -273,7 +274,7 @@ private void initializeSSLTCPTransport(NewNettyMQTTHandler handler, IConfig prop
273
274
if (DISABLED_PORT_BIND .equals (sslPortProp )) {
274
275
// Do nothing no SSL configured
275
276
LOG .info ("Property {} has been set to {}. SSL MQTT will be disabled" ,
276
- BrokerConstants .SSL_PORT_PROPERTY_NAME , DISABLED_PORT_BIND );
277
+ BrokerConstants .SSL_PORT_PROPERTY_NAME , DISABLED_PORT_BIND );
277
278
return ;
278
279
}
279
280
@@ -290,16 +291,7 @@ private void initializeSSLTCPTransport(NewNettyMQTTHandler handler, IConfig prop
290
291
void init (SocketChannel channel ) throws Exception {
291
292
ChannelPipeline pipeline = channel .pipeline ();
292
293
pipeline .addLast ("ssl" , createSslHandler (channel , sslContext , needsClientAuth ));
293
- pipeline .addFirst ("idleStateHandler" , new IdleStateHandler (nettyChannelTimeoutSeconds , 0 , 0 ));
294
- pipeline .addAfter ("idleStateHandler" , "idleEventHandler" , timeoutHandler );
295
- // pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
296
- pipeline .addFirst ("bytemetrics" , new BytesMetricsHandler (m_bytesMetricsCollector ));
297
- pipeline .addLast ("autoflush" , new AutoFlushHandler (1 , TimeUnit .SECONDS ));
298
- pipeline .addLast ("decoder" , new MqttDecoder (maxBytesInMessage ));
299
- pipeline .addLast ("encoder" , MqttEncoder .INSTANCE );
300
- pipeline .addLast ("metrics" , new MessageMetricsHandler (m_metricsCollector ));
301
- pipeline .addLast ("messageLogger" , new MQTTMessageLogger ());
302
- pipeline .addLast ("handler" , handler );
294
+ configureMQTTPipeline (pipeline , timeoutHandler , handler );
303
295
}
304
296
});
305
297
}
@@ -331,28 +323,21 @@ void init(SocketChannel channel) throws Exception {
331
323
new WebSocketServerProtocolHandler ("/mqtt" , MQTT_SUBPROTOCOL_CSV_LIST ));
332
324
pipeline .addLast ("ws2bytebufDecoder" , new WebSocketFrameToByteBufDecoder ());
333
325
pipeline .addLast ("bytebuf2wsEncoder" , new ByteBufToWebSocketFrameEncoder ());
334
- pipeline .addFirst ("idleStateHandler" , new IdleStateHandler (nettyChannelTimeoutSeconds , 0 , 0 ));
335
- pipeline .addAfter ("idleStateHandler" , "idleEventHandler" , timeoutHandler );
336
- pipeline .addFirst ("bytemetrics" , new BytesMetricsHandler (m_bytesMetricsCollector ));
337
- pipeline .addLast ("autoflush" , new AutoFlushHandler (1 , TimeUnit .SECONDS ));
338
- pipeline .addLast ("decoder" , new MqttDecoder (maxBytesInMessage ));
339
- pipeline .addLast ("encoder" , MqttEncoder .INSTANCE );
340
- pipeline .addLast ("metrics" , new MessageMetricsHandler (m_metricsCollector ));
341
- pipeline .addLast ("messageLogger" , new MQTTMessageLogger ());
342
- pipeline .addLast ("handler" , handler );
326
+
327
+ configureMQTTPipeline (pipeline , timeoutHandler , handler );
343
328
}
344
329
});
345
330
}
346
331
347
332
@ SuppressWarnings ("FutureReturnValueIgnored" )
348
333
public void close () {
349
334
LOG .debug ("Closing Netty acceptor..." );
350
- if (m_workerGroup == null || m_bossGroup == null ) {
335
+ if (workerGroup == null || bossGroup == null ) {
351
336
LOG .error ("Netty acceptor is not initialized" );
352
337
throw new IllegalStateException ("Invoked close on an Acceptor that wasn't initialized" );
353
338
}
354
- Future <?> workerWaiter = m_workerGroup .shutdownGracefully ();
355
- Future <?> bossWaiter = m_bossGroup .shutdownGracefully ();
339
+ Future <?> workerWaiter = workerGroup .shutdownGracefully ();
340
+ Future <?> bossWaiter = bossGroup .shutdownGracefully ();
356
341
357
342
/*
358
343
* We shouldn't raise an IllegalStateException if we are interrupted. If we did so, the
@@ -366,20 +351,20 @@ public void close() {
366
351
LOG .warn ("An InterruptedException was caught while waiting for event loops to terminate..." );
367
352
}
368
353
369
- if (!m_workerGroup .isTerminated ()) {
354
+ if (!workerGroup .isTerminated ()) {
370
355
LOG .warn ("Forcing shutdown of worker event loop..." );
371
- m_workerGroup .shutdownGracefully (0L , 0L , TimeUnit .MILLISECONDS );
356
+ workerGroup .shutdownGracefully (0L , 0L , TimeUnit .MILLISECONDS );
372
357
}
373
358
374
- if (!m_bossGroup .isTerminated ()) {
359
+ if (!bossGroup .isTerminated ()) {
375
360
LOG .warn ("Forcing shutdown of boss event loop..." );
376
- m_bossGroup .shutdownGracefully (0L , 0L , TimeUnit .MILLISECONDS );
361
+ bossGroup .shutdownGracefully (0L , 0L , TimeUnit .MILLISECONDS );
377
362
}
378
363
379
- MessageMetrics metrics = m_metricsCollector .computeMetrics ();
380
- BytesMetrics bytesMetrics = m_bytesMetricsCollector .computeMetrics ();
364
+ MessageMetrics metrics = metricsCollector .computeMetrics ();
365
+ BytesMetrics bytesMetrics = bytesMetricsCollector .computeMetrics ();
381
366
LOG .info ("Metrics messages[read={}, write={}] bytes[read={}, write={}]" , metrics .messagesRead (),
382
- metrics .messagesWrote (), bytesMetrics .readBytes (), bytesMetrics .wroteBytes ());
367
+ metrics .messagesWrote (), bytesMetrics .readBytes (), bytesMetrics .wroteBytes ());
383
368
}
384
369
385
370
private ChannelHandler createSslHandler (SocketChannel channel , SslContext sslContext , boolean needsClientAuth ) {
0 commit comments