io.vertx.mqtt.impl.MqttServerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vertx-mqtt Show documentation
Show all versions of vertx-mqtt Show documentation
Vert.x MQTT reactive client and server
/*
* Copyright 2016 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.vertx.mqtt.impl;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.compression.ZlibCodecFactory;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.http.websocketx.extensions.WebSocketServerExtensionHandler;
import io.netty.handler.codec.http.websocketx.extensions.WebSocketServerExtensionHandshaker;
import io.netty.handler.codec.http.websocketx.extensions.compression.DeflateFrameServerExtensionHandshaker;
import io.netty.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateServerExtensionHandshaker;
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.ReferenceCountUtil;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.net.impl.NetSocketInternal;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.net.NetServer;
import io.vertx.mqtt.MqttEndpoint;
import io.vertx.mqtt.MqttServer;
import io.vertx.mqtt.MqttServerOptions;
import java.util.ArrayList;
import java.util.List;
import static io.vertx.mqtt.MqttServerOptions.MQTT_SUBPROTOCOL_CSV_LIST;
/**
* An MQTT server implementation
*/
public class MqttServerImpl implements MqttServer {
private static final Logger log = LoggerFactory.getLogger(MqttServerImpl.class);
private final VertxInternal vertx;
private final NetServer server;
private Handler endpointHandler;
private Handler exceptionHandler;
private MqttServerOptions options;
public MqttServerImpl(Vertx vertx, MqttServerOptions options) {
this.vertx = (VertxInternal) vertx;
this.server = vertx.createNetServer(options);
this.options = options;
}
@Override
public Future listen() {
return listen(this.options.getPort());
}
@Override
public Future listen(int port, String host) {
Handler h1 = endpointHandler;
Handler h2 = exceptionHandler;
if (h1 == null) {
return vertx.getOrCreateContext().failedFuture(new IllegalStateException("Please set handler before server is listening"));
}
server.connectHandler(so -> {
NetSocketInternal soi = (NetSocketInternal) so;
ChannelPipeline pipeline = soi.channelHandlerContext().pipeline();
initChannel(pipeline);
MqttServerConnection conn = new MqttServerConnection(soi, h1, h2, options);
soi.eventHandler(evt -> {
if (evt instanceof WebSocketServerProtocolHandler.HandshakeComplete) {
synchronized (conn) {
conn.handleHandshakeComplete((WebSocketServerProtocolHandler.HandshakeComplete) evt);
}
}
ReferenceCountUtil.release(evt);
});
soi.messageHandler(msg -> {
synchronized (conn) {
conn.handleMessage(msg);
}
});
});
return server.listen(port, host).map(this);
}
@Override
public Future listen(int port) {
return listen(port, this.options.getHost());
}
@Override
public MqttServer listen(int port, Handler> listenHandler) {
return listen(port, this.options.getHost(), listenHandler);
}
@Override
public MqttServer listen(Handler> listenHandler) {
return listen(this.options.getPort(), listenHandler);
}
@Override
public MqttServer listen(int port, String host, Handler> listenHandler) {
Future fut = listen(port, host);
if (listenHandler != null) {
fut.onComplete(listenHandler);
}
return this;
}
@Override
public synchronized MqttServer endpointHandler(Handler handler) {
endpointHandler = handler;
return this;
}
@Override
public synchronized MqttServer exceptionHandler(Handler handler) {
exceptionHandler = handler;
return this;
}
@Override
public int actualPort() {
return server.actualPort();
}
@Override
public Future close() {
return server.close();
}
@Override
public void close(Handler> completionHandler) {
server.close(completionHandler);
}
static class WebSocketFrameToByteBufDecoder extends MessageToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext chc, BinaryWebSocketFrame frame, List