diff --git a/netty-model/pom.xml b/netty-model/pom.xml new file mode 100644 index 0000000..acd160d --- /dev/null +++ b/netty-model/pom.xml @@ -0,0 +1,37 @@ + + + + smartvenue + smartvenue-parent + 2.0.0 + ../pom.xml + + + 4.0.0 + netty-model + ${smartvenue.version} + + + + io.netty + netty-all + 4.1.10.Final + + + org.slf4j + slf4j-api + 1.7.33 + + + com.fasterxml.jackson.core + jackson-core + 2.13.1 + + + com.fasterxml.jackson.core + jackson-databind + 2.13.1 + + + diff --git a/netty-model/src/main/java/com/sv/netty/config/ChannelParam.java b/netty-model/src/main/java/com/sv/netty/config/ChannelParam.java new file mode 100644 index 0000000..6f61909 --- /dev/null +++ b/netty-model/src/main/java/com/sv/netty/config/ChannelParam.java @@ -0,0 +1,53 @@ +package com.sv.netty.config; + +/** + * 会话中存储的客户端对象 + * + * @author peakren + * @since 16/05/2017 11:09 PM + */ +public class ChannelParam { + + /** + * 设备ip + */ + private String clientIp; + + /** + * 设备编号 + */ + private String deviceName; + + /** + * 设备所在场馆 + */ + private Integer venueId; + + public ChannelParam(String clientIP) { + this.clientIp = clientIP; + } + + public String getClientIp() { + return clientIp; + } + + public void setClientIp(String clientIp) { + this.clientIp = clientIp; + } + + public String getDeviceName() { + return deviceName; + } + + public void setDeviceName(String deviceName) { + this.deviceName = deviceName; + } + + public Integer getVenueId() { + return venueId; + } + + public void setVenueId(Integer venueId) { + this.venueId = venueId; + } +} diff --git a/netty-model/src/main/java/com/sv/netty/config/NettyConstant.java b/netty-model/src/main/java/com/sv/netty/config/NettyConstant.java new file mode 100644 index 0000000..af1de4a --- /dev/null +++ b/netty-model/src/main/java/com/sv/netty/config/NettyConstant.java @@ -0,0 +1,16 @@ +package com.sv.netty.config; + +import io.netty.util.AttributeKey; + +public interface NettyConstant { + String SERVER_IP = "120.27.209.4"; + Integer SERVER_PORT = 56791; + + /** + * session中存储终端发送的额外参数 + */ + public static AttributeKey CHANNEL_PARAM = AttributeKey.newInstance("CHANNEL_PARAM"); + + public final static String SPIT_WORD = "#"; + +} diff --git a/netty-model/src/main/java/com/sv/netty/utils/EncodeMsg.java b/netty-model/src/main/java/com/sv/netty/utils/EncodeMsg.java new file mode 100644 index 0000000..8bf073b --- /dev/null +++ b/netty-model/src/main/java/com/sv/netty/utils/EncodeMsg.java @@ -0,0 +1,13 @@ +package com.sv.netty.utils; + +public enum EncodeMsg { + + INSTANCE; + + final String MAGIC = "\r\n"; + + public String encode(Object obj) { + return JsonUtils.encode(obj) + MAGIC; + } + +} diff --git a/netty-model/src/main/java/com/sv/netty/utils/JsonUtils.java b/netty-model/src/main/java/com/sv/netty/utils/JsonUtils.java new file mode 100644 index 0000000..6f1a76d --- /dev/null +++ b/netty-model/src/main/java/com/sv/netty/utils/JsonUtils.java @@ -0,0 +1,89 @@ +package com.sv.netty.utils; + +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +public class JsonUtils { + + + /** + * Logger for this class + */ + private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class); + + private final static ObjectMapper objectMapper = new ObjectMapper(); + + static { + objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); + objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); + objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + } + + private JsonUtils() { + } + + public static String encode(Object obj) { + try { + return objectMapper.writeValueAsString(obj); + } catch (JsonGenerationException e) { + logger.error("encode(Object)", e); //$NON-NLS-1$ + } catch (JsonMappingException e) { + logger.error("encode(Object)", e); //$NON-NLS-1$ + } catch (IOException e) { + logger.error("encode(Object)", e); //$NON-NLS-1$ + } + return null; + } + + /** + * 将json string反序列化成对象 + * + * @param json + * @param valueType + * @return + */ + public static T decode(String json, Class valueType) { + try { + return objectMapper.readValue(json, valueType); + } catch (JsonParseException e) { + logger.error("decode(String, Class)", e); + } catch (JsonMappingException e) { + logger.error("decode(String, Class)", e); + } catch (IOException e) { + logger.error("decode(String, Class)", e); + } + return null; + } + + /** + * 将json array反序列化为对象 + * + * @param json + * @param typeReference + * @return + */ + @SuppressWarnings("unchecked") + public static T decode(String json, TypeReference typeReference) { + try { + return (T) objectMapper.readValue(json, typeReference); + } catch (JsonParseException e) { + logger.error("decode(String, JsonTypeReference)", e); + } catch (JsonMappingException e) { + logger.error("decode(String, JsonTypeReference)", e); + } catch (IOException e) { + logger.error("decode(String, JsonTypeReference)", e); + } + return null; + } + +} diff --git a/netty-model/src/main/java/com/sv/netty/utils/ServerMessageUtils.java b/netty-model/src/main/java/com/sv/netty/utils/ServerMessageUtils.java new file mode 100644 index 0000000..c9ddf14 --- /dev/null +++ b/netty-model/src/main/java/com/sv/netty/utils/ServerMessageUtils.java @@ -0,0 +1,13 @@ +package com.sv.netty.utils; + +import com.sv.netty.config.VenueMessage; +import io.netty.channel.Channel; + +public enum ServerMessageUtils { + + INSTANCE; + + public void sendMsg(Channel channel, VenueMessage message) { + channel.writeAndFlush(EncodeMsg.INSTANCE.encode(message)); + } +}