PageRenderTime 119ms CodeModel.GetById 112ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 23package org.mobicents.protocols.smpp.util;
 24
 25import java.io.IOException;
 26
 27import org.slf4j.Logger;
 28import org.slf4j.LoggerFactory;
 29
 30import org.mobicents.protocols.smpp.Session;
 31import org.mobicents.protocols.smpp.event.SMPPEvent;
 32import org.mobicents.protocols.smpp.event.SessionObserver;
 33import org.mobicents.protocols.smpp.message.CommandId;
 34import org.mobicents.protocols.smpp.message.DataSM;
 35import org.mobicents.protocols.smpp.message.DataSMResp;
 36import org.mobicents.protocols.smpp.message.DeliverSM;
 37import org.mobicents.protocols.smpp.message.DeliverSMResp;
 38import org.mobicents.protocols.smpp.message.EnquireLink;
 39import org.mobicents.protocols.smpp.message.EnquireLinkResp;
 40import org.mobicents.protocols.smpp.message.SMPPPacket;
 41import org.mobicents.protocols.smpp.message.Unbind;
 42import org.mobicents.protocols.smpp.message.UnbindResp;
 43
 44/**
 45 * A connection observer that can automatically respond to some of the basic
 46 * packet types. An instance of this class can be added as an observer to
 47 * a {@link org.mobicents.protocols.smpp.Session} and configured to send response packets
 48 * to any of unbind, deliver_sm, data_sm or enquire_link requests.
 49 * <p>
 50 * After construction, the default configuration for an <code>AutoResponder
 51 * </code> is not to respond to anything. Each setting must be explicitly
 52 * enabled by the caller. As an example, in order to respond to enquire_link
 53 * and deliver_sm packets but to ignore bind and data_sm packets, code like
 54 * the following could be used:
 55 * <pre>
 56 * Connection connection = new Connection(...);
 57 * AutoResponder responder = new AutoResponder();
 58 * responder.setAckDeliverSm(true);
 59 * responder.setAckEnqureLink(true);
 60 * connection.addObserver(responder);
 61 * </pre>
 62 * </p>
 63 * @version $Id: AutoResponder.java 457 2009-01-15 17:37:42Z orank $
 64 */
 65public class AutoResponder implements SessionObserver {
 66    private static final Logger LOG = LoggerFactory.getLogger(AutoResponder.class);
 67    
 68    private boolean ackUnbind;
 69    private boolean ackDeliverSm;
 70    private boolean ackDataSm;
 71    private boolean ackEnquireLink;
 72
 73    /**
 74     * Constructor that will initialise with all 'ack' properties initially set
 75     * to false.
 76     */
 77    public AutoResponder() {
 78    }
 79    
 80    /**
 81     * Constructor that will initialise with all 'ack' properties initially set
 82     * to <code>respond</code>.
 83     * @param respond The value to assign to all of this class' ack properties.
 84     */
 85    public AutoResponder(boolean respond) {
 86        ackUnbind = ackDeliverSm = ackDataSm = ackEnquireLink = respond;
 87    }
 88    
 89    public boolean isAckDataSm() {
 90        return ackDataSm;
 91    }
 92
 93    public void setAckDataSm(boolean ackDataSm) {
 94        this.ackDataSm = ackDataSm;
 95    }
 96
 97    public boolean isAckDeliverSm() {
 98        return ackDeliverSm;
 99    }
100
101    public void setAckDeliverSm(boolean ackDeliverSm) {
102        this.ackDeliverSm = ackDeliverSm;
103    }
104
105    public boolean isAckEnquireLink() {
106        return ackEnquireLink;
107    }
108
109    public void setAckEnquireLink(boolean ackEnquireLink) {
110        this.ackEnquireLink = ackEnquireLink;
111    }
112
113    public boolean isAckUnbind() {
114        return ackUnbind;
115    }
116
117    public void setAckUnbind(boolean ackUnbind) {
118        this.ackUnbind = ackUnbind;
119    }
120
121    public void packetReceived(Session source, SMPPPacket packet) {
122        switch (packet.getCommandId()) {
123        case CommandId.DELIVER_SM:
124            if (ackDeliverSm) {
125                respond(source, new DeliverSMResp((DeliverSM) packet));
126            }
127            break;
128        case CommandId.DATA_SM:
129            if (ackDataSm) {
130                respond(source, new DataSMResp((DataSM) packet));
131            }
132            break;
133        case CommandId.ENQUIRE_LINK:
134            if (ackEnquireLink) {
135                respond(source, new EnquireLinkResp((EnquireLink) packet));
136            }
137            break;
138        case CommandId.UNBIND:
139            if (ackUnbind) {
140                respond(source, new UnbindResp((Unbind) packet));
141            }
142            break;
143        }
144    }
145
146    public void update(Session source, SMPPEvent event) {
147    }
148    
149    private void respond(Session connection, SMPPPacket response) {
150        try {
151            connection.sendPacket(response);
152        } catch (IOException x) {
153            LOG.error("IOException while trying to send packet {}: {}",
154                    response, x.getMessage());
155            LOG.debug("Stack trace", x);
156        }
157    }
158}