/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/util/AutoResponder.java

http://mobicents.googlecode.com/ · Java · 158 lines · 88 code · 19 blank · 51 comment · 5 complexity · eb53c2b91cfe76a2428a678a22229744 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.protocols.smpp.util;
  23. import java.io.IOException;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.mobicents.protocols.smpp.Session;
  27. import org.mobicents.protocols.smpp.event.SMPPEvent;
  28. import org.mobicents.protocols.smpp.event.SessionObserver;
  29. import org.mobicents.protocols.smpp.message.CommandId;
  30. import org.mobicents.protocols.smpp.message.DataSM;
  31. import org.mobicents.protocols.smpp.message.DataSMResp;
  32. import org.mobicents.protocols.smpp.message.DeliverSM;
  33. import org.mobicents.protocols.smpp.message.DeliverSMResp;
  34. import org.mobicents.protocols.smpp.message.EnquireLink;
  35. import org.mobicents.protocols.smpp.message.EnquireLinkResp;
  36. import org.mobicents.protocols.smpp.message.SMPPPacket;
  37. import org.mobicents.protocols.smpp.message.Unbind;
  38. import org.mobicents.protocols.smpp.message.UnbindResp;
  39. /**
  40. * A connection observer that can automatically respond to some of the basic
  41. * packet types. An instance of this class can be added as an observer to
  42. * a {@link org.mobicents.protocols.smpp.Session} and configured to send response packets
  43. * to any of unbind, deliver_sm, data_sm or enquire_link requests.
  44. * <p>
  45. * After construction, the default configuration for an <code>AutoResponder
  46. * </code> is not to respond to anything. Each setting must be explicitly
  47. * enabled by the caller. As an example, in order to respond to enquire_link
  48. * and deliver_sm packets but to ignore bind and data_sm packets, code like
  49. * the following could be used:
  50. * <pre>
  51. * Connection connection = new Connection(...);
  52. * AutoResponder responder = new AutoResponder();
  53. * responder.setAckDeliverSm(true);
  54. * responder.setAckEnqureLink(true);
  55. * connection.addObserver(responder);
  56. * </pre>
  57. * </p>
  58. * @version $Id: AutoResponder.java 457 2009-01-15 17:37:42Z orank $
  59. */
  60. public class AutoResponder implements SessionObserver {
  61. private static final Logger LOG = LoggerFactory.getLogger(AutoResponder.class);
  62. private boolean ackUnbind;
  63. private boolean ackDeliverSm;
  64. private boolean ackDataSm;
  65. private boolean ackEnquireLink;
  66. /**
  67. * Constructor that will initialise with all 'ack' properties initially set
  68. * to false.
  69. */
  70. public AutoResponder() {
  71. }
  72. /**
  73. * Constructor that will initialise with all 'ack' properties initially set
  74. * to <code>respond</code>.
  75. * @param respond The value to assign to all of this class' ack properties.
  76. */
  77. public AutoResponder(boolean respond) {
  78. ackUnbind = ackDeliverSm = ackDataSm = ackEnquireLink = respond;
  79. }
  80. public boolean isAckDataSm() {
  81. return ackDataSm;
  82. }
  83. public void setAckDataSm(boolean ackDataSm) {
  84. this.ackDataSm = ackDataSm;
  85. }
  86. public boolean isAckDeliverSm() {
  87. return ackDeliverSm;
  88. }
  89. public void setAckDeliverSm(boolean ackDeliverSm) {
  90. this.ackDeliverSm = ackDeliverSm;
  91. }
  92. public boolean isAckEnquireLink() {
  93. return ackEnquireLink;
  94. }
  95. public void setAckEnquireLink(boolean ackEnquireLink) {
  96. this.ackEnquireLink = ackEnquireLink;
  97. }
  98. public boolean isAckUnbind() {
  99. return ackUnbind;
  100. }
  101. public void setAckUnbind(boolean ackUnbind) {
  102. this.ackUnbind = ackUnbind;
  103. }
  104. public void packetReceived(Session source, SMPPPacket packet) {
  105. switch (packet.getCommandId()) {
  106. case CommandId.DELIVER_SM:
  107. if (ackDeliverSm) {
  108. respond(source, new DeliverSMResp((DeliverSM) packet));
  109. }
  110. break;
  111. case CommandId.DATA_SM:
  112. if (ackDataSm) {
  113. respond(source, new DataSMResp((DataSM) packet));
  114. }
  115. break;
  116. case CommandId.ENQUIRE_LINK:
  117. if (ackEnquireLink) {
  118. respond(source, new EnquireLinkResp((EnquireLink) packet));
  119. }
  120. break;
  121. case CommandId.UNBIND:
  122. if (ackUnbind) {
  123. respond(source, new UnbindResp((Unbind) packet));
  124. }
  125. break;
  126. }
  127. }
  128. public void update(Session source, SMPPEvent event) {
  129. }
  130. private void respond(Session connection, SMPPPacket response) {
  131. try {
  132. connection.sendPacket(response);
  133. } catch (IOException x) {
  134. LOG.error("IOException while trying to send packet {}: {}",
  135. response, x.getMessage());
  136. LOG.debug("Stack trace", x);
  137. }
  138. }
  139. }