/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/event/SimpleEventDispatcher.java

http://mobicents.googlecode.com/ · Java · 106 lines · 37 code · 10 blank · 59 comment · 2 complexity · a20c338ba4b930b916b409133e1e2930 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.event;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.mobicents.protocols.smpp.Session;
  26. import org.mobicents.protocols.smpp.message.SMPPPacket;
  27. /**
  28. * A simple implementation of the event dispatcher interface. This
  29. * implementation simply iterates over the set of registered observers and
  30. * notifies each of the event in turn. This means that whatever thread the
  31. * <code>Connection</code> object uses to call into this object will be
  32. * blocked, from the <code>Connection</code>'s point of view, until every
  33. * observer has successfully processed the event.
  34. *
  35. * <p>
  36. * Adding and removing observers from an instance of this class is protected
  37. * against multi-threaded access. However, event and packet notification
  38. * is not. If an event notification is currently in progress and another
  39. * thread modifies the set of registered observers, then it is possible for
  40. * the new observer to receive events before the call to <code>addObserver
  41. * </code> is complete.
  42. * </p>
  43. *
  44. * @version $Id: SimpleEventDispatcher.java 457 2009-01-15 17:37:42Z orank $
  45. * @see com.adenki.smpp.event.EventDispatcher
  46. */
  47. public class SimpleEventDispatcher extends AbstractEventDispatcher {
  48. private static final Logger LOG = LoggerFactory.getLogger(SimpleEventDispatcher.class);
  49. /**
  50. * Create a new SimpleEventDispatcher.
  51. */
  52. public SimpleEventDispatcher() {
  53. }
  54. /**
  55. * Create a new SimpleEventDispatcher and register one observer on it.
  56. */
  57. public SimpleEventDispatcher(SessionObserver observer) {
  58. addObserver(observer);
  59. }
  60. public void init() {
  61. // nothing to do.
  62. }
  63. public void destroy() {
  64. // nothing to do.
  65. }
  66. /**
  67. * Notify registered observers of an SMPP event.
  68. * @param conn the Connection with which the event is associated.
  69. * @param event the SMPP event to notify observers of.
  70. */
  71. public void notifyObservers(Session conn, SMPPEvent event) {
  72. SessionObserver[] observerList = getObserverList();
  73. for (SessionObserver observer : observerList) {
  74. try {
  75. observer.update(conn, event);
  76. } catch (Exception x) {
  77. LOG.error("An observer threw an exception during event processing", x);
  78. }
  79. }
  80. }
  81. /**
  82. * Notify registered observers of an incoming SMPP packet.
  83. * @param conn the Connection which the packet was received on.
  84. * @param packet the received packet to notify observers of.
  85. */
  86. public void notifyObservers(Session conn, SMPPPacket packet) {
  87. SessionObserver[] observerList = getObserverList();
  88. for (SessionObserver observer : observerList) {
  89. try {
  90. observer.packetReceived(conn, packet);
  91. } catch (Exception x) {
  92. LOG.error("An observer threw an exception during packet processing", x);
  93. }
  94. }
  95. }
  96. }