Search in sources :

Example 16 with CmppSubmitRequestMessage

use of com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage in project SMSGate by Lihuanghe.

the class TestSerializeSmsMessage method testTerminatedMsg.

@Test
public void testTerminatedMsg() throws IOException, ClassNotFoundException, InterruptedException {
    CmppSubmitRequestMessage m = CmppSubmitRequestMessage.create("13800138000", "10086", "aaa");
    m.setLifeTime(1);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bos);
    out.writeObject(m);
    byte[] b = bos.toByteArray();
    System.out.println(b.length);
    Thread.sleep(2000);
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b));
    CmppSubmitRequestMessage result = (CmppSubmitRequestMessage) in.readObject();
    Assert.assertArrayEquals(m.getDestterminalId(), result.getDestterminalId());
    Assert.assertEquals(m.getSrcId(), result.getSrcId());
    Assert.assertEquals(m.getTimestamp(), result.getTimestamp());
    Assert.assertTrue(m.isTerminated());
    Assert.assertTrue(result.isTerminated());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CmppSubmitRequestMessage(com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 17 with CmppSubmitRequestMessage

use of com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage in project SMSGate by Lihuanghe.

the class TestSpeedError method test.

@Test
public void test() throws InterruptedException {
    SessionStateManager sessionhandler = (SessionStateManager) ch.pipeline().get("session");
    // 测试超速错误的重发机制
    CmppSubmitRequestMessage msg = new CmppSubmitRequestMessage();
    msg.setDestterminalId(new String[] { "13800138000" });
    msg.setLinkID("0000");
    msg.setMsgContent("123");
    msg.setServiceId("10086");
    msg.setSrcId("10086");
    ch.writeOutbound(msg);
    // 等待重发
    Thread.sleep((reSendTime + 1) * 1000);
    // 一共发送了2条MT消息
    Assert.assertEquals(2, sessionhandler.getWriteCount());
    // 有一条等待发送的消息
    Assert.assertEquals(1, sessionhandler.getWaittingResp());
    ByteBuf recvMsg = ch.readOutbound();
    Assert.assertNotNull(recvMsg);
    // 新为上面等待超时,会重发一次,所以这里会收到两条
    recvMsg = ch.readOutbound();
    Assert.assertNotNull(recvMsg);
    // 回复一条超速错误
    CmppSubmitResponseMessage resp = new CmppSubmitResponseMessage(msg.getHeader().getSequenceId());
    resp.setResult(8L);
    // 把resp转化为ByteBuf
    ch.writeOutbound(resp);
    ch.writeInbound(ch.readOutbound());
    Thread.sleep((reSendTime + 2) * 1000);
    // 一共发送了3条MT消息
    Assert.assertEquals(3, sessionhandler.getWriteCount());
    // 收到超速错,会再次重发一次
    recvMsg = ch.readOutbound();
    Assert.assertNotNull(recvMsg);
    // 回复一条正确接收
    resp = new CmppSubmitResponseMessage(msg.getHeader().getSequenceId());
    resp.setResult(0L);
    // 把resp转化为ByteBuf
    ch.writeOutbound(resp);
    ch.writeInbound(ch.readOutbound());
    CmppSubmitResponseMessage respret = ch.readInbound();
    // System.out.println(respret.getRequest());
    Assert.assertSame(msg, respret.getRequest());
    Assert.assertNotEquals(respret.getMsgId(), msg.getMsgid());
    // 没有等待发送的消息
    Assert.assertEquals(0, sessionhandler.getWaittingResp());
    // 等待重发超时
    Thread.sleep((reSendTime + 1) * 1000);
    // 因为已收到response,发送成功了,不会再重发了,因此这次接收的是Null
    recvMsg = ch.readOutbound();
    Assert.assertNull(recvMsg);
}
Also used : CmppSubmitRequestMessage(com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage) ByteBuf(io.netty.buffer.ByteBuf) SessionStateManager(com.zx.sms.session.cmpp.SessionStateManager) CmppSubmitResponseMessage(com.zx.sms.codec.cmpp.msg.CmppSubmitResponseMessage) Test(org.junit.Test)

Example 18 with CmppSubmitRequestMessage

use of com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage in project SMSGate by Lihuanghe.

the class TestMsgCmppSubmit7FDecoder method testCodec.

@Test
public void testCodec() {
    CmppSubmitRequestMessage msg = new CmppSubmitRequestMessage();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("a", 1);
    map.put("b", "adf");
    msg.setAttachment((Serializable) map);
    msg.setMsgContent("12341");
    ByteBuf buf = encode(msg);
    ByteBuf copybuf = buf.copy();
    // packageLength
    buf.readInt();
    Assert.assertEquals(msg.getPacketType().getCommandId(), buf.readInt());
    Assert.assertEquals(msg.getHeader().getSequenceId(), buf.readInt());
    CmppSubmitRequestMessage result = decode(copybuf);
    Assert.assertEquals(msg.getAttachment(), result.getAttachment());
    Assert.assertEquals(msg.getMsgContent(), result.getMsgContent());
}
Also used : HashMap(java.util.HashMap) CmppSubmitRequestMessage(com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 19 with CmppSubmitRequestMessage

use of com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage in project SMSGate by Lihuanghe.

the class TestMsgCmppSubmit7FDecoder method testlongCodec.

public void testlongCodec(String content) {
    CmppSubmitRequestMessage msg = new CmppSubmitRequestMessage();
    msg.setDestterminalId(new String[] { "13800138000" });
    msg.setLinkID("0000");
    msg.setMsgContent(content);
    msg.setMsgid(new MsgId());
    msg.setServiceId("10086");
    msg.setSrcId("10086");
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("a", 1);
    map.put("b", "adf");
    msg.setAttachment((Serializable) map);
    channel().writeOutbound(msg);
    ByteBuf buf = (ByteBuf) channel().readOutbound();
    ByteBuf copybuf = Unpooled.buffer();
    while (buf != null) {
        copybuf.writeBytes(buf.copy());
        int length = buf.readableBytes();
        Assert.assertEquals(length, buf.readInt());
        Assert.assertEquals(msg.getPacketType().getCommandId(), buf.readInt());
        buf = (ByteBuf) channel().readOutbound();
    }
    CmppSubmitRequestMessage result = decode(copybuf);
    System.out.println(result.getMsgContent());
    Assert.assertEquals(msg.getServiceId(), result.getServiceId());
    Assert.assertEquals(msg.getMsgContent(), result.getMsgContent());
    Assert.assertTrue(result.getAttachment() instanceof Map);
    Map retmap = (Map) result.getAttachment();
    Assert.assertEquals("adf", retmap.get("b"));
}
Also used : HashMap(java.util.HashMap) CmppSubmitRequestMessage(com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) HashMap(java.util.HashMap) MsgId(com.zx.sms.common.util.MsgId)

Example 20 with CmppSubmitRequestMessage

use of com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage in project SMSGate by Lihuanghe.

the class Cmpp7FSubmitRequestMessageCodec method decode.

@Override
protected void decode(ChannelHandlerContext ctx, Message msg, List<Object> out) throws Exception {
    int commandId = msg.getHeader().getCommandId();
    if (packetType.getCommandId() != commandId) {
        // 不解析,交给下一个codec
        out.add(msg);
        return;
    }
    CmppSubmitRequestMessage requestMessage = new CmppSubmitRequestMessage(msg.getHeader());
    ByteBuf bodyBuffer = Unpooled.wrappedBuffer(msg.getBodyBuffer());
    requestMessage.setMsgid(DefaultMsgIdUtil.bytes2MsgId(toArray(bodyBuffer, CmppSubmitRequest.MSGID.getLength())));
    requestMessage.setPktotal(bodyBuffer.readUnsignedByte());
    requestMessage.setPknumber(bodyBuffer.readUnsignedByte());
    requestMessage.setRegisteredDelivery(bodyBuffer.readUnsignedByte());
    requestMessage.setMsglevel(bodyBuffer.readUnsignedByte());
    requestMessage.setServiceId(bodyBuffer.readCharSequence(CmppSubmitRequest.SERVICEID.getLength(), GlobalConstance.defaultTransportCharset).toString().trim());
    requestMessage.setFeeUserType(bodyBuffer.readUnsignedByte());
    requestMessage.setFeeterminalId(bodyBuffer.readCharSequence(CmppSubmitRequest.FEETERMINALID.getLength(), GlobalConstance.defaultTransportCharset).toString().trim());
    requestMessage.setFeeterminaltype(bodyBuffer.readUnsignedByte());
    requestMessage.setTppid(bodyBuffer.readUnsignedByte());
    requestMessage.setTpudhi(bodyBuffer.readUnsignedByte());
    requestMessage.setMsgfmt(new SmsDcs((byte) bodyBuffer.readUnsignedByte()));
    requestMessage.setMsgsrc(bodyBuffer.readCharSequence(CmppSubmitRequest.MSGSRC.getLength(), GlobalConstance.defaultTransportCharset).toString().trim());
    requestMessage.setFeeType(bodyBuffer.readCharSequence(CmppSubmitRequest.FEETYPE.getLength(), GlobalConstance.defaultTransportCharset).toString().trim());
    requestMessage.setFeeCode(bodyBuffer.readCharSequence(CmppSubmitRequest.FEECODE.getLength(), GlobalConstance.defaultTransportCharset).toString().trim());
    requestMessage.setValIdTime(bodyBuffer.readCharSequence(CmppSubmitRequest.VALIDTIME.getLength(), GlobalConstance.defaultTransportCharset).toString().trim());
    requestMessage.setAtTime(bodyBuffer.readCharSequence(CmppSubmitRequest.ATTIME.getLength(), GlobalConstance.defaultTransportCharset).toString().trim());
    requestMessage.setSrcId(bodyBuffer.readCharSequence(CmppSubmitRequest.SRCID.getLength(), GlobalConstance.defaultTransportCharset).toString().trim());
    short destUsrtl = bodyBuffer.readUnsignedByte();
    String[] destTermId = new String[destUsrtl];
    for (int i = 0; i < destUsrtl; i++) {
        destTermId[i] = bodyBuffer.readCharSequence(CmppSubmitRequest.DESTTERMINALID.getLength(), GlobalConstance.defaultTransportCharset).toString().trim();
    }
    requestMessage.setDestterminalId(destTermId);
    requestMessage.setDestterminaltype(bodyBuffer.readUnsignedByte());
    short msgLength = (short) (bodyBuffer.readUnsignedByte() & 0xffff);
    byte[] contentbytes = new byte[msgLength];
    bodyBuffer.readBytes(contentbytes);
    requestMessage.setMsgContentBytes(contentbytes);
    requestMessage.setMsgLength((short) msgLength);
    requestMessage.setLinkID(bodyBuffer.readCharSequence(CmppSubmitRequest.LINKID.getLength(), GlobalConstance.defaultTransportCharset).toString().trim());
    // 在线公司自定义的字段
    int attach = bodyBuffer.readInt();
    if (attach != 0) {
        byte[] objbytes = new byte[attach];
        bodyBuffer.readBytes(objbytes);
        try {
            requestMessage.setAttachment(FstObjectSerializeUtil.read(objbytes));
        } catch (Exception ex) {
            logger.warn("Attachment decode error", ex);
        }
    }
    out.add(requestMessage);
    ReferenceCountUtil.release(bodyBuffer);
}
Also used : CmppSubmitRequestMessage(com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage) ByteBuf(io.netty.buffer.ByteBuf) SmsDcs(org.marre.sms.SmsDcs)

Aggregations

CmppSubmitRequestMessage (com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage)39 Test (org.junit.Test)24 ByteBuf (io.netty.buffer.ByteBuf)21 MsgId (com.zx.sms.common.util.MsgId)9 SmsMessage (org.marre.sms.SmsMessage)6 SmsDcs (org.marre.sms.SmsDcs)5 SmsTextMessage (org.marre.sms.SmsTextMessage)4 SmsWapPushMessage (org.marre.wap.push.SmsWapPushMessage)4 CmppDeliverRequestMessage (com.zx.sms.codec.cmpp.msg.CmppDeliverRequestMessage)3 CmppSubmitResponseMessage (com.zx.sms.codec.cmpp.msg.CmppSubmitResponseMessage)3 CmppDeliverResponseMessage (com.zx.sms.codec.cmpp.msg.CmppDeliverResponseMessage)2 CmppQueryRequestMessage (com.zx.sms.codec.cmpp.msg.CmppQueryRequestMessage)2 CmppQueryResponseMessage (com.zx.sms.codec.cmpp.msg.CmppQueryResponseMessage)2 CmppReportRequestMessage (com.zx.sms.codec.cmpp.msg.CmppReportRequestMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 HashMap (java.util.HashMap)2 Random (java.util.Random)2