Package org.saf.smppagent

Source Code of org.saf.smppagent.ReceiverAgent

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package org.saf.smppagent;

import com.logica.smpp.Connection;
import com.logica.smpp.Data;
import com.logica.smpp.Session;
import com.logica.smpp.TCPIPConnection;
import com.logica.smpp.TimeoutException;
import com.logica.smpp.pdu.BindReceiver;
import com.logica.smpp.pdu.BindRequest;
import com.logica.smpp.pdu.DeliverSM;
import com.logica.smpp.pdu.DeliverSMResp;
import com.logica.smpp.pdu.EnquireLinkResp;
import com.logica.smpp.pdu.PDU;
import com.logica.smpp.pdu.Response;
import com.logica.smpp.pdu.WrongLengthOfStringException;

import java.io.IOException;
import java.util.Vector;
import org.apache.log4j.Logger;
import org.saf.settings.SMPPSettings;
import org.saf.settings.SMSCChannelSettings;

/**
*
* @author stefano.gabrielli
*/
class ReceiverAgent extends BaseSMPPAgent {
   
    private static final Logger logger = Logger.getLogger(ReceiverAgent.class);

    private Vector<MOListener> moListeners = null;
   
    ReceiverAgent(String channel, SMSCChannelSettings channelSettings,
      SMPPSettings smppSettings){
      super(channel, channelSettings, smppSettings);
        init(channel);
    }
   
    void addMOListener(MOListener moListener) {
      this.moListeners.add(moListener);
    }

    void sendDeliverSMResponse(DeliverSMResp response) {
      try {
      this.session.respond(response);
    } catch (IOException e) {
      logger.fatal("sending " + response.debugString() +
               ", received " + e.toString() + " request managed");
    }catch (Exception e) {
           logger.error("sending " + response.debugString() +
               ", received an unexpected exception " + e.toString());       
    }
    }

    @Override
  protected void workingProcess() {
    receive();
  }
 
  @Override
    protected boolean bind() {
        boolean ret = false;
       
        try{
            this.socket = SocketUtility.createSocket(this.settings, false);
            if(this.socket != null) {
              Connection conn = new TCPIPConnection(this.socket);
              this.session = new Session(conn);
              BindRequest request = getBindRequest();
              Response response = this.session.bind(request);
              if(response.getCommandStatus() == Data.ESME_ROK){
                  ret = true;
                  this.session.getReceiver().setQueueWaitTimeout(1000);
              }
              else{
                  logger.fatal("couldn't bind receiver . Status=" +
                          response.getCommandStatus());
              }
            }
        }catch(Exception ex){
            logger.fatal("error on bindReceiver exception:" + ex.toString());
        }
           
        return ret;
   
 
  @Override
  protected void localSafeStop() {
  }
 
    private void init(String channel) {
        this.moListeners = new Vector<MOListener>();
    }
   
    private void callMOListeners(DeliverSM deliverSM) {
      MOEvent moEvent = new MOEvent(deliverSM);
      for(int i = 0; i < this.moListeners.size(); i++)
      {
        this.moListeners.elementAt(i).moReceived(moEvent);
      }
    }
   
    private void receive() {
        boolean working = true;
        long timeout = this.settings.getQuerylinkTimeout() * 1000;
        while(working == true) {
          PDU pdu = null;
            try{
              pdu = this.session.receive(timeout);
            } catch(IOException e) {
                logger.fatal(e.toString() +
                    " receiving pdus, exit from process");
                working = false;
            } catch(TimeoutException e) {
              logger.fatal(e.toString() +
                  " receiving pdus, exit and unbind from process");
              unbind();
              working = false;
             } catch (Exception e) {
               logger.error(
                   "receiving pdus, received an unexpected exception " +
                  e.toString());            
             }
             if(pdu != null) { 
               if(safeStop == false) {
                 if(pdu instanceof DeliverSM) {
                   callMOListeners((DeliverSM)pdu);
                 } else {
                   logger.error("received an unexpected pdu" +
                       pdu.debugString());
                 }  
               } else {
                 logger.fatal("going out no manage of receiving pdu");
                 unbind();
                 working = false;
               }
             } else {
               if(safeStop == false) {
                 try {
                   logger.debug("send enq lnk");
                   EnquireLinkResp response =
                              this.session.enquireLink();
                   logger.trace("sent enq lnk");
                       if(response.getCommandStatus() != Data.ESME_ROK) {
                         logger.fatal("error on sending enquire link " +
                             response.getCommandStatus());
                         unbind();
                         working = false;
                      }
                 } catch(IOException e) {
                         logger.fatal(e.toString()+
                             " sending enquire link exit from process");
                         working = false;
                 } catch(TimeoutException e) {
                       logger.fatal(e.toString()+
                           " sending enquire link exit and unbind from process");
                      unbind();
                      working = false;                  
                 } catch(Exception e) {
                   logger.error(
                       "sending enquire link received an unexpected exception " +
                           e.toString());
                   unbind();
                   working = false;                  
                  
                    }
              } else {
                unbind();
                working = false;
              }
            }
        }
    }
   
    private BindRequest getBindRequest()
            throws WrongLengthOfStringException{
        BindRequest request = new BindReceiver();
        request.setSystemId(this.settings.getSystemId());
        request.setPassword(this.settings.getPassword());
        request.setSystemType(this.settings.getSystemType());
        request.setAddressRange( this.settings.getAddrTon(),
                this.settings.getAddrNpi(), this.settings.getAddressRange());
        return request;           
    }
}


TOP

Related Classes of org.saf.smppagent.ReceiverAgent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.