parent
f8937b3af6
commit
76574a4e42
@ -0,0 +1,30 @@
|
|||||||
|
package com.mashibing.common.enums;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zjw
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
public enum CMPP2DeliverEnums {
|
||||||
|
|
||||||
|
DELIVERED("DELIVRD","Message is delivered to destination"),
|
||||||
|
EXPIRED("EXPIRED","Message validity period has expired"),
|
||||||
|
DELETED("DELETED","Message has been deleted."),
|
||||||
|
UNDELIVERABLE("UNDELIV","Message is undeliverable"),
|
||||||
|
ACCEPTED("ACCEPTD","Message is in accepted state"),
|
||||||
|
UNKNOWN("UNKNOWN","Message is in invalid state"),
|
||||||
|
REJECTED("REJECTD","Message is in a rejected state"),
|
||||||
|
;
|
||||||
|
|
||||||
|
private String stat;
|
||||||
|
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
|
||||||
|
CMPP2DeliverEnums(String stat, String description) {
|
||||||
|
this.stat = stat;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.mashibing.common.enums;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zjw
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
public enum CMPP2ResultEnums {
|
||||||
|
|
||||||
|
OK(0,"正确"),
|
||||||
|
MESSAGE_BUILD_ERROR(1,"消息结构错"),
|
||||||
|
COMMAND_WORD_ERROR(2,"命令字错"),
|
||||||
|
MESSAGE_SEQUENCE_ERROR(3,"消息序号重复"),
|
||||||
|
MESSAGE_LENGTH_ERROR(4,"消息长度错"),
|
||||||
|
INCORRECT_TARIFF_CODE(5,"资费代码错"),
|
||||||
|
Exceeding_maximum_message_length(6,"超过最大信息长"),
|
||||||
|
BUSINESS_CODE_ERROR(7,"业务代码错"),
|
||||||
|
FLOW_CONTROL_ERROR(8,"流量控制错"),
|
||||||
|
UNKNOWN(9,"其他错误")
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
private Integer result;
|
||||||
|
private String msg;
|
||||||
|
|
||||||
|
CMPP2ResultEnums(Integer result, String msg) {
|
||||||
|
this.result = result;
|
||||||
|
this.msg = msg;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.mashibing.common.util;
|
||||||
|
|
||||||
|
import com.mashibing.common.enums.CMPP2DeliverEnums;
|
||||||
|
import com.mashibing.common.enums.CMPP2ResultEnums;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zjw
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
public class CMPP2DeliverUtil {
|
||||||
|
|
||||||
|
private static Map<String,String> stats = new HashMap<>();
|
||||||
|
|
||||||
|
static{
|
||||||
|
CMPP2DeliverEnums[] cmpp2DeliverEnums = CMPP2DeliverEnums.values();
|
||||||
|
for (CMPP2DeliverEnums cmpp2DeliverEnum : cmpp2DeliverEnums) {
|
||||||
|
stats.put(cmpp2DeliverEnum.getStat(),cmpp2DeliverEnum.getDescription());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过result结果拿到对应的错误信息
|
||||||
|
* @param stat
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getResultMessage(String stat){
|
||||||
|
return stats.get(stat);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.mashibing.common.util;
|
||||||
|
|
||||||
|
import com.mashibing.common.enums.CMPP2ResultEnums;
|
||||||
|
import com.mashibing.common.enums.MobileOperatorEnum;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zjw
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
public class CMPP2ResultUtil {
|
||||||
|
|
||||||
|
private static Map<Integer,String> results = new HashMap<>();
|
||||||
|
|
||||||
|
static{
|
||||||
|
CMPP2ResultEnums[] cmpp2ResultEnums = CMPP2ResultEnums.values();
|
||||||
|
for (CMPP2ResultEnums cmpp2ResultEnum : cmpp2ResultEnums) {
|
||||||
|
results.put(cmpp2ResultEnum.getResult(),cmpp2ResultEnum.getMsg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过result结果拿到对应的错误信息
|
||||||
|
* @param result
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getResultMessage(Integer result){
|
||||||
|
return results.get(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.mashibing.smsgateway.client;
|
||||||
|
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zjw
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
@FeignClient(value = "beacon-cache")
|
||||||
|
public interface BeaconCacheClient {
|
||||||
|
|
||||||
|
@GetMapping("/cache/hget/{key}/{field}")
|
||||||
|
Integer hgetInteger(@PathVariable(value = "key") String key, @PathVariable(value = "field") String field);
|
||||||
|
|
||||||
|
@GetMapping("/cache/hget/{key}/{field}")
|
||||||
|
String hget(@PathVariable(value = "key")String key, @PathVariable(value = "field")String field);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.mashibing.smsgateway.config;
|
||||||
|
|
||||||
|
import cn.hippo4j.core.executor.DynamicThreadPool;
|
||||||
|
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zjw
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class ThreadPoolConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@DynamicThreadPool
|
||||||
|
public ThreadPoolExecutor cmppSubmitPool() {
|
||||||
|
String threadPoolId = "cmpp-submit";
|
||||||
|
ThreadPoolExecutor messageConsumeDynamicExecutor = ThreadPoolBuilder.builder()
|
||||||
|
// 指定线程名称的前缀
|
||||||
|
.threadFactory(threadPoolId)
|
||||||
|
// 线程池在Hippo4j中的唯一标识
|
||||||
|
.threadPoolId(threadPoolId)
|
||||||
|
// 代表动态线程池
|
||||||
|
.dynamicPool()
|
||||||
|
.build();
|
||||||
|
return messageConsumeDynamicExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@DynamicThreadPool
|
||||||
|
public ThreadPoolExecutor cmppDeliverPool() {
|
||||||
|
String threadPoolId = "cmpp-deliver";
|
||||||
|
ThreadPoolExecutor messageProduceDynamicExecutor = ThreadPoolBuilder.builder()
|
||||||
|
.threadFactory(threadPoolId)
|
||||||
|
.threadPoolId(threadPoolId)
|
||||||
|
.dynamicPool()
|
||||||
|
.build();
|
||||||
|
return messageProduceDynamicExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.mashibing.smsgateway.controller;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zjw
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class TestController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ThreadPoolExecutor cmppSubmitPool;
|
||||||
|
|
||||||
|
@GetMapping("/test")
|
||||||
|
public String test(){
|
||||||
|
cmppSubmitPool.execute(() -> {
|
||||||
|
System.out.println(Thread.currentThread().getName());
|
||||||
|
});
|
||||||
|
return "ok!";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.mashibing.smsgateway.netty4;
|
||||||
|
|
||||||
|
import com.mashibing.smsgateway.netty4.entity.CmppMessageHeader;
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.handler.codec.MessageToByteEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码
|
||||||
|
*/
|
||||||
|
public class CMPPEncoder extends MessageToByteEncoder<Object> {
|
||||||
|
|
||||||
|
public CMPPEncoder(){
|
||||||
|
super(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
|
||||||
|
if (msg instanceof byte[]){
|
||||||
|
out.writeBytes((byte[])msg);
|
||||||
|
}else if(msg instanceof Integer){
|
||||||
|
out.writeInt((Integer)msg);
|
||||||
|
}else if(msg instanceof Byte){
|
||||||
|
out.writeByte((Byte)msg);
|
||||||
|
}else if(msg instanceof Long){
|
||||||
|
out.writeLong((Long)msg);
|
||||||
|
}else if(msg instanceof String){
|
||||||
|
out.writeBytes(((String)msg).getBytes("UTF-16BE"));
|
||||||
|
}else if (msg instanceof Character){
|
||||||
|
out.writeChar((Character)msg);
|
||||||
|
}else if (msg instanceof CmppMessageHeader){
|
||||||
|
CmppMessageHeader c=(CmppMessageHeader)msg;
|
||||||
|
out.writeBytes(c.toByteArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.mashibing.smsgateway.netty4;
|
||||||
|
|
||||||
|
|
||||||
|
import com.mashibing.smsgateway.netty4.entity.CmppActiveTest;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
import io.netty.handler.timeout.IdleState;
|
||||||
|
import io.netty.handler.timeout.IdleStateEvent;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 心跳Handler
|
||||||
|
*/
|
||||||
|
public class HeartHandler extends ChannelInboundHandlerAdapter {
|
||||||
|
|
||||||
|
private NettyClient client;
|
||||||
|
public HeartHandler(NettyClient client){
|
||||||
|
this.client=client;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
System.out.println("初始化创建连接。。。");
|
||||||
|
super.channelActive(ctx);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
||||||
|
if (evt instanceof IdleStateEvent) {
|
||||||
|
IdleStateEvent event = (IdleStateEvent) evt;
|
||||||
|
IdleState state = event.state();
|
||||||
|
if (state == IdleState.WRITER_IDLE || state == IdleState.ALL_IDLE) {
|
||||||
|
client.submit(new CmppActiveTest());
|
||||||
|
System.out.println("心跳启动!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
super.userEventTriggered(ctx, evt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
super.channelInactive(ctx);
|
||||||
|
client.reConnect(10);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,136 @@
|
|||||||
|
package com.mashibing.smsgateway.netty4;
|
||||||
|
|
||||||
|
import com.mashibing.smsgateway.netty4.entity.CmppConnect;
|
||||||
|
import com.mashibing.smsgateway.netty4.entity.CmppMessageHeader;
|
||||||
|
import io.netty.bootstrap.Bootstrap;
|
||||||
|
import io.netty.channel.*;
|
||||||
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
|
import io.netty.handler.logging.LogLevel;
|
||||||
|
import io.netty.handler.logging.LoggingHandler;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 封装的netty客户端
|
||||||
|
*/
|
||||||
|
public class NettyClient {
|
||||||
|
|
||||||
|
// 通道
|
||||||
|
private Channel channel;
|
||||||
|
// IP
|
||||||
|
private String host;
|
||||||
|
// 端口
|
||||||
|
private int port;
|
||||||
|
// 用户名
|
||||||
|
private String serviceId;
|
||||||
|
// 密码
|
||||||
|
private String pwd;
|
||||||
|
|
||||||
|
// 构建NettyClient
|
||||||
|
public NettyClient(String host, int port, String serviceId, String pwd) {
|
||||||
|
this.host = host;
|
||||||
|
this.port = port;
|
||||||
|
this.serviceId = serviceId;
|
||||||
|
this.pwd = pwd;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开启连接
|
||||||
|
public void start() {
|
||||||
|
try {
|
||||||
|
doConnect(host, port);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 在保持连接的状态下提交信息
|
||||||
|
public boolean submit(CmppMessageHeader submit) {
|
||||||
|
if (isActive()) {
|
||||||
|
channel.writeAndFlush(submit);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 是否保持连接
|
||||||
|
public boolean isActive() {
|
||||||
|
if (channel == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!channel.isOpen() || !channel.isActive() || !channel.isWritable()) {
|
||||||
|
//channel没开 或 没激活
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重连机制 在 channelInactive 触发时调用
|
||||||
|
*
|
||||||
|
* @param reConnect
|
||||||
|
*/
|
||||||
|
public void reConnect(int reConnect) {
|
||||||
|
int times = 0;
|
||||||
|
while (true && times < reConnect) {
|
||||||
|
try {
|
||||||
|
if (!isActive()) {
|
||||||
|
start();
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
Thread.sleep(10 * 1000);
|
||||||
|
times++;
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.out.println("尝试重连...:" + host + ":" + port + " / " + serviceId);
|
||||||
|
try {
|
||||||
|
Thread.sleep(10 * 1000);
|
||||||
|
times++;
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 建立连接
|
||||||
|
* @param host
|
||||||
|
* @param port
|
||||||
|
* @throws InterruptedException
|
||||||
|
*/
|
||||||
|
public void doConnect(final String host, final int port) throws InterruptedException {
|
||||||
|
|
||||||
|
//创建线程组 - 手动设置线程数,默认为cpu核心数2倍
|
||||||
|
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
|
||||||
|
//创建引导程序
|
||||||
|
Bootstrap bootstrap = new Bootstrap();
|
||||||
|
//保持长连接
|
||||||
|
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
|
||||||
|
//将线程加入bootstrap
|
||||||
|
bootstrap.group(eventLoopGroup)
|
||||||
|
.remoteAddress(host, port)
|
||||||
|
//使用指定通道类
|
||||||
|
.channel(NioSocketChannel.class)
|
||||||
|
//设置日志
|
||||||
|
.handler(new LoggingHandler(LogLevel.INFO))
|
||||||
|
//重写通道初始化方法
|
||||||
|
.handler(new NettyClientInitializer(this));
|
||||||
|
|
||||||
|
ChannelFuture channelFuture = bootstrap.connect().sync();
|
||||||
|
|
||||||
|
channel = channelFuture.channel();
|
||||||
|
|
||||||
|
//账号登陆
|
||||||
|
CmppMessageHeader cmppConnect = new CmppConnect(serviceId, pwd);
|
||||||
|
channel.writeAndFlush(cmppConnect);
|
||||||
|
|
||||||
|
channelFuture.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
|
||||||
|
|
||||||
|
//关闭前阻塞
|
||||||
|
// channelFuture.channel().closeFuture().sync();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.mashibing.smsgateway.netty4;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class NettyStartCMPP {
|
||||||
|
|
||||||
|
//ip
|
||||||
|
public static String host = "127.0.0.1";
|
||||||
|
//端口
|
||||||
|
public static int port = 7890;
|
||||||
|
//账号
|
||||||
|
public static String serviceId = "laozheng";
|
||||||
|
//密码
|
||||||
|
public static String pwd = "JavaLaoZheng123!";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Bean(initMethod = "start")
|
||||||
|
public NettyClient nettyClient() {
|
||||||
|
NettyClient cmppClient = new NettyClient(host, port, serviceId, pwd);
|
||||||
|
return cmppClient;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.mashibing.smsgateway.netty4.entity;
|
||||||
|
|
||||||
|
import com.mashibing.smsgateway.netty4.utils.Command;
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.buffer.Unpooled;
|
||||||
|
|
||||||
|
public class CmppActiveTest extends CmppMessageHeader {
|
||||||
|
|
||||||
|
public CmppActiveTest() {
|
||||||
|
super(Command.CMPP_ACTIVE_TEST, Command.CMPP2_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实现类必须自定义对象序列化
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public byte[] toByteArray() {
|
||||||
|
ByteBuf buf = Unpooled.buffer(12);
|
||||||
|
buf.writeInt(12);
|
||||||
|
buf.writeInt(Command.CMPP_ACTIVE_TEST);
|
||||||
|
buf.writeInt(0);
|
||||||
|
return buf.array();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.mashibing.smsgateway.netty4.entity;
|
||||||
|
|
||||||
|
import com.mashibing.smsgateway.netty4.utils.Command;
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.buffer.Unpooled;
|
||||||
|
|
||||||
|
|
||||||
|
public class CmppActiveTestResp extends CmppMessageHeader {
|
||||||
|
public CmppActiveTestResp() {
|
||||||
|
super(Command.CMPP_ACTIVE_TEST_RESP, Command.CMPP2_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实现类必须自定义对象序列化
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public byte[] toByteArray() {
|
||||||
|
ByteBuf buf = Unpooled.buffer(4 + 4 + 4 + 1);
|
||||||
|
buf.writeInt(4 + 4 + 4 + 1);
|
||||||
|
buf.writeInt(Command.CMPP_ACTIVE_TEST_RESP);
|
||||||
|
buf.writeInt(0);
|
||||||
|
buf.writeByte(0);
|
||||||
|
return buf.array();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.mashibing.smsgateway.netty4.entity;
|
||||||
|
|
||||||
|
import com.mashibing.smsgateway.netty4.utils.Command;
|
||||||
|
import com.mashibing.smsgateway.netty4.utils.MsgUtils;
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.buffer.Unpooled;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 与CMPP建立连接需要的信息
|
||||||
|
*/
|
||||||
|
public class CmppConnect extends CmppMessageHeader {
|
||||||
|
|
||||||
|
private String serviceId;
|
||||||
|
|
||||||
|
private String pwd;
|
||||||
|
|
||||||
|
public CmppConnect(String serviceId, String pwd) {
|
||||||
|
super(Command.CMPP_CONNECT, Command.CMPP2_VERSION);
|
||||||
|
this.serviceId = serviceId;
|
||||||
|
this.pwd = pwd;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] toByteArray() {
|
||||||
|
ByteBuf buf = Unpooled.buffer(4 + 4 + 4 + 6 + 16 + 1 + 4);
|
||||||
|
|
||||||
|
//Total_Length
|
||||||
|
buf.writeInt(4 + 4 + 4 + 6 + 16 + 1 + 4);
|
||||||
|
//Command_Id
|
||||||
|
buf.writeInt(Command.CMPP_CONNECT);
|
||||||
|
//Sequence_Id
|
||||||
|
buf.writeInt(MsgUtils.getSequence());
|
||||||
|
//sourceAddr
|
||||||
|
buf.writeBytes(MsgUtils.getLenBytes(serviceId, 6));
|
||||||
|
//authenticatorSource
|
||||||
|
buf.writeBytes(MsgUtils.getAuthenticatorSource(serviceId, pwd));
|
||||||
|
//version
|
||||||
|
buf.writeByte(1);
|
||||||
|
//timestamp
|
||||||
|
buf.writeInt(Integer.parseInt(MsgUtils.getTimestamp()));
|
||||||
|
|
||||||
|
return buf.array();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.mashibing.smsgateway.netty4.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import com.mashibing.smsgateway.netty4.utils.MsgUtils;
|
||||||
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CMPP响应信息
|
||||||
|
*/
|
||||||
|
public class CmppSubmitResp {
|
||||||
|
|
||||||
|
private int sequenceId;
|
||||||
|
|
||||||
|
private int result;
|
||||||
|
|
||||||
|
private long msgId;
|
||||||
|
|
||||||
|
public CmppSubmitResp(byte[] bytes) {
|
||||||
|
this.sequenceId = MsgUtils.bytesToInt(ArrayUtils.subarray(bytes, 8, 12));
|
||||||
|
this.msgId = MsgUtils.bytesToLong(ArrayUtils.subarray(bytes, 12, 20));
|
||||||
|
this.msgId = Math.abs(this.msgId);
|
||||||
|
this.result = bytes[20];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getResult() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getMsgId() {
|
||||||
|
return msgId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSequenceId() {
|
||||||
|
return sequenceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,150 @@
|
|||||||
|
package com.mashibing.smsgateway.netty4.utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 以下常量均为CMPP2.0协议中规定的值
|
||||||
|
*/
|
||||||
|
public interface Command {
|
||||||
|
/**
|
||||||
|
* 请求连接
|
||||||
|
*/
|
||||||
|
public final int CMPP_CONNECT = 0x00000001;
|
||||||
|
/**
|
||||||
|
* 请求连接应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_CONNECT_RESP = 0x80000001;
|
||||||
|
/**
|
||||||
|
* 终止连接
|
||||||
|
*/
|
||||||
|
public final int CMPP_TERMINATE = 0x00000002;
|
||||||
|
/**
|
||||||
|
* 终止连接应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_TERMINATE_RESP = 0x80000002;
|
||||||
|
/**
|
||||||
|
* 提交短信
|
||||||
|
*/
|
||||||
|
public final int CMPP_SUBMIT = 0x00000004;
|
||||||
|
/**
|
||||||
|
* 提交短信应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_SUBMIT_RESP = 0x80000004;
|
||||||
|
/**
|
||||||
|
* 短信下发
|
||||||
|
*/
|
||||||
|
public final int CMPP_DELIVER = 0x00000005;
|
||||||
|
/**
|
||||||
|
* + 下发短信应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_DELIVER_RESP = 0x80000005;
|
||||||
|
/**
|
||||||
|
* 发送短信状态查询
|
||||||
|
*/
|
||||||
|
public final int CMPP_QUERY = 0x00000006;
|
||||||
|
/**
|
||||||
|
* 发送短信状态查询应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_QUERY_RESP = 0x80000006;
|
||||||
|
/**
|
||||||
|
* 删除短信
|
||||||
|
*/
|
||||||
|
public final int CMPP_CANCEL = 0x00000007;
|
||||||
|
/**
|
||||||
|
* 删除短信应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_CANCEL_RESP = 0x80000007;
|
||||||
|
/**
|
||||||
|
* 激活测试
|
||||||
|
*/
|
||||||
|
public final int CMPP_ACTIVE_TEST = 0x00000008;
|
||||||
|
/**
|
||||||
|
* 激活测试应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_ACTIVE_TEST_RESP = 0x80000008;
|
||||||
|
/**
|
||||||
|
* 消息前转
|
||||||
|
*/
|
||||||
|
public final int CMPP_FWD = 0x00000009;
|
||||||
|
/**
|
||||||
|
* 消息前转应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_FWD_RESP = 0x80000009;
|
||||||
|
/**
|
||||||
|
* MT路由请求
|
||||||
|
*/
|
||||||
|
public final int CMPP_MT_ROUTE = 0x00000010;
|
||||||
|
/**
|
||||||
|
* MT路由请求应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_MT_ROUTE_RESP = 0x80000010;
|
||||||
|
/**
|
||||||
|
* MO路由请求
|
||||||
|
*/
|
||||||
|
public final int CMPP_MO_ROUTE = 0x00000011;
|
||||||
|
/**
|
||||||
|
* MO路由请求应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_MO_ROUTE_RESP = 0x80000011;
|
||||||
|
/**
|
||||||
|
* 获取MT路由请求
|
||||||
|
*/
|
||||||
|
public final int CMPP_GET_MT_ROUTE = 0x00000012;
|
||||||
|
/**
|
||||||
|
* 获取MT路由请求应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_GET_MT_ROUTE_RESP = 0x80000012;
|
||||||
|
/**
|
||||||
|
* MT路由更新
|
||||||
|
*/
|
||||||
|
public final int CMPP_MT_ROUTE_UPDATE = 0x00000013;
|
||||||
|
/**
|
||||||
|
* MT路由更新应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_MT_ROUTE_UPDATE_RESP = 0x80000013;
|
||||||
|
/**
|
||||||
|
* MO路由更新
|
||||||
|
*/
|
||||||
|
public final int CMPP_MO_ROUTE_UPDATE = 0x00000014;
|
||||||
|
/**
|
||||||
|
* MO路由更新应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_MO_ROUTE_UPDATE_RESP = 0x80000014;
|
||||||
|
/**
|
||||||
|
* MT路由更新
|
||||||
|
*/
|
||||||
|
public final int CMPP_PUSH_MT_ROUTE_UPDATE = 0x00000015;
|
||||||
|
/**
|
||||||
|
* MT路由更新应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_PUSH_MT_ROUTE_UPDATE_RESP = 0x80000015;
|
||||||
|
/**
|
||||||
|
* MO路由更新
|
||||||
|
*/
|
||||||
|
public final int CMPP_PUSH_MO_ROUTE_UPDATE = 0x00000016;
|
||||||
|
/**
|
||||||
|
* MO路由更新应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_PUSH_MO_ROUTE_UPDATE_RESP = 0x80000016;
|
||||||
|
/**
|
||||||
|
* 获取MO路由请求
|
||||||
|
*/
|
||||||
|
public final int CMPP_GET_MO_ROUTE = 0x00000017;
|
||||||
|
/**
|
||||||
|
* 获取MO路由请求应答
|
||||||
|
*/
|
||||||
|
public final int CMPP_GET_MO_ROUTE_RESP = 0x80000017;
|
||||||
|
|
||||||
|
|
||||||
|
/** Cmpp协议版本字节常量 */
|
||||||
|
public static final byte CMPP2_VERSION = (byte) 32;
|
||||||
|
/** Cmpp协议版本字节常量 */
|
||||||
|
public static final byte CMPP3_VERSION = (byte) 48;
|
||||||
|
|
||||||
|
/** 下行长度(不包含短信内容长度) */
|
||||||
|
public static final int CMPP2_SUBMIT_LEN_EXPMSGLEN = 8 + 1 + 1 + 1 + 1 + 10 + 1 + 21 + 1 + 1 + 1
|
||||||
|
+ 6 + 2 + 6 + 17 + 17 + 21 + 1 + 1 + 8;
|
||||||
|
|
||||||
|
public static final int CMPP3_SUBMIT_LEN_EXPMSGLEN = 8 + 1 + 1 + 1 + 1 + 10 + 1 + 32 + 1 + 1
|
||||||
|
+ 1 + 1 + 6 + 2 + 6 + 17 + 17 + 21 + 1 + 1 + 1 + 20;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.mashibing.smsgateway.util;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zjw
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class SpringUtil implements ApplicationContextAware {
|
||||||
|
|
||||||
|
private static ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
SpringUtil.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Object getBeanByName(String beanName){
|
||||||
|
return SpringUtil.applicationContext.getBean(beanName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T getBeanByClass(Class<T> clazz){
|
||||||
|
return SpringUtil.applicationContext.getBean(clazz);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue