use of com.zx.sms.handler.api.AbstractBusinessHandler in project SMSGate by Lihuanghe.
the class TestCMPPEndPoint method testCMPPEndpoint.
@Test
public void testCMPPEndpoint() throws Exception {
ResourceLeakDetector.setLevel(Level.ADVANCED);
final EndpointManager manager = EndpointManager.INS;
CMPPServerEndpointEntity server = new CMPPServerEndpointEntity();
server.setId("server");
server.setHost("0.0.0.0");
server.setPort(7890);
server.setValid(true);
// 使用ssl加密数据流
server.setUseSSL(false);
CMPPServerChildEndpointEntity child = new CMPPServerChildEndpointEntity();
child.setId("child");
child.setChartset(Charset.forName("utf-8"));
child.setGroupName("test");
child.setUserName("901783");
child.setPassword("ICP001");
child.setValid(true);
child.setVersion((short) 0x30);
child.setMaxChannels((short) 1);
child.setRetryWaitTimeSec((short) 30);
child.setMaxRetryCnt((short) 3);
// 不开启IP白名单
// List<String> iplist = new ArrayList<String>();
// iplist.add("192.168.98.48/18");
// child.setAllowedAddr(iplist);
child.setReSendFailMsg(false);
// child.setWriteLimit(200);
// child.setReadLimit(200);
List<BusinessHandlerInterface> serverhandlers = new ArrayList<BusinessHandlerInterface>();
CMPPMessageReceiveHandler receiver = new CMPPMessageReceiveHandler();
serverhandlers.add(new AbstractBusinessHandler() {
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
CMPPResponseSenderHandler handler = new CMPPResponseSenderHandler();
handler.setEndpointEntity(getEndpointEntity());
ctx.pipeline().addAfter("sessionStateManager", handler.name(), handler);
ctx.pipeline().remove(this);
}
@Override
public String name() {
return "AddCMPPResponseSenderHandler";
}
});
serverhandlers.add(receiver);
child.setBusinessHandlerSet(serverhandlers);
server.addchild(child);
manager.addEndpointEntity(server);
CMPPClientEndpointEntity client = new CMPPClientEndpointEntity();
client.setId("client");
// client.setLocalhost("127.0.0.1");
// client.setLocalport(65521);
client.setHost("127.0.0.1");
client.setPort(7890);
client.setChartset(Charset.forName("utf-8"));
client.setGroupName("test");
client.setUserName("901783");
client.setPassword("ICP001");
client.setMaxChannels((short) 1);
client.setVersion((short) 0x30);
client.setRetryWaitTimeSec((short) 30);
client.setUseSSL(false);
// client.setWriteLimit(100);
client.setReSendFailMsg(true);
client.setSupportLongmsg(SupportLongMessage.BOTH);
List<BusinessHandlerInterface> clienthandlers = new ArrayList<BusinessHandlerInterface>();
int count = 2000000;
CMPPSessionConnectedHandler sender = new CMPPSessionConnectedHandler(count);
clienthandlers.add(sender);
client.setBusinessHandlerSet(clienthandlers);
manager.addEndpointEntity(client);
manager.openEndpoint(server);
manager.openEndpoint(client);
Thread.sleep(5000);
manager.startConnectionCheckTask();
System.out.println("start.....");
while (receiver.getCnt().get() < count) {
Thread.sleep(1000);
}
Thread.sleep(3000);
EndpointManager.INS.close();
Thread.sleep(4000);
System.out.println("end.....");
}
use of com.zx.sms.handler.api.AbstractBusinessHandler in project SMSGate by Lihuanghe.
the class CMPPChargingDemoTest method testCharging.
/*
*使用自己扩展的Entity
*/
@Test
public void testCharging() throws Exception {
final EndpointManager manager = EndpointManager.INS;
CMPPServerEndpointEntity server = new CMPPServerEndpointEntity();
server.setId("server");
server.setHost("127.0.0.1");
server.setPort(7892);
server.setValid(true);
// 使用ssl加密数据流
server.setUseSSL(false);
CMPPServerChildEndpointEntity child = new CMPPServerChildEndpointEntity();
child.setId("child");
child.setChartset(Charset.forName("utf-8"));
child.setGroupName("test");
child.setUserName("GSDT01");
child.setPassword("1qaz2wsx");
child.setValid(true);
child.setVersion((short) 0x20);
child.setMaxChannels((short) 20);
child.setRetryWaitTimeSec((short) 30);
child.setMaxRetryCnt((short) 3);
// child.setReSendFailMsg(true);
// child.setWriteLimit(200);
// child.setReadLimit(200);
List<BusinessHandlerInterface> serverhandlers = new ArrayList<BusinessHandlerInterface>();
serverhandlers.add(new CMPPMessageReceiveHandler());
serverhandlers.add(new AbstractBusinessHandler() {
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
CMPPResponseSenderHandler handler = new CMPPResponseSenderHandler();
handler.setEndpointEntity(getEndpointEntity());
ctx.pipeline().addBefore("sessionStateManager", handler.name(), handler);
ctx.pipeline().remove(this);
}
@Override
public String name() {
return "AddCMPPResponseSenderHandler";
}
});
child.setBusinessHandlerSet(serverhandlers);
server.addchild(child);
manager.addEndpointEntity(server);
MyCMPPClientEndpointEntity client = new MyCMPPClientEndpointEntity();
client.setId("client");
client.setHost("127.0.0.1");
client.setPort(7892);
client.setChartset(Charset.forName("utf-8"));
client.setGroupName("test");
client.setUserName("GSDT01");
client.setPassword("1qaz2wsx");
client.setMaxChannels((short) 12);
client.setVersion((short) 0x20);
client.setRetryWaitTimeSec((short) 10);
client.setUseSSL(false);
client.setReSendFailMsg(false);
List<BusinessHandlerInterface> clienthandlers = new ArrayList<BusinessHandlerInterface>();
clienthandlers.add(new CMPPSessionConnectedHandler(10));
client.setBusinessHandlerSet(clienthandlers);
manager.addEndpointEntity(client);
manager.openAll();
// LockSupport.park();
System.out.println("start.....");
Thread.sleep(300000);
EndpointManager.INS.close();
}
use of com.zx.sms.handler.api.AbstractBusinessHandler in project SMSGate by Lihuanghe.
the class AbstractEndpointConnector method bindHandler.
/**
* 连接建立成功后要加载的channelHandler
*/
protected void bindHandler(ChannelPipeline pipe, EndpointEntity entity) {
if (entity instanceof CMPPServerEndpointEntity) {
return;
}
pipe.addFirst("socketLog", new LoggingHandler(String.format(GlobalConstance.loggerNamePrefix, entity.getId()), LogLevel.TRACE));
// 调用子类的bind方法
doBindHandler(pipe, entity);
pipe.addAfter(GlobalConstance.codecName, "msgLog", new MessageLogHandler(entity));
List<BusinessHandlerInterface> handlers = entity.getBusinessHandlerSet();
if (handlers != null && handlers.size() > 0) {
for (BusinessHandlerInterface handler : handlers) {
if (handler instanceof AbstractBusinessHandler) {
AbstractBusinessHandler buziHandler = (AbstractBusinessHandler) handler;
buziHandler.setEndpointEntity(entity);
if (buziHandler.isSharable()) {
pipe.addLast(buziHandler.name(), buziHandler);
} else {
AbstractBusinessHandler cloned = null;
try {
cloned = buziHandler.clone();
} catch (CloneNotSupportedException e) {
logger.error("handlers is not shareable and not implements Cloneable", e);
}
if (cloned != null) {
cloned.setEndpointEntity(entity);
pipe.addLast(buziHandler.name(), cloned);
logger.info("handlers is not shareable . clone it success. {}", cloned);
}
}
} else {
handler.setEndpointEntity(entity);
pipe.addLast(handler.name(), handler);
logger.info("add share handlers . {}", handler);
}
}
}
// 黑洞处理,丢弃所有消息
pipe.addLast("BlackHole", GlobalConstance.blackhole);
}
Aggregations