vn.vnpay.sms.receiver.SMSCSession.java Source code

Java tutorial

Introduction

Here is the source code for vn.vnpay.sms.receiver.SMSCSession.java

Source

/*
 * Copyright (c) 1996-2001
 * Logica Mobile Networks Limited
 * All rights reserved.
 *
 * This software is distributed under Logica Open Source License Version 1.0
 * ("Licence Agreement"). You shall use it and distribute only in accordance
 * with the terms of the License Agreement.
 *
 */
package vn.vnpay.sms.receiver;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.smpp.*;
import org.smpp.pdu.PDU;
import org.smpp.pdu.Request;
import org.smpp.pdu.Response;
import org.smpp.pdu.ValueNotSetException;

import java.io.IOException;

import static org.smpp.util.Utils.MD5Hash;

/**
 * This class represent one client connection to the server starting
 * by accepting the connection, authenticating of the client,
 * communication and finished by unbinding.
 * The <code>SMSCSession</code> object is generated by <code>SMSCListener</code>
 * which also sets the session's PDU processor. Session is run in separate
 * thread; it reads PDUs from the connection and calls PDU processor's
 * client methods to process the received PDUs. PDU processor on turn can
 * use the session to submit PDUs to the client.
 * For receiving and sending of PDUs the session uses instances of
 * <code>SMSCReceiver</code> and <code>Transmitter</code>.
 *
 * @author Logica Mobile Networks SMPP Open Source Team
 * @version 1.0, 21 Jun 2001
 * @see SMSCListener
 * @see PDUProcessor
 * @see Connection
 * @see SMSCReceiver
 * @see Transmitter
 */
public class SMSCSession extends SmppObject implements Runnable {
    private Log logger = LogFactory.getLog(SMSCSession.class);
    private Receiver receiver;
    private Transmitter transmitter;
    private PDUProcessor pduProcessor;
    private Connection connection;
    private long receiveTimeout = Data.RECEIVER_TIMEOUT;
    private boolean keepReceiving = true;
    private boolean isReceiving = false;
    private String sessionId;
    private String ipAddress;

    /**
     * Initialises the session with the connection the session
     * should communicate over.
     *
     * @param connection the connection object for communication with client
     */
    public SMSCSession(Connection connection) {
        this.connection = connection;
        ipAddress = connection.getAddress();
        transmitter = new Transmitter(connection);
        receiver = new Receiver(transmitter, connection);
        sessionId = MD5Hash(String.valueOf(ipAddress + System.currentTimeMillis()));
        receiver.setSessionId(sessionId);
    }

    /**
     * Signals the session's thread that it should stop.
     * Doesn't wait for the thread to be completly finished.
     * Note that it can take some time before the thread is completly
     * stopped.
     *
     * @see #run()
     */
    public void stop() {
        logger.info(receiver + "SMSCSession stopping");
        keepReceiving = false;
    }

    /**
     * Implements the logic of receiving of the PDUs from client and passing
     * them to PDU processor. First starts SMSCReceiver, then in cycle
     * receives PDUs and passes them to the proper PDU processor's
     * methods. After the function <code>stop</code> is called (externally)
     * stops the SMSCReceiver, exits the PDU processor and closes the connection,
     * so no extry tidy-up routines are necessary.
     *
     * @see #stop()
     * @see PDUProcessor#clientResponse(Response)
     */
    public void run() {
        PDU pdu = null;
        //logger.info("SMSCSession starting SMSCReceiver");
        logger.info(txt_receiver + "SMSCSession starting receive from " + ipAddress + "/" + sessionId);
        receiver.start();
        isReceiving = true;
        try {
            while (keepReceiving) {
                try {
                    //logger.info("SMSCSession going to receive PDU");
                    pdu = receiver.receive(getReceiveTimeout());
                } catch (Exception e) {
                    logger.info(txt_receiver + "SMSCSession caught exception receiving PDU " + e.getMessage());
                }

                if (pdu != null) {
                    if (pdu.isRequest()) {
                        //logger.info("[" + pduProcessor.getSystemId() + "/" + ipAddress + "] Client Request: " + pdu.debugString());
                        pduProcessor.clientRequest((Request) pdu);
                    } else if (pdu.isResponse()) {
                        //logger.info("[" + pduProcessor.getSystemId() + "/" + ipAddress + "] Client Response: " + pdu.debugString());
                        pduProcessor.clientResponse((Response) pdu);
                    } else {
                        logger.info(txt_receiver + "SMSCSession not request nor response => not doing anything.");
                    }
                } else {
                    logger.info(txt_receiver + "[" + pduProcessor.getSystemId() + "/" + ipAddress
                            + "] client timeout (close thread: " + Thread.currentThread().getName() + ")");
                    keepReceiving = false;
                }
            }
        } finally {
            isReceiving = false;
        }
        //logger.info("SMSCSession stopping SMSCReceiver");
        receiver.stop();
        //logger.info("SMSCSession exiting PDUProcessor");
        pduProcessor.exit();

        try {
            logger.info(txt_receiver + "SMSCSession closing connection for " + pduProcessor.getSystemId() + "/"
                    + ipAddress);
            connection.close();
        } catch (IOException e) {
            logger.warn(txt_receiver + "closing SMSCSession's connection: " + e.getMessage());
        }
    }

    /**
     * Sends PropertiesConfig PDU to the client.
     *
     * @param pdu the PDU to send
     */
    public void send(PDU pdu) {
        try {
            //logger.info("SMSCSession going to send pdu over transmitter");
            transmitter.send(pdu);
        } catch (ValueNotSetException e) {
            event.write(e, "");
        } catch (IOException e) {
            event.write(e, "");
        }
    }

    /**
     * Sets new PDU processor.
     *
     * @param pduProcessor the new PDU processor
     */
    public void setPDUProcessor(PDUProcessor pduProcessor) {
        this.pduProcessor = pduProcessor;
    }

    /**
     * Sets the timeout for receiving the complete message.
     *
     * @param timeout the new timeout value
     */
    public void setReceiveTimeout(long timeout) {
        receiveTimeout = timeout;
    }

    /**
     * Returns the current setting of receiving timeout.
     *
     * @return the current timeout value
     */
    public long getReceiveTimeout() {
        return receiveTimeout;
    }

    public String getSessionId() {
        return sessionId;
    }

    public String getIpAddress() {
        return ipAddress;
    }
}