@@ -44,12 +44,12 @@ public class DipperPositionApplication { | |||||
log.info("北斗定位服务启动!"); | log.info("北斗定位服务启动!"); | ||||
//启动服务端 | //启动服务端 | ||||
ConfigurableEnvironment environment = applicationContext.getEnvironment(); | ConfigurableEnvironment environment = applicationContext.getEnvironment(); | ||||
log.info(environment.toString()); | |||||
log.info(environment.getProperty("position-server")); | |||||
//log.info(environment.toString()); | |||||
//log.info(environment.getProperty("position-server")); | |||||
//PositionConfigInfo pci; | //PositionConfigInfo pci; | ||||
//pci = (PositionConfigInfo)environment.getProperty("position-server"); | //pci = (PositionConfigInfo)environment.getProperty("position-server"); | ||||
DipperPositionServer nettyServer2 = new DipperPositionServer(environment); | DipperPositionServer nettyServer2 = new DipperPositionServer(environment); | ||||
log.info(environment.getSystemProperties().toString()); | |||||
//log.info(environment.getSystemProperties().toString()); | |||||
nettyServer2.start(); | nettyServer2.start(); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
log.error(e.getMessage()); | log.error(e.getMessage()); | ||||
@@ -13,6 +13,7 @@ import lombok.extern.slf4j.Slf4j; | |||||
import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
import org.springframework.beans.factory.annotation.Value; | import org.springframework.beans.factory.annotation.Value; | ||||
import java.io.InputStream; | |||||
import java.time.LocalDateTime; | import java.time.LocalDateTime; | ||||
/** | /** | ||||
@@ -24,6 +25,9 @@ import java.time.LocalDateTime; | |||||
@Slf4j | @Slf4j | ||||
public class NettyServerHandler extends ChannelInboundHandlerAdapter { | public class NettyServerHandler extends ChannelInboundHandlerAdapter { | ||||
private static String AST_TIME_CMD = "54494d45"; | |||||
private static String AST_POS_CMD = "504f53"; | |||||
private static String AST_EPH_CMD = "455048"; | |||||
@Autowired | @Autowired | ||||
private IDipperAstTimeAsyncTaskService dipperTimeAsyncTaskService; | private IDipperAstTimeAsyncTaskService dipperTimeAsyncTaskService; | ||||
@@ -110,13 +114,18 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { | |||||
String channelAns = ""; | String channelAns = ""; | ||||
// 返回时间指令 | // 返回时间指令 | ||||
if (Integer.parseInt(timeAsycServerPort) == channel.localAddress().getPort()) { | |||||
//if (Integer.parseInt(timeAsycServerPort) == channel.localAddress().getPort()) { | |||||
ByteBuf recvmg = (ByteBuf) msg; | |||||
String recvmgStr = recvmg.toString(); | |||||
if (AST_TIME_CMD.equals(recvmg)) { | |||||
// 初始时间辅助输入; | // 初始时间辅助输入; | ||||
channelAns = dipperTimeAsyncTaskService.pushAstTime(); | channelAns = dipperTimeAsyncTaskService.pushAstTime(); | ||||
} | } | ||||
// 发送SDBP-AST-POS获取辅助位置信息 | // 发送SDBP-AST-POS获取辅助位置信息 | ||||
if (Integer.parseInt(posAsycServerPort) == channel.localAddress().getPort()) { | |||||
// if (Integer.parseInt(posAsycServerPort) == channel.localAddress().getPort()) { | |||||
if (AST_POS_CMD.equals(recvmg)) { | |||||
channelAns = dipperAstPosAsyncTaskService.pushAstPos(ipAddress, | channelAns = dipperAstPosAsyncTaskService.pushAstPos(ipAddress, | ||||
centerProvinceFilePath, | centerProvinceFilePath, | ||||
centerProvince, | centerProvince, | ||||
@@ -125,7 +134,8 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { | |||||
} | } | ||||
// 从缓存获取SDBP-AST-EPH星历数 | // 从缓存获取SDBP-AST-EPH星历数 | ||||
if (Integer.parseInt(starsAsycServerPort) == channel.localAddress().getPort()) { | |||||
//if (Integer.parseInt(starsAsycServerPort) == channel.localAddress().getPort()) { | |||||
if (AST_POS_CMD.equals(recvmg)) { | |||||
channelAns = dipperDataAsyncTaskService.getAstEPH(); | channelAns = dipperDataAsyncTaskService.getAstEPH(); | ||||
} | } | ||||
@@ -28,6 +28,7 @@ public class DipperPositionServer { | |||||
// @Autowired PositionConfigInfo positionConfigInfo; | // @Autowired PositionConfigInfo positionConfigInfo; | ||||
private String serverAddr; | private String serverAddr; | ||||
private Integer serverPort; | |||||
private Integer timeAsycPort; | private Integer timeAsycPort; | ||||
private Integer posAsycPort; | private Integer posAsycPort; | ||||
private Integer starsAsycPort; | private Integer starsAsycPort; | ||||
@@ -35,6 +36,7 @@ public class DipperPositionServer { | |||||
public DipperPositionServer(ConfigurableEnvironment environment) throws NacosException { | public DipperPositionServer(ConfigurableEnvironment environment) throws NacosException { | ||||
this.serverAddr = environment.getProperty("position-server.serverAddr"); | this.serverAddr = environment.getProperty("position-server.serverAddr"); | ||||
this.serverPort = Integer.parseInt(environment.getProperty("server.port")); | |||||
this.timeAsycPort = Integer.parseInt(environment.getProperty("position-server.timeAsycPort")); | this.timeAsycPort = Integer.parseInt(environment.getProperty("position-server.timeAsycPort")); | ||||
this.posAsycPort = Integer.parseInt(environment.getProperty("position-server.posAsycPort")); | this.posAsycPort = Integer.parseInt(environment.getProperty("position-server.posAsycPort")); | ||||
this.starsAsycPort = Integer.parseInt(environment.getProperty("position-server.starsAsycPort")); | this.starsAsycPort = Integer.parseInt(environment.getProperty("position-server.starsAsycPort")); | ||||
@@ -211,4 +213,45 @@ public class DipperPositionServer { | |||||
workThreadGroup.shutdownGracefully(); | workThreadGroup.shutdownGracefully(); | ||||
} | } | ||||
} | } | ||||
public void start2() { | |||||
//new 一个主线程组 | |||||
EventLoopGroup mainThreadGroup = new NioEventLoopGroup(1); | |||||
//new 一个工作线程组 | |||||
EventLoopGroup workThreadGroup = new NioEventLoopGroup(200); | |||||
//InetSocketAddress socketAddress = new InetSocketAddress(serverAddr,null); | |||||
ServerBootstrap bootstrap = new ServerBootstrap() | |||||
.group(mainThreadGroup, workThreadGroup) | |||||
.channel(NioServerSocketChannel.class) | |||||
.childHandler(new ServerChannelInitializer()) | |||||
//.localAddress(socketAddress) | |||||
//设置队列大小 | |||||
.option(ChannelOption.SO_BACKLOG, 1024) | |||||
// 两小时内没有数据的通信时,TCP会自动发送一个活动探测数据报文 | |||||
.childOption(ChannelOption.SO_KEEPALIVE, true); | |||||
//绑定端口,开始接收进来的连接 | |||||
try { | |||||
ChannelFuture channelFuture = bootstrap.bind(serverPort).sync(); | |||||
log.info("时间辅助服务器启动开始监听端口: {}", serverPort); | |||||
channelFuture.addListener(future -> { | |||||
if (future.isSuccess()){ | |||||
System.out.println("start success"); | |||||
}else{ | |||||
System.out.println("start failed"); | |||||
} | |||||
}); | |||||
//channelFuture.channel().closeFuture().sync(); | |||||
} catch (InterruptedException e) { | |||||
e.printStackTrace(); | |||||
} finally { | |||||
//关闭主线程组 | |||||
mainThreadGroup.shutdownGracefully(); | |||||
//关闭工作线程组 | |||||
workThreadGroup.shutdownGracefully(); | |||||
} | |||||
} | |||||
} | } |