package org.jsmpp.session;

import java.io.IOException;
import java.net.SocketTimeoutException;

import org.jsmpp.session.connection.Connection;
import org.jsmpp.session.connection.ServerConnection;
import org.jsmpp.session.connection.ServerConnectionFactory;
import org.jsmpp.session.connection.socket.ServerSocketConnectionFactory;

/**
 * @author uudashr
 *
 */
public class SMPPServerSessionListener {
    private final int port;
    private final ServerConnection serverConn;
    private int initiationTimer = 5000;
    private SessionStateListener sessionStateListener;
    private ServerMessageReceiverListener messageReceiverListener;
    
    public SMPPServerSessionListener(int port) throws IOException {
        this(port, new ServerSocketConnectionFactory());
    }
    
    public SMPPServerSessionListener(int port,  
            ServerConnectionFactory serverConnFactory) throws IOException {
        this.port = port;
        serverConn = serverConnFactory.listen(port);
    }
    
    public SMPPServerSessionListener(int port, int timeout, 
            ServerConnectionFactory serverConnFactory) throws IOException {
        this.port = port;
        serverConn = serverConnFactory.listen(port, timeout);
    }
    
    public SMPPServerSessionListener(int port, int timeout, int backlog,
            ServerConnectionFactory serverConnFactory) throws IOException {
        this.port = port;
        serverConn = serverConnFactory.listen(port, timeout, backlog);
    }
    
    public int getTimeout(int timeout) throws IOException {
        return serverConn.getSoTimeout();
    }
    
    
    /**
     * Timeout listening. When timeout reach and connection request didn't
     * arrive then {@link SocketTimeoutException} will be thrown but the
     * listener still valid.
     * 
     * @param timeout
     * @throws IOException
     */
    public void setTimeout(int timeout) throws IOException {
        serverConn.setSoTimeout(timeout);
    }
    
    public int getPort() {
        return port;
    }
    
    public int getInitiationTimer() {
        return initiationTimer;
    }
    
    public void setInitiationTimer(int initiationTimer) {
        this.initiationTimer = initiationTimer;
    }
    
    public SessionStateListener getSessionStateListener() {
        return sessionStateListener;
    }
    
    public void setSessionStateListener(
            SessionStateListener sessionStateListener) {
        this.sessionStateListener = sessionStateListener;
    }
    
    public ServerMessageReceiverListener getMessageReceiverListener() {
        return messageReceiverListener;
    }
    
    public void setMessageReceiverListener(
            ServerMessageReceiverListener messageReceiverListener) {
        this.messageReceiverListener = messageReceiverListener;
    }
    
    /**
     * Accept session request from client.
     * 
     * @return the accepted {@link SMPPServerSession}.
     * @throws SocketTimeoutException if timeout reach with no session accepted.
     * @throws IOException if there is an IO error occur.
     */
    public SMPPServerSession accept() throws IOException {
        Connection conn = serverConn.accept();
        conn.setSoTimeout(initiationTimer);
        return new SMPPServerSession(conn, sessionStateListener, messageReceiverListener);
    }
    
    public void close() throws IOException {
        serverConn.close();
    }
}

@@ -332,16 +332,711 @@
 y;%0A%0A/**%0A
+ * This object responsible to for new SMPP Session request from ESME. It will%0A * listen on specified port.%0A * %0A * %3Cpre%3E%0A * SMPPServerSession session = listener.accept();%0A * BindRequest bindReq = session.waitForBind(5000);%0A * %0A * if (checkPassword(bindReq)) %7B%0A *     bindReq.accept(&quot;sys&quot;);%0A * %7D else %7B%0A *     bindReq.reject(SMPPConstant.STAT_ESME_RINVPASWD);%0A * %7D%0A * %3C/pre%3E%0A * %0A * %3Cp%3E%0A * The listening trough getting the bind request should take less than session%0A * initiation timer, otherwise if there is network open has been requested, ESME%0A * will close the connection. Accepting the bind request should take less than%0A * transaction timer or ESME will issued timeout.%0A * %3C/p%3E%0A * %0A
  * @auth
@@ -1048,16 +1048,17 @@
 dashr%0A *
+ 
 %0A */%0Apub
@@ -3556,16 +3556,446 @@
  client.
+ The session state is still OPEN. To%0A     * communicate with ESME properly binding request should be accepted.%0A     * %0A     * %3Cpre%3E%0A     * SMPPServerSession session = listener.accept();%0A     * BindRequest bindReq = session.waitForBind(5000);%0A     * %0A     * if (checkPassword(bindReq)) %7B%0A     *     bindReq.accept(&quot;sys&quot;);%0A     * %7D else %7B%0A     *     bindReq.reject(SMPPConstant.STAT_ESME_RINVPASWD);%0A     * %7D%0A     * %3C/pre%3E
 %0A     * 
@@ -4189,16 +4189,70 @@
  occur.%0A
+     * @see SMPPServerSession%0A     * @see BindRequest%0A
      */%0A

Add comment