Commit 9eaa8a21 authored by AfirSraftGarrier's avatar AfirSraftGarrier

格式并加些打印

parent eb48ade2
......@@ -39,7 +39,7 @@ public class DeviceDecoder extends ByteToMessageDecoder {
return;
}
// buffer.resetReaderIndex();
// buffer.resetReaderIndex();
byte[] content = new byte[real_len - cmd_ack_len];
buffer.readBytes(content);
......
......@@ -4,8 +4,7 @@ import lombok.Data;
@Data
public class DeviceProtocol {
// |STX |LEN |CMD |ACK |DATA |END
// |STX |LEN |CMD |ACK |DATA |END
private short stx;//
private int len;//
......@@ -15,7 +14,7 @@ public class DeviceProtocol {
private byte[] content;// 数据
private byte end;
public DeviceProtocol(short stx, int len, byte cmd, byte ack, byte[] content,byte end) {
public DeviceProtocol(short stx, int len, byte cmd, byte ack, byte[] content, byte end) {
super();
this.stx = stx;
this.len = len;
......@@ -24,5 +23,4 @@ public class DeviceProtocol {
this.content = content;
this.end = end;
}
}
\ No newline at end of file
......@@ -27,7 +27,6 @@ import java.net.InetSocketAddress;
@ChannelHandler.Sharable
@Slf4j
public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
@Autowired
EventPublisher eventPublisher;
@Autowired
......@@ -39,9 +38,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
// TODO Auto-generated method stub
log.info("come read...");
SocketChannel channel = (SocketChannel) ctx.channel();
InetSocketAddress socketAddr = (InetSocketAddress) ctx.channel().remoteAddress();
String remoteIp = socketAddr.getHostString();
......@@ -49,49 +46,43 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
DeviceProtocol protocol = (DeviceProtocol) msg;
String channelId = channel.id().asLongText();
log.debug("设备服务器,channelRead0:{},{}", channelId, HexUtil.bytes2hex(protocol.getContent()));
/*
TODO:
1.透传前先进行鉴权
2.鉴权通过后,创建桥接客户端,非鉴权消息全部放行
3.未鉴权的不能进行透传,强制下线
*/
/* TODO: 1.透传前先进行鉴权 2.鉴权通过后,创建桥接客户端,非鉴权消息全部放行 3.未鉴权的不能进行透传,强制下线 */
byte cmd = protocol.getCmd();
int cmdInt = cmd & 0xFF;
boolean auth = false;
if (cmdInt == Consts.CMD_LICENSE) {
log.info("come auth...");
auth = handlerCheckAuth(channel, remoteIp, remotePort, protocol);
} else {
log.info("no auth...");
auth = handlerCheckAuthStatus(channel, remoteIp, remotePort, protocol);
if (auth) {
handlerForward(channel, remoteIp, remotePort, protocol);
}
}
//TODO 以下为正式代码
// TODO 以下为正式代码
if (auth == false) {
log.info("close...");
channel.close();
}
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
super.channelRegistered(ctx);
log.debug("设备服务器,channelRegistered:{}", ctx.channel().id().asLongText());
}
@Override
public synchronized void channelActive(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
super.channelActive(ctx);
log.debug("设备服务器,channelActive:{}", ctx.channel().id().asLongText());
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
super.channelInactive(ctx);
SocketChannel channel = (SocketChannel) ctx.channel();
String channelId = channel.id().asLongText();
......@@ -105,29 +96,26 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
super.channelReadComplete(ctx);
log.debug("设备服务器,channelReadComplete:{}", ctx.channel().id().asLongText());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception{
// TODO Auto-generated method stub
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
log.debug("设备服务器,exceptionCaught:{}", ctx.channel().id().asLongText());
ctx.close();
}
private boolean handlerCheckAuth(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) {
//TODO 正式代码要放开
// TODO 正式代码要放开
byte[] bytes = safety.decodeExtendedPayload(protocol.getContent(), 0, protocol.getContent().length);
if (bytes == null) {
return false;
}
String decodeInfo = CommonUtil.bytesToStr(bytes);
log.info("byte to string:" + decodeInfo);
if (decodeInfo == null) {
return false;
}
......@@ -176,6 +164,4 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
forwardClientRequestEvent.setProtocol(protocol);
eventPublisher.publishEvent(forwardClientRequestEvent);
}
}
package iot.sixiang.license.event;
import io.netty.channel.socket.SocketChannel;
import iot.sixiang.license.device.DeviceProtocol;
import lombok.Data;
......@@ -9,5 +8,4 @@ public class ForwardClientRequestEvent extends BaseEvent {
private String appId;
private String deviceChannelId;
private DeviceProtocol protocol;
}
\ No newline at end of file
......@@ -6,7 +6,7 @@ import lombok.Data;
@Data
public class ForwardMessageResponseEvent extends BaseEvent {
// private String appId;
// private String appId;
private String channelId;
private SocketChannel channel;
private DeviceProtocol protocol;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment