Refactored reusable code for QoS 1 sending

This commit is contained in:
andrea
2012-08-18 17:59:06 +02:00
parent 96f24bf8d4
commit 34af828653
@@ -37,6 +37,7 @@ import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
import org.dna.mqtt.moquette.proto.messages.ConnAckMessage; import org.dna.mqtt.moquette.proto.messages.ConnAckMessage;
import org.dna.mqtt.moquette.proto.messages.ConnectMessage; import org.dna.mqtt.moquette.proto.messages.ConnectMessage;
import org.dna.mqtt.moquette.proto.messages.DisconnectMessage; import org.dna.mqtt.moquette.proto.messages.DisconnectMessage;
import org.dna.mqtt.moquette.proto.messages.MessageIDMessage;
import org.dna.mqtt.moquette.proto.messages.PingReqMessage; import org.dna.mqtt.moquette.proto.messages.PingReqMessage;
import org.dna.mqtt.moquette.proto.messages.PublishMessage; import org.dna.mqtt.moquette.proto.messages.PublishMessage;
import org.dna.mqtt.moquette.proto.messages.SubscribeMessage; import org.dna.mqtt.moquette.proto.messages.SubscribeMessage;
@@ -262,49 +263,50 @@ public final class Client {
boolean unlocked = false; //true when a SUBACK is received boolean unlocked = false; //true when a SUBACK is received
try { try {
for (int retries = 0; retries < RETRIES_QOS_GT0 || !unlocked; retries++) { manageSendQoS1(msg);
if (retries > 0) { // for (int retries = 0; retries < RETRIES_QOS_GT0 || !unlocked; retries++) {
msg.setDupFlag(true); // if (retries > 0) {
} // msg.setDupFlag(true);
// }
WriteFuture wf = m_session.write(msg); //
try { // WriteFuture wf = m_session.write(msg);
wf.await(); // try {
} catch (InterruptedException ex) { // wf.await();
LOG.error(null, ex); // } catch (InterruptedException ex) {
throw new SubscribeException(ex); // LOG.error(null, ex);
} // throw new SubscribeException(ex);
LOG.info("subscribe message sent"); // }
// LOG.info("subscribe message sent");
Throwable ex = wf.getException(); //
if (ex != null) { // Throwable ex = wf.getException();
throw new SubscribeException(ex); // if (ex != null) {
} // throw new SubscribeException(ex);
// }
//wait for the SubAck //
m_subscribeBarrier = new CountDownLatch(1); // //wait for the SubAck
// m_subscribeBarrier = new CountDownLatch(1);
//suspend until the server respond with CONN_ACK //
try { // //suspend until the server respond with CONN_ACK
LOG.info("subscribe waiting for suback"); // try {
unlocked = m_subscribeBarrier.await(SUBACK_TIMEOUT, TimeUnit.MILLISECONDS); //TODO parametrize // LOG.info("subscribe waiting for suback");
} catch (InterruptedException iex) { // unlocked = m_subscribeBarrier.await(SUBACK_TIMEOUT, TimeUnit.MILLISECONDS); //TODO parametrize
throw new SubscribeException(iex); // } catch (InterruptedException iex) {
} // throw new SubscribeException(iex);
} // }
// }
//if not arrive into certain limit, raise an error //
if (!unlocked) { // //if not arrive into certain limit, raise an error
throw new SubscribeException(String.format("Server doesn't replyed with a SUB_ACK after %d replies", RETRIES_QOS_GT0)); // if (!unlocked) {
} else { // throw new SubscribeException(String.format("Server doesn't replyed with a SUB_ACK after %d replies", RETRIES_QOS_GT0));
//check if message ID match // } else {
if (m_receivedSubAckMessageID != messageID) { // //check if message ID match
throw new SubscribeException(String.format("Server replyed with " // if (m_receivedSubAckMessageID != messageID) {
+ "a broken MessageID in SUB_ACK, expected %d but received %d", // throw new SubscribeException(String.format("Server replyed with "
messageID, m_receivedSubAckMessageID)); // + "a broken MessageID in SUB_ACK, expected %d but received %d",
} // messageID, m_receivedSubAckMessageID));
} // }
} catch(Exception ex) { // }
} catch(Throwable ex) {
//in case errors arise, remove the registration because the subscription //in case errors arise, remove the registration because the subscription
// hasn't get well // hasn't get well
unregister(topic); unregister(topic);
@@ -316,6 +318,44 @@ public final class Client {
//TODO check the ACK messageID //TODO check the ACK messageID
} }
private void manageSendQoS1(MessageIDMessage msg) throws Throwable{
int messageID = msg.getMessageID();
boolean unlocked = false;
for (int retries = 0; retries < RETRIES_QOS_GT0 || !unlocked; retries++) {
if (retries > 0) {
msg.setDupFlag(true);
}
WriteFuture wf = m_session.write(msg);
wf.await();
LOG.info("message sent");
Throwable ex = wf.getException();
if (ex != null) {
throw ex;
}
//wait for the SubAck
m_subscribeBarrier = new CountDownLatch(1);
//suspend until the server respond with CONN_ACK
LOG.info("subscribe waiting for suback");
unlocked = m_subscribeBarrier.await(SUBACK_TIMEOUT, TimeUnit.MILLISECONDS); //TODO parametrize
}
//if not arrive into certain limit, raise an error
if (!unlocked) {
throw new SubscribeException(String.format("Server doesn't replyed with a SUB_ACK after %d replies", RETRIES_QOS_GT0));
} else {
//check if message ID match
if (m_receivedSubAckMessageID != messageID) {
throw new SubscribeException(String.format("Server replyed with "
+ "a broken MessageID in SUB_ACK, expected %d but received %d",
messageID, m_receivedSubAckMessageID));
}
}
}
/** /**
* TODO extract this SPI method in a SPI * TODO extract this SPI method in a SPI