Search in sources :

Example 1 with BaseMessage

use of com.zx.sms.BaseMessage in project SMSGate by Lihuanghe.

the class AbstractEndpointConnector method synwrite.

public <T extends BaseMessage> List<Promise<T>> synwrite(List<T> msgs) {
    Channel ch = fetchOneWritable();
    if (ch == null)
        return null;
    AbstractSessionStateManager session = (AbstractSessionStateManager) ch.pipeline().get(sessionHandler);
    if (session == null)
        return null;
    List<Promise<T>> arrPromise = new ArrayList<Promise<T>>();
    for (BaseMessage msg : msgs) {
        arrPromise.add(session.writeMessagesync(msg));
    }
    return arrPromise;
}
Also used : Promise(io.netty.util.concurrent.Promise) BaseMessage(com.zx.sms.BaseMessage) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) AbstractSessionStateManager(com.zx.sms.session.AbstractSessionStateManager)

Example 2 with BaseMessage

use of com.zx.sms.BaseMessage in project SMSGate by Lihuanghe.

the class SessionConnectedHandler method userEventTriggered.

@Override
public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception {
    final AtomicInteger tmptotal = totleCnt;
    if (evt == SessionState.Connect) {
        final Channel ch = ctx.channel();
        EventLoopGroupFactory.INS.submitUnlimitCircleTask(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                int cnt = RandomUtils.nextInt() & 0x4fff;
                Promise<BaseMessage> frefuture = null;
                while (cnt > 0 && tmptotal.get() > 0) {
                    List<Promise<BaseMessage>> futures = null;
                    ChannelFuture chfuture = null;
                    BaseMessage msg = createTestReq(UUID.randomUUID().toString());
                    // chfuture = ChannelUtil.asyncWriteToEntity(getEndpointEntity().getId(), msg);
                    futures = ChannelUtil.syncWriteLongMsgToEntity(getEndpointEntity().getId(), msg);
                    if (chfuture != null)
                        chfuture.sync();
                    cnt--;
                    tmptotal.decrementAndGet();
                    if (futures != null) {
                        try {
                            for (Promise<BaseMessage> future : futures) {
                                future.addListener(new GenericFutureListener<Future<BaseMessage>>() {

                                    @Override
                                    public void operationComplete(Future<BaseMessage> future) throws Exception {
                                        if (future.isSuccess()) {
                                        // logger.info("response:{}",future.get());
                                        } else {
                                        // logger.error("response:{}",future.cause());
                                        }
                                    }
                                });
                                frefuture = future;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            cnt--;
                            tmptotal.decrementAndGet();
                            break;
                        }
                    } else {
                        // 连接不可写了,等待上一个response回来
                        // 再把消息发出去
                        ctx.writeAndFlush(msg);
                        if (frefuture != null) {
                            frefuture.sync();
                            frefuture = null;
                        } else {
                            break;
                        }
                    }
                }
                return true;
            }
        }, new ExitUnlimitCirclePolicy<Boolean>() {

            @Override
            public boolean notOver(Future<Boolean> future) {
                if (future.cause() != null)
                    future.cause().printStackTrace();
                boolean over = ch.isActive() && tmptotal.get() > 0;
                if (!over) {
                    logger.info("========send over.============");
                // ch.writeAndFlush(new CmppTerminateRequestMessage());
                }
                return over;
            }
        }, 1);
    }
    ctx.fireUserEventTriggered(evt);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) Promise(io.netty.util.concurrent.Promise) BaseMessage(com.zx.sms.BaseMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) List(java.util.List) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener)

Example 3 with BaseMessage

use of com.zx.sms.BaseMessage in project SMSGate by Lihuanghe.

the class ChannelUtil method syncWriteLongMsgToEntity.

public static <T extends BaseMessage> List<Promise<T>> syncWriteLongMsgToEntity(EndpointEntity e, BaseMessage msg) throws Exception {
    EndpointConnector connector = e.getSingletonConnector();
    if (connector == null)
        return null;
    if (msg instanceof LongSMSMessage) {
        LongSMSMessage<BaseMessage> lmsg = (LongSMSMessage<BaseMessage>) msg;
        if (!lmsg.isReport()) {
            // 长短信拆分
            SmsMessage msgcontent = lmsg.getSmsMessage();
            List<LongMessageFrame> frameList = LongMessageFrameHolder.INS.splitmsgcontent(msgcontent);
            // 保证同一条长短信,通过同一个tcp连接发送
            List<BaseMessage> msgs = new ArrayList<BaseMessage>();
            for (LongMessageFrame frame : frameList) {
                BaseMessage basemsg = (BaseMessage) lmsg.generateMessage(frame);
                msgs.add(basemsg);
            }
            return connector.synwrite(msgs);
        }
    }
    Promise promise = connector.synwrite(msg);
    if (promise == null) {
        // 为空,可能是连接断了,直接返回
        return null;
    }
    List<Promise<T>> arrPromise = new ArrayList<Promise<T>>();
    arrPromise.add(promise);
    return arrPromise;
}
Also used : EndpointConnector(com.zx.sms.connect.manager.EndpointConnector) LongSMSMessage(com.zx.sms.LongSMSMessage) Promise(io.netty.util.concurrent.Promise) SmsMessage(org.marre.sms.SmsMessage) BaseMessage(com.zx.sms.BaseMessage) LongMessageFrame(com.zx.sms.codec.cmpp.wap.LongMessageFrame) ArrayList(java.util.ArrayList)

Aggregations

BaseMessage (com.zx.sms.BaseMessage)3 Promise (io.netty.util.concurrent.Promise)3 Channel (io.netty.channel.Channel)2 ArrayList (java.util.ArrayList)2 LongSMSMessage (com.zx.sms.LongSMSMessage)1 LongMessageFrame (com.zx.sms.codec.cmpp.wap.LongMessageFrame)1 EndpointConnector (com.zx.sms.connect.manager.EndpointConnector)1 AbstractSessionStateManager (com.zx.sms.session.AbstractSessionStateManager)1 ChannelFuture (io.netty.channel.ChannelFuture)1 Future (io.netty.util.concurrent.Future)1 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SmsMessage (org.marre.sms.SmsMessage)1