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;
}
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);
}
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;
}
Aggregations