broker/ConnectionDescriptor: code formatter used

This commit is contained in:
Andreas Mager
2017-02-01 17:13:37 +01:00
parent 6821548d50
commit 5c9d0808dd
@@ -13,15 +13,14 @@
* *
* You may elect to redistribute this code under either of these licenses. * You may elect to redistribute this code under either of these licenses.
*/ */
package io.moquette.server; package io.moquette.server;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import io.moquette.server.netty.AutoFlushHandler; import io.moquette.server.netty.AutoFlushHandler;
import io.moquette.server.netty.NettyUtils; import io.moquette.server.netty.NettyUtils;
import io.moquette.server.netty.metrics.BytesMetrics; import io.moquette.server.netty.metrics.BytesMetrics;
@@ -30,10 +29,9 @@ import io.moquette.server.netty.metrics.MessageMetrics;
import io.moquette.server.netty.metrics.MessageMetricsHandler; import io.moquette.server.netty.metrics.MessageMetricsHandler;
import io.netty.channel.Channel; import io.netty.channel.Channel;
/** /**
* Value object to maintain the information of single connection, like ClientID, Channel, * Value object to maintain the information of single connection, like ClientID, Channel, and clean
* and clean session flag. * session flag.
* *
* *
* @author andrea * @author andrea
@@ -43,10 +41,16 @@ public class ConnectionDescriptor {
private static final Logger LOG = LoggerFactory.getLogger(ConnectionDescriptor.class); private static final Logger LOG = LoggerFactory.getLogger(ConnectionDescriptor.class);
public enum ConnectionState { public enum ConnectionState {
//Connection states // Connection states
DISCONNECTED, SENDACK, SESSION_CREATED, MESSAGES_REPUBLISHED, ESTABLISHED, DISCONNECTED,
//Disconnection states SENDACK,
SUBSCRIPTIONS_REMOVED, MESSAGES_DROPPED, INTERCEPTORS_NOTIFIED; SESSION_CREATED,
MESSAGES_REPUBLISHED,
ESTABLISHED,
// Disconnection states
SUBSCRIPTIONS_REMOVED,
MESSAGES_DROPPED,
INTERCEPTORS_NOTIFIED;
} }
public final String clientID; public final String clientID;
@@ -61,74 +65,85 @@ public class ConnectionDescriptor {
} }
public void writeAndFlush(Object payload) { public void writeAndFlush(Object payload) {
this.channel.writeAndFlush(payload); this.channel.writeAndFlush(payload);
} }
public void setupAutoFlusher(int flushIntervalMs) { public void setupAutoFlusher(int flushIntervalMs) {
try { try {
this.channel.pipeline().addAfter("idleEventHandler", "autoFlusher", new AutoFlushHandler(flushIntervalMs, TimeUnit.MILLISECONDS)); this.channel.pipeline().addAfter(
"idleEventHandler",
"autoFlusher",
new AutoFlushHandler(flushIntervalMs, TimeUnit.MILLISECONDS));
} catch (NoSuchElementException nseex) { } catch (NoSuchElementException nseex) {
//the idleEventHandler is not present on the pipeline // the idleEventHandler is not present on the pipeline
this.channel.pipeline().addFirst("autoFlusher", new AutoFlushHandler(flushIntervalMs, TimeUnit.MILLISECONDS)); this.channel.pipeline()
.addFirst("autoFlusher", new AutoFlushHandler(flushIntervalMs, TimeUnit.MILLISECONDS));
} }
} }
public boolean doesNotUseChannel(Channel channel) { public boolean doesNotUseChannel(Channel channel) {
return !(this.channel.equals(channel)); return !(this.channel.equals(channel));
} }
public boolean close() { public boolean close() {
LOG.info("Closing connection descriptor. MqttClientId = {}.", clientID); LOG.info("Closing connection descriptor. MqttClientId = {}.", clientID);
final boolean success = assignState(ConnectionState.INTERCEPTORS_NOTIFIED, ConnectionState.DISCONNECTED); final boolean success = assignState(ConnectionState.INTERCEPTORS_NOTIFIED, ConnectionState.DISCONNECTED);
if (!success) { if (!success) {
return false; return false;
} }
this.channel.close(); this.channel.close();
return true; return true;
} }
public String getUsername() { public String getUsername() {
return NettyUtils.userName(this.channel); return NettyUtils.userName(this.channel);
} }
public void abort() { public void abort() {
LOG.info("Closing connection descriptor. MqttClientId = {}.", clientID); LOG.info("Closing connection descriptor. MqttClientId = {}.", clientID);
// try { // try {
//this.channel.disconnect().sync(); // this.channel.disconnect().sync();
this.channel.close();//.sync(); this.channel.close();// .sync();
// } catch (InterruptedException e) { // } catch (InterruptedException e) {
// e.printStackTrace(); // e.printStackTrace();
// } // }
} }
public boolean assignState(ConnectionState expected, ConnectionState newState) { public boolean assignState(ConnectionState expected, ConnectionState newState) {
LOG.debug("Updating state of connection descriptor. MqttClientId = {}, expectedState = {}, newState = {}.", LOG.debug(
clientID, expected, newState); "Updating state of connection descriptor. MqttClientId = {}, expectedState = {}, newState = {}.",
boolean retval = channelState.compareAndSet(expected, newState); clientID,
if (!retval) { expected,
LOG.error( newState);
"Unable to update state of connection descriptor. MqttclientId = {}, expectedState = {}, newState = {}.", boolean retval = channelState.compareAndSet(expected, newState);
clientID, expected, newState); if (!retval) {
} LOG.error(
return retval; "Unable to update state of connection descriptor."
} + " MqttclientId = {}, expectedState = {}, newState = {}.",
clientID,
expected,
newState);
}
return retval;
}
@Override @Override
public String toString() { public String toString() {
return "ConnectionDescriptor{" + "clientID=" + clientID + return "ConnectionDescriptor{" + "clientID=" + clientID + ", cleanSession=" + cleanSession + ", state="
", cleanSession=" + cleanSession + + channelState.get() + '}';
", state=" + channelState.get() +
'}';
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o)
if (o == null || getClass() != o.getClass()) return false; return true;
if (o == null || getClass() != o.getClass())
return false;
ConnectionDescriptor that = (ConnectionDescriptor) o; ConnectionDescriptor that = (ConnectionDescriptor) o;
if (clientID != null ? !clientID.equals(that.clientID) : that.clientID != null) return false; if (clientID != null ? !clientID.equals(that.clientID) : that.clientID != null)
return false;
return !(channel != null ? !channel.equals(that.channel) : that.channel != null); return !(channel != null ? !channel.equals(that.channel) : that.channel != null);
} }