Switched from Moquette's MQTT codec to Netty's MQTT codec ones
This commit is contained in:
@@ -4,9 +4,12 @@ import com.hazelcast.core.HazelcastInstance;
|
||||
import com.hazelcast.core.ITopic;
|
||||
import io.moquette.interception.messages.InterceptPublishMessage;
|
||||
import io.moquette.server.Server;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static io.moquette.spi.impl.Utils.readBytesAndRewind;
|
||||
|
||||
/**
|
||||
* Created by mackristof on 28/05/2016.
|
||||
*/
|
||||
@@ -26,7 +29,11 @@ public class HazelcastInterceptHandler extends AbstractInterceptHandler {
|
||||
|
||||
@Override
|
||||
public void onPublish(InterceptPublishMessage msg) {
|
||||
LOG.info("{} publish on {} message: {}", msg.getClientID(), msg.getTopicName(), new String(msg.getPayload().array()));
|
||||
//TODO ugly, too much array copy
|
||||
ByteBuf payload = msg.getPayload();
|
||||
byte[] payloadContent = readBytesAndRewind(payload);
|
||||
|
||||
LOG.info("{} publish on {} message: {}", msg.getClientID(), msg.getTopicName(), new String(payloadContent));
|
||||
ITopic<HazelcastMsg> topic = hz.getTopic("moquette");
|
||||
HazelcastMsg hazelcastMsg = new HazelcastMsg(msg);
|
||||
topic.publish(hazelcastMsg);
|
||||
|
||||
@@ -4,6 +4,8 @@ import io.moquette.interception.messages.InterceptPublishMessage;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import static io.moquette.spi.impl.Utils.readBytesAndRewind;
|
||||
|
||||
/**
|
||||
* Created by mackristof on 28/05/2016.
|
||||
*/
|
||||
@@ -11,22 +13,22 @@ public class HazelcastMsg implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -1431584750134928273L;
|
||||
private final String clientId;
|
||||
private final byte qos;
|
||||
private final int qos;
|
||||
private final byte[] payload;
|
||||
private final String topic;
|
||||
|
||||
public HazelcastMsg(InterceptPublishMessage msg) {
|
||||
this.clientId = msg.getClientID();
|
||||
this.topic = msg.getTopicName();
|
||||
this.qos = msg.getQos().byteValue();
|
||||
this.payload = msg.getPayload().array();
|
||||
this.qos = msg.getQos().value();
|
||||
this.payload = readBytesAndRewind(msg.getPayload());
|
||||
}
|
||||
|
||||
public String getClientId() {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public byte getQos() {
|
||||
public int getQos() {
|
||||
return qos;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,22 +15,16 @@
|
||||
*/
|
||||
package io.moquette.interception;
|
||||
|
||||
import io.moquette.interception.messages.InterceptAcknowledgedMessage;
|
||||
import io.moquette.interception.messages.InterceptConnectMessage;
|
||||
import io.moquette.interception.messages.InterceptConnectionLostMessage;
|
||||
import io.moquette.interception.messages.InterceptDisconnectMessage;
|
||||
import io.moquette.interception.messages.InterceptPublishMessage;
|
||||
import io.moquette.interception.messages.InterceptSubscribeMessage;
|
||||
import io.moquette.interception.messages.InterceptUnsubscribeMessage;
|
||||
import io.moquette.parser.proto.messages.AbstractMessage;
|
||||
import io.moquette.interception.messages.*;
|
||||
import io.moquette.spi.impl.subscriptions.Subscription;
|
||||
import io.netty.handler.codec.mqtt.MqttMessage;
|
||||
|
||||
/**
|
||||
* This interface is used to inject code for intercepting broker events.
|
||||
* <p>
|
||||
* The events can act only as observers.
|
||||
* <p>
|
||||
* Almost every method receives a subclass of {@link AbstractMessage}, except
|
||||
* Almost every method receives a subclass of {@link MqttMessage}, except
|
||||
* <code>onDisconnect</code> that receives the client id string and
|
||||
* <code>onSubscribe</code> and <code>onUnsubscribe</code> that receive a
|
||||
* {@link Subscription} object.
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
package io.moquette.interception;
|
||||
|
||||
import io.moquette.interception.messages.InterceptAcknowledgedMessage;
|
||||
import io.moquette.parser.proto.messages.ConnectMessage;
|
||||
import io.moquette.parser.proto.messages.PublishMessage;
|
||||
import io.moquette.spi.impl.subscriptions.Subscription;
|
||||
import io.netty.handler.codec.mqtt.MqttConnectMessage;
|
||||
import io.netty.handler.codec.mqtt.MqttPublishMessage;
|
||||
|
||||
/**
|
||||
* This interface is to be used internally by the broker components.
|
||||
@@ -35,13 +35,13 @@ import io.moquette.spi.impl.subscriptions.Subscription;
|
||||
*/
|
||||
public interface Interceptor {
|
||||
|
||||
void notifyClientConnected(ConnectMessage msg);
|
||||
void notifyClientConnected(MqttConnectMessage msg);
|
||||
|
||||
void notifyClientDisconnected(String clientID, String username);
|
||||
|
||||
void notifyClientConnectionLost(String clientID, String username);
|
||||
|
||||
void notifyTopicPublished(PublishMessage msg, String clientID, final String username);
|
||||
void notifyTopicPublished(MqttPublishMessage msg, String clientID, final String username);
|
||||
|
||||
void notifyTopicSubscribed(Subscription sub, final String username);
|
||||
|
||||
|
||||
+8
-7
@@ -15,27 +15,28 @@
|
||||
*/
|
||||
package io.moquette.interception.messages;
|
||||
|
||||
import io.moquette.parser.proto.messages.AbstractMessage;
|
||||
import io.netty.handler.codec.mqtt.MqttMessage;
|
||||
import io.netty.handler.codec.mqtt.MqttQoS;
|
||||
|
||||
/**
|
||||
* @author Wagner Macedo
|
||||
*/
|
||||
public abstract class InterceptAbstractMessage implements InterceptMessage {
|
||||
private final AbstractMessage msg;
|
||||
private final MqttMessage msg;
|
||||
|
||||
InterceptAbstractMessage(AbstractMessage msg) {
|
||||
InterceptAbstractMessage(MqttMessage msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public boolean isRetainFlag() {
|
||||
return msg.isRetainFlag();
|
||||
return msg.fixedHeader().isRetain();
|
||||
}
|
||||
|
||||
public boolean isDupFlag() {
|
||||
return msg.isDupFlag();
|
||||
return msg.fixedHeader().isDup();
|
||||
}
|
||||
|
||||
public AbstractMessage.QOSType getQos() {
|
||||
return msg.getQos();
|
||||
public MqttQoS getQos() {
|
||||
return msg.fixedHeader().qosLevel();
|
||||
}
|
||||
}
|
||||
|
||||
+17
-17
@@ -1,71 +1,71 @@
|
||||
package io.moquette.interception.messages;
|
||||
|
||||
import io.moquette.parser.proto.messages.ConnectMessage;
|
||||
import io.netty.handler.codec.mqtt.MqttConnectMessage;
|
||||
|
||||
/**
|
||||
* @author Wagner Macedo
|
||||
*/
|
||||
public class InterceptConnectMessage extends InterceptAbstractMessage {
|
||||
private final ConnectMessage msg;
|
||||
private final MqttConnectMessage msg;
|
||||
|
||||
public InterceptConnectMessage(ConnectMessage msg) {
|
||||
public InterceptConnectMessage(MqttConnectMessage msg) {
|
||||
super(msg);
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public String getClientID() {
|
||||
return msg.getClientID();
|
||||
return msg.payload().clientIdentifier();
|
||||
}
|
||||
|
||||
public boolean isCleanSession() {
|
||||
return msg.isCleanSession();
|
||||
return msg.variableHeader().isCleanSession();
|
||||
}
|
||||
|
||||
public int getKeepAlive() {
|
||||
return msg.getKeepAlive();
|
||||
return msg.variableHeader().keepAliveTimeSeconds();
|
||||
}
|
||||
|
||||
public boolean isPasswordFlag() {
|
||||
return msg.isPasswordFlag();
|
||||
return msg.variableHeader().hasPassword();
|
||||
}
|
||||
|
||||
public byte getProtocolVersion() {
|
||||
return msg.getProtocolVersion();
|
||||
return (byte) msg.variableHeader().version();
|
||||
}
|
||||
|
||||
public String getProtocolName() {
|
||||
return msg.getProtocolName();
|
||||
return msg.variableHeader().name();
|
||||
}
|
||||
|
||||
public boolean isUserFlag() {
|
||||
return msg.isUserFlag();
|
||||
return msg.variableHeader().hasUserName();
|
||||
}
|
||||
|
||||
public boolean isWillFlag() {
|
||||
return msg.isWillFlag();
|
||||
return msg.variableHeader().isWillFlag();
|
||||
}
|
||||
|
||||
public byte getWillQos() {
|
||||
return msg.getWillQos();
|
||||
return (byte) msg.variableHeader().willQos();
|
||||
}
|
||||
|
||||
public boolean isWillRetain() {
|
||||
return msg.isWillRetain();
|
||||
return msg.variableHeader().isWillRetain();
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return msg.getUsername();
|
||||
return msg.payload().userName();
|
||||
}
|
||||
|
||||
public byte[] getPassword() {
|
||||
return msg.getPassword();
|
||||
return msg.payload().password().getBytes();
|
||||
}
|
||||
|
||||
public String getWillTopic() {
|
||||
return msg.getWillTopic();
|
||||
return msg.payload().willTopic();
|
||||
}
|
||||
|
||||
public byte[] getWillMessage() {
|
||||
return msg.getWillMessage();
|
||||
return msg.payload().willMessage().getBytes();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
package io.moquette.interception.messages;
|
||||
|
||||
import io.moquette.parser.proto.messages.PublishMessage;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.handler.codec.mqtt.MqttPublishMessage;
|
||||
|
||||
/**
|
||||
* @author Wagner Macedo
|
||||
*/
|
||||
public class InterceptPublishMessage extends InterceptAbstractMessage {
|
||||
private final PublishMessage msg;
|
||||
private final MqttPublishMessage msg;
|
||||
private final String clientID;
|
||||
private final String username;
|
||||
|
||||
public InterceptPublishMessage(PublishMessage msg, String clientID, String username) {
|
||||
public InterceptPublishMessage(MqttPublishMessage msg, String clientID, String username) {
|
||||
super(msg);
|
||||
this.msg = msg;
|
||||
this.clientID = clientID;
|
||||
@@ -20,11 +19,11 @@ public class InterceptPublishMessage extends InterceptAbstractMessage {
|
||||
}
|
||||
|
||||
public String getTopicName() {
|
||||
return msg.getTopicName();
|
||||
return msg.variableHeader().topicName();
|
||||
}
|
||||
|
||||
public ByteBuffer getPayload() {
|
||||
return msg.getPayload();
|
||||
public ByteBuf getPayload() {
|
||||
return msg.payload();
|
||||
}
|
||||
|
||||
public String getClientID() {
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
package io.moquette.interception.messages;
|
||||
|
||||
import io.moquette.parser.proto.messages.AbstractMessage;
|
||||
import io.moquette.spi.impl.subscriptions.Subscription;
|
||||
import io.netty.handler.codec.mqtt.MqttQoS;
|
||||
|
||||
/**
|
||||
* @author Wagner Macedo
|
||||
@@ -19,7 +19,7 @@ public class InterceptSubscribeMessage implements InterceptMessage {
|
||||
return subscription.getClientId();
|
||||
}
|
||||
|
||||
public AbstractMessage.QOSType getRequestedQos() {
|
||||
public MqttQoS getRequestedQos() {
|
||||
return subscription.getRequestedQos();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user