加入Spring依赖,修改用户使用的方法,增加自定义注解

This commit is contained in:
zhangzc
2017-06-17 23:05:47 +08:00
parent c8fe421825
commit 65adf996e3
9 changed files with 139 additions and 73 deletions

View File

@@ -7,6 +7,7 @@
<groupId>cn.94zichao</groupId>
<artifactId>server</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
@@ -15,7 +16,11 @@
<artifactId>netty-all</artifactId>
<version>4.1.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>

View File

@@ -0,0 +1,16 @@
package cn._94zichao.server.annotation;
import org.springframework.stereotype.Component;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ ElementType.TYPE })//注解用在接口<E68EA5>?
@Retention(RetentionPolicy.RUNTIME)//VM将在运行期也保留注释因此可以<E58FAF>?<3F>过反射机制读取注解的信<E79A84>?
@Component
public @interface ZzcService {
String value();
}

View File

@@ -0,0 +1,8 @@
package cn._94zichao.server.bootstrap;
/**
* Created by Administrator on 2017/6/17 0017.
*/
public interface TestService {
public void test(byte[] bytes);
}

View File

@@ -0,0 +1,14 @@
package cn._94zichao.server.bootstrap;
import cn._94zichao.server.annotation.ZzcService;
/**
* Created by Administrator on 2017/6/17 0017.
*/
@ZzcService("TestService")
public class TestServiceImpl implements TestService {
@Override
public void test(byte[] bytes) {
System.out.println("测试");
}
}

View File

@@ -0,0 +1,46 @@
package cn._94zichao.server.bootstrap;
import cn._94zichao.server.annotation.ZzcService;
import cn._94zichao.server.decoder.EndBasedDecoder;
import cn._94zichao.server.encoder.ToModelEncoder;
import cn._94zichao.server.handler.BarrierServerHandler;
import cn._94zichao.server.tcpServer.BaseServer;
import cn._94zichao.server.util.Content;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2017/6/17 0017.
*/
public class ZzcServer implements ApplicationContextAware {
public static Map<Object,Method[]> methodsMap;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String,Object> map =applicationContext.getBeansWithAnnotation(ZzcService.class);
for (Object serviceBean : map.values()) {
try {
//获取自定义注解上的value
String value = serviceBean.getClass().getAnnotation(ZzcService.class).value();
System.out.println("注解上的value: " + value);
//拿到类下面的所有方法
Method[] methods = serviceBean.getClass().getDeclaredMethods();
methodsMap = new HashMap();
methodsMap.put(serviceBean,methods);
//启动服务器
BaseServer.create().port(9999).decoder(new EndBasedDecoder(Content.END,true)).encoder(new ToModelEncoder()).in(new BarrierServerHandler()).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
}
}

View File

@@ -1,8 +1,6 @@
package cn._94zichao.server.decoder;
import cn._94zichao.server.util.ByteUtil;
import cn._94zichao.server.entity.SocketModel;
import cn._94zichao.server.util.Content;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
@@ -16,57 +14,37 @@ import java.util.List;
**/
public class EndBasedDecoder extends ByteToMessageDecoder {
private byte end;
private byte[] start;
private SocketModel sk;
private boolean skip;
public EndBasedDecoder(byte end){
public EndBasedDecoder(byte end,boolean skipEnd){
this.end = end;
this.skip = skipEnd;
}
public EndBasedDecoder(SocketModel sk,byte end,byte... start){
this.end = end;
this.start = start;
this.sk = sk;
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
Object decoded = decode(ctx, in,end);
Object decoded = decode(in,end);
if (decoded != null) {
sk = sk.getModel();
int beforeHead=0;
ByteBuf bf = (ByteBuf) decoded;
for (int i =0;i<start.length;i++){
if ((beforeHead = bf.bytesBefore(start[i])) >= 0){
sk.setHead(start[i]);
}
}
if (sk.getHead() != 0) {
//跳过头前面的字节
bf.skipBytes(beforeHead);
//获取全部字节
byte[] temp = ByteUtil.readAllBytes(bf);
if (temp==null||temp.length<4){
sk.setEnd(Content.END);
out.add(sk);
return;
}
//保存操作码
sk.setType(temp[0]);
//保存数据
sk.setData(ByteUtil.getBytes(temp, 0, temp.length - 2));
//保存CRC
sk.setCrcData(ByteUtil.getBytes(temp, temp.length - 2, 2));
//返回结果
out.add(sk);
}
//获取全部字节
byte[] temp = ByteUtil.readAllBytes(bf);
out.add(temp);
}
}
private Object decode(ChannelHandlerContext ctx, ByteBuf in,byte end) {
private Object decode(ByteBuf in,byte end) {
//如果找到end就把数据传给下个handler
int i = in.bytesBefore(end);
if(i > 0){
final int length = i + 1;
return in.readRetainedSlice(length);
final ByteBuf frame;
if (skip){
final int length = i;
frame = in.readRetainedSlice(length);
in.skipBytes(1);
}else {
final int length = i + 1;
frame = in.readRetainedSlice(length);
}
return frame;
}
return null;
}

View File

@@ -1,12 +1,12 @@
package cn._94zichao.server.handler;
import cn._94zichao.server.util.ByteUtil;
import cn._94zichao.server.entity.SocketModel;
import cn._94zichao.server.util.Content;
import cn._94zichao.server.bootstrap.ZzcServer;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.lang.reflect.Method;
/**
* Created by zzc on 2017/5/16.
@@ -16,39 +16,24 @@ public class BarrierServerHandler extends ChannelInboundHandlerAdapter { // (1)
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
SocketModel sk = (SocketModel)msg;
//记录日志
ctx.writeAndFlush(sk);
//校验CRC
if (!ByteUtil.isCRC(sk.retAllData(), sk.getCrcData())){
//发送错误标识的包
//返回
if (ZzcServer.methodsMap.size() == 0){
}
//通信请求
if (sk.getHead()==Content.REQ) {
if (sk.getType()==Content.UP_LOGIN_U){
//处理设备注册
}else if(sk.getType()==Content.UP_CNT_U){
//处理上报人数
for (Object serviceBean:ZzcServer.methodsMap.keySet()){
Method[] methods = ZzcServer.methodsMap.get(serviceBean);
for (Method method:methods){
try {
method.invoke(serviceBean,msg);
} catch (Exception e){
}
}
}//应答(不回复数据)
else if (sk.getHead()==Content.ACK) {
//解析状态码,更新系统状态
}//回复数据
else if (sk.getHead()==Content.ANS) {
if(sk.getType()==Content.DAT_CNT_R){
//处理上报人数
}else if(sk.getType()==Content.DAT_VER_R){
//处理程序版本
}
}//接收到不合法的命令
else if (sk.getHead()==Content.NAK) {
//解析状态码,更新系统状态
}
}

View File

@@ -1,8 +1,8 @@
package cn._94zichao.server.tcpServer;
import cn._94zichao.server.decoder.EndBasedDecoder;
import cn._94zichao.server.handler.BarrierServerHandler;
import cn._94zichao.server.encoder.ToModelEncoder;
import cn._94zichao.server.handler.BarrierServerHandler;
import cn._94zichao.server.util.Content;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
@@ -170,7 +170,7 @@ public class BaseServer implements Runnable {
}
public static void main(String[] args) {
BaseServer.create().port(9999).decoder(new EndBasedDecoder(Content.END)).encoder(new ToModelEncoder()).in(new BarrierServerHandler()).start();
BaseServer.create().port(9999).decoder(new EndBasedDecoder(Content.END,true)).encoder(new ToModelEncoder()).in(new BarrierServerHandler()).start();
}
}

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn._94zichao.server"/>
<bean id="server" class="cn._94zichao.server.bootstrap.ZzcServer">
</bean>
</beans>