⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smpppacket.java

📁 SMPP(点到点短消息协议)的java实现
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            }        } else {            this.serviceType = null;        }    }    /** Get the service type. */    public String getServiceType() {        return serviceType;    }    /**     * Set the scheduled delivery time for the short message.     *      * @param d     *            The date and time the message should be delivered.     */    public void setDeliveryTime(SMPPDate d) {        this.deliveryTime = d;    }    /**     * Set the scheduled delivery time for the short message.     *      * @param d     *            The date and time the message should be delivered.     */    public void setDeliveryTime(Date d) {        this.deliveryTime = new SMPPDate(d);    }    /**     * Get the current value of the scheduled delivery time for the short     * message.     */    public SMPPDate getDeliveryTime() {        return deliveryTime;    }    /**     * Set the expiry time of the message. If the message is not delivered by     * time 'd', it will be cancelled and never delivered to it's destination.     *      * @param d     *            the date and time the message should expire.     */    public void setExpiryTime(SMPPDate d) {        expiryTime = d;    }    /**     * Set the expiry time of the message. If the message is not delivered by     * time 'd', it will be cancelled and never delivered to it's destination.     */    public void setExpiryTime(Date d) {        expiryTime = new SMPPDate(d);    }    /**     * Get the current expiry time of the message.     */    public SMPPDate getExpiryTime() {        return expiryTime;    }    /**     * Set the final date of the message. The final date is the date and time     * that the message reached it's final destination.     *      * @param d     *            the date the message was delivered.     */    public void setFinalDate(SMPPDate d) {        finalDate = d;    }    /**     * Set the final date of the message. The final date is the date and time     * that the message reached it's final destination.     *      * @param d     *            the date the message was delivered.     */    public void setFinalDate(Date d) {        this.finalDate = new SMPPDate(d);    }    /**     * Get the final date of the message.     */    public SMPPDate getFinalDate() {        return finalDate;    }    /**     * Set the message Id. Each submitted short message is assigned an Id by the     * SMSC which is used to uniquely identify it. SMPP v3.3 message Ids are     * hexadecimal numbers up to 9 characters long. This gives them a range of     * 0x0 - 0xffffffff.     * <p>     * SMPP v3.4 Ids, on the other hand, are opaque objects represented as     * C-Strings assigned by the SMSC and can be up to 64 characters (plus 1     * nul-terminator).     *      * @param id     *            The message's id.     * @throws ie.omk.smpp.message.InvalidParameterValueException     *             If the message ID is invalid.     */    public void setMessageId(String id) throws InvalidParameterValueException {        if (id != null) {            if (version.validateMessageId(id)) {                this.messageId = id;            } else {                throw new InvalidParameterValueException("Bad message Id", id);            }        } else {            this.messageId = null;        }    }    /**     * Get the message id.     */    public String getMessageId() {        return this.messageId;    }    /**     * Set the message status. The message status (or message state) describes     * the current state of the message at the SMSC. There are a number of     * states defined in the SMPP specification.     *      * @param st     *            The message status.     * @see ie.omk.smpp.util.PacketStatus     */    public void setMessageStatus(int st) throws InvalidParameterValueException {        if (version.validateMessageState(st)) {            this.messageStatus = st;        } else {            throw new InvalidParameterValueException(                    "Invalid message state", st);        }    }    /**     * Get the message status.     */    public int getMessageStatus() {        return this.messageStatus;    }    /**     * Set the error code.     *      * @param code     *            The error code.     */    public void setErrorCode(int code) throws InvalidParameterValueException {        if (version.validateErrorCode(code)) {            errorCode = code;        } else {            throw new InvalidParameterValueException("Invalid error code", code);        }    }    /**     * Get the error code.     */    public int getErrorCode() {        return errorCode;    }    /**     * Get the optional parameter (TLV) table.     *      * @see ie.omk.smpp.message.tlv.TLVTable     */    public TLVTable getTLVTable() {        return tlvTable;    }    /**     * Set the optional parameter (TLV) table. This method discards the entire     * optional paramter table and replaces it with <code>table</code>. The     * discarded table is returned. If <code>null</code> is passed in, a new,     * empty TLVTable object will be created.     *      * @see ie.omk.smpp.message.tlv.TLVTable     * @return the old tlvTable.     */    public TLVTable setTLVTable(TLVTable table) {        TLVTable t = this.tlvTable;        if (table == null) {            this.tlvTable = new TLVTable();        } else {            this.tlvTable = table;        }        return t;    }    /**     * Set an optional parameter. This is a convenience method and merely calls     * {@link ie.omk.smpp.message.tlv.TLVTable#set}on this message's optional     * parameter table.     *      * @param tag     *            the tag of the parameter to set.     * @param value     *            the value object to set.     * @throws ie.omk.smpp.message.tlv.BadValueTypeException     *             if the type of <code>value</code> is incorrect for the     *             <code>tag</code>.     * @return the previous value of the parameter, or null if it was unset.     */    public Object setOptionalParameter(Tag tag, Object value) {        return tlvTable.set(tag, value);    }    /**     * Get an optional parameter. This is a convenience method and merely calls     * {@link ie.omk.smpp.message.tlv.TLVTable#get}on this message's optional     * parameter table.     *      * @param tag     *            the tag of the parameter value to get.     */    public Object getOptionalParameter(Tag tag) {        return tlvTable.get(tag);    }    /**     * Check if a particular optional parameter is set. This is a convenience     * method and merely calls {@link ie.omk.smpp.message.tlv.TLVTable#isSet}on     * this message's optional parameter table.     *      * @param tag     *            the tag of the parameter to check.     * @return true if the parameter is set, false if it is not.     */    public boolean isSet(Tag tag) {        return tlvTable.isSet(tag);    }    /**     * Set the alphabet encoding and dcs value for this message. The data_coding     * (dcs) value of this message will be set to the return value of     * {@link MessageEncoding#getDataCoding}.     *      * @param enc     *            The alphabet to use. If null, use DefaultAlphabetEncoding.     * @see ie.omk.smpp.util.AlphabetEncoding     * @see ie.omk.smpp.util.DefaultAlphabetEncoding     */    public void setAlphabet(AlphabetEncoding enc) {        if (enc == null) {            this.encoding = EncodingFactory.getInstance().getDefaultAlphabet();        } else {            this.encoding = enc;        }        this.dataCoding = enc.getDataCoding();    }    /**     * Set the alphabet encoding for this message with an alternate dcs.     * <code>enc</code> will be used to encode the message but     * <code>dcs</code> will be used as the data coding value. This method is     * useful when the SMSC uses an alternate value to those build-in to the     * smppapi.     */    public void setAlphabet(AlphabetEncoding enc, int dcs) {        if (enc == null) {            this.encoding = EncodingFactory.getInstance().getDefaultAlphabet();            this.dataCoding = enc.getDataCoding();        } else {            this.encoding = enc;            this.dataCoding = dcs;        }    }    /**     * Set the message encoding handler class for this packet.     */    public void setMessageEncoding(MessageEncoding enc) {        if (enc == null) {            this.encoding = EncodingFactory.getInstance().getDefaultAlphabet();        } else {            this.encoding = enc;        }    }    /**     * Get the current message encoding object.     */    public MessageEncoding getMessageEncoding() {        return this.encoding;    }    /**     * Return a String representation of this packet. This method does not     * return any value which is useful programatically...it returns a     * description of the packet's header as follows: <br>     * <code>"SMPP(l:[len], c:[commandId], s:[status], n:[sequence])"</code>     */    public String toString() {        return new StringBuffer("SMPP(l:").append(                Integer.toString(getLength())).append(", c:0x").append(                Integer.toHexString(commandId)).append(", s:").append(                Integer.toString(commandStatus)).append(", n:").append(                Integer.toString(sequenceNum)).append(")").toString();    }    /**     * Encode the body of the SMPP Packet to the output stream. Sub classes     * should override this method to output their packet-specific fields. This     * method is called from SMPPPacket.writeTo(java.io.OutputStream) to encode     * the message.     *      * @param out     *            The output stream to write to.     * @throws java.io.IOException     *             if there's an error writing to the output stream.     */    protected void encodeBody(OutputStream out) throws java.io.IOException {        // some packets ain't got a body...provide a default adapter instead of        // making it abstract.    }    /**     * Write the byte representation of this SMPP packet to an OutputStream     *      * @param out     *            The OutputStream to use     * @throws java.io.IOException     *             if there's an error writing to the output stream.     */    public final void writeTo(OutputStream out) throws java.io.IOException {        this.writeTo(out, true);    }    /**     * Write the byte representation of this SMPP packet to an OutputStream     *      * @param out     *            The OutputStream to use     * @param withOptional     *            true to send optional parameters over the link, false to only     *            write the mandatory parameters.     * @throws java.io.IOException     *             if there's an error writing to the output stream.     */    public final void writeTo(OutputStream out, boolean withOptional)            throws java.io.IOException {        int commandLen = getLength();        SMPPIO.writeInt(commandLen, 4, out);        SMPPIO.writeInt(commandId, 4, out);        SMPPIO.writeInt(commandStatus, 4, out);        SMPPIO.writeInt(sequenceNum, 4, out);        encodeBody(out);        if (withOptional) {            tlvTable.writeTo(out);        }    }    /**     * Decode an SMPP packet from a byte array.     *      * @param b     *            the byte array to read the SMPP packet's fields from.     * @param offset     *            the offset into <code>b</code> to begin reading the packet     *            fields from.     * @throws ie.omk.smpp.message.SMPPProtocolException     *             if there is an error parsing the packet fields.     */    public void readFrom(byte[] b, int offset) throws SMPPProtocolException {        // Clear out the TLVTable..        tlvTable.clear();        if (b.length < (offset + 16)) {            throw new SMPPProtocolException("Not enough bytes for a header: "                    + (b.length - (offset + 16)));        }        int len = SMPPIO.bytesToInt(b, offset, 4);        int id = SMPPIO.bytesToInt(b, offset + 4, 4);        if (id != commandId) {            // Command type mismatch...ye can't do that, lad!            throw new SMPPProtocolException(                    "The packet on the input stream is not"                            + " the same as this packet's type.");        }        if (b.length < (offset + len)) {            // not enough bytes there for me to read in, buddy!            throw new SMPPProtocolException(                    "Header specifies the packet length is longer"                            + " than the number of bytes available.");        }        commandStatus = SMPPIO.bytesToInt(b, offset + 8, 4);        sequenceNum = SMPPIO.bytesToInt(b, offset + 12, 4);        try {            if (commandStatus == 0) {                // Read the mandatory body parameters..                int ptr = 16 + offset;                readBodyFrom(b, ptr);                // Read the optional parameters..                int bl = getBodyLength();                len -= 16 + bl;                if (len > 0) {                    tlvTable.readFrom(b, ptr + bl, len);                }            }        } catch (ArrayIndexOutOfBoundsException x) {            throw new SMPPProtocolException(                    "Ran out of bytes to read for packet body", x);        }        // Set the message encoding type        if (dataCoding != 0) {            encoding = EncodingFactory.getInstance().getEncoding(dataCoding);        }        if (encoding == null) {            encoding = EncodingFactory.getInstance().getDefaultAlphabet();        }    }    /**     * Read this packet's mandatory parameters from a byte array.     *      * @param b     *            the byte array to read the mandatory parameters from.     * @param offset     *            the offset into b that the mandatory parameter's begin at.     * @throws ie.omk.smpp.message.SMPPProtocolException     *             if there is an error parsing the packet fields.     */    protected abstract void readBodyFrom(byte[] b, int offset)            throws SMPPProtocolException;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -