Commit 81366567 by 叶立庆
2 parents 46365e7a ff393b86
...@@ -29,7 +29,7 @@ public class TimeSettingRspHandler implements TcpHandler<TimeSettingRsp, TimeSet ...@@ -29,7 +29,7 @@ public class TimeSettingRspHandler implements TcpHandler<TimeSettingRsp, TimeSet
.parseCp56Time2aStr(data.getTime()) /* DateUtils.datetimeToStr(ByteUtils.parseCp56Time2a(timeSettingRsp.getData().getTime())) */); .parseCp56Time2aStr(data.getTime()) /* DateUtils.datetimeToStr(ByteUtils.parseCp56Time2a(timeSettingRsp.getData().getTime())) */);
// TODO // TODO
String key = channel.attr(ProxyHelper.PILE_ID_KEY).get() + "+" + rsp.getSeq(); String key = ProxyHelper.getCallbackKey(rsp, channel);
TcpCallback<TcpRsp> callback = ProxyHelper.getCallback(key); TcpCallback<TcpRsp> callback = ProxyHelper.getCallback(key);
callback.callback(rsp); callback.callback(rsp);
......
package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server; package cn.com.glsx.charging.modules.gbt.ocpp16.tcp.server;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpBase;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpCallback; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpCallback;
import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpRsp; import cn.com.glsx.charging.modules.gbt.ocpp16.tcp.base.TcpRsp;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
...@@ -174,4 +175,11 @@ public class ProxyHelper { ...@@ -174,4 +175,11 @@ public class ProxyHelper {
public static TcpCallback<TcpRsp> getCallback(String key) { public static TcpCallback<TcpRsp> getCallback(String key) {
return callbackMap.get(key); return callbackMap.get(key);
} }
public static <T extends TcpBase> String getCallbackKey(T data, Channel channel) {
if (channel == null) {
throw new RuntimeException("channel为null!!!");
}
return channel.attr(ProxyHelper.PILE_ID_KEY).get() + "+" + data.getSeq();
}
} }
...@@ -34,10 +34,15 @@ public class TcpServer { ...@@ -34,10 +34,15 @@ public class TcpServer {
public static Integer PROTOCOL_MAX_LENGTH = 200; public static Integer PROTOCOL_MAX_LENGTH = 200;
/** 用于分配处理业务线程的线程组个数 */ /**
* 用于分配处理业务线程的线程组个数
*/
protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors(); // 默认 protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors(); // 默认
/** 业务线程大小 */ /**
protected static final int BIZTHREADSIZE = Math.max(Runtime.getRuntime().availableProcessors() * 2, 16); * 业务线程大小
*/
protected static final int BIZTHREADSIZE = Math.max(
Runtime.getRuntime().availableProcessors() * 2, 16);
private final String host; private final String host;
private final int port; private final int port;
...@@ -61,7 +66,8 @@ public class TcpServer { ...@@ -61,7 +66,8 @@ public class TcpServer {
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("lengthDecoder", new LengthFieldBasedFrameDecoder(PROTOCOL_MAX_LENGTH, 1, 1, 2, 0)); pipeline.addLast("lengthDecoder",
new LengthFieldBasedFrameDecoder(PROTOCOL_MAX_LENGTH, 1, 1, 2, 0));
pipeline.addLast("decoder", new ByteArrayDecoder()); pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new ByteArrayEncoder()); pipeline.addLast("encoder", new ByteArrayEncoder());
pipeline.addLast("handler", new TcpServerHandler()); pipeline.addLast("handler", new TcpServerHandler());
...@@ -111,10 +117,10 @@ public class TcpServer { ...@@ -111,10 +117,10 @@ public class TcpServer {
log.info("【{}】关闭TCP服务器!!!!!!", port); log.info("【{}】关闭TCP服务器!!!!!!", port);
} }
@SuppressWarnings({ "rawtypes" }) @SuppressWarnings({"rawtypes"})
public <T extends TcpBase> TcpCallback<TcpRsp> sendSync(T data) { public <T extends TcpBase> TcpCallback<TcpRsp> sendSync(T data) {
ProxyInfo proxyInfo = ProxyHelper.getProxyInfo(data.getData().getPileIdStr()); ProxyInfo proxyInfo = ProxyHelper.getProxyInfo(data.getData().getPileIdStr());
String ip=proxyInfo.getIp(); String ip = proxyInfo.getIp();
// seq字段处理 // seq字段处理
String seqKey = ip; String seqKey = ip;
if (data.getData() != null) { if (data.getData() != null) {
...@@ -124,7 +130,7 @@ public class TcpServer { ...@@ -124,7 +130,7 @@ public class TcpServer {
byte[] bs = Ocpp16Helper.build(data); byte[] bs = Ocpp16Helper.build(data);
log.info("【{}】读取proxyInfo:{}", data.getData().getPileIdStr(), proxyInfo); log.info("【{}】读取proxyInfo:{}", data.getData().getPileIdStr(), proxyInfo);
String key = proxyInfo.getChannel().attr(ProxyHelper.PILE_ID_KEY).get() + "+" + data.getSeq(); String key = ProxyHelper.getCallbackKey(data, proxyInfo.getChannel());
TcpCallback<TcpRsp> callback = new TcpCallback<>(TcpRsp.class); TcpCallback<TcpRsp> callback = new TcpCallback<>(TcpRsp.class);
ProxyHelper.addCallback(key, callback); ProxyHelper.addCallback(key, callback);
...@@ -138,7 +144,7 @@ public class TcpServer { ...@@ -138,7 +144,7 @@ public class TcpServer {
private void sendSync(byte[] bs, Channel channel) { private void sendSync(byte[] bs, Channel channel) {
try { try {
if (channel == null ) { if (channel == null) {
log.info("channel null ..."); log.info("channel null ...");
return; return;
} }
......
...@@ -54,11 +54,11 @@ public class TimeSettingTest { ...@@ -54,11 +54,11 @@ public class TimeSettingTest {
req.setData(data); req.setData(data);
Thread.sleep(30000); Thread.sleep(20000);
TcpCallback<TcpRsp> callback = tcpServer.sendSync(req); TcpCallback<TcpRsp> callback = tcpServer.sendSync(req);
Thread.sleep(30000); Thread.sleep(10000);
TcpRsp rsp = callback.get(); TcpRsp rsp = callback.get();
log.info("callback{}",req); log.info("callback{}",req);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!