Search in sources :

Example 1 with PduResponse

use of com.zx.sms.codec.smpp.msg.PduResponse in project SMSGate by Lihuanghe.

the class SMPPSessionLoginManager method failedLogin.

@Override
protected void failedLogin(ChannelHandlerContext ctx, Object msg, long status) {
    if (msg instanceof BaseBind) {
        logger.error("Connected error status :{},msg : {}", status, msg);
        BaseBind message = (BaseBind) msg;
        // 认证失败
        PduResponse resp = message.createResponse();
        resp.setCommandStatus((int) SmppConstants.STATUS_BINDFAIL);
        ChannelFuture promise = ctx.writeAndFlush(resp);
        final ChannelHandlerContext finalctx = ctx;
        promise.addListener(new GenericFutureListener() {

            public void operationComplete(Future future) throws Exception {
                finalctx.close();
            }
        });
    } else {
        logger.error("connect msg type error : {}", msg);
        ctx.close();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) PduResponse(com.zx.sms.codec.smpp.msg.PduResponse) BaseBind(com.zx.sms.codec.smpp.msg.BaseBind) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) TlvConvertException(com.zx.sms.codec.smpp.TlvConvertException)

Example 2 with PduResponse

use of com.zx.sms.codec.smpp.msg.PduResponse in project SMSGate by Lihuanghe.

the class DefaultPduTranscoder method encode.

@Override
public ByteBuf encode(Pdu pdu, ByteBufAllocator allocator) throws UnrecoverablePduException, RecoverablePduException {
    // see if we can map the command status into a message
    if (pdu instanceof PduResponse) {
        PduResponse response = (PduResponse) pdu;
        if (response.getResultMessage() == null) {
            response.setResultMessage(context.lookupResultMessage(pdu.getCommandStatus()));
        }
    }
    // should really be the only interface creating PDUs
    if (!pdu.hasCommandLengthCalculated()) {
        pdu.calculateAndSetCommandLength();
    }
    if (!pdu.hasSequenceNumberAssigned()) {
        pdu.setSequenceNumber((int) DefaultSequenceNumberUtil.getSequenceNo());
    }
    // @trustin:
    // You don't need to call buffer.order(ByteOrder.BIG_ENDIAN).  It's always big endian in Netty 4.
    // Instead of allocating an unpooled buffer, please use ByteBufAllocator.buffer(...). To do this,
    // PduTranscoder.encode() must have ByteBufAllocator as an additional parameter.  In your handler,
    // you'll usually get your allocator using ChannelHandlerContext.alloc() (or Channel.alloc()) method:
    // ByteBuf pduBuf = transcoder.encode(ctx.alloc());
    // The default implementation is an unpooled allocator, so you will not see the difference.
    // In your bootstrap code, set the following option:
    // serverBootstrap.childOption(
    // ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    // clientBootstrap.option(
    // ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    // create the buffer and add the header
    ByteBuf buffer = allocator.buffer(pdu.getCommandLength());
    // TODO: directBuffer?
    // buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.writeInt(pdu.getCommandLength());
    buffer.writeInt(pdu.getCommandId());
    buffer.writeInt(pdu.getCommandStatus());
    buffer.writeInt(pdu.getSequenceNumber());
    // add mandatory body (a noop if no body exists)
    pdu.writeBody(buffer);
    // add optional parameters (a noop if none exist)
    pdu.writeOptionalParameters(buffer, context);
    // from earlier -- if it doesn't match, the our encoding process went awry
    if (buffer.readableBytes() != pdu.getCommandLength()) {
        throw new NotEnoughDataInBufferException("During PDU encoding the expected commandLength did not match the actual encoded (a serious error with our own encoding process)", pdu.getCommandLength(), buffer.readableBytes());
    }
    return buffer;
}
Also used : PduResponse(com.zx.sms.codec.smpp.msg.PduResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with PduResponse

use of com.zx.sms.codec.smpp.msg.PduResponse in project SMSGate by Lihuanghe.

the class DefaultPduTranscoder method doDecode.

protected Pdu doDecode(int commandLength, ByteBuf buffer) throws UnrecoverablePduException, RecoverablePduException {
    // skip the length field because we already parsed it
    buffer.skipBytes(SmppConstants.PDU_INT_LENGTH);
    // read the remaining portion of the PDU header
    int commandId = buffer.readInt();
    int commandStatus = buffer.readInt();
    int sequenceNumber = buffer.readInt();
    Pdu pdu = null;
    // any command id with its 31st bit set to true is a response
    if (PduUtil.isRequestCommandId(commandId)) {
        if (commandId == SmppConstants.CMD_ID_ENQUIRE_LINK) {
            pdu = new EnquireLink();
        } else if (commandId == SmppConstants.CMD_ID_DELIVER_SM) {
            pdu = new DeliverSm();
        } else if (commandId == SmppConstants.CMD_ID_SUBMIT_SM) {
            pdu = new SubmitSm();
        } else if (commandId == SmppConstants.CMD_ID_DATA_SM) {
            pdu = new DataSm();
        } else if (commandId == SmppConstants.CMD_ID_CANCEL_SM) {
            pdu = new CancelSm();
        } else if (commandId == SmppConstants.CMD_ID_QUERY_SM) {
            pdu = new QuerySm();
        } else if (commandId == SmppConstants.CMD_ID_REPLACE_SM) {
            pdu = new ReplaceSm();
        } else if (commandId == SmppConstants.CMD_ID_BIND_TRANSCEIVER) {
            pdu = new BindTransceiver();
        } else if (commandId == SmppConstants.CMD_ID_BIND_TRANSMITTER) {
            pdu = new BindTransmitter();
        } else if (commandId == SmppConstants.CMD_ID_BIND_RECEIVER) {
            pdu = new BindReceiver();
        } else if (commandId == SmppConstants.CMD_ID_UNBIND) {
            pdu = new Unbind();
        } else if (commandId == SmppConstants.CMD_ID_ALERT_NOTIFICATION) {
            pdu = new AlertNotification();
        } else {
            pdu = new PartialPdu(commandId);
        }
    } else {
        if (commandId == SmppConstants.CMD_ID_SUBMIT_SM_RESP) {
            pdu = new SubmitSmResp();
        } else if (commandId == SmppConstants.CMD_ID_DELIVER_SM_RESP) {
            pdu = new DeliverSmResp();
        } else if (commandId == SmppConstants.CMD_ID_DATA_SM_RESP) {
            pdu = new DataSmResp();
        } else if (commandId == SmppConstants.CMD_ID_CANCEL_SM_RESP) {
            pdu = new CancelSmResp();
        } else if (commandId == SmppConstants.CMD_ID_QUERY_SM_RESP) {
            pdu = new QuerySmResp();
        } else if (commandId == SmppConstants.CMD_ID_REPLACE_SM_RESP) {
            pdu = new ReplaceSmResp();
        } else if (commandId == SmppConstants.CMD_ID_ENQUIRE_LINK_RESP) {
            pdu = new EnquireLinkResp();
        } else if (commandId == SmppConstants.CMD_ID_BIND_TRANSCEIVER_RESP) {
            pdu = new BindTransceiverResp();
        } else if (commandId == SmppConstants.CMD_ID_BIND_RECEIVER_RESP) {
            pdu = new BindReceiverResp();
        } else if (commandId == SmppConstants.CMD_ID_BIND_TRANSMITTER_RESP) {
            pdu = new BindTransmitterResp();
        } else if (commandId == SmppConstants.CMD_ID_UNBIND_RESP) {
            pdu = new UnbindResp();
        } else if (commandId == SmppConstants.CMD_ID_GENERIC_NACK) {
            pdu = new GenericNack();
        } else {
            pdu = new PartialPduResp(commandId);
        }
    }
    // set pdu header values
    pdu.setCommandLength(commandLength);
    pdu.setCommandStatus(commandStatus);
    pdu.setSequenceNumber(sequenceNumber);
    // check if we need to throw an exception
    if (pdu instanceof PartialPdu) {
        throw new UnknownCommandIdException(pdu, "Unsupported or unknown PDU request commandId [0x" + HexUtil.toHexString(commandId) + "]");
    } else if (pdu instanceof PartialPduResp) {
        throw new UnknownCommandIdException(pdu, "Unsupported or unknown PDU response commandId [0x" + HexUtil.toHexString(commandId) + "]");
    }
    // see if we can map the command status into a message
    if (pdu instanceof PduResponse) {
        PduResponse response = (PduResponse) pdu;
        response.setResultMessage(context.lookupResultMessage(commandStatus));
    }
    try {
        // parse pdu body parameters (may throw exception)
        pdu.readBody(buffer);
        // parse pdu optional parameters (may throw exception)
        pdu.readOptionalParameters(buffer, context);
    } catch (RecoverablePduException e) {
        // check if we should add the partial pdu to the exception
        if (e.getPartialPdu() == null) {
            e.setPartialPdu(pdu);
        }
        // rethrow it
        throw e;
    }
    return pdu;
}
Also used : BindTransceiver(com.zx.sms.codec.smpp.msg.BindTransceiver) BindReceiverResp(com.zx.sms.codec.smpp.msg.BindReceiverResp) GenericNack(com.zx.sms.codec.smpp.msg.GenericNack) PduResponse(com.zx.sms.codec.smpp.msg.PduResponse) ReplaceSm(com.zx.sms.codec.smpp.msg.ReplaceSm) BindTransmitter(com.zx.sms.codec.smpp.msg.BindTransmitter) ReplaceSmResp(com.zx.sms.codec.smpp.msg.ReplaceSmResp) DataSmResp(com.zx.sms.codec.smpp.msg.DataSmResp) PartialPdu(com.zx.sms.codec.smpp.msg.PartialPdu) EnquireLink(com.zx.sms.codec.smpp.msg.EnquireLink) DataSm(com.zx.sms.codec.smpp.msg.DataSm) SubmitSm(com.zx.sms.codec.smpp.msg.SubmitSm) CancelSm(com.zx.sms.codec.smpp.msg.CancelSm) QuerySmResp(com.zx.sms.codec.smpp.msg.QuerySmResp) DeliverSmResp(com.zx.sms.codec.smpp.msg.DeliverSmResp) PartialPdu(com.zx.sms.codec.smpp.msg.PartialPdu) Pdu(com.zx.sms.codec.smpp.msg.Pdu) AlertNotification(com.zx.sms.codec.smpp.msg.AlertNotification) QuerySm(com.zx.sms.codec.smpp.msg.QuerySm) BindReceiver(com.zx.sms.codec.smpp.msg.BindReceiver) EnquireLinkResp(com.zx.sms.codec.smpp.msg.EnquireLinkResp) CancelSmResp(com.zx.sms.codec.smpp.msg.CancelSmResp) BindTransmitterResp(com.zx.sms.codec.smpp.msg.BindTransmitterResp) BindTransceiverResp(com.zx.sms.codec.smpp.msg.BindTransceiverResp) UnbindResp(com.zx.sms.codec.smpp.msg.UnbindResp) SubmitSmResp(com.zx.sms.codec.smpp.msg.SubmitSmResp) PartialPduResp(com.zx.sms.codec.smpp.msg.PartialPduResp) Unbind(com.zx.sms.codec.smpp.msg.Unbind) DeliverSm(com.zx.sms.codec.smpp.msg.DeliverSm)

Aggregations

PduResponse (com.zx.sms.codec.smpp.msg.PduResponse)3 TlvConvertException (com.zx.sms.codec.smpp.TlvConvertException)1 AlertNotification (com.zx.sms.codec.smpp.msg.AlertNotification)1 BaseBind (com.zx.sms.codec.smpp.msg.BaseBind)1 BindReceiver (com.zx.sms.codec.smpp.msg.BindReceiver)1 BindReceiverResp (com.zx.sms.codec.smpp.msg.BindReceiverResp)1 BindTransceiver (com.zx.sms.codec.smpp.msg.BindTransceiver)1 BindTransceiverResp (com.zx.sms.codec.smpp.msg.BindTransceiverResp)1 BindTransmitter (com.zx.sms.codec.smpp.msg.BindTransmitter)1 BindTransmitterResp (com.zx.sms.codec.smpp.msg.BindTransmitterResp)1 CancelSm (com.zx.sms.codec.smpp.msg.CancelSm)1 CancelSmResp (com.zx.sms.codec.smpp.msg.CancelSmResp)1 DataSm (com.zx.sms.codec.smpp.msg.DataSm)1 DataSmResp (com.zx.sms.codec.smpp.msg.DataSmResp)1 DeliverSm (com.zx.sms.codec.smpp.msg.DeliverSm)1 DeliverSmResp (com.zx.sms.codec.smpp.msg.DeliverSmResp)1 EnquireLink (com.zx.sms.codec.smpp.msg.EnquireLink)1 EnquireLinkResp (com.zx.sms.codec.smpp.msg.EnquireLinkResp)1 GenericNack (com.zx.sms.codec.smpp.msg.GenericNack)1 PartialPdu (com.zx.sms.codec.smpp.msg.PartialPdu)1