📄 connection.java
字号:
if (resp.getCommandId() == SMPPPacket.UNBIND_RESP && resp.getCommandStatus() == 0) setState(UNBOUND); } /** Bind this connection to the SMSC. An application must bind to an SMSC as * one of transmitter, receiver or transceiver. Binding as transmitter * allows general manipulation of messages at the SMSC including submitting * messages for delivery, cancelling, replacing and querying the state of * previously submitted messages. Binding as a receiver allows an * application to receive all messages previously queued for delivery to * it's address. The transceiver mode, which was added in version 3.4 of the * SMPP protocol, combines the functionality of both transmitter and * receiver into one connection type. * <p>The connection object will negotiate the appropriate version for the * protocol link at bind time. If the SMSC returns the SC_INTERFACE_VERSION * optional parameter in its bind response packet, the * <code>Connection</code> will read it. If the version stated by the SMSC * is older than the current version setting of the <code>Connection</code> * then the <code>Connection</code>'s version will be downgraded to that of * the SMSC's. Otherwise, the current version will be left alone. An SMSC * supporting only version 3.3 of the API will never be able to return this * optional parameter (as they were only introduced in version 3.4). The * <code>Connection</code> will therefore assume the version to be 3.3 if it * does not receive the SC_INTERFACE_VERSION in the bind response packet. * If your SMSC does support a higher version but does not supply the * SC_INTERFACE_VERSION in its bind response, you will need to use * {@link #setInterfaceVersion} after the bind operation is complete.</p> * <p>Note that it is only necessary to supply values for * <code>type, systemID</code> and <code>password</code>. The other * arguments may be left at null (or zero, as applicable).</p> * @param type connection type to use, either {@link #TRANSMITTER}, * {@link #RECEIVER} or {@link #TRANSCEIVER}. * @param systemID the system ID to identify as to the SMSC. * @param password password to use to authenticate to the SMSC. * @param systemType the system type to bind as. * @return the bind response packet. * @throws java.lang.IllegalArgumentException if a bad <code>type</code> * value is supplied. * @throws ie.omk.smpp.VersionException if an attempt is made * to bind as transceiver while using SMPP version 3.3. * @throws ie.omk.smpp.InvalidParameterValueException If any of systemID, * password, system type or address range are outside allowed bounds or the * TON or NPI is invalid. * @throws java.io.IOException If an I/O error occurs while writing the bind * packet to the output stream. * @throws ie.omk.smpp.AlreadyBoundException If the Connection is already * bound. * @throws ie.omk.smpp.SMPPProtocolExcpetion if synchronous communication is * in use and the incoming response packet violates the SMPP protocol. */ public BindResp bind(int type, String systemID, String password, String systemType) throws java.io.IOException, InvalidParameterValueException, IllegalArgumentException, AlreadyBoundException, VersionException, SMPPProtocolException { return (this.bind(type, systemID, password, systemType, 0, 0, null)); } /** Bind this connection to the SMSC. An application must bind to an SMSC as * one of transmitter, receiver or transceiver. Binding as transmitter * allows general manipulation of messages at the SMSC including submitting * messages for delivery, cancelling, replacing and querying the state of * previously submitted messages. Binding as a receiver allows an * application to receive all messages previously queued for delivery to * it's address. The transceiver mode, which was added in version 3.4 of the * SMPP protocol, combines the functionality of both transmitter and * receiver into one connection type. * <p>Note that it is only necessary to supply values for * <code>type, systemID</code> and <code>password</code>. The other * arguments may be left at null (or zero, as applicable).</p> * @param type connection type to use, either {@link #TRANSMITTER}, * {@link #RECEIVER} or {@link #TRANSCEIVER}. * @param systemID the system ID to identify as to the SMSC. * @param password password to use to authenticate to the SMSC. * @param systemType the system type to bind as. * @param typeOfNum the TON of the address to bind as. * @param numberPlan the NPI of the address to bind as. * @param addrRange the address range regular expression to bind as. * @return the bind response packet. * @throws java.lang.IllegalArgumentException if a bad <code>type</code> * value is supplied. * @throws ie.omk.smpp.VersionException if an attempt is made * to bind as transceiver while using SMPP version 3.3. * @throws ie.omk.smpp.InvalidParameterValueException If any of systemID, * password, system type or address range are outside allowed bounds. * @throws java.io.IOException If an I/O error occurs while writing the bind * packet to the output stream. * @throws ie.omk.smpp.AlreadyBoundException If the Connection is already * bound. * @throws ie.omk.smpp.message.SMPPProtocolException If synchronous comms is * in use and the incoming bind response packet violates the SMPP * specification, this exception is thrown. */ public BindResp bind(int type, String systemID, String password, String systemType, int typeOfNum, int numberPlan, String addrRange) throws java.io.IOException, InvalidParameterValueException, IllegalArgumentException, AlreadyBoundException, VersionException, SMPPProtocolException { Bind bindReq = null; switch (type) { case TRANSMITTER: bindReq = new BindTransmitter(); break; case RECEIVER: bindReq = new BindReceiver(); break; case TRANSCEIVER: if (this.interfaceVersion.isOlder(SMPPVersion.V34)) { throw new VersionException("Cannot bind as " + "transceiver " + interfaceVersion.toString()); } bindReq = new BindTransceiver(); break; default: throw new IllegalArgumentException("No such connection type."); } this.connectionType = type; logger.info("Binding to the SMSC as type " + type); bindReq.setVersion(interfaceVersion); bindReq.setSystemId(systemID); bindReq.setPassword(password); bindReq.setSystemType(systemType); bindReq.setAddressTon(typeOfNum); bindReq.setAddressNpi(numberPlan); bindReq.setAddressRange(addrRange); return ((BindResp)sendRequestInternal(bindReq)); } /** Unbind from the SMSC. This method will unbind the SMPP protocol * connection from the SMSC. No further SMPP operations will be possible * once unbound. If the calling application is using sync mode, it should be * sure there are no incoming packets awaiting a response (check using * {@link #packetAvailable}), otherwise an * <code>IllegalStateException</code> may be thrown. Note that this method * will <b>not</b> close the underlying network connection. * @return The Unbind response packet, or null if asynchronous * communication is being used. * @throws ie.omk.smpp.NotBoundException if the connection is not yet * bound. * @throws ie.omk.smpp.message.SMPPProtocolException If synchronous comms is * in use and the incoming unbind_resp packet violates the SMPP * specification, this exception is thrown. * @throws java.io.IOException If an I/O error occurs while writing the * unbind request or reading the unbind response. */ public UnbindResp unbind() throws java.io.IOException, NotBoundException, SMPPProtocolException { if((state != BOUND) || !(link.isConnected())) throw new NotBoundException(); try { logger.info("Unbinding from the SMSC"); Unbind u = (Unbind)newInstance(SMPPPacket.UNBIND); return ((UnbindResp)sendRequestInternal(u)); } catch (VersionException x) { // impossible...every version supports unbind! throw new SMPPRuntimeException("Internal smppapi error"); } catch (BadCommandIDException x) { // similarly impossible! throw new SMPPRuntimeException("Internal smppapi error"); } } /** Unbind from the SMSC. This method is used to acknowledge an unbind * request from the SMSC. * @throws ie.omk.smpp.NotBoundException if the connection is not currently * bound. * @throws ie.omk.smpp.AlreadyBoundException if no unbind request has * been received from the SMSC. * @throws java.io.IOException If an I/O error occurs while writing the * response packet to the network connection. */ public void unbind(UnbindResp ubr) throws java.io.IOException, ie.omk.smpp.SMPPException { if (state != UNBINDING) throw new NotBoundException("Link is not connected."); if (!(link.isConnected())) throw new AlreadyBoundException("No unbind request received."); sendResponse(ubr); } /** Force the SMPP connection down. * Only use this method once it's full sure that graceful unbinding and * disconnection isn't going to work. This method cleans up it's internal * state, forcing the network connection closed and terminating the receiver * thread if necessary. * <p>If you end up having to use this method to terminate a Connection, it * is advisable not to attempt to reuse the connection at all. Create a new * object and start from scratch. Use of this method indicates something * seriously wrong!</p> */ public void force_unbind() { logger.warn("Attempting to force SMPP connection down."); try { setState(UNBOUND); // The thread must DIE!!!! if(rcvThread != null && rcvThread.isAlive()) { try { // Wait to see if the thread will terminate due to the state // becoming UNBOUND. Thread.sleep(1000); } catch (InterruptedException x) { logger.debug("Interrupted exception waiting on receiver to die", x); } if (rcvThread != null) logger.error("Listener thread has not died."); rcvThread = null; } link.close(); } catch(Throwable t) { logger.warn("Exception when trying to force unbind", t); } return; } /** Acknowledge an EnquireLink received from the Smsc * @throws java.io.IOException If an I/O error occurs while writing to the * network connection. */ public void ackEnquireLink(EnquireLink rq) throws java.io.IOException { EnquireLinkResp resp = new EnquireLinkResp(rq); sendResponse(resp); logger.info("enquire_link_resp sent."); } /** Do a confidence check on the SMPP link to the SMSC. * @return The Enquire link response packet or null if asynchronous * communication is in use. * @throws java.io.IOException If an I/O error occurs while writing to the * network connection. * @throws ie.omk.smpp.message.SMPPProtocolException If synchronous * communications is in use and the incoming enquire_link_resp packet * violates the SMPP specification, this exception is thrown. */ public EnquireLinkResp enquireLink() throws java.io.IOException, SMPPProtocolException { try { EnquireLink s = (EnquireLink)newInstance(SMPPPacket.ENQUIRE_LINK); SMPPResponse resp = sendRequest(s); logger.debug("enquire_link request sent."); if (resp != null) logger.debug("enquire_link_response received."); return ((EnquireLinkResp)resp); } catch (BadCommandIDException x) { throw new SMPPRuntimeException("Internal smppapi error"); } } /** Get the type of this SMPP connection. The connection type is one of * TRANSMITTER, RECEIVER or TRANSCEIVER. */ public int getConnectionType() { return (connectionType); } /** Report whether the connection is bound or not. * @return true if the connection is bound. */ public boolean isBound() { return (state == BOUND); } /** Reset this connection's sequence numbering to the beginning. The * underlying SequenceNumberScheme's reset method is called to start from * that sequence's 'beginning'. * @throws ie.omk.smpp.AlreadyBoundException if the connection is * currently bound to the SMSC. */ public void reset() throws AlreadyBoundException { if(state == BOUND) { logger.warn("Attempt to reset sequence numbering on a bound connection"); throw new AlreadyBoundException("Cannot reset connection while bound"); } if (this.seqNumScheme != null) this.seqNumScheme.reset(); logger.info("Sequence numbering reset."); } /** Set the sequence numbering scheme for this connection. A sequence * numbering scheme determines what sequence number each SMPP packet will * have. By default, {@link ie.omk.smpp.util.DefaultSequenceScheme} is used, * which will begin with sequence number 1 and increase the number by 1 for * each packet thereafter. * <p>If the application sets the <code>scheme</code> to null, it is * responsible for maintaining and setting the sequence number for each SMPP * request it sends to the SMSC. * @see ie.omk.smpp.util.SequenceNumberScheme * @see ie.omk.smpp.message.SMPPPacket#setSequenceNum */ public void setSeqNumScheme(SequenceNumberScheme scheme) { this.seqNumScheme = scheme; } /** Get the current sequence numbering scheme object being used by this * connection. */ public SequenceNumberScheme getSeqNumScheme() { return (this.seqNumScheme); } /** Read in the next packet from the SMSC link. * If asynchronous communications is in use, calling this method results in * an SMPPException as the listener thread will be hogging the input stream * of the socket connection. * @return The next SMPP packet from the SMSC. * @throws java.io.IOException If an I/O error occurs while reading from * the network connection. * @throws ie.omk.smpp.InvalidOperationException If asynchronous comms * is in use. * @throws ie.omk.smpp.message.SMPPProtocolException if the incoming data * violates the SMPP protocol specifications. */ public SMPPPacket readNextPacket() throws java.io.IOException, InvalidOperationException, SMPPProtocolException { if (asyncComms) { throw new InvalidOperationException("Asynchronous comms in use.");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -