Commit 9eaa8a21 authored by AfirSraftGarrier's avatar AfirSraftGarrier

格式并加些打印

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