/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/ReceiverThread.java

http://mobicents.googlecode.com/ · Java · 167 lines · 118 code · 23 blank · 26 comment · 12 complexity · 30131a98cd03ba66ac275240b173fa4d 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;
  23. import java.io.IOException;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.mobicents.protocols.smpp.event.ReceiverExceptionEvent;
  27. import org.mobicents.protocols.smpp.event.ReceiverExitEvent;
  28. import org.mobicents.protocols.smpp.event.SMPPEvent;
  29. import org.mobicents.protocols.smpp.message.SMPPPacket;
  30. import org.mobicents.protocols.smpp.net.ReadTimeoutException;
  31. import org.mobicents.protocols.smpp.util.APIConfig;
  32. import org.mobicents.protocols.smpp.util.APIConfigFactory;
  33. import org.mobicents.protocols.smpp.util.PacketFactory;
  34. /**
  35. * Receiver thread for the connection.
  36. * @author amit bhayani
  37. * @version $Id: ReceiverThread.java 456 2009-01-15 17:15:38Z orank $
  38. */
  39. public class ReceiverThread implements Receiver, Runnable {
  40. private static final Logger LOG = LoggerFactory.getLogger(ReceiverThread.class);
  41. private Thread thread;
  42. private boolean running;
  43. private Session session;
  44. private PacketFactory packetFactory = new PacketFactory();
  45. public ReceiverThread() {
  46. thread = new Thread(this);
  47. thread.setDaemon(true);
  48. }
  49. public ReceiverThread(Session session) {
  50. this();
  51. this.session = session;
  52. }
  53. public PacketFactory getPacketFactory() {
  54. return packetFactory;
  55. }
  56. public void setPacketFactory(PacketFactory packetFactory) {
  57. this.packetFactory = packetFactory;
  58. }
  59. public Session getSession() {
  60. return session;
  61. }
  62. public void setSession(Session session) {
  63. this.session = session;
  64. }
  65. public String getName() {
  66. return thread.getName();
  67. }
  68. public void setName(String name) {
  69. thread.setName(name);
  70. }
  71. public void run() {
  72. LOG.debug("Receiver thread starting.");
  73. SMPPEvent exitEvent = null;
  74. try {
  75. running = true;
  76. exitEvent = processPackets();
  77. } catch (Exception x) {
  78. LOG.error("Error in receiver thread", x);
  79. exitEvent = new ReceiverExitEvent(session, x);
  80. }
  81. session.getEventDispatcher().notifyObservers(session, exitEvent);
  82. LOG.debug("Destroying event dispatcher.");
  83. session.getEventDispatcher().destroy();
  84. LOG.debug("Receiver thread exiting.");
  85. }
  86. public boolean isStarted() {
  87. return thread.isAlive();
  88. }
  89. public void start() {
  90. thread.start();
  91. }
  92. public void stop() {
  93. running = false;
  94. }
  95. private ReceiverExitEvent processPackets() throws Exception {
  96. ReceiverExitEvent exitEvent = null;
  97. ReceiverExceptionEvent excpEvent = null;
  98. int ioExceptions = 0;
  99. APIConfig config = APIConfigFactory.getConfig();
  100. final int ioExceptionLimit =
  101. config.getInt(APIConfig.TOO_MANY_IO_EXCEPTIONS, 5);
  102. SMPPPacket packet = null;
  103. while (running && session.getState() != SessionState.UNBOUND) {
  104. try {
  105. packet = readNextPacket();
  106. if (packet == null) {
  107. continue;
  108. }
  109. session.processReceivedPacket(packet);
  110. session.getEventDispatcher().notifyObservers(session, packet);
  111. ioExceptions = 0;
  112. } catch (ReadTimeoutException x) {
  113. SessionState state = session.getState();
  114. if (state == SessionState.BINDING) {
  115. LOG.debug("Bind timeout occurred.");
  116. exitEvent = new ReceiverExitEvent(session, null, state);
  117. exitEvent.setReason(ReceiverExitEvent.BIND_TIMEOUT);
  118. break;
  119. }
  120. if (state == SessionState.BOUND) {
  121. LOG.debug("Read timeout occurred.");
  122. excpEvent = new ReceiverExceptionEvent(session, x);
  123. session.getEventDispatcher().notifyObservers(session, excpEvent);
  124. }
  125. } catch (IOException x) {
  126. LOG.debug("Exception in receiver", x);
  127. ioExceptions++;
  128. if (ioExceptions >= ioExceptionLimit) {
  129. SessionState state = session.getState();
  130. exitEvent = new ReceiverExitEvent(session, x, state);
  131. exitEvent.setReason(ReceiverExitEvent.EXCEPTION);
  132. break;
  133. }
  134. }
  135. }
  136. if (exitEvent == null) {
  137. exitEvent = new ReceiverExitEvent(session);
  138. }
  139. return exitEvent;
  140. }
  141. private SMPPPacket readNextPacket() throws IOException {
  142. return session.getSmscLink().read();
  143. }
  144. }