session.java
来自「Logica lastest SMPP API」· Java 代码 · 共 1,451 行 · 第 1/4 页
JAVA
1,451 行
* <code>Receiver</code> from the SMSC. * Note that this method implicitly sets asynchronous type of * receiving. * @param pduListener the listener which processes the received PDUs */ private void setServerPDUEventListener(ServerPDUEventListener pduListener) { this.pduListener = pduListener; receiver.setServerPDUEventListener(pduListener); asynchronous = pduListener != null; } /** * Returns the current <code>ServerPDUEventListener</code> set for * this session. */ private ServerPDUEventListener getServerPDUEventListener() { return pduListener; } /** * Sets the type of the session. The type can be either ESME or MC viewed * from the side where the instance of the <code>Session</code> is used. * Set the type of the session before opening the session. * @param type the new type of the session * @see #getType() * @see #TYPE_ESME * @see #TYPE_MC */ public void setType(int type) { this.type = type; } /** * Returns the type of the session. * @return the current type of the session. * @see #setType(int) * @see #TYPE_ESME * @see #TYPE_MC */ public int getType() { return type; } /** * Opens connection and binds to SMSC using the bind method provided * as bindReq parameter. Binds synchronously as no server pdu listener * is provided. On details about bind see * <a href="#bind(BindRequest,ServerPDUEventListener)">bind(BindRequest,ServerPDUEventListener)</a>. * @see #bind(BindRequest,ServerPDUEventListener) */ final public BindResponse bind(BindRequest bindReq) throws ValueNotSetException, TimeoutException, PDUException, IOException, WrongSessionStateException { checkState(bindReq); return bind(bindReq,null); } /** * Opens connection and binds to SMSC using the bind method provided * as <code>bindReq</code> parameter. For transmittong PDUs creates instance * of <code>Transmitter</code> class, for receiving PDUs from SMSC * creates instance of <code>Receiver</code> class.<br> * Receiver starts an extra thread * which receives PDUs from connection and puts them into a queue * (if synchronous) or processes them using <code>pduListener</code>. * If the bind to SMSC isn't successfull, the thread is stopped and * the connection is closed.<br> * If the variable <code>pduListener</code> is not <code>null</code>, * the session is asynchronous, otherwise the session is synchronous. * Note that the <code>bind</code> method is <emp>always</emp> synchronous, * regardless of the value of the <code>pduListener</code> variable. * <p> * See "SMPP Protocol Specification 3.4, 4.1 BIND Operation." * * @param bindReq the bind request * * @exception IOException exception during communication * @exception PDUException incorrect format of PDU * @exception TimeoutException rest of data not received for too long time * @exception ValueNotSetException optional param not set but requested * @see BindRequest * @see BindResponse * @see BindTransmitter * @see BindReceiver * @see BindTransciever */ final public BindResponse bind(BindRequest bindReq, ServerPDUEventListener pduListener) throws ValueNotSetException, TimeoutException, PDUException, IOException, WrongSessionStateException { checkState(bindReq); if (bound) { // exception? yes, checked above return null; } else { open(); transmitter = new Transmitter(connection); receiver = new Receiver(transmitter,connection); BindResponse bindResp = (BindResponse)send(bindReq,false); bound = ((bindResp != null) && (bindResp.getCommandStatus() == Data.ESME_ROK)); if (!bound) { //receiver.stop(); close(); } else { receiver.start(); if (bindReq.isTransmitter()) { if (bindReq.isReceiver()) { setState(STATE_TRANSCEIVER); } else { setState(STATE_TRANSMITTER); } } else { setState(STATE_RECEIVER); } setServerPDUEventListener(pduListener); } return bindResp; } } /** * Sends an outbind PDU over the connection. For sending outbind * PDU the session must not be bound to smsc (!), this is the * logic of outbind operation. Don't mess it up with receiving * outbind PDUs from SMSC! Also this operation is only allowed when your * session acts as SMSC (MC) session. ESMEs can't send outbinds. * <p> * See SMPP Protocol Specification 3.4, 4.1.7 OUTBIND Operation. * * @param request The outbind PDU. * * @exception IOException exception during communication * @exception PDUException incorrect format of PDU * @exception TimeoutException rest of data not received for too long time * @exception ValueNotSetException optional param not set but requested * (this exception doesn't apply for outbind) * @see Outbind * @see OutbindReceiver */ final public void outbind(Outbind request) throws ValueNotSetException, TimeoutException, PDUException, IOException, WrongSessionStateException { checkState(request); send(request); } /** * Stops <code>receiver</code> (if applicable), unbinds from SMSC * by sending <code>Unbind</code> PDU, waits for <code>UnbindResp</code> * and then closes the connection.<br> * Note that the <code>unbind</code> method is <emp>always</emp> synchronous * or asynchronous depending on the value of the <code>asynchronous</code> * variable. * <p> * See "SMPP Protocol Specification 3.4, 4.2 UNBIND Operation." * * @return response PDU if successfully unbound, i.e. if still not bound * after call to the method; null if unbind was not successful, i.e. * no response has been received for unbind PDU * * @exception IOException exception during communication * @exception PDUException incorrect format of PDU * @exception TimeoutException rest of data not received for too long time * @exception ValueNotSetException optional param not set but requested * (this exception doesn't apply for unbind) * @see Unbind * @see UnbindResp */ final public UnbindResp unbind() throws ValueNotSetException, TimeoutException, PDUException, IOException, WrongSessionStateException { UnbindResp unbindResp = null; if (bound) { Unbind unbindReq = new Unbind(); checkState(unbindReq); ServerPDUEventListener origListener = null; if (asynchronous) { // we must assign the number now as we'll start waiting for // the response before we actually send the request unbindReq.assignSequenceNumber(); origListener = getServerPDUEventListener(); UnbindServerPDUEventListener unbindListener = new UnbindServerPDUEventListener(this,origListener,unbindReq); setServerPDUEventListener(unbindListener); synchronized (unbindListener) { send(unbindReq); try { unbindListener.wait(receiver.getReceiveTimeout()); unbindResp = unbindListener.getUnbindResp(); } catch (InterruptedException e) { // unbind reponse wasn't received in time } } } else { debug.write(DSESS,"going to unbound sync session"); unbindResp = (UnbindResp)send(unbindReq); } bound = (unbindResp == null); if (!bound) { setState(STATE_OPENED); receiver.stop(); receiver = null; transmitter = null; close(); } else { // restore the listener - unbind unsuccessfull setServerPDUEventListener(origListener); } } return unbindResp; } /** * Sends GenericNack PDU provided as parameter. * <p> * See "SMPP Protocol Specification 3.4, 4.3 GENERIC_NACK Operation." * * @param request The generic nack PDU. * * @exception IOException exception during communication * @exception TimeoutException rest of data not received for too long time * @exception ValueNotSetException optional param not set but requested * (this exception doesn't apply for genericNack) * @see GenericNack */ final public void genericNack(GenericNack response) throws ValueNotSetException, TimeoutException, IOException, WrongSessionStateException { checkState(response); try { respond(response); } catch (WrongSessionStateException e) { debug.write("strange, generic nack thrown "+e); debug.write("this shouldn't happend"); event.write(e,"Unexpected exeption caught"); } } /** * Creates and sends <code>GenericNack</code> PDU with command status * and sequence number provided as parameters. * <p> * See "SMPP Protocol Specification 3.4, 4.3 GENERIC_NACK Operation." * * @param commandStatus the code of error to be reported * @param sequenceNumber the sequence number of the PDU the GenericNack * is related to * @exception IOException exception during communication * @exception TimeoutException rest of data not received for too long time * @exception ValueNotSetException optional param not set but requested * (this exception doesn't apply for genericNack) * @see GenericNack */ final public void genericNack(int commandStatus, int sequenceNumber) throws ValueNotSetException, TimeoutException, IOException, WrongSessionStateException { GenericNack gnack = new GenericNack(commandStatus,sequenceNumber); checkState(gnack); genericNack(gnack); } /** * Submits provided <code>SubmitSM</code> PDU to SMSC and returns response * to the submission. * <p> * See "SMPP Protocol Specification 3.4, 4.4 SUBMIT_SM Operation." * * @param request the pdu to be submitted * @return correspondent response pdu * * @exception IOException exception during communication * @exception PDUException incorrect format of PDU * @exception TimeoutException rest of data not received for too long time * @exception ValueNotSetException optional param not set but requested * @see SubmitSM * @see SubmitSMResp */ final public SubmitSMResp submit(SubmitSM request) throws ValueNotSetException, TimeoutException, PDUException, IOException, WrongSessionStateException { checkState(request); return (SubmitSMResp)send(request); } /** * Submits provided <code>SubmitMultiSM</code> PDU to SMSC and returns * response to the submission. * <p> * See "SMPP Protocol Specification 3.4, 4.5 SUBMIT_MULTI Operation." * * @param request the submit multi pdu to be submitted * @return correspondent response pdu * * @exception IOException exception during communication * @exception PDUException incorrect format of PDU * @exception TimeoutException rest of data not received for too long time * @exception ValueNotSetException optional param not set but requested * @see SubmitMultiSM * @see SubmitMultiSMResp */ final public SubmitMultiSMResp submitMulti(SubmitMultiSM request) throws ValueNotSetException, TimeoutException, PDUException, IOException, WrongSessionStateException { checkState(request); return (SubmitMultiSMResp)send(request); } /** * Submits provided <code>DeliverSM</code> PDU to SMSC and returns * response to the submission. * <p> * See "SMPP Protocol Specification 3.4, 4.6 DELIVER_SM Operation." * * @param request the deliver pdu to be submitted * @return correspondent response pdu * * @exception IOException exception during communication * @exception PDUException incorrect format of PDU * @exception TimeoutException rest of data not received for too long time * @exception ValueNotSetException optional param not set but requested * @see DeliverSM * @see DeliverSMResp */ final public DeliverSMResp deliver(DeliverSM request) throws ValueNotSetException, TimeoutException, PDUException, IOException, WrongSessionStateException { checkState(request); return (DeliverSMResp)send(request); } /** * Submits provided <code>DataSM</code> PDU to SMSC and returns * response to the submission. * <p> * See "SMPP Protocol Specification 3.4, 4.7 DATA_SM Operation." * * @param request the data pdu to be submitted * @return correspondent response pdu * * @exception IOException exception during communication * @exception PDUException incorrect format of PDU * @exception TimeoutException rest of data not received for too long time * @exception ValueNotSetException optional param not set but requested * @see DataSM * @see DataSMResp */ final public DataSMResp data(DataSM request) throws ValueNotSetException, TimeoutException, PDUException, IOException, WrongSessionStateException { checkState(request); return (DataSMResp)send(request); } /**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?