| 1 | package fr.sii.ogham.sms.sender.impl.cloudhopper.session; | |
| 2 | ||
| 3 | import org.slf4j.Logger; | |
| 4 | import org.slf4j.LoggerFactory; | |
| 5 | ||
| 6 | import com.cloudhopper.smpp.SmppClient; | |
| 7 | import com.cloudhopper.smpp.SmppSession; | |
| 8 | import com.cloudhopper.smpp.pdu.EnquireLink; | |
| 9 | import com.cloudhopper.smpp.type.RecoverablePduException; | |
| 10 | import com.cloudhopper.smpp.type.SmppChannelException; | |
| 11 | import com.cloudhopper.smpp.type.SmppTimeoutException; | |
| 12 | import com.cloudhopper.smpp.type.UnrecoverablePduException; | |
| 13 | ||
| 14 | import fr.sii.ogham.core.exception.MessageException; | |
| 15 | import fr.sii.ogham.core.retry.RetryExecutor; | |
| 16 | import fr.sii.ogham.sms.builder.cloudhopper.SmppClientSupplier; | |
| 17 | import fr.sii.ogham.sms.builder.cloudhopper.SmppSessionHandlerSupplier; | |
| 18 | import fr.sii.ogham.sms.message.Sms; | |
| 19 | import fr.sii.ogham.sms.sender.impl.cloudhopper.ExtendedSmppSessionConfiguration; | |
| 20 | import fr.sii.ogham.sms.sender.impl.cloudhopper.exception.SmppException; | |
| 21 | ||
| 22 | /** | |
| 23 | * Strategy that attempts to reuse the previous session if possible. | |
| 24 | * | |
| 25 | * <p> | |
| 26 | * When sending the first message, a new session is created. Later, when sending | |
| 27 | * the next message, if the session is still alive, this session is reused. As | |
| 28 | * the connection is not actively maintained, the session may be killed by the | |
| 29 | * server. Therefore to check if the session is still alive, an | |
| 30 | * {@link EnquireLink} request is sent. If a response is received from the | |
| 31 | * server, then the session is still alive and the message can be sent using the | |
| 32 | * same session. If a failure response or no response is received after some | |
| 33 | * time from the server, then a new session must be created. | |
| 34 | * | |
| 35 | * <p> | |
| 36 | * To check if the session is still alive, the {@link EnquireLink} request is | |
| 37 | * sent just before sending the real message. In order to prevent sending an | |
| 38 | * {@link EnquireLink} request before <strong>every</strong> message, the date | |
| 39 | * of the last sent message or {@link EnquireLink} is kept. This date is | |
| 40 | * compared to a delay to ensure that no {@link EnquireLink} is sent during this | |
| 41 | * delay. | |
| 42 | * | |
| 43 | * @author Aurélien Baudet | |
| 44 | * | |
| 45 | */ | |
| 46 | public class MayReuseSessionStrategy extends BaseSessionHandlingStrategy implements ErrorHandler { | |
| 47 | private static final Logger LOG = LoggerFactory.getLogger(MayReuseSessionStrategy.class); | |
| 48 | ||
| 49 | private final ErrorAnalyzer errorAnalyzer; | |
| 50 | private long lastSentOrSession; | |
| 51 | ||
| 52 | public MayReuseSessionStrategy(ExtendedSmppSessionConfiguration configuration, SmppClientSupplier clientSupplier, SmppSessionHandlerSupplier smppSessionHandlerSupplier, RetryExecutor retry, | |
| 53 | ErrorAnalyzer errorAnalyzer) { | |
| 54 | super(LOG, configuration, clientSupplier, smppSessionHandlerSupplier, retry); | |
| 55 | this.errorAnalyzer = errorAnalyzer; | |
| 56 | } | |
| 57 | ||
| 58 | @Override | |
| 59 | public SmppSession getSession() throws SmppException { | |
| 60 |
2
1. getSession : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::initClient → NO_COVERAGE 2. getSession : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::initClient → TIMED_OUT |
initClient(); |
| 61 |
2
1. getSession : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::initSession → NO_COVERAGE 2. getSession : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::initSession → KILLED |
initSession(); |
| 62 |
2
1. getSession : negated conditional → NO_COVERAGE 2. getSession : negated conditional → KILLED |
if (!isSessionStillAlive()) { |
| 63 | LOG.debug("Current session seems to be broken. Trying to reconnect..."); | |
| 64 |
2
1. getSession : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::reconnect → NO_COVERAGE 2. getSession : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::reconnect → KILLED |
reconnect(); |
| 65 | } | |
| 66 |
2
1. getSession : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::getSession → NO_COVERAGE 2. getSession : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::getSession → KILLED |
return currentSession; |
| 67 | } | |
| 68 | ||
| 69 | @Override | |
| 70 | public void messageSent(Sms sms) throws MessageException { | |
| 71 |
2
1. messageSent : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::updateLastSentOrSession → SURVIVED 2. messageSent : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::updateLastSentOrSession → NO_COVERAGE |
updateLastSentOrSession(); |
| 72 | } | |
| 73 | ||
| 74 | @Override | |
| 75 | public void messageNotSent(Sms sms, SmppException e) throws MessageException { | |
| 76 | // If message couldn't be sent, re-throw the exception. | |
| 77 | // | |
| 78 | // The cause may be a broken connection because the delay to wait before | |
| 79 | // sending a new EnquireLink is too long compared to the server | |
| 80 | // configuration. But in this case, the developer has to adjust the | |
| 81 | // settings accordingly to the server. | |
| 82 | // | |
| 83 | // If a new connection is required, clean the current session now | |
| 84 | // therefore a new session will be created next time. | |
| 85 |
2
1. messageNotSent : negated conditional → NO_COVERAGE 2. messageNotSent : negated conditional → KILLED |
if (errorAnalyzer.requiresNewConnection(e)) { |
| 86 |
2
1. messageNotSent : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::clean → NO_COVERAGE 2. messageNotSent : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::clean → KILLED |
clean(); |
| 87 | } | |
| 88 | throw new MessageException("Failed to send SMS", sms, e); | |
| 89 | } | |
| 90 | ||
| 91 | @Override | |
| 92 | public void handleFailure(Throwable e) { | |
| 93 | // If a new connection is required, clean the current session now | |
| 94 | // therefore a new session will be created next time. | |
| 95 |
1
1. handleFailure : negated conditional → NO_COVERAGE |
if (errorAnalyzer.requiresNewConnection(e)) { |
| 96 |
1
1. handleFailure : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::clean → NO_COVERAGE |
clean(); |
| 97 | } | |
| 98 | } | |
| 99 | ||
| 100 | @Override | |
| 101 | public void messageProcessed(Sms sms) { | |
| 102 | // nothing to do | |
| 103 | } | |
| 104 | ||
| 105 | @Override | |
| 106 | public void clean() { | |
| 107 |
2
1. clean : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::destroySession → NO_COVERAGE 2. clean : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::destroySession → KILLED |
destroySession(); |
| 108 |
2
1. clean : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::destroyClient → NO_COVERAGE 2. clean : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::destroyClient → KILLED |
destroyClient(); |
| 109 | } | |
| 110 | ||
| 111 | @Override | |
| 112 | protected synchronized SmppSession connect(SmppClient client) throws SmppException { | |
| 113 | SmppSession session = super.connect(client); | |
| 114 |
2
1. connect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::updateLastSentOrSession → NO_COVERAGE 2. connect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::updateLastSentOrSession → KILLED |
updateLastSentOrSession(); |
| 115 |
2
1. connect : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::connect → NO_COVERAGE 2. connect : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::connect → KILLED |
return session; |
| 116 | } | |
| 117 | ||
| 118 | private boolean isSessionStillAlive() { | |
| 119 |
2
1. isSessionStillAlive : Replaced long subtraction with addition → NO_COVERAGE 2. isSessionStillAlive : Replaced long subtraction with addition → KILLED |
long elapsedTime = now() - lastSentOrSession; |
| 120 |
4
1. isSessionStillAlive : changed conditional boundary → NO_COVERAGE 2. isSessionStillAlive : changed conditional boundary → SURVIVED 3. isSessionStillAlive : negated conditional → NO_COVERAGE 4. isSessionStillAlive : negated conditional → TIMED_OUT |
boolean skipEnquireLink = elapsedTime < configuration.getReuseSession().getLastInteractionExpirationDelay(); |
| 121 | LOG.trace("Skip EnquireLink? {} {} => {}", elapsedTime, configuration.getReuseSession().getLastInteractionExpirationDelay(), skipEnquireLink); | |
| 122 |
2
1. isSessionStillAlive : negated conditional → NO_COVERAGE 2. isSessionStillAlive : negated conditional → TIMED_OUT |
if (skipEnquireLink) { |
| 123 |
2
1. isSessionStillAlive : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::isSessionStillAlive → NO_COVERAGE 2. isSessionStillAlive : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::isSessionStillAlive → KILLED |
return true; |
| 124 | } | |
| 125 | try { | |
| 126 | LOG.trace("Sending EnquireLink to check if session is still alive..."); | |
| 127 | currentSession.enquireLink(new EnquireLink(), configuration.getReuseSession().getEnquireLinkTimeout()); | |
| 128 | LOG.trace("Session is still alive"); | |
| 129 |
2
1. isSessionStillAlive : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::updateLastSentOrSession → NO_COVERAGE 2. isSessionStillAlive : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::updateLastSentOrSession → SURVIVED |
updateLastSentOrSession(); |
| 130 |
2
1. isSessionStillAlive : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::isSessionStillAlive → NO_COVERAGE 2. isSessionStillAlive : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::isSessionStillAlive → KILLED |
return true; |
| 131 | } catch (RecoverablePduException | UnrecoverablePduException | SmppTimeoutException | SmppChannelException e) { | |
| 132 | LOG.trace("Failure while sending EnquireLink", e); | |
| 133 | } catch (InterruptedException e) { | |
| 134 | LOG.trace("Failure while sending EnquireLink (interrupted)", e); | |
| 135 |
1
1. isSessionStillAlive : removed call to java/lang/Thread::interrupt → NO_COVERAGE |
Thread.currentThread().interrupt(); |
| 136 | } | |
| 137 |
2
1. isSessionStillAlive : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::isSessionStillAlive → NO_COVERAGE 2. isSessionStillAlive : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::isSessionStillAlive → KILLED |
return false; |
| 138 | } | |
| 139 | ||
| 140 | private void reconnect() throws SmppException { | |
| 141 |
2
1. reconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::destroySession → NO_COVERAGE 2. reconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::destroySession → KILLED |
destroySession(); |
| 142 |
2
1. reconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::destroyClient → NO_COVERAGE 2. reconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::destroyClient → KILLED |
destroyClient(); |
| 143 |
2
1. reconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::initClient → NO_COVERAGE 2. reconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::initClient → TIMED_OUT |
initClient(); |
| 144 |
2
1. reconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::initSession → NO_COVERAGE 2. reconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::initSession → KILLED |
initSession(); |
| 145 | } | |
| 146 | ||
| 147 | private void updateLastSentOrSession() { | |
| 148 | lastSentOrSession = now(); | |
| 149 | LOG.trace("lastSentOrSession updated: {}", lastSentOrSession); | |
| 150 | } | |
| 151 | ||
| 152 | private static long now() { | |
| 153 |
2
1. now : replaced long return with 0 for fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::now → NO_COVERAGE 2. now : replaced long return with 0 for fr/sii/ogham/sms/sender/impl/cloudhopper/session/MayReuseSessionStrategy::now → KILLED |
return System.currentTimeMillis(); |
| 154 | } | |
| 155 | ||
| 156 | } | |
Mutations | ||
| 60 |
1.1 2.2 |
|
| 61 |
1.1 2.2 |
|
| 62 |
1.1 2.2 |
|
| 64 |
1.1 2.2 |
|
| 66 |
1.1 2.2 |
|
| 71 |
1.1 2.2 |
|
| 85 |
1.1 2.2 |
|
| 86 |
1.1 2.2 |
|
| 95 |
1.1 |
|
| 96 |
1.1 |
|
| 107 |
1.1 2.2 |
|
| 108 |
1.1 2.2 |
|
| 114 |
1.1 2.2 |
|
| 115 |
1.1 2.2 |
|
| 119 |
1.1 2.2 |
|
| 120 |
1.1 2.2 3.3 4.4 |
|
| 122 |
1.1 2.2 |
|
| 123 |
1.1 2.2 |
|
| 129 |
1.1 2.2 |
|
| 130 |
1.1 2.2 |
|
| 135 |
1.1 |
|
| 137 |
1.1 2.2 |
|
| 141 |
1.1 2.2 |
|
| 142 |
1.1 2.2 |
|
| 143 |
1.1 2.2 |
|
| 144 |
1.1 2.2 |
|
| 153 |
1.1 2.2 |