Merge branch Sofia2 with lot of improvements
This commit is contained in:
@@ -15,11 +15,21 @@
|
||||
*/
|
||||
package io.moquette.server;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import io.moquette.server.netty.AutoFlushHandler;
|
||||
import io.moquette.server.netty.NettyUtils;
|
||||
import io.moquette.server.netty.metrics.BytesMetrics;
|
||||
import io.moquette.server.netty.metrics.BytesMetricsHandler;
|
||||
import io.moquette.server.netty.metrics.MessageMetrics;
|
||||
import io.moquette.server.netty.metrics.MessageMetricsHandler;
|
||||
import io.netty.channel.Channel;
|
||||
|
||||
|
||||
/**
|
||||
* Value object to maintain the information of single connection, like ClientID, Channel,
|
||||
@@ -40,7 +50,7 @@ public class ConnectionDescriptor {
|
||||
}
|
||||
|
||||
public final String clientID;
|
||||
public final Channel channel;
|
||||
private final Channel channel;
|
||||
public final boolean cleanSession;
|
||||
private final AtomicReference<ConnectionState> channelState = new AtomicReference<>(ConnectionState.DISCONNECTED);
|
||||
|
||||
@@ -49,20 +59,59 @@ public class ConnectionDescriptor {
|
||||
this.channel = session;
|
||||
this.cleanSession = cleanSession;
|
||||
}
|
||||
|
||||
public void writeAndFlush(Object payload) {
|
||||
this.channel.writeAndFlush(payload);
|
||||
}
|
||||
|
||||
public void setupAutoFlusher(int flushIntervalMs) {
|
||||
try {
|
||||
this.channel.pipeline().addAfter("idleEventHandler", "autoFlusher", new AutoFlushHandler(flushIntervalMs, TimeUnit.MILLISECONDS));
|
||||
} catch (NoSuchElementException nseex) {
|
||||
//the idleEventHandler is not present on the pipeline
|
||||
this.channel.pipeline().addFirst("autoFlusher", new AutoFlushHandler(flushIntervalMs, TimeUnit.MILLISECONDS));
|
||||
}
|
||||
}
|
||||
|
||||
public boolean doesNotUseChannel(Channel channel) {
|
||||
return !(this.channel.equals(channel));
|
||||
}
|
||||
|
||||
public boolean close() {
|
||||
LOG.info("Closing connection descriptor. MqttClientId = {}.", clientID);
|
||||
final boolean success = assignState(ConnectionState.INTERCEPTORS_NOTIFIED, ConnectionState.DISCONNECTED);
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
this.channel.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return NettyUtils.userName(this.channel);
|
||||
}
|
||||
|
||||
public void abort() {
|
||||
LOG.info("closing the channel");
|
||||
LOG.info("Closing connection descriptor. MqttClientId = {}.", clientID);
|
||||
// try {
|
||||
//this.channel.disconnect().sync();
|
||||
this.channel.close();//.sync();
|
||||
this.channel.close();//.sync();
|
||||
// } catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
}
|
||||
|
||||
public boolean assignState(ConnectionState expected, ConnectionState newState) {
|
||||
return channelState.compareAndSet(expected, newState);
|
||||
}
|
||||
LOG.debug("Updating state of connection descriptor. MqttClientId = {}, expectedState = {}, newState = {}.",
|
||||
clientID, expected, newState);
|
||||
boolean retval = channelState.compareAndSet(expected, newState);
|
||||
if (!retval) {
|
||||
LOG.error(
|
||||
"Unable to update state of connection descriptor. MqttclientId = {}, expectedState = {}, newState = {}.",
|
||||
clientID, expected, newState);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
@@ -84,6 +133,14 @@ public class ConnectionDescriptor {
|
||||
|
||||
}
|
||||
|
||||
public BytesMetrics getBytesMetrics() {
|
||||
return BytesMetricsHandler.getBytesMetrics(channel);
|
||||
}
|
||||
|
||||
public MessageMetrics getMessageMetrics() {
|
||||
return MessageMetricsHandler.getMessageMetrics(channel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = clientID != null ? clientID.hashCode() : 0;
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
package io.moquette.server;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.moquette.connections.IConnectionsManager;
|
||||
import io.moquette.connections.MqttConnectionMetrics;
|
||||
import io.moquette.connections.MqttSession;
|
||||
import io.moquette.connections.MqttSubscription;
|
||||
import io.moquette.parser.proto.messages.AbstractMessage;
|
||||
import io.moquette.server.netty.metrics.BytesMetrics;
|
||||
import io.moquette.server.netty.metrics.MessageMetrics;
|
||||
import io.moquette.spi.ClientSession;
|
||||
import io.moquette.spi.ISessionsStore;
|
||||
import io.moquette.spi.impl.subscriptions.Subscription;
|
||||
|
||||
public class ConnectionDescriptorStore implements IConnectionsManager {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ConnectionDescriptorStore.class);
|
||||
|
||||
private final ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors;
|
||||
private final ISessionsStore sessionsStore;
|
||||
|
||||
public ConnectionDescriptorStore(ISessionsStore sessionsStore) {
|
||||
this.connectionDescriptors = new ConcurrentHashMap<String, ConnectionDescriptor>();
|
||||
this.sessionsStore = sessionsStore;
|
||||
}
|
||||
|
||||
public boolean sendMessage(AbstractMessage message, String clientId) {
|
||||
return sendMessage(message, null, clientId);
|
||||
}
|
||||
|
||||
public boolean sendMessage(AbstractMessage message, Integer messageID, String clientID) {
|
||||
try {
|
||||
// The connection descriptors map will never be null: it's a final
|
||||
// attribute.
|
||||
|
||||
if (messageID != null) {
|
||||
LOG.info("Sending {} message. MqttClientId = {}, messageId = {}.", message.getMessageType(), clientID,
|
||||
messageID);
|
||||
} else {
|
||||
LOG.debug("Sending {} message. MqttClientId = {}.", message.getMessageType(), clientID);
|
||||
}
|
||||
|
||||
ConnectionDescriptor descriptor = connectionDescriptors.get(clientID);
|
||||
if (descriptor == null) {
|
||||
if (messageID != null) {
|
||||
LOG.error(
|
||||
"The client has just disconnected. The {} message could not be sent. MqttClientId = {}, messageId = {}.",
|
||||
message.getMessageType(), clientID, messageID);
|
||||
} else {
|
||||
LOG.error("The client has just disconnected. The {} could not be sent. MqttClientId = {}.",
|
||||
message.getMessageType(), clientID);
|
||||
}
|
||||
/*
|
||||
* If the client hast just disconnected, its connection
|
||||
* descriptor will be null. We don't have to make the broker
|
||||
* crash: we'll just discard the PUBACK message.
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
descriptor.writeAndFlush(message);
|
||||
return true;
|
||||
} catch (Throwable e) {
|
||||
if (messageID != null) {
|
||||
LOG.error(
|
||||
"Unable to send {} message. MqttClientId = {}, messageId = {}, cause = {}, errorMessage = {}.",
|
||||
message.getMessageType(), clientID, messageID, e.getCause(), e.getMessage());
|
||||
} else {
|
||||
LOG.error("Unable to send {} message. MqttClientId = {}, cause = {}, errorMessage = {}.",
|
||||
message.getMessageType(), clientID, e.getCause(), e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public ConnectionDescriptor addConnection(ConnectionDescriptor descriptor) {
|
||||
return connectionDescriptors.putIfAbsent(descriptor.clientID, descriptor);
|
||||
}
|
||||
|
||||
public boolean removeConnection(ConnectionDescriptor descriptor) {
|
||||
return connectionDescriptors.remove(descriptor.clientID, descriptor);
|
||||
}
|
||||
|
||||
public ConnectionDescriptor getConnection(String clientID) {
|
||||
return connectionDescriptors.get(clientID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConnected(String clientID) {
|
||||
return connectionDescriptors.containsKey(clientID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getActiveConnectionsNo() {
|
||||
return connectionDescriptors.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> getConnectedClientIds() {
|
||||
return connectionDescriptors.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean closeConnection(String clientID, boolean closeImmediately) {
|
||||
ConnectionDescriptor descriptor = connectionDescriptors.get(clientID);
|
||||
if (descriptor == null) {
|
||||
LOG.error(
|
||||
"The connection descriptor doesn't exist. The MQTT connection cannot be closed. MqttClientId = {}, closeImmediately = {}.",
|
||||
clientID, closeImmediately);
|
||||
return false;
|
||||
}
|
||||
if (closeImmediately) {
|
||||
descriptor.abort();
|
||||
return true;
|
||||
} else {
|
||||
return descriptor.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MqttSession getSessionStatus(String clientID) {
|
||||
LOG.info("Retrieving status of session. MqttClientId = {}.", clientID);
|
||||
ClientSession session = sessionsStore.sessionForClient(clientID);
|
||||
if (session == null) {
|
||||
LOG.error("The given MQTT client ID doesn't have an associated session. MqttClientId = {}.", clientID);
|
||||
return null;
|
||||
}
|
||||
return buildMqttSession(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<MqttSession> getSessions() {
|
||||
LOG.info("Retrieving status of all sessions.");
|
||||
Collection<MqttSession> result = new ArrayList<MqttSession>();
|
||||
for (ClientSession session : sessionsStore.getAllSessions()) {
|
||||
result.add(buildMqttSession(session));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private MqttSession buildMqttSession(ClientSession session) {
|
||||
MqttSession result = new MqttSession();
|
||||
Collection<MqttSubscription> mqttSubscriptions = new ArrayList<>();
|
||||
for (Subscription subscription : session.getSubscriptions()) {
|
||||
mqttSubscriptions.add(new MqttSubscription(subscription.getRequestedQos().toString(),
|
||||
subscription.getClientId(), subscription.getTopicFilter(), subscription.isActive()));
|
||||
}
|
||||
result.setActiveSubscriptions(mqttSubscriptions);
|
||||
result.setCleanSession(session.isCleanSession());
|
||||
ConnectionDescriptor descriptor = this.getConnection(session.clientID);
|
||||
if (descriptor != null) {
|
||||
result.setConnectionEstablished(true);
|
||||
BytesMetrics bytesMetrics = descriptor.getBytesMetrics();
|
||||
MessageMetrics messageMetrics = descriptor.getMessageMetrics();
|
||||
result.setConnectionMetrics(new MqttConnectionMetrics(bytesMetrics.readBytes(), bytesMetrics.wroteBytes(),
|
||||
messageMetrics.messagesRead(), messageMetrics.messagesWrote()));
|
||||
} else {
|
||||
result.setConnectionEstablished(false);
|
||||
}
|
||||
result.setPendingPublishMessagesNo(session.getPendingPublishMessagesNo());
|
||||
result.setSecondPhaseAckPendingMessages(session.getSecondPhaseAckPendingMessages());
|
||||
result.setInflightMessages(session.getInflightMessagesNo());
|
||||
LOG.info("The status of the session has been retrieved successfully. MqttClientId = {}.", session.clientID);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -49,11 +49,12 @@ class DefaultMoquetteSslContextCreator implements ISslContextCreator {
|
||||
|
||||
@Override
|
||||
public SSLContext initSSLContext() {
|
||||
LOG.info("Checking SSL configuration properties...");
|
||||
final String jksPath = props.getProperty(BrokerConstants.JKS_PATH_PROPERTY_NAME);
|
||||
LOG.info("Starting SSL using keystore at {}", jksPath);
|
||||
LOG.info("Initializing SSL context. KeystorePath = {}.", jksPath);
|
||||
if (jksPath == null || jksPath.isEmpty()) {
|
||||
//key_store_password or key_manager_password are empty
|
||||
LOG.warn("You have configured the SSL port but not the jks_path, SSL not started");
|
||||
LOG.warn("The keystore path is null or empty. The SSL context won't be initialized.");
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -63,12 +64,12 @@ class DefaultMoquetteSslContextCreator implements ISslContextCreator {
|
||||
final String keyManagerPassword = props.getProperty(BrokerConstants.KEY_MANAGER_PASSWORD_PROPERTY_NAME);
|
||||
if (keyStorePassword == null || keyStorePassword.isEmpty()) {
|
||||
//key_store_password or key_manager_password are empty
|
||||
LOG.warn("You have configured the SSL port but not the key_store_password, SSL not started");
|
||||
LOG.warn("The keystore password is null or empty. The SSL context won't be initialized.");
|
||||
return null;
|
||||
}
|
||||
if (keyManagerPassword == null || keyManagerPassword.isEmpty()) {
|
||||
//key_manager_password or key_manager_password are empty
|
||||
LOG.warn("You have configured the SSL port but not the key_manager_password, SSL not started");
|
||||
LOG.warn("The key manager password is null or empty. The SSL context won't be initialized.");
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -78,26 +79,33 @@ class DefaultMoquetteSslContextCreator implements ISslContextCreator {
|
||||
boolean needsClientAuth = Boolean.valueOf(sNeedsClientAuth);
|
||||
|
||||
try {
|
||||
LOG.info("Loading keystore. KeystorePath = {}.", jksPath);
|
||||
InputStream jksInputStream = jksDatastore(jksPath);
|
||||
SSLContext serverContext = SSLContext.getInstance("TLS");
|
||||
final KeyStore ks = KeyStore.getInstance("JKS");
|
||||
ks.load(jksInputStream, keyStorePassword.toCharArray());
|
||||
LOG.info("Initializing key manager...");
|
||||
final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
||||
kmf.init(ks, keyManagerPassword.toCharArray());
|
||||
TrustManager[] trustManagers = null;
|
||||
if (needsClientAuth) {
|
||||
LOG.warn(
|
||||
"Client authentication is enabled. The keystore will be used as a truststore. KeystorePath = {}.",
|
||||
jksPath);
|
||||
// use keystore as truststore, as server needs to trust certificates signed by the server certificates
|
||||
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||
tmf.init(ks);
|
||||
trustManagers = tmf.getTrustManagers();
|
||||
}
|
||||
// init sslContext
|
||||
LOG.info("Initializing SSL context...");
|
||||
serverContext.init(kmf.getKeyManagers(), trustManagers, null);
|
||||
|
||||
LOG.info("The SSL context has been initialized successfully.");
|
||||
|
||||
return serverContext;
|
||||
} catch (NoSuchAlgorithmException | UnrecoverableKeyException | CertificateException | KeyStoreException
|
||||
| KeyManagementException | IOException ex) {
|
||||
LOG.error("Can't start SSL layer!", ex);
|
||||
LOG.error("Unable to initialize SSL context. Cause = {}, errorMessage = {}.", ex.getCause(), ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -108,13 +116,13 @@ class DefaultMoquetteSslContextCreator implements ISslContextCreator {
|
||||
LOG.info("Starting with jks at {}, jks normal {}", jksUrl.toExternalForm(), jksUrl);
|
||||
return getClass().getClassLoader().getResourceAsStream(jksPath);
|
||||
}
|
||||
LOG.info("jks not found in bundled resources, try on the filesystem");
|
||||
LOG.warn("No keystore has been found in the bundled resources. Scanning filesystem...");
|
||||
File jksFile = new File(jksPath);
|
||||
if (jksFile.exists()) {
|
||||
LOG.info("Using {} ", jksFile.getAbsolutePath());
|
||||
LOG.info("Loading external keystore. Url = {}.", jksFile.getAbsolutePath());
|
||||
return new FileInputStream(jksFile);
|
||||
}
|
||||
LOG.warn("File {} doesn't exists", jksFile.getAbsolutePath());
|
||||
LOG.warn("The keystore file does not exist. Url = {}.", jksFile.getAbsolutePath());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.hazelcast.core.HazelcastInstance;
|
||||
import com.hazelcast.core.HazelcastInstanceNotActiveException;
|
||||
import com.hazelcast.core.ITopic;
|
||||
import io.moquette.BrokerConstants;
|
||||
import io.moquette.connections.IConnectionsManager;
|
||||
import io.moquette.interception.HazelcastInterceptHandler;
|
||||
import io.moquette.interception.HazelcastMsg;
|
||||
import io.moquette.interception.InterceptHandler;
|
||||
@@ -45,6 +46,8 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import static io.moquette.logging.LoggingUtils.getInterceptorIds;
|
||||
|
||||
/**
|
||||
* Launch a configured version of the server.
|
||||
* @author andrea
|
||||
@@ -84,7 +87,9 @@ public class Server {
|
||||
* @throws IOException in case of any IO error.
|
||||
*/
|
||||
public void startServer() throws IOException {
|
||||
IResourceLoader filesystemLoader = new FileResourceLoader(defaultConfigFile());
|
||||
File defaultConfigurationFile = defaultConfigFile();
|
||||
LOG.info("Starting Moquette server. Configuration file path = {}.", defaultConfigurationFile.getAbsolutePath());
|
||||
IResourceLoader filesystemLoader = new FileResourceLoader(defaultConfigurationFile);
|
||||
final IConfig config = new ResourceLoaderConfig(filesystemLoader);
|
||||
startServer(config);
|
||||
}
|
||||
@@ -100,7 +105,7 @@ public class Server {
|
||||
* @throws IOException in case of any IO Error.
|
||||
*/
|
||||
public void startServer(File configFile) throws IOException {
|
||||
LOG.info("Using m_config file: " + configFile.getAbsolutePath());
|
||||
LOG.info("Starting Moquette server. Configuration file path = {}.", configFile.getAbsolutePath());
|
||||
IResourceLoader filesystemLoader = new FileResourceLoader(configFile);
|
||||
final IConfig config = new ResourceLoaderConfig(filesystemLoader);
|
||||
startServer(config);
|
||||
@@ -119,6 +124,7 @@ public class Server {
|
||||
* @throws IOException in case of any IO Error.
|
||||
*/
|
||||
public void startServer(Properties configProps) throws IOException {
|
||||
LOG.info("Starting Moquette server using properties object...");
|
||||
final IConfig config = new MemoryConfig(configProps);
|
||||
startServer(config);
|
||||
}
|
||||
@@ -129,6 +135,7 @@ public class Server {
|
||||
* @throws IOException in case of any IO Error.
|
||||
*/
|
||||
public void startServer(IConfig config) throws IOException {
|
||||
LOG.info("Starting Moquette server using IConfig instance...");
|
||||
startServer(config, null);
|
||||
}
|
||||
|
||||
@@ -140,6 +147,7 @@ public class Server {
|
||||
* @throws IOException in case of any IO Error.
|
||||
* */
|
||||
public void startServer(IConfig config, List<? extends InterceptHandler> handlers) throws IOException {
|
||||
LOG.info("Starting moquette server using IConfig instance and intercept handlers...");
|
||||
startServer(config, handlers, null, null, null);
|
||||
}
|
||||
|
||||
@@ -149,29 +157,37 @@ public class Server {
|
||||
if (handlers == null) {
|
||||
handlers = Collections.emptyList();
|
||||
}
|
||||
LOG.info("Starting Moquette Server. MQTT message interceptors = {}.", getInterceptorIds(handlers));
|
||||
|
||||
final String handlerProp = System.getProperty(BrokerConstants.INTERCEPT_HANDLER_PROPERTY_NAME);
|
||||
if (handlerProp != null) {
|
||||
config.setProperty(BrokerConstants.INTERCEPT_HANDLER_PROPERTY_NAME, handlerProp);
|
||||
}
|
||||
configureCluster(config);
|
||||
LOG.info("Persistent store file: {}", config.getProperty(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME));
|
||||
LOG.info("Configuring Using persistent store file. Path = {}.", config.getProperty(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME));
|
||||
m_processorBootstrapper = new ProtocolProcessorBootstrapper();
|
||||
LOG.info("Initializing MQTT protocol processor...");
|
||||
final ProtocolProcessor processor = m_processorBootstrapper.init(config, handlers, authenticator, authorizator, this);
|
||||
|
||||
if (sslCtxCreator == null) {
|
||||
LOG.warn("Using default SSL context creator...");
|
||||
sslCtxCreator = new DefaultMoquetteSslContextCreator(config);
|
||||
}
|
||||
|
||||
LOG.info("Binding server to the configured ports...");
|
||||
m_acceptor = new NettyAcceptor();
|
||||
m_acceptor.initialize(processor, config, sslCtxCreator);
|
||||
m_processor = processor;
|
||||
|
||||
LOG.info("The Moquette server has been initialized successfully.");
|
||||
m_initialized = true;
|
||||
}
|
||||
|
||||
private void configureCluster(IConfig config) throws FileNotFoundException {
|
||||
LOG.info("Configuring embedded Hazelcast instance...");
|
||||
String interceptHandlerClassname = config.getProperty(BrokerConstants.INTERCEPT_HANDLER_PROPERTY_NAME);
|
||||
if (interceptHandlerClassname == null || !HZ_INTERCEPT_HANDLER.equals(interceptHandlerClassname)) {
|
||||
LOG.info("There are no Hazelcast intercept handlers. The server won't start a Hazelcast instance.");
|
||||
return;
|
||||
}
|
||||
String hzConfigPath = config.getProperty(BrokerConstants.HAZELCAST_CONFIGURATION);
|
||||
@@ -180,16 +196,17 @@ public class Server {
|
||||
Config hzconfig = isHzConfigOnClasspath ?
|
||||
new ClasspathXmlConfig(hzConfigPath) :
|
||||
new FileSystemXmlConfig(hzConfigPath);
|
||||
LOG.info(String.format("starting server with hazelcast configuration file : %s",hzconfig));
|
||||
LOG.info("Starting Hazelcast instance. ConfigurationFile = {}.",hzconfig);
|
||||
hazelcastInstance = Hazelcast.newHazelcastInstance(hzconfig);
|
||||
} else {
|
||||
LOG.info("starting server with hazelcast default file");
|
||||
LOG.info("Starting Hazelcast instance with default configuration.");
|
||||
hazelcastInstance = Hazelcast.newHazelcastInstance();
|
||||
}
|
||||
listenOnHazelCastMsg();
|
||||
}
|
||||
|
||||
private void listenOnHazelCastMsg() {
|
||||
LOG.info("Subscribing to Hazelcast topic. TopicName = {}.", "moquette");
|
||||
HazelcastInstance hz = getHazelcastInstance();
|
||||
ITopic<HazelcastMsg> topic = hz.getTopic("moquette");
|
||||
topic.addMessageListener(new HazelcastListener(this));
|
||||
@@ -208,24 +225,31 @@ public class Server {
|
||||
* */
|
||||
public void internalPublish(PublishMessage msg) {
|
||||
if (!m_initialized) {
|
||||
throw new IllegalStateException("Can't publish on a server is not yet started");
|
||||
LOG.error("The server is not started. The message cannot be published. MqttClientId = {}, messageId = {}.",
|
||||
msg.getClientId(), msg.getMessageID());
|
||||
throw new IllegalStateException("Can't publish on a server is not yet started");
|
||||
}
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Publishing message. MqttClientId = {}, messageId = {}.", msg.getClientId(), msg.getMessageID());
|
||||
}
|
||||
m_processor.internalPublish(msg);
|
||||
}
|
||||
|
||||
public void stopServer() {
|
||||
LOG.info("Server stopping...");
|
||||
LOG.info("Unbinding server from the configured ports...");
|
||||
m_acceptor.close();
|
||||
LOG.info("Stopping MQTT protocol processor...");
|
||||
m_processorBootstrapper.shutdown();
|
||||
m_initialized = false;
|
||||
if (hazelcastInstance != null) {
|
||||
LOG.info("Stopping embedded Hazelcast instance...");
|
||||
try {
|
||||
hazelcastInstance.shutdown();
|
||||
} catch (HazelcastInstanceNotActiveException e) {
|
||||
LOG.info("hazelcast already shutdown");
|
||||
LOG.warn("The embedded Hazelcast instance is already shut down.");
|
||||
}
|
||||
}
|
||||
LOG.info("Server stopped");
|
||||
LOG.info("The Moquette server has been stopped.");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,11 +270,15 @@ public class Server {
|
||||
* @param interceptHandler the handler to add.
|
||||
* @return true id operation was successful.
|
||||
* */
|
||||
public boolean addInterceptHandler(InterceptHandler interceptHandler) {
|
||||
public void addInterceptHandler(InterceptHandler interceptHandler) {
|
||||
if (!m_initialized) {
|
||||
LOG.error("The server is not started. The MQTT message interceptor cannot be added. InterceptorId = {}.",
|
||||
interceptHandler.getID());
|
||||
throw new IllegalStateException("Can't register interceptors on a server that is not yet started");
|
||||
|
||||
}
|
||||
return m_processor.addInterceptHandler(interceptHandler);
|
||||
LOG.info("Adding MQTT message interceptor. InterceptorId = {}.", interceptHandler.getID());
|
||||
m_processor.addInterceptHandler(interceptHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -258,11 +286,23 @@ public class Server {
|
||||
* @param interceptHandler the handler to remove.
|
||||
* @return true id operation was successful.
|
||||
* */
|
||||
public boolean removeInterceptHandler(InterceptHandler interceptHandler) {
|
||||
public void removeInterceptHandler(InterceptHandler interceptHandler) {
|
||||
if (!m_initialized) {
|
||||
LOG.error("The server is not started. The MQTT message interceptor cannot be removed. InterceptorId = {}.",
|
||||
interceptHandler.getID());
|
||||
throw new IllegalStateException("Can't deregister interceptors from a server that is not yet started");
|
||||
}
|
||||
return m_processor.removeInterceptHandler(interceptHandler);
|
||||
LOG.info("Removing MQTT message interceptor. InterceptorId = {}.", interceptHandler.getID());
|
||||
m_processor.removeInterceptHandler(interceptHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the connections manager of this broker.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public IConnectionsManager getConnectionsManager() {
|
||||
return m_processorBootstrapper.getConnectionDescriptors();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,8 +4,13 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ClasspathResourceLoader implements IResourceLoader {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ClasspathResourceLoader.class);
|
||||
|
||||
private final String defaultResource;
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
@@ -29,6 +34,7 @@ public class ClasspathResourceLoader implements IResourceLoader {
|
||||
|
||||
@Override
|
||||
public Reader loadResource(String relativePath) {
|
||||
LOG.info("Loading resource. RelativePath = {}.", relativePath);
|
||||
InputStream is = this.classLoader.getResourceAsStream(relativePath);
|
||||
return is != null ? new InputStreamReader(is) : null;
|
||||
}
|
||||
|
||||
@@ -5,8 +5,13 @@ import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.Reader;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class FileResourceLoader implements IResourceLoader {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FileResourceLoader.class);
|
||||
|
||||
private final File defaultFile;
|
||||
private final String parentPath;
|
||||
|
||||
@@ -42,12 +47,15 @@ public class FileResourceLoader implements IResourceLoader {
|
||||
}
|
||||
|
||||
public Reader loadResource(File f) {
|
||||
LOG.info("Loading file. Path = {}.", f.getAbsolutePath());
|
||||
if (f.isDirectory()) {
|
||||
LOG.error("The given file is a directory. Path = {}.", f.getAbsolutePath());
|
||||
throw new ResourceIsDirectoryException("File \"" + f + "\" is a directory!");
|
||||
}
|
||||
try {
|
||||
return new FileReader(f);
|
||||
} catch (FileNotFoundException e) {
|
||||
LOG.error("The file does not exist. Path = {}.", f.getAbsolutePath());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,9 @@ public interface IResourceLoader {
|
||||
String getName();
|
||||
|
||||
class ResourceIsDirectoryException extends RuntimeException {
|
||||
public ResourceIsDirectoryException(String message) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public ResourceIsDirectoryException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,20 +37,40 @@ public class ResourceLoaderConfig extends IConfig {
|
||||
}
|
||||
|
||||
public ResourceLoaderConfig(IResourceLoader resourceLoader, String configName) {
|
||||
LOG.info("Loading configuration. ResourceLoader = {}, configName = {}.", resourceLoader.getName(), configName);
|
||||
this.resourceLoader = resourceLoader;
|
||||
Reader configReader = configName != null ? resourceLoader.loadResource(configName) : resourceLoader.loadDefaultResource();
|
||||
if (configReader == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Can't locate " + resourceLoader.getName() + " \"" + configName + "\"");
|
||||
|
||||
/*
|
||||
* If we use a conditional operator, the loadResource() and the
|
||||
* loadDefaultResource() methods will be always called. This makes the
|
||||
* log traces confusing.
|
||||
*/
|
||||
|
||||
Reader configReader;
|
||||
if (configName != null) {
|
||||
configReader = resourceLoader.loadResource(configName);
|
||||
} else {
|
||||
configReader = resourceLoader.loadDefaultResource();
|
||||
}
|
||||
|
||||
if (configReader == null) {
|
||||
LOG.error("The resource loader returned no configuration reader. ResourceLoader = {}, configName = {}.",
|
||||
resourceLoader.getName(), configName);
|
||||
throw new IllegalArgumentException("Can't locate " + resourceLoader.getName() + " \"" + configName + "\"");
|
||||
}
|
||||
|
||||
LOG.info("Parsing configuration properties. ResourceLoader = {}, configName = {}.", resourceLoader.getName(),
|
||||
configName);
|
||||
ConfigurationParser confParser = new ConfigurationParser();
|
||||
m_properties = confParser.getProperties();
|
||||
assignDefaults();
|
||||
try {
|
||||
confParser.parse(configReader);
|
||||
} catch (ParseException pex) {
|
||||
LOG.warn("An error occurred in parsing configuration, fallback on default configuration", pex);
|
||||
}
|
||||
try {
|
||||
confParser.parse(configReader);
|
||||
} catch (ParseException pex) {
|
||||
LOG.warn(
|
||||
"Unable to parse configuration properties. Using default configuration. ResourceLoader = {}, configName = {}, cause = {}, errorMessage = {}.",
|
||||
resourceLoader.getName(), configName, pex.getCause(), pex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,9 +17,11 @@ package io.moquette.server.netty;
|
||||
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.timeout.IdleStateEvent;
|
||||
import io.netty.util.concurrent.EventExecutor;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -31,6 +33,7 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
public class AutoFlushHandler extends ChannelDuplexHandler {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(AutoFlushHandler.class);
|
||||
private static final long MIN_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(1);
|
||||
|
||||
private final long writerIdleTimeNanos;
|
||||
@@ -109,6 +112,9 @@ public class AutoFlushHandler extends ChannelDuplexHandler {
|
||||
private void initialize(ChannelHandlerContext ctx) {
|
||||
// Avoid the case where destroy() is called before scheduling timeouts.
|
||||
// See: https://github.com/netty/netty/issues/143
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Initializing autoflush handler. MqttClientId = {}.", NettyUtils.clientID(ctx.channel()));
|
||||
}
|
||||
switch (state) {
|
||||
case 1:
|
||||
case 2:
|
||||
@@ -142,6 +148,9 @@ public class AutoFlushHandler extends ChannelDuplexHandler {
|
||||
*/
|
||||
protected void channelIdle(ChannelHandlerContext ctx/*, IdleStateEvent evt*/) throws Exception {
|
||||
// ctx.fireUserEventTriggered(evt);
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Flushing idle Netty channel. MqttClientId = {}.", NettyUtils.clientID(ctx.channel()));
|
||||
}
|
||||
ctx.channel().flush();
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package io.moquette.server.netty;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandler.Sharable;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
@@ -23,16 +26,24 @@ import io.netty.handler.timeout.IdleStateEvent;
|
||||
|
||||
@Sharable
|
||||
class MoquetteIdleTimeoutHandler extends ChannelDuplexHandler {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MoquetteIdleTimeoutHandler.class);
|
||||
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
||||
if (evt instanceof IdleStateEvent) {
|
||||
IdleState e = ((IdleStateEvent) evt).state();
|
||||
if (e == IdleState.ALL_IDLE) {
|
||||
LOG.info("Firing channel inactive event. MqttClientId = {}.", NettyUtils.clientID(ctx.channel()));
|
||||
//fire a channelInactive to trigger publish of Will
|
||||
ctx.fireChannelInactive();
|
||||
ctx.close();
|
||||
}
|
||||
} else {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Firing Netty event. MqttClientId = {}, eventClass = {}.", NettyUtils.clientID(ctx.channel()),
|
||||
evt.getClass().getName());
|
||||
}
|
||||
super.userEventTriggered(ctx, evt);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ package io.moquette.server.netty;
|
||||
|
||||
import io.moquette.BrokerConstants;
|
||||
import static io.moquette.BrokerConstants.*;
|
||||
import io.moquette.parser.commons.Constants;
|
||||
import io.moquette.parser.netty.MQTTDecoder;
|
||||
import io.moquette.parser.netty.MQTTEncoder;
|
||||
import io.moquette.spi.security.ISslContextCreator;
|
||||
@@ -38,7 +37,6 @@ import io.netty.handler.codec.http.HttpRequestDecoder;
|
||||
import io.netty.handler.codec.http.HttpResponseEncoder;
|
||||
import io.netty.handler.codec.http.HttpServerCodec;
|
||||
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
|
||||
import io.netty.handler.codec.http.websocketx.Utf8FrameValidator;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
||||
import io.netty.handler.ssl.SslHandler;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
@@ -50,6 +48,7 @@ import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLEngine;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -95,8 +94,23 @@ public class NettyAcceptor implements ServerAcceptor {
|
||||
BytesMetricsCollector m_bytesMetricsCollector = new BytesMetricsCollector();
|
||||
MessageMetricsCollector m_metricsCollector = new MessageMetricsCollector();
|
||||
|
||||
private int nettySoBacklog;
|
||||
private boolean nettySoReuseaddr;
|
||||
private boolean nettyTcpNodelay;
|
||||
private boolean nettySoKeepalive;
|
||||
private int nettyChannelTimeoutSeconds;
|
||||
|
||||
@Override
|
||||
public void initialize(ProtocolProcessor processor, IConfig props, ISslContextCreator sslCtxCreator) throws IOException {
|
||||
LOG.info("Initializing Netty acceptor...");
|
||||
|
||||
nettySoBacklog = Integer.parseInt(props.getProperty(BrokerConstants.NETTY_SO_BACKLOG_PROPERTY_NAME, "128"));
|
||||
nettySoReuseaddr = Boolean.parseBoolean(props.getProperty(BrokerConstants.NETTY_SO_REUSEADDR_PROPERTY_NAME, "true"));
|
||||
nettyTcpNodelay = Boolean.parseBoolean(props.getProperty(BrokerConstants.NETTY_TCP_NODELAY_PROPERTY_NAME, "true"));
|
||||
nettySoKeepalive = Boolean.parseBoolean(props.getProperty(BrokerConstants.NETTY_SO_KEEPALIVE_PROPERTY_NAME, "true"));
|
||||
nettyChannelTimeoutSeconds = Integer
|
||||
.parseInt(props.getProperty(BrokerConstants.NETTY_CHANNEL_TIMEOUT_SECONDS_PROPERTY_NAME, "10"));
|
||||
|
||||
m_bossGroup = new NioEventLoopGroup();
|
||||
m_workerGroup = new NioEventLoopGroup();
|
||||
final NettyMQTTHandler handler = new NettyMQTTHandler(processor);
|
||||
@@ -116,7 +130,8 @@ public class NettyAcceptor implements ServerAcceptor {
|
||||
}
|
||||
}
|
||||
|
||||
private void initFactory(String host, int port, final PipelineInitializer pipeliner) {
|
||||
private void initFactory(String host, int port, String protocol, final PipelineInitializer pipeliner) {
|
||||
LOG.info("Initializing server. Protocol = {}.", protocol);
|
||||
ServerBootstrap b = new ServerBootstrap();
|
||||
b.group(m_bossGroup, m_workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
@@ -132,33 +147,35 @@ public class NettyAcceptor implements ServerAcceptor {
|
||||
}
|
||||
}
|
||||
})
|
||||
.option(ChannelOption.SO_BACKLOG, 128)
|
||||
.option(ChannelOption.SO_REUSEADDR, true)
|
||||
.option(ChannelOption.TCP_NODELAY, true)
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, true);
|
||||
.option(ChannelOption.SO_BACKLOG, nettySoBacklog)
|
||||
.option(ChannelOption.SO_REUSEADDR, nettySoReuseaddr)
|
||||
.option(ChannelOption.TCP_NODELAY, nettyTcpNodelay)
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, nettySoKeepalive);
|
||||
try {
|
||||
LOG.info("Binding server. Protocol = {}, host = {}, port = {}.", host, port);
|
||||
// Bind and start to accept incoming connections.
|
||||
ChannelFuture f = b.bind(host, port);
|
||||
LOG.info("Server binded host: {}, port: {}", host, port);
|
||||
LOG.info("The server has been binded. Protocol = {}, host = {}, port = {}", host, port);
|
||||
f.sync();
|
||||
} catch (InterruptedException ex) {
|
||||
LOG.error(null, ex);
|
||||
LOG.error("An interruptedException was caught while initializing server. Protocol = {}.", protocol, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void initializePlainTCPTransport(final NettyMQTTHandler handler, IConfig props) throws IOException {
|
||||
LOG.info("Configuring TCP MQTT transport...");
|
||||
final MoquetteIdleTimeoutHandler timeoutHandler = new MoquetteIdleTimeoutHandler();
|
||||
String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME);
|
||||
String tcpPortProp = props.getProperty(PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
|
||||
if (DISABLED_PORT_BIND.equals(tcpPortProp)) {
|
||||
LOG.info("tcp MQTT is disabled because the value for the property with key {}", BrokerConstants.PORT_PROPERTY_NAME);
|
||||
LOG.info("The property {} has been setted to {}. TCP MQTT will be disabled.", BrokerConstants.PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
|
||||
return;
|
||||
}
|
||||
int port = Integer.parseInt(tcpPortProp);
|
||||
initFactory(host, port, new PipelineInitializer() {
|
||||
initFactory(host, port, "TCP MQTT", new PipelineInitializer() {
|
||||
@Override
|
||||
void init(ChannelPipeline pipeline) {
|
||||
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
|
||||
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, nettyChannelTimeoutSeconds));
|
||||
pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
|
||||
// pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
|
||||
pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector));
|
||||
@@ -172,10 +189,12 @@ public class NettyAcceptor implements ServerAcceptor {
|
||||
}
|
||||
|
||||
private void initializeWebSocketTransport(final NettyMQTTHandler handler, IConfig props) throws IOException {
|
||||
LOG.info("Configuring Websocket MQTT transport...");
|
||||
String webSocketPortProp = props.getProperty(WEB_SOCKET_PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
|
||||
if (DISABLED_PORT_BIND.equals(webSocketPortProp)) {
|
||||
//Do nothing no WebSocket configured
|
||||
LOG.info("WebSocket is disabled");
|
||||
LOG.info("The property {} has been setted to {}. Websocket MQTT will be disabled.",
|
||||
BrokerConstants.WEB_SOCKET_PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
|
||||
return;
|
||||
}
|
||||
int port = Integer.parseInt(webSocketPortProp);
|
||||
@@ -183,7 +202,7 @@ public class NettyAcceptor implements ServerAcceptor {
|
||||
final MoquetteIdleTimeoutHandler timeoutHandler = new MoquetteIdleTimeoutHandler();
|
||||
|
||||
String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME);
|
||||
initFactory(host, port, new PipelineInitializer() {
|
||||
initFactory(host, port, "Websocket MQTT", new PipelineInitializer() {
|
||||
@Override
|
||||
void init(ChannelPipeline pipeline) {
|
||||
pipeline.addLast(new HttpServerCodec());
|
||||
@@ -191,7 +210,7 @@ public class NettyAcceptor implements ServerAcceptor {
|
||||
pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler("/mqtt", MQTT_SUBPROTOCOL_CSV_LIST));
|
||||
pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
|
||||
pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
|
||||
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
|
||||
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, nettyChannelTimeoutSeconds));
|
||||
pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
|
||||
pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector));
|
||||
pipeline.addLast("decoder", new MQTTDecoder());
|
||||
@@ -204,10 +223,12 @@ public class NettyAcceptor implements ServerAcceptor {
|
||||
}
|
||||
|
||||
private void initializeSSLTCPTransport(final NettyMQTTHandler handler, IConfig props, final SSLContext sslContext) throws IOException {
|
||||
String sslPortProp = props.getProperty(SSL_PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
|
||||
LOG.info("Configuring SSL MQTT transport...");
|
||||
String sslPortProp = props.getProperty(SSL_PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
|
||||
if (DISABLED_PORT_BIND.equals(sslPortProp)) {
|
||||
//Do nothing no SSL configured
|
||||
LOG.info("SSL MQTT is disabled because there is no value in properties for key {}", BrokerConstants.SSL_PORT_PROPERTY_NAME);
|
||||
LOG.info("The property {} has been setted to {}. SSL MQTT will be disabled.",
|
||||
BrokerConstants.SSL_PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -218,11 +239,11 @@ public class NettyAcceptor implements ServerAcceptor {
|
||||
String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME);
|
||||
String sNeedsClientAuth = props.getProperty(BrokerConstants.NEED_CLIENT_AUTH, "false");
|
||||
final boolean needsClientAuth = Boolean.valueOf(sNeedsClientAuth);
|
||||
initFactory(host, sslPort, new PipelineInitializer() {
|
||||
initFactory(host, sslPort, "SSL MQTT", new PipelineInitializer() {
|
||||
@Override
|
||||
void init(ChannelPipeline pipeline) throws Exception {
|
||||
pipeline.addLast("ssl", createSslHandler(sslContext, needsClientAuth));
|
||||
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
|
||||
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, nettyChannelTimeoutSeconds));
|
||||
pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
|
||||
//pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
|
||||
pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector));
|
||||
@@ -236,10 +257,12 @@ public class NettyAcceptor implements ServerAcceptor {
|
||||
}
|
||||
|
||||
private void initializeWSSTransport(final NettyMQTTHandler handler, IConfig props, final SSLContext sslContext) throws IOException {
|
||||
String sslPortProp = props.getProperty(WSS_PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
|
||||
LOG.info("Configuring secure websocket MQTT transport...");
|
||||
String sslPortProp = props.getProperty(WSS_PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
|
||||
if (DISABLED_PORT_BIND.equals(sslPortProp)) {
|
||||
//Do nothing no SSL configured
|
||||
LOG.info("SSL websocket is disabled because there is no value in properties for key {}", BrokerConstants.WSS_PORT_PROPERTY_NAME);
|
||||
LOG.info("The property {} has been setted to {}. Secure websocket MQTT will be disabled.",
|
||||
BrokerConstants.WSS_PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
|
||||
return;
|
||||
}
|
||||
int sslPort = Integer.parseInt(sslPortProp);
|
||||
@@ -247,7 +270,7 @@ public class NettyAcceptor implements ServerAcceptor {
|
||||
String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME);
|
||||
String sNeedsClientAuth = props.getProperty(BrokerConstants.NEED_CLIENT_AUTH, "false");
|
||||
final boolean needsClientAuth = Boolean.valueOf(sNeedsClientAuth);
|
||||
initFactory(host, sslPort, new PipelineInitializer() {
|
||||
initFactory(host, sslPort, "Secure websocket", new PipelineInitializer() {
|
||||
@Override
|
||||
void init(ChannelPipeline pipeline) throws Exception {
|
||||
pipeline.addLast("ssl", createSslHandler(sslContext, needsClientAuth));
|
||||
@@ -257,7 +280,7 @@ public class NettyAcceptor implements ServerAcceptor {
|
||||
pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler("/mqtt", MQTT_SUBPROTOCOL_CSV_LIST));
|
||||
pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
|
||||
pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
|
||||
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
|
||||
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, nettyChannelTimeoutSeconds));
|
||||
pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
|
||||
pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector));
|
||||
pipeline.addLast("decoder", new MQTTDecoder());
|
||||
@@ -270,32 +293,45 @@ public class NettyAcceptor implements ServerAcceptor {
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (m_workerGroup == null) {
|
||||
throw new IllegalStateException("Invoked close on an Acceptor that wasn't initialized");
|
||||
}
|
||||
if (m_bossGroup == null) {
|
||||
throw new IllegalStateException("Invoked close on an Acceptor that wasn't initialized");
|
||||
}
|
||||
Future workerWaiter = m_workerGroup.shutdownGracefully();
|
||||
Future bossWaiter = m_bossGroup.shutdownGracefully();
|
||||
|
||||
LOG.info("Closing Netty acceptor...");
|
||||
if (m_workerGroup == null || m_bossGroup == null) {
|
||||
LOG.error("The Netty acceptor is not initialized.");
|
||||
throw new IllegalStateException("Invoked close on an Acceptor that wasn't initialized");
|
||||
}
|
||||
Future<?> workerWaiter = m_workerGroup.shutdownGracefully();
|
||||
Future<?> bossWaiter = m_bossGroup.shutdownGracefully();
|
||||
|
||||
/*
|
||||
* We shouldn't raise an IllegalStateException if we are interrupted. If
|
||||
* we did so, the broker is not shut down properly.
|
||||
*/
|
||||
LOG.info("Waiting for worker and boss event loop groups to terminate...");
|
||||
try {
|
||||
workerWaiter.await(100);
|
||||
workerWaiter.await(10, TimeUnit.SECONDS);
|
||||
bossWaiter.await(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException iex) {
|
||||
throw new IllegalStateException(iex);
|
||||
LOG.warn("An InterruptedException was caught while waiting for event loops to terminate...");
|
||||
}
|
||||
|
||||
try {
|
||||
bossWaiter.await(100);
|
||||
} catch (InterruptedException iex) {
|
||||
throw new IllegalStateException(iex);
|
||||
|
||||
if (!m_workerGroup.isTerminated()) {
|
||||
LOG.warn("Forcing shutdown of worker event loop...");
|
||||
m_workerGroup.shutdownGracefully(0L, 0L, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
if (!m_bossGroup.isTerminated()) {
|
||||
LOG.warn("Forcing shutdown of boss event loop...");
|
||||
m_bossGroup.shutdownGracefully(0L, 0L, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
LOG.info("Collecting message metrics...");
|
||||
MessageMetrics metrics = m_metricsCollector.computeMetrics();
|
||||
LOG.info("Msg read: {}, msg wrote: {}", metrics.messagesRead(), metrics.messagesWrote());
|
||||
LOG.info("The metrics have been collected. Read messages = {}, written messages = {}.",
|
||||
metrics.messagesRead(), metrics.messagesWrote());
|
||||
|
||||
LOG.info("Collecting bytes metrics...");
|
||||
BytesMetrics bytesMetrics = m_bytesMetricsCollector.computeMetrics();
|
||||
LOG.info(String.format("Bytes read: %d, bytes wrote: %d", bytesMetrics.readBytes(), bytesMetrics.wroteBytes()));
|
||||
LOG.info("The bytes metrics have been collected. Read bytes = {}, written bytes = {}.", bytesMetrics.readBytes(),
|
||||
bytesMetrics.wroteBytes());
|
||||
}
|
||||
|
||||
private ChannelHandler createSslHandler(SSLContext sslContext, boolean needsClientAuth) {
|
||||
|
||||
@@ -23,12 +23,10 @@ import io.netty.channel.ChannelHandler.Sharable;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
|
||||
import io.netty.handler.codec.CorruptedFrameException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -47,7 +45,10 @@ public class NettyMQTTHandler extends ChannelInboundHandlerAdapter {
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object message) {
|
||||
AbstractMessage msg = (AbstractMessage) message;
|
||||
LOG.info("Received a message of type {}", Utils.msgType2String(msg.getMessageType()));
|
||||
String messageType = Utils.msgType2String(msg.getMessageType());
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Processing MQTT message. MessageType = {}.", messageType);
|
||||
}
|
||||
try {
|
||||
switch (msg.getMessageType()) {
|
||||
case CONNECT:
|
||||
@@ -82,9 +83,11 @@ public class NettyMQTTHandler extends ChannelInboundHandlerAdapter {
|
||||
ctx.writeAndFlush(pingResp);
|
||||
break;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
LOG.error("Bad error in processing the message", ex);
|
||||
ctx.fireExceptionCaught(ex);
|
||||
} catch (Throwable ex) {
|
||||
LOG.error(
|
||||
"An unexpected exception was caught while processing MQTT message. MessageType = {}, cause = {}, errorMessage = {}.",
|
||||
messageType, ex.getCause(), ex.getMessage());
|
||||
ctx.fireExceptionCaught(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +95,7 @@ public class NettyMQTTHandler extends ChannelInboundHandlerAdapter {
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
String clientID = NettyUtils.clientID(ctx.channel());
|
||||
if (clientID != null && !clientID.isEmpty()) {
|
||||
LOG.info("Notifying connection lost event. MqttClientId = {}.", clientID);
|
||||
m_processor.processConnectionLost(clientID, ctx.channel());
|
||||
}
|
||||
ctx.close();
|
||||
@@ -99,14 +103,9 @@ public class NettyMQTTHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
if (cause instanceof CorruptedFrameException) {
|
||||
//something goes bad with decoding
|
||||
LOG.warn("Error decoding a packet, probably a bad formatted packet, message: " + cause.getMessage());
|
||||
} else if (cause instanceof IOException && "Connection reset by peer".equals(cause.getMessage())) {
|
||||
LOG.warn("Network connection closed abruptly");
|
||||
} else {
|
||||
LOG.error("Ugly error on networking", cause);
|
||||
}
|
||||
LOG.error(
|
||||
"An unexpected exception was caught while processing MQTT message. Closing Netty channel. MqttClientId = {}, cause = {}, errorMessage = {}.",
|
||||
NettyUtils.clientID(ctx.channel()), cause.getCause(), cause.getMessage());
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package io.moquette.server.netty;
|
||||
|
||||
import io.moquette.server.Constants;
|
||||
import io.moquette.spi.impl.ProtocolProcessor;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.util.Attribute;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package io.moquette.server.netty.metrics;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
@@ -34,7 +35,7 @@ public class BytesMetricsHandler extends ChannelDuplexHandler {
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
Attribute<BytesMetrics> attr = ctx.attr(ATTR_KEY_METRICS);
|
||||
Attribute<BytesMetrics> attr = ctx.channel().attr(ATTR_KEY_METRICS);
|
||||
attr.set(new BytesMetrics());
|
||||
|
||||
super.channelActive(ctx);
|
||||
@@ -42,14 +43,14 @@ public class BytesMetricsHandler extends ChannelDuplexHandler {
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
BytesMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
|
||||
BytesMetrics metrics = ctx.channel().attr(ATTR_KEY_METRICS).get();
|
||||
metrics.incrementRead(((ByteBuf)msg).readableBytes());
|
||||
ctx.fireChannelRead(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||
BytesMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
|
||||
BytesMetrics metrics = ctx.channel().attr(ATTR_KEY_METRICS).get();
|
||||
metrics.incrementWrote(((ByteBuf)msg).writableBytes());
|
||||
ctx.write(msg, promise);
|
||||
}
|
||||
@@ -58,9 +59,13 @@ public class BytesMetricsHandler extends ChannelDuplexHandler {
|
||||
@Override
|
||||
public void close(ChannelHandlerContext ctx,
|
||||
ChannelPromise promise) throws Exception {
|
||||
BytesMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
|
||||
BytesMetrics metrics = ctx.channel().attr(ATTR_KEY_METRICS).get();
|
||||
m_collector.sumReadBytes(metrics.readBytes());
|
||||
m_collector.sumWroteBytes(metrics.wroteBytes());
|
||||
super.close(ctx, promise);
|
||||
}
|
||||
|
||||
public static BytesMetrics getBytesMetrics(Channel channel) {
|
||||
return channel.attr(ATTR_KEY_METRICS).get();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package io.moquette.server.netty.metrics;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelDuplexHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
@@ -33,7 +34,7 @@ public class MessageMetricsHandler extends ChannelDuplexHandler {
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
Attribute<MessageMetrics> attr = ctx.attr(ATTR_KEY_METRICS);
|
||||
Attribute<MessageMetrics> attr = ctx.channel().attr(ATTR_KEY_METRICS);
|
||||
attr.set(new MessageMetrics());
|
||||
|
||||
super.channelActive(ctx);
|
||||
@@ -41,14 +42,14 @@ public class MessageMetricsHandler extends ChannelDuplexHandler {
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
MessageMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
|
||||
MessageMetrics metrics = ctx.channel().attr(ATTR_KEY_METRICS).get();
|
||||
metrics.incrementRead(1);
|
||||
ctx.fireChannelRead(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||
MessageMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
|
||||
MessageMetrics metrics = ctx.channel().attr(ATTR_KEY_METRICS).get();
|
||||
metrics.incrementWrote(1);
|
||||
ctx.write(msg, promise);
|
||||
}
|
||||
@@ -57,9 +58,13 @@ public class MessageMetricsHandler extends ChannelDuplexHandler {
|
||||
@Override
|
||||
public void close(ChannelHandlerContext ctx,
|
||||
ChannelPromise promise) throws Exception {
|
||||
MessageMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
|
||||
MessageMetrics metrics = ctx.channel().attr(ATTR_KEY_METRICS).get();
|
||||
m_collector.sumReadMessages(metrics.messagesRead());
|
||||
m_collector.sumWroteMessages(metrics.messagesWrote());
|
||||
super.close(ctx, promise);
|
||||
}
|
||||
|
||||
public static MessageMetrics getMessageMetrics(Channel channel) {
|
||||
return channel.attr(ATTR_KEY_METRICS).get();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user