diff --git a/config/application-local.properties b/config/application-local.properties index 078a4f8..b018519 100644 --- a/config/application-local.properties +++ b/config/application-local.properties @@ -27,6 +27,8 @@ spring.datasource.dbcp2.connection-properties=characterEncoding=utf8 #mybatis.configuration.map-underscore-to-camel-case: true mybatis.mapper-locations=classpath*:mappers/*.xml mybatis.type-aliases-package=com.zjyr.beidouservice.pojo.entry -#mybatis.configuration.default-enum-type-handler=com.dispose.common.CommonEnumHandler +mybatis.configuration.default-enum-type-handler=com.zjyr.beidouservice.common.CommonEnumHandler +#mybatis.configuration.log-impl=lombok.extern.slf4j.Slf4j #config log -logging.config=file:config/logback.xml \ No newline at end of file +logging.config=file:config/logback.xml +log4j.logger.org.mybatis=debug \ No newline at end of file diff --git a/config/logback.xml b/config/logback.xml index 13a00dc..64a24bd 100644 --- a/config/logback.xml +++ b/config/logback.xml @@ -4,6 +4,8 @@ + + @@ -46,14 +48,7 @@ - - - - - - + diff --git a/pom.xml b/pom.xml index 5ba4735..a05da81 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,12 @@ org.springframework.boot spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + org.mybatis.spring.boot @@ -70,11 +76,21 @@ mapper-spring-boot-starter 4.2.3 + + com.github.seancfoley + ipaddress + 5.4.0 + com.microsoft.sqlserver mssql-jdbc 12.4.0.jre11 + + org.springframework.boot + spring-boot-starter-undertow + 3.1.2 + diff --git a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/ChannelInit.java b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/ChannelInit.java index f318a73..e6d57a6 100644 --- a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/ChannelInit.java +++ b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/ChannelInit.java @@ -23,8 +23,8 @@ public class ChannelInit extends ChannelInitializer { protected void initChannel(SocketChannel channel) { channel.pipeline() .addLast("idle", new IdleStateHandler(0, 0, 60, TimeUnit.SECONDS)) - .addLast("encode", new YuanRongProtocolDecode()) - .addLast("decode", new StringDecoder(CharsetUtil.UTF_8)) + .addLast("decode", new YuanRongProtocolDecode()) + .addLast("encode", new StringDecoder(CharsetUtil.UTF_8)) .addLast("message", messageHandler); } } diff --git a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/MessageHandler.java b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/MessageHandler.java index 279fab1..138f50a 100644 --- a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/MessageHandler.java +++ b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/MessageHandler.java @@ -1,6 +1,8 @@ package com.zjyr.beidouservice.adapter.impl.netty; -import com.zjyr.beidouservice.misc.HelperUtils; +import com.zjyr.beidouservice.common.impl.ControlDeviceTypeName; +import com.zjyr.beidouservice.mapper.ControlDeviceMapper; +import com.zjyr.beidouservice.pojo.entry.ControlDevice; import com.zjyr.beidouservice.pojo.vo.ControlAdapterSocketCtx; import com.zjyr.beidouservice.pojo.vo.YuanRongBinProtocol; import io.netty.channel.ChannelHandler; @@ -8,10 +10,12 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; +import jakarta.annotation.Resource; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; +import java.net.InetSocketAddress; import java.util.concurrent.ConcurrentHashMap; @Slf4j @@ -20,6 +24,8 @@ import java.util.concurrent.ConcurrentHashMap; @RequiredArgsConstructor public class MessageHandler extends SimpleChannelInboundHandler { public static ConcurrentHashMap ctxMap = new ConcurrentHashMap<>(); + @Resource + private ControlDeviceMapper controlDeviceMapper; @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { @@ -40,20 +46,26 @@ public class MessageHandler extends SimpleChannelInboundHandler list = controlDeviceMapper.selectAll(); + log.info("{}:: Connected <-- {}:{}", ctx.channel().id(), sa.getAddress().getHostAddress(), sa.getPort()); + controlDeviceMapper.addControlDevice(ControlDevice.builder().deviceType(ControlDeviceTypeName.DEVICE_SOCKET_TCP) + .deviceAddr(sa.getAddress().getHostAddress()).build()); super.channelActive(ctx); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - log.info("{}:: Disonnected", ctx.channel().id()); + InetSocketAddress sa = (InetSocketAddress) ctx.channel().remoteAddress(); + log.info("{}:: Disonnected <-- {}", ctx.channel().id(), sa.getAddress().getHostAddress()); super.channelActive(ctx); ctx.close(); } diff --git a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/decode/YuanRongProtocolDecode.java b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/decode/YuanRongProtocolDecode.java index 65c652d..7a6d6e3 100644 --- a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/decode/YuanRongProtocolDecode.java +++ b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/decode/YuanRongProtocolDecode.java @@ -3,10 +3,12 @@ package com.zjyr.beidouservice.adapter.impl.netty.decode; import com.zjyr.beidouservice.pojo.vo.ControlAdapterSocketCtx; import com.zjyr.beidouservice.pojo.vo.YuanRongBinProtocol; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.nio.ByteBuffer; @@ -19,6 +21,7 @@ import java.util.regex.Pattern; @Component @RequiredArgsConstructor +@Slf4j public class YuanRongProtocolDecode extends ByteToMessageDecoder { public static final ByteBuf receiveBuffer = Unpooled.buffer(1024 * 1024); @@ -63,16 +66,31 @@ public class YuanRongProtocolDecode extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List out) throws Exception { // cache receive data - receiveBuffer.writeBytes(buffer.array()); + //receiveBuffer.writeBytes(buffer.array()); + log.info(ByteBufUtil.prettyHexDump(buffer)); - ctx.channel().eventLoop().execute(new Runnable() { - @Override - public void run() { - if (receiveBuffer.readableBytes() > YuanRongBinProtocol.MIN_LEN) { - byte[] val = new byte[buffer.readableBytes()]; - buffer.readBytes(val, 0, buffer.readableBytes()); + if(buffer.readableBytes() > YuanRongBinProtocol.MIN_LEN) { + byte[] data = ByteBufUtil.getBytes(buffer); + byte[] startFlag = YuanRongBinProtocol.START.getBytes(); + + for(int i = 0; i < data.length - startFlag.length; i++) { + byte[] sclicedArr = Arrays.copyOfRange(data, i, i + startFlag.length); + if (Arrays.equals(sclicedArr, startFlag)) { + for (int j = i; j < data.length; j++) { + // Find package head end flag + if (data[j] == '$') { + // It must be a package + Matcher matcher = YuanRongBinProtocol.compile.matcher(new String(ByteBufUtil.getBytes(buffer, i, j - i + 1))); + + if(matcher.find()) { + for(int k = 0; k <= matcher.groupCount(); k++){ + System.out.println(matcher.group(k)); + } + } + } + } } } - }); + } } } diff --git a/src/main/java/com/zjyr/beidouservice/common/CommonEnumHandler.java b/src/main/java/com/zjyr/beidouservice/common/CommonEnumHandler.java new file mode 100644 index 0000000..a3d354a --- /dev/null +++ b/src/main/java/com/zjyr/beidouservice/common/CommonEnumHandler.java @@ -0,0 +1,139 @@ +package com.zjyr.beidouservice.common; + +import org.apache.ibatis.type.BaseTypeHandler; +import org.apache.ibatis.type.JdbcType; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; + +public final class CommonEnumHandler extends BaseTypeHandler { + /** + * The Enum type. + */ + private final Class enumType; + /** + * The Enums. + */ + private final List enums; + + /** + * Instantiates a new Common enum handler. + * + * @param type the type + */ + public CommonEnumHandler(Class type) { + if (type == null) { + throw new IllegalArgumentException("Type argument cannot be null"); + } + + this.enumType = type; + this.enums = Arrays.asList(type.getEnumConstants()); + } + + /** + * Code of e. + * + * @param the type parameter + * @param enumClass the enum class + * @param code the code + * @return the e + */ + public static & EnumerationBase> E codeOf(Class enumClass, int code) { + E[] enumCodes = enumClass.getEnumConstants(); + + for (E e : enumCodes) { + if (e.getValue() == code) { + return e; + } + } + + return null; + } + + /** + * Sets non null parameter. + * + * @param preparedStatement the prepared statement + * @param i the + * @param e the e + * @param jdbcType the jdbc type + * @throws SQLException the sql exception + */ + @Override + public void setNonNullParameter(PreparedStatement preparedStatement, int i, E e, JdbcType jdbcType) throws SQLException { + preparedStatement.setInt(i, e.getValue()); + } + + /** + * Gets nullable result. + * + * @param resultSet the result set + * @param s the s + * @return the nullable result + * @throws SQLException the sql exception + */ + @Override + public E getNullableResult(ResultSet resultSet, String s) throws SQLException { + if (resultSet.getObject(s) == null) { + return null; + } + + int val = resultSet.getInt(s); + return locateEnumStatus(val); + } + + /** + * Gets nullable result. + * + * @param resultSet the result set + * @param index the index + * @return the nullable result + * @throws SQLException the sql exception + */ + @Override + public E getNullableResult(ResultSet resultSet, int index) throws SQLException { + if (resultSet.getObject(index) == null) { + return null; + } + + int val = resultSet.getInt(index); + return locateEnumStatus(val); + } + + /** + * Gets nullable result. + * + * @param callableStatement the callable statement + * @param index the index + * @return the nullable result + * @throws SQLException the sql exception + */ + @Override + public E getNullableResult(CallableStatement callableStatement, int index) throws SQLException { + if (callableStatement.getObject(index) == null) { + return null; + } + + int val = callableStatement.getInt(index); + return locateEnumStatus(val); + } + + /** + * Locate enum status e. + * + * @param index the index + * @return the e + */ + private E locateEnumStatus(int index) { + for (E e : enums) { + if (e.getValue() == index) { + return e; + } + } + throw new IllegalArgumentException(enumType.getName() + " unknown enumerated type index:" + index); + } +} diff --git a/src/main/java/com/zjyr/beidouservice/common/EnumerationBase.java b/src/main/java/com/zjyr/beidouservice/common/EnumerationBase.java new file mode 100644 index 0000000..1b14972 --- /dev/null +++ b/src/main/java/com/zjyr/beidouservice/common/EnumerationBase.java @@ -0,0 +1,6 @@ +package com.zjyr.beidouservice.common; + +public interface EnumerationBase { + Integer getValue(); + String getDescription(); +} diff --git a/src/main/java/com/zjyr/beidouservice/common/impl/ControlDeviceTypeName.java b/src/main/java/com/zjyr/beidouservice/common/impl/ControlDeviceTypeName.java new file mode 100644 index 0000000..ef79597 --- /dev/null +++ b/src/main/java/com/zjyr/beidouservice/common/impl/ControlDeviceTypeName.java @@ -0,0 +1,28 @@ +package com.zjyr.beidouservice.common.impl; + +import com.zjyr.beidouservice.common.EnumerationBase; + +public enum ControlDeviceTypeName implements EnumerationBase { + DEVICE_SOCKET_TCP(0, "HOST_TOTAL_TRAFFIC"), + + DEVICE_SOCKET_UDP(1, "HOST_TOTAL_TRAFFIC"), + ; + + private final Integer code; + private final String desc; + + ControlDeviceTypeName(int val, String desc) { + this.code = val; + this.desc = desc; + } + + @Override + public Integer getValue() { + return this.code; + } + + @Override + public String getDescription() { + return this.desc; + } +} diff --git a/src/main/java/com/zjyr/beidouservice/mapper/ControlDeviceMapper.java b/src/main/java/com/zjyr/beidouservice/mapper/ControlDeviceMapper.java new file mode 100644 index 0000000..d9d9f2b --- /dev/null +++ b/src/main/java/com/zjyr/beidouservice/mapper/ControlDeviceMapper.java @@ -0,0 +1,13 @@ +package com.zjyr.beidouservice.mapper; + +import com.zjyr.beidouservice.common.impl.ControlDeviceTypeName; +import com.zjyr.beidouservice.pojo.entry.ControlDevice; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface ControlDeviceMapper { + List selectAll(); + + int addControlDevice(@Param("device") ControlDevice device); +} diff --git a/src/main/java/com/zjyr/beidouservice/pojo/entry/ControlDevice.java b/src/main/java/com/zjyr/beidouservice/pojo/entry/ControlDevice.java index 0c97b84..1d127ad 100644 --- a/src/main/java/com/zjyr/beidouservice/pojo/entry/ControlDevice.java +++ b/src/main/java/com/zjyr/beidouservice/pojo/entry/ControlDevice.java @@ -2,6 +2,7 @@ package com.zjyr.beidouservice.pojo.entry; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.zjyr.beidouservice.common.impl.ControlDeviceTypeName; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -25,5 +26,6 @@ public class ControlDevice { @Id @KeySql(useGeneratedKeys = true) private Long id; - private Long deviceType; + private ControlDeviceTypeName deviceType; + private String deviceAddr; } diff --git a/src/main/java/com/zjyr/beidouservice/pojo/vo/YuanRongBinProtocol.java b/src/main/java/com/zjyr/beidouservice/pojo/vo/YuanRongBinProtocol.java index 4af4c0e..77e8063 100644 --- a/src/main/java/com/zjyr/beidouservice/pojo/vo/YuanRongBinProtocol.java +++ b/src/main/java/com/zjyr/beidouservice/pojo/vo/YuanRongBinProtocol.java @@ -2,12 +2,15 @@ package com.zjyr.beidouservice.pojo.vo; import lombok.Data; +import java.util.regex.Pattern; + @Data public class YuanRongBinProtocol { public static int MIN_LEN = 25; public static String START = "[length="; - public static String HEAD_PARTERN = "\\[length=\\d{1,4}\\]@\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}:\\d{1,5}\\|\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}@\\$"; + private static String HEAD_PARTERN = "^\\[length=(\\d{1,4})]@(\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}):(\\d{1,5})\\|(\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3})@\\$.*$"; + public static Pattern compile = Pattern.compile(HEAD_PARTERN); private byte[] proHeader; private byte[] proData; diff --git a/src/main/resources/mappers/ControlDevice.xml b/src/main/resources/mappers/ControlDevice.xml new file mode 100644 index 0000000..a158d56 --- /dev/null +++ b/src/main/resources/mappers/ControlDevice.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + INSERT IGNORE INTO control_device(deviceAddr, deviceType) + SELECT #{device.deviceAddr}, #{device.deviceType} FROM DUAL WHERE NOT EXISTS (SELECT deviceAddr FROM control_device + WHERE deviceAddr = #{device.deviceAddr}) + + \ No newline at end of file diff --git a/src/test/java/com/zjyr/beidouservice/BaidouServiceApplicationTests.java b/src/test/java/com/zjyr/beidouservice/BaidouServiceApplicationTests.java index 55de68a..0820819 100644 --- a/src/test/java/com/zjyr/beidouservice/BaidouServiceApplicationTests.java +++ b/src/test/java/com/zjyr/beidouservice/BaidouServiceApplicationTests.java @@ -1,8 +1,11 @@ package com.zjyr.beidouservice; +import inet.ipaddr.HostName; +import inet.ipaddr.IPAddress; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -14,13 +17,24 @@ class BaidouServiceApplicationTests { @Test void contextLoads() { String str = "[length=108]@127.0.0.1:51141|192.168.1.80@$"; - Pattern compile = Pattern.compile("\\[length=\\d{1,4}\\]@\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}:\\d{1,5}\\|\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}@\\$"); + Pattern compile = Pattern.compile("^\\[length=(\\d{1,4})]@(\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}):(\\d{1,5})\\|(\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3})@\\$.*$"); Matcher matcher = compile.matcher(str); List targetList = new ArrayList<>(); while (matcher.find()) { String substring = str.substring(matcher.start(), matcher.end()); targetList.add(substring); + for(int i = 0; i <= matcher.groupCount(); i++){ + System.out.println(matcher.group(i)); + } } } + @Test + void ipAddressTest() { + String host = "127.0.0.1:23355"; + HostName host2 = new HostName(host); + IPAddress adr = host2.asAddress(); + System.out.println(adr.toIPAddress()); + } + } diff --git a/src/test/java/com/zjyr/beidouservice/mapper/ControlDeviceMapperTest.java b/src/test/java/com/zjyr/beidouservice/mapper/ControlDeviceMapperTest.java new file mode 100644 index 0000000..3b91906 --- /dev/null +++ b/src/test/java/com/zjyr/beidouservice/mapper/ControlDeviceMapperTest.java @@ -0,0 +1,43 @@ +package com.zjyr.beidouservice.mapper; + +import com.zjyr.beidouservice.common.impl.ControlDeviceTypeName; +import com.zjyr.beidouservice.pojo.entry.ControlDevice; +import com.zjyr.beidouservice.pojo.entry.ControlDeviceType; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.Rollback; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@Slf4j +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) +//@Transactional +//@Rollback +public class ControlDeviceMapperTest { + @Resource + private ControlDeviceMapper controlDeviceMapper; + @Test + public void a1_getAllControlDevice() { + List typeList = controlDeviceMapper.selectAll(); + Assertions.assertNotEquals(typeList.size(), 0); + } + + @Test + public void a2_addControlDevice() { + ControlDevice dev = ControlDevice.builder().deviceType(ControlDeviceTypeName.DEVICE_SOCKET_TCP).deviceAddr("127.0.0.2").build(); + int i = controlDeviceMapper.addControlDevice(dev); + System.out.println("Add " + i); + } + + @Test + public void a2_addNew() { + int i = controlDeviceMapper.addNew("127.0.0.3", 0L); + System.out.println("Add " + i); + } +} diff --git a/src/test/java/com/zjyr/beidouservice/mapper/ControlDeviceTypeMapperTest.java b/src/test/java/com/zjyr/beidouservice/mapper/ControlDeviceTypeMapperTest.java index a12b604..835e85d 100644 --- a/src/test/java/com/zjyr/beidouservice/mapper/ControlDeviceTypeMapperTest.java +++ b/src/test/java/com/zjyr/beidouservice/mapper/ControlDeviceTypeMapperTest.java @@ -1,15 +1,14 @@ package com.zjyr.beidouservice.mapper; -import com.fasterxml.jackson.core.JsonProcessingException; import com.zjyr.beidouservice.pojo.entry.ControlDeviceType; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.Rollback; import org.springframework.transaction.annotation.Transactional; -import org.junit.jupiter.api.Test; import java.util.List; @@ -22,8 +21,9 @@ import java.util.List; public class ControlDeviceTypeMapperTest { @Resource private ControlDeviceTypeMapper mapper; + @Test - public void a5_getTaskInfo() { + public void a1_getTaskInfo() { List typeList = mapper.selectAll(); Assertions.assertNotEquals(typeList.size(), 0); }