diff --git a/kenaito-config-common/src/main/java/cn/odboy/config/model/SmallMessage.java b/kenaito-config-common/src/main/java/cn/odboy/config/model/SmallMessage.java index 4c86825..c69176f 100644 --- a/kenaito-config-common/src/main/java/cn/odboy/config/model/SmallMessage.java +++ b/kenaito-config-common/src/main/java/cn/odboy/config/model/SmallMessage.java @@ -21,23 +21,23 @@ public class SmallMessage implements Serializable { public static class Response implements Serializable { private Boolean success = true; private String errorCode = "0"; - private String errorMsg = "success"; + private String errorMessage = "success"; private Object data; - public static Response bad(String errorMsg) { + public static Response bad(String errorMessage) { Response response = new Response(); response.setSuccess(false); response.setErrorCode("400"); - response.setErrorMsg(errorMsg); + response.setErrorMessage(errorMessage); response.setData(null); return response; } - public static Response ok(Object data, String errorMsg) { + public static Response ok(Object data, String errorMessage) { Response response = new Response(); response.setSuccess(true); response.setErrorCode("0"); - response.setErrorMsg(errorMsg); + response.setErrorMessage(errorMessage); response.setData(data); return response; } @@ -46,7 +46,7 @@ public class SmallMessage implements Serializable { Response response = new Response(); response.setSuccess(true); response.setErrorCode("0"); - response.setErrorMsg("success"); + response.setErrorMessage("success"); response.setData(data); return response; } diff --git a/kenaito-config-common/src/main/java/cn/odboy/config/util/ChannelUtil.java b/kenaito-config-common/src/main/java/cn/odboy/config/util/ChannelUtil.java new file mode 100644 index 0000000..e0263f2 --- /dev/null +++ b/kenaito-config-common/src/main/java/cn/odboy/config/util/ChannelUtil.java @@ -0,0 +1,18 @@ +package cn.odboy.config.util; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelId; +/** + * 统一操作Channel + * @author odboy + * @date 2024-12-06 + */ +public class ChannelUtil { + public static String getId(ChannelHandlerContext ctx){ + return ctx.channel().id().asShortText(); + } + + public static String getId(ChannelId channelId){ + return channelId.asShortText(); + } +} diff --git a/kenaito-config-common/src/main/java/cn/odboy/config/util/MessageUtil.java b/kenaito-config-common/src/main/java/cn/odboy/config/util/MessageUtil.java index 147d179..1c8158b 100644 --- a/kenaito-config-common/src/main/java/cn/odboy/config/util/MessageUtil.java +++ b/kenaito-config-common/src/main/java/cn/odboy/config/util/MessageUtil.java @@ -1,18 +1,59 @@ package cn.odboy.config.util; +import cn.odboy.config.constant.TransferMessageType; import cn.odboy.config.model.SmallMessage; +import cn.odboy.config.model.msgtype.ConfigFileInfo; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import java.util.ArrayList; +import java.util.List; +/** + * 传输消息序列化 + * + * @author odboy + * @date 2024-12-06 + */ public class MessageUtil { - public static ByteBuf toByteBuf(Object data){ - return Unpooled.copiedBuffer(ProtostuffUtil.serializer(data)); - } + public static ByteBuf toByteBuf(Object data) { + return Unpooled.copiedBuffer(ProtostuffUtil.serializer(data)); + } - public static SmallMessage getMessage(Object msg) { - ByteBuf buf = (ByteBuf) msg; - byte[] bytes = new byte[buf.readableBytes()]; - buf.readBytes(bytes); - return ProtostuffUtil.deserializer(bytes, SmallMessage.class); + public static SmallMessage getMessage(Object msg) { + ByteBuf buf = (ByteBuf) msg; + byte[] bytes = new byte[buf.readableBytes()]; + buf.readBytes(bytes); + return ProtostuffUtil.deserializer(bytes, SmallMessage.class); + } + + // ================ 以下为很糙的自定义方法 + public static ByteBuf toRegisterBad(String errorMessage) { + return toByteBuf( + new SmallMessage(TransferMessageType.REGISTER, SmallMessage.Response.bad(errorMessage))); + } + + public static ByteBuf toRegisterOk(Object data) { + return toByteBuf( + new SmallMessage(TransferMessageType.REGISTER, SmallMessage.Response.ok(data))); + } + + public static ByteBuf toPushConfigBad(String errorMessage) { + return toByteBuf( + new SmallMessage(TransferMessageType.PUSH_CONFIG, SmallMessage.Response.bad(errorMessage))); + } + + public static ByteBuf toPushConfigOk(Object data) { + return toByteBuf( + new SmallMessage(TransferMessageType.PUSH_CONFIG, SmallMessage.Response.ok(data))); + } + + public static List toConfigFileInfoList(Object o) { + if (o instanceof List) { + List list = (List) o; + if (!list.isEmpty() && list.get(0) instanceof ConfigFileInfo) { + return (List) list; + } } + return new ArrayList<>(); + } } diff --git a/kenaito-config-common/src/main/java/cn/odboy/config/util/PropertyNameUtil.java b/kenaito-config-common/src/main/java/cn/odboy/config/util/PropertyNameUtil.java new file mode 100644 index 0000000..c1d460e --- /dev/null +++ b/kenaito-config-common/src/main/java/cn/odboy/config/util/PropertyNameUtil.java @@ -0,0 +1,15 @@ +package cn.odboy.config.util; + +/** + * 配置命名空间 名称 + * + * @author odboy + * @date 2024-12-06 + */ +public class PropertyNameUtil { + private static final String DEFAULT_PREFIX = "kenaito"; + + public static String get(String fileName) { + return DEFAULT_PREFIX + "_" + fileName; + } +} diff --git a/kenaito-config-common/src/main/java/cn/odboy/config/util/ProtostuffUtil.java b/kenaito-config-common/src/main/java/cn/odboy/config/util/ProtostuffUtil.java index 1cfda1d..8c28553 100644 --- a/kenaito-config-common/src/main/java/cn/odboy/config/util/ProtostuffUtil.java +++ b/kenaito-config-common/src/main/java/cn/odboy/config/util/ProtostuffUtil.java @@ -14,15 +14,15 @@ import java.util.concurrent.ConcurrentHashMap; */ public class ProtostuffUtil { - private static Map, Schema> cachedSchema = new ConcurrentHashMap, Schema>(); + private static final Map, Schema> CACHED_SCHEMA = new ConcurrentHashMap, Schema>(); private static Schema getSchema(Class clazz) { @SuppressWarnings("unchecked") - Schema schema = (Schema) cachedSchema.get(clazz); + Schema schema = (Schema) CACHED_SCHEMA.get(clazz); if (schema == null) { schema = RuntimeSchema.getSchema(clazz); if (schema != null) { - cachedSchema.put(clazz, schema); + CACHED_SCHEMA.put(clazz, schema); } } return schema; @@ -30,9 +30,6 @@ public class ProtostuffUtil { /** * 序列化 - * - * @param obj - * @return */ public static byte[] serializer(T obj) { @SuppressWarnings("unchecked") @@ -50,10 +47,6 @@ public class ProtostuffUtil { /** * 反序列化 - * - * @param data - * @param clazz - * @return */ public static T deserializer(byte[] data, Class clazz) { try {