Added IoSession attribute to PublishEvent to avoid looking up

This commit is contained in:
selva.andre
2012-09-01 10:22:53 +02:00
parent 04872e07f7
commit 3be2ccca8f
5 changed files with 24 additions and 12 deletions
@@ -18,12 +18,12 @@ public interface IMessaging {
* notification also after all the current subscriber to the given topic has * notification also after all the current subscriber to the given topic has
* been notified. * been notified.
*/ */
void publish(String topic, byte[] message, QOSType qos, boolean retain, String clientID); void publish(String topic, byte[] message, QOSType qos, boolean retain, String clientID, IoSession session);
/** /**
* Used to publish messages with QoS > 0 * Used to publish messages with QoS > 0
* */ * */
void publish(String topic, byte[] message, QOSType qos, boolean retain, String clientID, int messageID); void publish(String topic, byte[] message, QOSType qos, boolean retain, String clientID, int messageID, IoSession session);
/** /**
* Subscribe a client to a specified topic with a defined level * Subscribe a client to a specified topic with a defined level
@@ -14,6 +14,7 @@ import org.dna.mqtt.moquette.messaging.spi.IMessaging;
import org.dna.mqtt.moquette.messaging.spi.impl.SubscriptionsStore.Token; import org.dna.mqtt.moquette.messaging.spi.impl.SubscriptionsStore.Token;
import org.dna.mqtt.moquette.messaging.spi.impl.events.*; import org.dna.mqtt.moquette.messaging.spi.impl.events.*;
import org.dna.mqtt.moquette.proto.messages.AbstractMessage.QOSType; import org.dna.mqtt.moquette.proto.messages.AbstractMessage.QOSType;
import org.dna.mqtt.moquette.server.Constants;
import org.dna.mqtt.moquette.server.Server; import org.dna.mqtt.moquette.server.Server;
import org.fusesource.hawtbuf.codec.StringCodec; import org.fusesource.hawtbuf.codec.StringCodec;
import org.fusesource.hawtdb.api.BTreeIndexFactory; import org.fusesource.hawtdb.api.BTreeIndexFactory;
@@ -108,20 +109,20 @@ public class SimpleMessaging implements IMessaging, Runnable {
} }
public void publish(String topic, byte[] message, QOSType qos, boolean retain, String clientID) { public void publish(String topic, byte[] message, QOSType qos, boolean retain, String clientID, IoSession session) {
//TODO for this moment no qos > 0 trivial implementation //TODO for this moment no qos > 0 trivial implementation
// QOSType defQos = QOSType.MOST_ONE; // QOSType defQos = QOSType.MOST_ONE;
try { try {
m_inboundQueue.put(new PublishEvent(topic, qos, message, retain, clientID)); m_inboundQueue.put(new PublishEvent(topic, qos, message, retain, clientID, session));
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
LOG.error(null, ex); LOG.error(null, ex);
} }
} }
public void publish(String topic, byte[] message, QOSType qos, boolean retain, String clientID, int messageID) { public void publish(String topic, byte[] message, QOSType qos, boolean retain, String clientID, int messageID, IoSession session) {
try { try {
m_inboundQueue.put(new PublishEvent(topic, qos, message, retain, clientID, messageID)); m_inboundQueue.put(new PublishEvent(topic, qos, message, retain, clientID, messageID, session));
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
LOG.error(null, ex); LOG.error(null, ex);
} }
@@ -267,8 +268,11 @@ public class SimpleMessaging implements IMessaging, Runnable {
for (final Subscription sub : subscriptions.matches(topic)) { for (final Subscription sub : subscriptions.matches(topic)) {
if (qos == QOSType.MOST_ONE) { if (qos == QOSType.MOST_ONE) {
//QoS 0
m_outboundQueue.put(new NotifyEvent(sub.clientId, topic, qos, message, false)); m_outboundQueue.put(new NotifyEvent(sub.clientId, topic, qos, message, false));
} else { } else {
//QoS 1 or 2
//if the target subscription is not clean session and is not connected => store it
m_outboundQueue.put(new NotifyEvent(sub.clientId, topic, qos, message, false, evt.getMessageID())); m_outboundQueue.put(new NotifyEvent(sub.clientId, topic, qos, message, false, evt.getMessageID()));
} }
} }
@@ -1,5 +1,6 @@
package org.dna.mqtt.moquette.messaging.spi.impl.events; package org.dna.mqtt.moquette.messaging.spi.impl.events;
import org.apache.mina.core.session.IoSession;
import org.dna.mqtt.moquette.proto.messages.AbstractMessage.QOSType; import org.dna.mqtt.moquette.proto.messages.AbstractMessage.QOSType;
import java.io.Serializable; import java.io.Serializable;
@@ -16,19 +17,22 @@ public class PublishEvent extends MessagingEvent implements Serializable {
String m_clientID; String m_clientID;
//Optional attribute, available only fo QoS 1 and 2 //Optional attribute, available only fo QoS 1 and 2
int m_msgID; int m_msgID;
transient IoSession m_session;
public PublishEvent(String topic, QOSType qos, byte[] message, boolean retain, public PublishEvent(String topic, QOSType qos, byte[] message, boolean retain,
String clientID) { String clientID, IoSession session) {
m_topic = topic; m_topic = topic;
m_qos = qos; m_qos = qos;
m_message = message; m_message = message;
m_retain = retain; m_retain = retain;
m_clientID = clientID; m_clientID = clientID;
m_session = session;
} }
public PublishEvent(String topic, QOSType qos, byte[] message, boolean retain, public PublishEvent(String topic, QOSType qos, byte[] message, boolean retain,
String clientID, int msgID) { String clientID, int msgID, IoSession session) {
this(topic, qos, message, retain, clientID); this(topic, qos, message, retain, clientID, session);
m_msgID = msgID; m_msgID = msgID;
} }
@@ -55,4 +59,8 @@ public class PublishEvent extends MessagingEvent implements Serializable {
public int getMessageID() { public int getMessageID() {
return m_msgID; return m_msgID;
} }
public IoSession getSession() {
return m_session;
}
} }
@@ -57,7 +57,7 @@ public class SimpleMessagingTest {
// messaging.setNotifier(notifier); // messaging.setNotifier(notifier);
//Exercise //Exercise
PublishEvent pubEvt = new PublishEvent(FAKE_TOPIC, QOSType.MOST_ONE, "Hello".getBytes(), false, "FakeCLI"); PublishEvent pubEvt = new PublishEvent(FAKE_TOPIC, QOSType.MOST_ONE, "Hello".getBytes(), false, "FakeCLI", null);
messaging.processPublish(pubEvt); messaging.processPublish(pubEvt);
//Verify //Verify
@@ -86,7 +86,7 @@ public class MQTTHandlerTest {
//Verify //Verify
assertEquals(ConnAckMessage.CONNECTION_ACCEPTED, m_returnCode); assertEquals(ConnAckMessage.CONNECTION_ACCEPTED, m_returnCode);
verify(mockedMessaging).publish(eq("topic"), eq("Topic message".getBytes()), verify(mockedMessaging).publish(eq("topic"), eq("Topic message".getBytes()),
any(AbstractMessage.QOSType.class), anyBoolean(), eq("123")); any(AbstractMessage.QOSType.class), anyBoolean(), eq("123"), any(IoSession.class));
} }
@@ -99,7 +99,7 @@ public class MQTTHandlerTest {
(byte)AbstractMessage.QOSType.EXACTLY_ONCE.ordinal(), topicName)); (byte)AbstractMessage.QOSType.EXACTLY_ONCE.ordinal(), topicName));
IMessaging mockedMessaging = mock(IMessaging.class); IMessaging mockedMessaging = mock(IMessaging.class);
m_session.setAttribute(MQTTHandler.ATTR_CLIENTID, "fakeID"); m_session.setAttribute(Constants.ATTR_CLIENTID, "fakeID");
//Exercise //Exercise