Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
ioc_sixiang_license
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zengtianlai3
ioc_sixiang_license
Commits
8b373441
Commit
8b373441
authored
Jun 16, 2022
by
zengtianlai3
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
日志打印格式化和项目代码优化
parent
bce0537f
Changes
28
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
28 changed files
with
364 additions
and
656 deletions
+364
-656
DeviceDecoder.java
...c/main/java/iot/sixiang/license/device/DeviceDecoder.java
+8
-35
DeviceManager.java
...c/main/java/iot/sixiang/license/device/DeviceManager.java
+0
-1
DeviceServerHandler.java
.../java/iot/sixiang/license/device/DeviceServerHandler.java
+9
-15
DeviceClientBeForcedOfflineEventHandler.java
...icense/event/DeviceClientBeForcedOfflineEventHandler.java
+2
-5
DeviceClientInactiveEventHandler.java
...xiang/license/event/DeviceClientInactiveEventHandler.java
+1
-4
DeviceClientLicenseEventHandler.java
...ixiang/license/event/DeviceClientLicenseEventHandler.java
+0
-1
ForwardClientBeForcedOfflineEventHandler.java
...cense/event/ForwardClientBeForcedOfflineEventHandler.java
+3
-4
ForwardClientConnectEventHandler.java
...xiang/license/event/ForwardClientConnectEventHandler.java
+2
-6
ForwardClientInactiveEventHandler.java
...iang/license/event/ForwardClientInactiveEventHandler.java
+3
-4
ForwardClientRequestEventHandler.java
...xiang/license/event/ForwardClientRequestEventHandler.java
+1
-2
ForwardMessageResponseEventHandler.java
...ang/license/event/ForwardMessageResponseEventHandler.java
+4
-7
ForwardClient.java
.../main/java/iot/sixiang/license/forward/ForwardClient.java
+3
-4
ForwardClientHandler.java
...ava/iot/sixiang/license/forward/ForwardClientHandler.java
+11
-38
ForwardConnectionListener.java
...ot/sixiang/license/forward/ForwardConnectionListener.java
+8
-17
ForwardDecoder.java
...main/java/iot/sixiang/license/forward/ForwardDecoder.java
+5
-9
ForwardEncoder.java
...main/java/iot/sixiang/license/forward/ForwardEncoder.java
+2
-8
MySSLContext.java
...e/src/main/java/iot/sixiang/license/net/MySSLContext.java
+0
-66
MyX509TrustManager.java
...main/java/iot/sixiang/license/net/MyX509TrustManager.java
+0
-53
NetMsgSeq.java
license/src/main/java/iot/sixiang/license/net/NetMsgSeq.java
+0
-13
TcpClient.java
license/src/main/java/iot/sixiang/license/net/TcpClient.java
+0
-5
TcpServer.java
license/src/main/java/iot/sixiang/license/net/TcpServer.java
+4
-11
OperateChannelInitializer.java
...ot/sixiang/license/operate/OperateChannelInitializer.java
+0
-3
OperateClientHandler.java
...ava/iot/sixiang/license/operate/OperateClientHandler.java
+9
-33
OperateConnectionListener.java
...ot/sixiang/license/operate/OperateConnectionListener.java
+7
-5
OperateDecoder.java
...main/java/iot/sixiang/license/operate/OperateDecoder.java
+1
-2
OperateEncoder.java
...main/java/iot/sixiang/license/operate/OperateEncoder.java
+1
-1
SpringUtil.java
...se/src/main/java/iot/sixiang/license/util/SpringUtil.java
+20
-34
Util.java
license/src/main/java/iot/sixiang/license/util/Util.java
+260
-270
No files found.
license/src/main/java/iot/sixiang/license/device/DeviceDecoder.java
View file @
8b373441
...
...
@@ -14,55 +14,28 @@ import java.util.List;
*/
@Slf4j
public
class
DeviceDecoder
extends
ByteToMessageDecoder
{
public
final
int
BASE_LENGTH
=
2
+
2
+
1
+
1
+
1
;
public
final
int
BASE_LENGTH
=
2
+
2
+
1
+
1
+
1
;
@Override
protected
void
decode
(
ChannelHandlerContext
ctx
,
ByteBuf
buffer
,
List
<
Object
>
out
)
{
byte
[]
packet
=
new
byte
[
buffer
.
readableBytes
()];
byte
[]
packet
=
new
byte
[
buffer
.
readableBytes
()];
buffer
.
readBytes
(
packet
);
buffer
.
resetReaderIndex
();
Util
.
DEBUG_HEX
(
"SERVER -> IN"
,
packet
,
packet
.
length
);
Util
.
DEBUG_HEX
(
"SERVER -> IN"
,
packet
,
packet
.
length
);
try
{
if
(
buffer
.
readableBytes
()
<
BASE_LENGTH
)
{
return
;
}
buffer
.
markReaderIndex
();
short
stx
=
buffer
.
readShort
();
//55AA->21930
short
stx
=
buffer
.
readShort
();
short
len
=
buffer
.
readShortLE
();
// char cmd = buffer.readChar();
// char ack = buffer.readChar();//stx:21930,len:52,cmd:退,ack:ꭵ
// short cmd = buffer.readUnsignedByte();
// short ack = buffer.readUnsignedByte();//stx:21930,len:52,cmd:144,ack:0
// byte cmd = buffer.readByte();
// byte ack = buffer.readByte();////stx:21930,len:52,cmd:-112,ack:0
byte
cmd
=
buffer
.
readByte
();
byte
ack
=
buffer
.
readByte
();
////stx:21930,len:52,cmd:-112,ack:0
// int cmd2 = cmd & 0xFF;
// int ack2 = ack & 0xFF;//stx:21930,len:52,cmd:144,ack:0
// byte[] cmd_bytes = new byte[1];
// buffer.readBytes(cmd_bytes);
// String cmd = new String(cmd_bytes, 0, cmd_bytes.length);
//
// byte[] ack_bytes = new byte[1];
// buffer.readBytes(ack_bytes);
// String ack = new String(ack_bytes, 0, ack_bytes.length);//: stx:21930,len:52,cmd:�,ack:
byte
ack
=
buffer
.
readByte
();
log
.
debug
(
"stx:"
+
stx
+
",len:"
+
len
+
",cmd:"
+
cmd
+
",ack:"
+
ack
);
//stx:21930,len:52,cmd:-112,ack:0
int
real_len
=
len
;
int
real_len
=
len
;
int
cmd_ack_len
=
2
;
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
+
1
)
{
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
+
1
)
{
buffer
.
resetReaderIndex
();
return
;
}
...
...
@@ -74,7 +47,7 @@ public class DeviceDecoder extends ByteToMessageDecoder {
byte
end
=
buffer
.
readByte
();
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
len
,
cmd
,
ack
,
content
,
end
);
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
len
,
cmd
,
ack
,
content
,
end
);
out
.
add
(
protocol
);
}
catch
(
Exception
e
)
{
...
...
license/src/main/java/iot/sixiang/license/device/DeviceManager.java
View file @
8b373441
...
...
@@ -43,7 +43,6 @@ public class DeviceManager {
}
private
void
startTcpService
()
{
log
.
debug
(
"tcp server start......,prot:"
+
port
);
sessionContexts
=
new
HashMap
<
String
,
SessionContext
>();
channelInitializer
=
new
DeviceChannelInitializer
(
handler
);
server
=
new
TcpServer
(
port
,
channelInitializer
);
...
...
license/src/main/java/iot/sixiang/license/device/DeviceServerHandler.java
View file @
8b373441
...
...
@@ -22,7 +22,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Component
;
import
java.net.InetSocketAddress
;
import
java.util.Date
;
@Component
@ChannelHandler
.
Sharable
...
...
@@ -48,7 +47,8 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
String
remoteIp
=
socketAddr
.
getHostString
();
int
remotePort
=
socketAddr
.
getPort
();
DeviceProtocol
protocol
=
(
DeviceProtocol
)
msg
;
String
channelId
=
channel
.
id
().
asLongText
();
log
.
debug
(
"设备服务器,channelRead0:{},{}"
,
channelId
,
HexUtil
.
bytes2hex
(
protocol
.
getContent
()));
/*
TODO:
1.透传前先进行鉴权
...
...
@@ -57,7 +57,6 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
*/
byte
cmd
=
protocol
.
getCmd
();
int
cmdInt
=
cmd
&
0xFF
;
log
.
debug
(
"recev msg "
+
HexUtil
.
bytes2hex
(
protocol
.
getContent
()));
boolean
auth
=
false
;
if
(
cmdInt
==
Consts
.
CMD_LICENSE
)
{
...
...
@@ -80,8 +79,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
public
void
channelRegistered
(
ChannelHandlerContext
ctx
)
throws
Exception
{
// TODO Auto-generated method stub
super
.
channelRegistered
(
ctx
);
log
.
debug
(
"client channelRegistered"
);
log
.
debug
(
"channel.id().asLongText():"
+
ctx
.
channel
().
id
().
asLongText
());
log
.
debug
(
"设备服务器,channelRegistered:{}"
,
ctx
.
channel
().
id
().
asLongText
());
}
@Override
...
...
@@ -91,8 +89,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
InetSocketAddress
socketAddr
=
(
InetSocketAddress
)
ctx
.
channel
().
remoteAddress
();
String
clientIp
=
socketAddr
.
getHostString
();
int
port
=
socketAddr
.
getPort
();
log
.
debug
(
"client connected,ip:"
+
clientIp
+
",port:"
+
port
);
log
.
debug
(
"channel.id().asLongText():"
+
ctx
.
channel
().
id
().
asLongText
());
log
.
debug
(
"设备服务器,channelActive:{}"
,
ctx
.
channel
().
id
().
asLongText
());
// TODO 以下为模拟的测试代码
...
...
@@ -129,6 +126,8 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
super
.
channelInactive
(
ctx
);
SocketChannel
channel
=
(
SocketChannel
)
ctx
.
channel
();
String
channelId
=
channel
.
id
().
asLongText
();
log
.
debug
(
"设备服务器,channelInactive:{}"
,
channelId
);
DeviceClientInactiveEvent
deviceClientInactiveEvent
=
new
DeviceClientInactiveEvent
();
deviceClientInactiveEvent
.
setChannelId
(
channelId
);
eventPublisher
.
publishEvent
(
deviceClientInactiveEvent
);
...
...
@@ -139,20 +138,16 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
public
void
channelReadComplete
(
ChannelHandlerContext
ctx
)
throws
Exception
{
// TODO Auto-generated method stub
super
.
channelReadComplete
(
ctx
);
log
.
debug
(
"client channelReadComplete"
);
log
.
debug
(
"channel.id().asLongText():"
+
ctx
.
channel
().
id
().
asLongText
());
log
.
debug
(
"设备服务器,channelReadComplete:{}"
,
ctx
.
channel
().
id
().
asLongText
());
}
@Override
public
void
exceptionCaught
(
ChannelHandlerContext
ctx
,
Throwable
cause
)
throws
Exception
{
// TODO Auto-generated method stub
super
.
exceptionCaught
(
ctx
,
cause
);
InetSocketAddress
socketAddr
=
(
InetSocketAddress
)
ctx
.
channel
().
remoteAddress
();
String
clientIp
=
socketAddr
.
getHostString
();
int
port
=
socketAddr
.
getPort
();
log
.
debug
(
"设备服务器,exceptionCaught:{}"
,
ctx
.
channel
().
id
().
asLongText
());
cause
.
printStackTrace
();
ctx
.
close
();
log
.
error
(
"client has error,ip:"
+
clientIp
+
",port:"
+
port
);
}
private
boolean
handlerCheckAuth
(
SocketChannel
channel
,
String
remoteIp
,
int
remotePort
,
DeviceProtocol
protocol
)
{
...
...
@@ -173,7 +168,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
AuthManager
authManager
=
SpringUtil
.
getBean
(
AuthManager
.
class
);
boolean
license
=
authManager
.
auth
(
appId
,
sn
,
sign
);
log
.
debug
(
"auth result "
+
license
+
","
+
appId
+
","
+
sn
+
","
+
sign
);
log
.
info
(
"设备鉴权信息和结果,{},{},{},{} "
,
appId
,
sn
,
sign
,
license
);
String
channelId
=
channel
.
id
().
asLongText
();
if
(
license
)
{
SessionContext
session
=
new
SessionContext
();
...
...
@@ -191,7 +186,6 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
deviceManager
.
putSession
(
appId
,
session
);
//TODO 创建透传的客户端
log
.
info
(
"forward client begin start ..."
+
appId
);
CreateForwarClientEvent
event
=
new
CreateForwarClientEvent
();
event
.
setAppId
(
appId
);
eventPublisher
.
publishEvent
(
event
);
...
...
license/src/main/java/iot/sixiang/license/event/DeviceClientBeForcedOfflineEventHandler.java
View file @
8b373441
...
...
@@ -2,8 +2,6 @@ package iot.sixiang.license.event;
import
io.netty.channel.socket.SocketChannel
;
import
iot.sixiang.license.device.DeviceManager
;
import
iot.sixiang.license.device.DeviceProtocol
;
import
iot.sixiang.license.forward.ForwardManager
;
import
iot.sixiang.license.model.SessionContext
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -20,7 +18,6 @@ public class DeviceClientBeForcedOfflineEventHandler {
}
// @Async("asyncExecutor")
@EventListener
public
void
handlerEvent
(
DeviceClientBeForcedOfflineEvent
event
)
{
String
appId
=
event
.
getAppId
();
...
...
@@ -30,10 +27,10 @@ public class DeviceClientBeForcedOfflineEventHandler {
SocketChannel
deviceClientChannel
=
deviceSessionContext
.
getClientChannel
();
if
(
deviceClientChannel
!=
null
)
{
deviceClientChannel
.
close
();
log
.
info
(
"device client be forced offline success ..."
+
appId
);
log
.
debug
(
"device client be forced offline success ..."
+
appId
);
}
}
else
{
log
.
info
(
"device client be forced offline undo ..."
+
appId
);
log
.
debug
(
"device client be forced offline undo ..."
+
appId
);
}
}
...
...
license/src/main/java/iot/sixiang/license/event/DeviceClientInactiveEventHandler.java
View file @
8b373441
package
iot
.
sixiang
.
license
.
event
;
import
io.netty.channel.socket.SocketChannel
;
import
iot.sixiang.license.device.DeviceManager
;
import
iot.sixiang.license.model.SessionContext
;
import
iot.sixiang.license.util.SpringUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.event.EventListener
;
...
...
@@ -21,14 +19,13 @@ public class DeviceClientInactiveEventHandler {
}
// @Async("asyncExecutor")
@EventListener
public
void
handlerEvent
(
DeviceClientInactiveEvent
event
)
{
String
channelId
=
event
.
getChannelId
();
SessionContext
session
=
deviceManager
.
getSessionByChannelId
(
channelId
);
if
(
session
==
null
)
{
log
.
debug
(
"device client inactive undo ..."
);
log
.
debug
(
"device client inactive undo ..."
);
return
;
}
else
{
String
appId
=
session
.
getAppId
();
...
...
license/src/main/java/iot/sixiang/license/event/DeviceClientLicenseEventHandler.java
View file @
8b373441
...
...
@@ -21,7 +21,6 @@ public class DeviceClientLicenseEventHandler {
}
// @Async("asyncExecutor")
@EventListener
public
void
handlerEvent
(
DeviceClientLicenseEvent
event
)
{
String
appId
=
event
.
getAppId
();
...
...
license/src/main/java/iot/sixiang/license/event/ForwardClientBeForcedOfflineEventHandler.java
View file @
8b373441
...
...
@@ -20,7 +20,6 @@ public class ForwardClientBeForcedOfflineEventHandler {
}
// @Async("asyncExecutor")
@EventListener
public
void
handlerEvent
(
ForwardClientBeForcedOfflineEvent
event
)
{
String
appId
=
event
.
getAppId
();
...
...
@@ -29,10 +28,10 @@ public class ForwardClientBeForcedOfflineEventHandler {
SocketChannel
forwardClientChannel
=
forwardSessionContext
.
getClientChannel
();
if
(
forwardClientChannel
!=
null
)
{
forwardClientChannel
.
close
();
log
.
info
(
"forward client be forced offline success ..."
+
appId
);
log
.
debug
(
"forward client be forced offline success ..."
+
appId
);
}
}
else
{
log
.
info
(
"forward client be forced offline undo ..."
+
appId
);
}
else
{
log
.
debug
(
"forward client be forced offline undo ..."
+
appId
);
}
...
...
license/src/main/java/iot/sixiang/license/event/ForwardClientConnectEventHandler.java
View file @
8b373441
...
...
@@ -3,7 +3,6 @@ package iot.sixiang.license.event;
import
io.netty.channel.socket.SocketChannel
;
import
iot.sixiang.license.forward.ForwardManager
;
import
iot.sixiang.license.model.SessionContext
;
import
iot.sixiang.license.util.SpringUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.event.EventListener
;
...
...
@@ -22,7 +21,6 @@ public class ForwardClientConnectEventHandler {
}
// @Async("asyncExecutor")
@EventListener
public
void
handlerEvent
(
ForwardClientConnectEvent
event
)
{
...
...
@@ -41,11 +39,9 @@ public class ForwardClientConnectEventHandler {
session
.
setClientChannel
(
channel
);
forwardManager
.
putSession
(
appId
,
session
);
forwardManager
.
putSession
(
appId
,
session
);
log
.
debug
(
"forward client connect:"
+
event
);
log
.
debug
(
"forward client connect:"
+
event
);
}
...
...
license/src/main/java/iot/sixiang/license/event/ForwardClientInactiveEventHandler.java
View file @
8b373441
...
...
@@ -23,16 +23,15 @@ public class ForwardClientInactiveEventHandler {
}
// @Async("asyncExecutor")
@EventListener
public
void
handlerEvent
(
ForwardClientInactiveEvent
event
)
{
String
channelId
=
event
.
getChannelId
();
SessionContext
session
=
forwardManager
.
getSessionByChannelId
(
channelId
);
if
(
session
==
null
)
{
log
.
debug
(
"forward client inactive undo ..."
);
log
.
debug
(
"forward client inactive undo ..."
);
return
;
}
else
{
}
else
{
String
appId
=
session
.
getAppId
();
boolean
result
=
forwardManager
.
removeSessionByChannelId
(
channelId
);
if
(
result
)
{
...
...
@@ -40,7 +39,7 @@ public class ForwardClientInactiveEventHandler {
DeviceClientBeForcedOfflineEvent
deviceClientBeForcedOfflineEvent
=
new
DeviceClientBeForcedOfflineEvent
();
deviceClientBeForcedOfflineEvent
.
setAppId
(
appId
);
eventPublisher
.
publishEvent
(
deviceClientBeForcedOfflineEvent
);
log
.
debug
(
"forward client inactive success ..."
);
log
.
debug
(
"forward client inactive success ..."
);
}
}
...
...
license/src/main/java/iot/sixiang/license/event/ForwardClientRequestEventHandler.java
View file @
8b373441
...
...
@@ -25,7 +25,6 @@ public class ForwardClientRequestEventHandler {
}
// @Async("asyncExecutor")
@EventListener
public
void
handlerEvent
(
ForwardClientRequestEvent
event
)
{
String
deviceChannelId
=
event
.
getDeviceChannelId
();
...
...
@@ -35,7 +34,7 @@ public class ForwardClientRequestEventHandler {
String
appId
=
deviceSessionContext
.
getAppId
();
SessionContext
forwardSessionContext
=
forwardManager
.
getSessionContextByAppId
(
appId
);
log
.
info
(
"forward client request:"
+
appId
+
","
+
forwardSessionContext
);
log
.
debug
(
"forward client request:"
+
appId
+
","
+
forwardSessionContext
);
SocketChannel
clientChannel
=
forwardSessionContext
.
getClientChannel
();
clientChannel
.
writeAndFlush
(
protocol
);
...
...
license/src/main/java/iot/sixiang/license/event/ForwardMessageResponseEventHandler.java
View file @
8b373441
...
...
@@ -25,7 +25,6 @@ public class ForwardMessageResponseEventHandler {
}
// @Async("asyncExecutor")
@EventListener
public
void
handlerEvent
(
ForwardMessageResponseEvent
event
)
{
String
channelId
=
event
.
getChannelId
();
...
...
@@ -36,17 +35,15 @@ public class ForwardMessageResponseEventHandler {
String
appId
=
forwardSessionContext
.
getAppId
();
SessionContext
deviceSessionContext
=
deviceManager
.
getSessionContextByAppId
(
appId
);
if
(
deviceSessionContext
!=
null
)
{
if
(
deviceSessionContext
!=
null
)
{
SocketChannel
deviceClientChannel
=
deviceSessionContext
.
getClientChannel
();
log
.
info
(
"forward client response..."
+
appId
+
",forward session:"
+
deviceSessionContext
);
log
.
debug
(
"forward client response..."
+
appId
+
",forward session:"
+
deviceSessionContext
);
deviceClientChannel
.
writeAndFlush
(
protocol
);
}
else
{
log
.
info
(
"forward client response undo ..."
+
appId
);
}
else
{
log
.
debug
(
"forward client response undo ..."
+
appId
);
}
}
...
...
license/src/main/java/iot/sixiang/license/forward/ForwardClient.java
View file @
8b373441
...
...
@@ -17,12 +17,11 @@ public class ForwardClient {
ForwardClientHandler
handler
;
public
ForwardClient
()
{
log
.
debug
(
"ProxyNodeClient ProxyNodeClientHandler*****"
+
handler
);
}
public
void
startTcp
(
String
host
,
int
port
,
String
appId
)
{
log
.
debug
(
"begin connect the server "
+
host
+
",port "
+
port
);
}
public
void
startTcp
(
String
host
,
int
port
,
String
appId
)
{
log
.
debug
(
"桥接客户端,开始连接桥接服务:{},{},{}"
,
host
,
port
,
appId
);
ForwardConnectionListener
listener
=
new
ForwardConnectionListener
();
listener
.
setAppId
(
appId
);
listener
.
setHost
(
host
);
...
...
license/src/main/java/iot/sixiang/license/forward/ForwardClientHandler.java
View file @
8b373441
...
...
@@ -4,15 +4,11 @@ import io.netty.channel.ChannelHandler;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.SimpleChannelInboundHandler
;
import
io.netty.channel.socket.SocketChannel
;
import
iot.sixiang.license.device.DeviceManager
;
import
iot.sixiang.license.device.DeviceProtocol
;
import
iot.sixiang.license.event.DeviceClientInactiveEvent
;
import
iot.sixiang.license.event.EventPublisher
;
import
iot.sixiang.license.event.ForwardClientInactiveEvent
;
import
iot.sixiang.license.event.ForwardMessageResponseEvent
;
import
iot.sixiang.license.model.SessionContext
;
import
iot.sixiang.license.util.HexUtil
;
import
iot.sixiang.license.util.SpringUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
...
...
@@ -29,7 +25,6 @@ public class ForwardClientHandler extends SimpleChannelInboundHandler<Object> {
public
ForwardClientHandler
()
{
super
();
log
.
debug
(
"ProxyNodeClientHandler ****"
);
}
@Override
...
...
@@ -41,11 +36,9 @@ public class ForwardClientHandler extends SimpleChannelInboundHandler<Object> {
InetSocketAddress
socketAddr
=
(
InetSocketAddress
)
ctx
.
channel
().
remoteAddress
();
String
serverIp
=
socketAddr
.
getHostString
();
int
serverPort
=
socketAddr
.
getPort
();
log
.
debug
(
"channelRead0..."
);
DeviceProtocol
protocol
=
(
DeviceProtocol
)
msg
;
log
.
info
(
"桥接服务器响应:"
+
HexUtil
.
bytes2hex
(
protocol
.
getContent
()));
String
channelId
=
channel
.
id
().
asLongText
();
log
.
debug
(
"桥接客户端,channelRead0:{},{}"
,
channelId
,
HexUtil
.
bytes2hex
(
protocol
.
getContent
()));
ForwardMessageResponseEvent
forwardMessageResponseEvent
=
new
ForwardMessageResponseEvent
();
forwardMessageResponseEvent
.
setChannelId
(
channelId
);
...
...
@@ -57,30 +50,17 @@ public class ForwardClientHandler extends SimpleChannelInboundHandler<Object> {
@Override
public
void
channelRegistered
(
ChannelHandlerContext
ctx
)
{
public
void
channelRegistered
(
ChannelHandlerContext
ctx
)
throws
Exception
{
// TODO Auto-generated method stub
try
{
super
.
channelRegistered
(
ctx
);
log
.
debug
(
"channelRegistered----------->"
);
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
log
.
debug
(
"桥接客户端,channelRegistered:{}"
,
ctx
.
channel
().
id
().
asLongText
());
}
@Override
public
synchronized
void
channelActive
(
ChannelHandlerContext
ctx
)
throws
Exception
{
// TODO Auto-generated method stub
super
.
channelActive
(
ctx
);
try
{
SocketChannel
channel
=
(
SocketChannel
)
ctx
.
channel
();
log
.
debug
(
"channelActive********"
+
ctx
.
channel
().
id
().
asLongText
());
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
log
.
debug
(
"桥接客户端,channelActive:{}"
,
ctx
.
channel
().
id
().
asLongText
());
}
@Override
...
...
@@ -89,6 +69,8 @@ public class ForwardClientHandler extends SimpleChannelInboundHandler<Object> {
super
.
channelInactive
(
ctx
);
SocketChannel
channel
=
(
SocketChannel
)
ctx
.
channel
();
String
channelId
=
channel
.
id
().
asLongText
();
log
.
debug
(
"桥接客户端,channelInactive:{}"
,
channelId
);
ForwardClientInactiveEvent
forwardClientInactiveEvent
=
new
ForwardClientInactiveEvent
();
forwardClientInactiveEvent
.
setChannelId
(
channelId
);
eventPublisher
.
publishEvent
(
forwardClientInactiveEvent
);
...
...
@@ -99,31 +81,22 @@ public class ForwardClientHandler extends SimpleChannelInboundHandler<Object> {
public
void
channelReadComplete
(
ChannelHandlerContext
ctx
)
throws
Exception
{
// TODO Auto-generated method stub
super
.
channelReadComplete
(
ctx
);
log
.
debug
(
"
channelReadComplete"
);
log
.
debug
(
"
桥接客户端,channelReadComplete:{}"
,
ctx
.
channel
().
id
().
asLongText
()
);
}
@Override
public
void
exceptionCaught
(
ChannelHandlerContext
ctx
,
Throwable
cause
)
throws
Exception
{
// TODO Auto-generated method stub
super
.
exceptionCaught
(
ctx
,
cause
);
InetSocketAddress
socketAddr
=
(
InetSocketAddress
)
ctx
.
channel
().
remoteAddress
();
String
clientIp
=
socketAddr
.
getHostString
();
int
port
=
socketAddr
.
getPort
();
log
.
debug
(
"桥接客户端,exceptionCaught:{}"
,
ctx
.
channel
().
id
().
asLongText
());
cause
.
printStackTrace
();
ctx
.
close
();
log
.
error
(
"client has error,ip:"
+
clientIp
+
",port:"
+
port
);
}
@Override
public
synchronized
void
userEventTriggered
(
ChannelHandlerContext
ctx
,
Object
obj
)
throws
Exception
{
try
{
SocketChannel
channel
=
(
SocketChannel
)
ctx
.
channel
();
log
.
debug
(
"userEventTriggered"
);
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
log
.
debug
(
"桥接客户端,userEventTriggered:{}"
,
ctx
.
channel
().
id
().
asLongText
());
}
}
license/src/main/java/iot/sixiang/license/forward/ForwardConnectionListener.java
View file @
8b373441
...
...
@@ -21,13 +21,12 @@ public class ForwardConnectionListener extends BaseConnectionListener {
AlarmService
alarmService
=
SpringUtil
.
getBean
(
AlarmService
.
class
);
if
(!
channelFuture
.
isSuccess
())
{
//TODO 失败进行告警
log
.
debug
(
"桥接客户端,连接服务器失败:{},{},{}"
,
this
.
host
,
this
.
port
,
this
.
appId
);
String
remoteIp
=
this
.
host
;
String
remotePort
=
String
.
valueOf
(
this
.
port
);
int
typeId
=
1
;
String
title
=
"连接服器失败"
;
String
content
=
"连接服务器:"
+
remoteIp
+
":"
+
remotePort
+
"失败"
;
alarmService
.
addAlarm
(
typeId
,
title
,
content
);
String
content
=
"连接服务器:"
+
this
.
host
+
":"
+
this
.
port
+
"失败"
;
alarmService
.
addAlarm
(
typeId
,
title
,
content
);
//TODO forward client连接失败,则强制踢掉设备客户端
...
...
@@ -36,7 +35,7 @@ public class ForwardConnectionListener extends BaseConnectionListener {
EventPublisher
eventPublisher
=
SpringUtil
.
getBean
(
EventPublisher
.
class
);
eventPublisher
.
publishEvent
(
deviceClientBeForcedOfflineEvent
);
}
else
{
log
.
debug
(
"桥接客户端,连接服务器成功:{},{},{}"
,
this
.
host
,
this
.
port
,
this
.
appId
);
OperateManager
operateManager
=
SpringUtil
.
getBean
(
OperateManager
.
class
);
operateManager
.
autoIncrement
();
SocketChannel
channel
=
(
SocketChannel
)
channelFuture
.
channel
();
...
...
@@ -50,25 +49,17 @@ public class ForwardConnectionListener extends BaseConnectionListener {
EventPublisher
eventPublisher
=
SpringUtil
.
getBean
(
EventPublisher
.
class
);
eventPublisher
.
publishEvent
(
forwardClientConnectEvent
);
log
.
debug
(
"Successful connection to the server"
);
log
.
info
(
"forward client connect success ..."
+
appId
);
// byte ack2 = 0x01;
short
stx
=
21930
;
byte
ack
=
0x0
;
int
len
=
3
;
byte
cmd
=
0x1
;
byte
[]
content
=
new
byte
[
1
];
content
[
0
]
=
0x7e
;
byte
end
=
0x1
;
// stx:21930,len:76,cmd:1,ack:0
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
len
,
cmd
,
ack
,
content
,
end
);
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
len
,
cmd
,
ack
,
content
,
end
);
DeviceClientLicenseEvent
deviceClientLicenseEvent
=
new
DeviceClientLicenseEvent
();
...
...
license/src/main/java/iot/sixiang/license/forward/ForwardDecoder.java
View file @
8b373441
...
...
@@ -18,10 +18,10 @@ public class ForwardDecoder extends ByteToMessageDecoder {
protected
void
decode
(
ChannelHandlerContext
ctx
,
ByteBuf
buffer
,
List
<
Object
>
out
)
{
// 可读长度必须大于等于基本长度
try
{
byte
[]
packet
=
new
byte
[
buffer
.
readableBytes
()];
byte
[]
packet
=
new
byte
[
buffer
.
readableBytes
()];
buffer
.
readBytes
(
packet
);
buffer
.
resetReaderIndex
();
Util
.
DEBUG_HEX
(
"SERVER -> IN"
,
packet
,
packet
.
length
);
Util
.
DEBUG_HEX
(
"SERVER -> IN"
,
packet
,
packet
.
length
);
if
(
buffer
.
readableBytes
()
<
BASE_LENGTH
)
{
return
;
...
...
@@ -36,13 +36,10 @@ public class ForwardDecoder extends ByteToMessageDecoder {
byte
cmd
=
buffer
.
readByte
();
byte
ack
=
buffer
.
readByte
();
////stx:21930,len:52,cmd:-112,ack:0
log
.
debug
(
"stx:"
+
stx
+
",len:"
+
len
);
int
real_len
=
len
;
//注意,透传前已经去掉了END一个字符
int
cmd_ack_len
=
2
;
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
+
1
)
{
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
+
1
)
{
buffer
.
resetReaderIndex
();
return
;
}
...
...
@@ -55,14 +52,13 @@ public class ForwardDecoder extends ByteToMessageDecoder {
byte
end
=
buffer
.
readByte
();
log
.
debug
(
"ForwardDecoder.msg.getLen()...."
+
real_len
);
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
real_len
,
cmd
,
ack
,
content
,
end
);
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
real_len
,
cmd
,
ack
,
content
,
end
);
out
.
add
(
protocol
);
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
log
.
error
(
"De
viceDecoder error!
"
);
log
.
error
(
"De
coder error
"
);
}
}
...
...
license/src/main/java/iot/sixiang/license/forward/ForwardEncoder.java
View file @
8b373441
...
...
@@ -13,19 +13,13 @@ public class ForwardEncoder extends MessageToByteEncoder<DeviceProtocol> {
protected
void
encode
(
ChannelHandlerContext
tcx
,
DeviceProtocol
msg
,
ByteBuf
out
)
throws
Exception
{
try
{
// short stx = in.readShort();//55AA->21930
// short len = in.readShortLE();
out
.
writeShort
(
msg
.
getStx
());
// out.writeShort(msg.getLen());
out
.
writeShortLE
(
msg
.
getLen
());
out
.
writeByte
(
msg
.
getCmd
());
out
.
writeByte
(
msg
.
getAck
());
log
.
debug
(
"ForwardEncoder.msg.getLen()...."
+
msg
.
getLen
());
if
(
msg
.
getContent
()
==
null
)
{
// log
ger
.debug("body数据为空");
// log.debug("body数据为空");
}
else
{
out
.
writeBytes
(
msg
.
getContent
());
}
...
...
@@ -33,7 +27,7 @@ public class ForwardEncoder extends MessageToByteEncoder<DeviceProtocol> {
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
log
.
error
(
"
DeviceEncoder error---->
"
);
log
.
error
(
"
Encoder error
"
);
}
}
}
license/src/main/java/iot/sixiang/license/net/MySSLContext.java
deleted
100644 → 0
View file @
bce0537f
package
iot
.
sixiang
.
license
.
net
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.util.ClassUtils
;
import
javax.net.ssl.KeyManagerFactory
;
import
javax.net.ssl.SSLContext
;
import
javax.net.ssl.TrustManagerFactory
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.security.*
;
import
java.security.cert.CertificateException
;
public
class
MySSLContext
{
static
Logger
logger
=
LoggerFactory
.
getLogger
(
MySSLContext
.
class
.
getName
());
private
static
SSLContext
sslContext
;
public
static
SSLContext
getSSLContext
(
String
serverKeystorePath
,
String
serverKeystorePwd
,
String
tserverKeystorePath
,
String
serverTrustKeystorePwd
)
{
try
{
KeyStore
keyStore
=
KeyStore
.
getInstance
(
"JKS"
);
KeyStore
trustKeyStore
=
KeyStore
.
getInstance
(
"JKS"
);
try
{
keyStore
.
load
(
ClassUtils
.
getDefaultClassLoader
().
getResourceAsStream
(
"static/kserver.keystore"
),
serverKeystorePwd
.
toCharArray
());
trustKeyStore
.
load
(
ClassUtils
.
getDefaultClassLoader
().
getResourceAsStream
(
"static/tserver.keystore"
),
serverTrustKeystorePwd
.
toCharArray
());
/*
* keyStore.load(new FileInputStream(serverKeystorePath),
* serverKeystorePwd.toCharArray()); trustKeyStore.load(new
* FileInputStream(tserverKeystorePath),
* serverTrustKeystorePwd.toCharArray());
*/
}
catch
(
CertificateException
e
)
{
e
.
printStackTrace
();
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
KeyManagerFactory
keyManagerFactory
=
KeyManagerFactory
.
getInstance
(
"SunX509"
);
TrustManagerFactory
trustManagerFactory
=
TrustManagerFactory
.
getInstance
(
"SunX509"
);
try
{
keyManagerFactory
.
init
(
keyStore
,
serverKeystorePwd
.
toCharArray
());
}
catch
(
UnrecoverableKeyException
e
)
{
e
.
printStackTrace
();
}
trustManagerFactory
.
init
(
trustKeyStore
);
sslContext
=
SSLContext
.
getInstance
(
"TLS"
);
try
{
sslContext
.
init
(
keyManagerFactory
.
getKeyManagers
(),
trustManagerFactory
.
getTrustManagers
(),
null
);
// sslContext.init(keyManagerFactory.getKeyManagers(),null,
// null);
logger
.
debug
(
"SSLContext init success"
);
}
catch
(
KeyManagementException
e
)
{
e
.
printStackTrace
();
}
}
catch
(
NoSuchAlgorithmException
e
)
{
e
.
printStackTrace
();
}
catch
(
KeyStoreException
e
)
{
e
.
printStackTrace
();
}
return
sslContext
;
}
}
license/src/main/java/iot/sixiang/license/net/MyX509TrustManager.java
deleted
100644 → 0
View file @
bce0537f
package
iot
.
sixiang
.
license
.
net
;
import
javax.net.ssl.SSLEngine
;
import
javax.net.ssl.X509ExtendedTrustManager
;
import
java.net.Socket
;
import
java.security.cert.CertificateException
;
import
java.security.cert.X509Certificate
;
public
class
MyX509TrustManager
extends
X509ExtendedTrustManager
{
@Override
public
void
checkClientTrusted
(
X509Certificate
[]
arg0
,
String
arg1
)
throws
CertificateException
{
// TODO Auto-generated method stub
}
@Override
public
void
checkServerTrusted
(
X509Certificate
[]
arg0
,
String
arg1
)
throws
CertificateException
{
// TODO Auto-generated method stub
}
@Override
public
X509Certificate
[]
getAcceptedIssuers
()
{
// TODO Auto-generated method stub
return
null
;
}
@Override
public
void
checkClientTrusted
(
X509Certificate
[]
arg0
,
String
arg1
,
Socket
arg2
)
throws
CertificateException
{
// TODO Auto-generated method stub
}
@Override
public
void
checkClientTrusted
(
X509Certificate
[]
arg0
,
String
arg1
,
SSLEngine
arg2
)
throws
CertificateException
{
// TODO Auto-generated method stub
}
@Override
public
void
checkServerTrusted
(
X509Certificate
[]
arg0
,
String
arg1
,
Socket
arg2
)
throws
CertificateException
{
// TODO Auto-generated method stub
}
@Override
public
void
checkServerTrusted
(
X509Certificate
[]
arg0
,
String
arg1
,
SSLEngine
arg2
)
throws
CertificateException
{
// TODO Auto-generated method stub
}
}
license/src/main/java/iot/sixiang/license/net/NetMsgSeq.java
deleted
100644 → 0
View file @
bce0537f
package
iot
.
sixiang
.
license
.
net
;
public
class
NetMsgSeq
{
private
static
short
seq
=
1
;
public
static
synchronized
short
getSeq
()
{
if
(
seq
>
65535
)
{
seq
=
1
;
}
seq
++;
return
seq
;
}
}
license/src/main/java/iot/sixiang/license/net/TcpClient.java
View file @
8b373441
...
...
@@ -6,16 +6,11 @@ import io.netty.channel.ChannelOption;
import
io.netty.channel.EventLoopGroup
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.nio.NioSocketChannel
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
//import com.xiezuo.rsretail.node.ConnectionListener;
public
class
TcpClient
{
static
Logger
logger
=
LoggerFactory
.
getLogger
(
TcpClient
.
class
);
private
int
port
;
private
String
host
;
private
int
index
;
private
BaseChannelInitializer
channelInitializer
;
private
BaseConnectionListener
connectionListener
;
...
...
license/src/main/java/iot/sixiang/license/net/TcpServer.java
View file @
8b373441
...
...
@@ -2,24 +2,17 @@ package iot.sixiang.license.net;
import
io.netty.bootstrap.ServerBootstrap
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelId
;
import
io.netty.channel.ChannelOption
;
import
io.netty.channel.EventLoopGroup
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.SocketChannel
;
import
io.netty.channel.socket.nio.NioServerSocketChannel
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
lombok.extern.slf4j.Slf4j
;
@Slf4j
public
class
TcpServer
{
static
Logger
logger
=
LoggerFactory
.
getLogger
(
TcpServer
.
class
.
getName
());
private
int
port
;
private
BaseChannelInitializer
channelInitializer
;
public
static
Map
<
ChannelId
,
SocketChannel
>
clients
=
new
LinkedHashMap
<
ChannelId
,
SocketChannel
>();
public
TcpServer
(
int
port
,
BaseChannelInitializer
channelInitializer
)
{
this
.
port
=
port
;
...
...
@@ -33,7 +26,7 @@ public class TcpServer {
//创建两个线程组 bossGroup、workerGroup
EventLoopGroup
bossGroup
=
new
NioEventLoopGroup
(
4
);
EventLoopGroup
workerGroup
=
new
NioEventLoopGroup
(
4
);
log
ger
.
debug
(
"begin open the port:"
+
port
);
log
.
debug
(
"Tcp服务,开始监听端口:{}"
,
port
);
try
{
//创建服务端的启动对象,设置参数
ServerBootstrap
b
=
new
ServerBootstrap
();
...
...
@@ -57,7 +50,7 @@ public class TcpServer {
}
}
finally
{
log
ger
.
debug
(
"close the server
"
);
log
.
debug
(
"Tcp服务,停止退出
"
);
workerGroup
.
shutdownGracefully
();
bossGroup
.
shutdownGracefully
();
}
...
...
license/src/main/java/iot/sixiang/license/operate/OperateChannelInitializer.java
View file @
8b373441
...
...
@@ -4,9 +4,6 @@ import io.netty.channel.socket.SocketChannel;
import
io.netty.util.concurrent.DefaultEventExecutorGroup
;
import
io.netty.util.concurrent.EventExecutorGroup
;
import
iot.sixiang.license.consts.Consts
;
import
iot.sixiang.license.forward.ForwardClientHandler
;
import
iot.sixiang.license.forward.ForwardDecoder
;
import
iot.sixiang.license.forward.ForwardEncoder
;
import
iot.sixiang.license.net.BaseChannelInitializer
;
public
class
OperateChannelInitializer
extends
BaseChannelInitializer
{
...
...
license/src/main/java/iot/sixiang/license/operate/OperateClientHandler.java
View file @
8b373441
...
...
@@ -3,7 +3,6 @@ package iot.sixiang.license.operate;
import
io.netty.channel.ChannelHandler
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.SimpleChannelInboundHandler
;
import
io.netty.channel.socket.SocketChannel
;
import
iot.sixiang.license.device.DeviceProtocol
;
import
iot.sixiang.license.event.EventPublisher
;
import
iot.sixiang.license.event.OperateSAMStatusResponseEvent
;
...
...
@@ -39,38 +38,24 @@ public class OperateClientHandler extends SimpleChannelInboundHandler<Object> {
@Override
public
void
channelRegistered
(
ChannelHandlerContext
ctx
)
{
public
void
channelRegistered
(
ChannelHandlerContext
ctx
)
throws
Exception
{
// TODO Auto-generated method stub
try
{
super
.
channelRegistered
(
ctx
);
SocketChannel
channel
=
(
SocketChannel
)
ctx
.
channel
();
log
.
debug
(
"channelRegistered "
+
ctx
.
channel
().
id
().
asLongText
());
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
log
.
debug
(
"运维客户端,channelRegistered:{}"
,
ctx
.
channel
().
id
().
asLongText
());
}
@Override
public
synchronized
void
channelActive
(
ChannelHandlerContext
ctx
)
throws
Exception
{
// TODO Auto-generated method stub
super
.
channelActive
(
ctx
);
try
{
SocketChannel
channel
=
(
SocketChannel
)
ctx
.
channel
();
log
.
debug
(
"channelActive "
+
ctx
.
channel
().
id
().
asLongText
());
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
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
();
log
.
debug
(
"channelInactive "
+
ctx
.
channel
().
id
().
asLongText
());
log
.
debug
(
"运维客户端,channelInactive:{}"
,
ctx
.
channel
().
id
().
asLongText
());
ctx
.
close
();
}
...
...
@@ -78,30 +63,21 @@ public class OperateClientHandler extends SimpleChannelInboundHandler<Object> {
public
void
channelReadComplete
(
ChannelHandlerContext
ctx
)
throws
Exception
{
// TODO Auto-generated method stub
super
.
channelReadComplete
(
ctx
);
SocketChannel
channel
=
(
SocketChannel
)
ctx
.
channel
();
log
.
debug
(
"channelReadComplete "
+
ctx
.
channel
().
id
().
asLongText
());
log
.
debug
(
"运维客户端,channelReadComplete:{}"
,
ctx
.
channel
().
id
().
asLongText
());
}
@Override
public
void
exceptionCaught
(
ChannelHandlerContext
ctx
,
Throwable
cause
)
throws
Exception
{
// TODO Auto-generated method stub
super
.
exceptionCaught
(
ctx
,
cause
);
SocketChannel
channel
=
(
SocketChannel
)
ctx
.
channel
();
log
.
debug
(
"exceptionCaught "
+
ctx
.
channel
().
id
().
asLongText
());
log
.
debug
(
"运维客户端,exceptionCaught:{}"
,
ctx
.
channel
().
id
().
asLongText
());
cause
.
printStackTrace
();
ctx
.
close
();
}
@Override
public
synchronized
void
userEventTriggered
(
ChannelHandlerContext
ctx
,
Object
obj
)
throws
Exception
{
try
{
SocketChannel
channel
=
(
SocketChannel
)
ctx
.
channel
();
log
.
debug
(
"userEventTriggered"
);
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
public
synchronized
void
userEventTriggered
(
ChannelHandlerContext
ctx
,
Object
obj
)
{
log
.
debug
(
"运维客户端,userEventTriggered:{}"
,
ctx
.
channel
().
id
().
asLongText
());
}
}
license/src/main/java/iot/sixiang/license/operate/OperateConnectionListener.java
View file @
8b373441
...
...
@@ -14,17 +14,19 @@ import lombok.extern.slf4j.Slf4j;
public
class
OperateConnectionListener
extends
BaseConnectionListener
{
@Override
public
void
operationComplete
(
ChannelFuture
channelFuture
)
throws
Exception
{
public
void
operationComplete
(
ChannelFuture
channelFuture
)
{
if
(!
channelFuture
.
isSuccess
())
{
//TODO 失败进行告警
log
.
info
(
"运维客户端,连接服务器失败:{},{}"
,
this
.
host
,
this
.
port
);
AlarmService
alarmService
=
SpringUtil
.
getBean
(
AlarmService
.
class
);
String
remoteIp
=
this
.
host
;
String
remotePort
=
String
.
valueOf
(
this
.
port
);
int
typeId
=
1
;
String
title
=
"连接服器失败"
;
String
content
=
"连接服务器:"
+
remoteIp
+
":"
+
remotePort
+
"失败"
;
alarmService
.
addAlarm
(
typeId
,
title
,
content
);
String
content
=
"连接服务器:"
+
this
.
host
+
":"
+
this
.
port
+
"失败"
;
alarmService
.
addAlarm
(
typeId
,
title
,
content
);
}
else
{
log
.
info
(
"运维客户端,连接服务器成功:{},{}"
,
this
.
host
,
this
.
port
);
//TODO 查询SAM状态
OperateSAMStatusRequestEvent
operateSAMStatusQueryEvent
=
new
OperateSAMStatusRequestEvent
();
short
stx
=
21930
;
...
...
license/src/main/java/iot/sixiang/license/operate/OperateDecoder.java
View file @
8b373441
...
...
@@ -4,7 +4,6 @@ import io.netty.buffer.ByteBuf;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.handler.codec.ByteToMessageDecoder
;
import
iot.sixiang.license.device.DeviceProtocol
;
import
iot.sixiang.license.util.Util
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.List
;
...
...
@@ -53,7 +52,7 @@ public class OperateDecoder extends ByteToMessageDecoder {
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
log
.
error
(
e
.
getMessage
()
);
log
.
error
(
"Decoder error"
);
}
}
...
...
license/src/main/java/iot/sixiang/license/operate/OperateEncoder.java
View file @
8b373441
...
...
@@ -28,7 +28,7 @@ public class OperateEncoder extends MessageToByteEncoder<DeviceProtocol> {
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
log
.
error
(
e
.
getMessage
()
);
log
.
error
(
"Encoder error"
);
}
}
}
license/src/main/java/iot/sixiang/license/util/SpringUtil.java
View file @
8b373441
package
iot
.
sixiang
.
license
.
util
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.BeansException
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContextAware
;
...
...
@@ -9,7 +7,6 @@ import org.springframework.stereotype.Component;
@Component
public
class
SpringUtil
implements
ApplicationContextAware
{
static
Logger
logger
=
LoggerFactory
.
getLogger
(
SpringUtil
.
class
.
getName
());
private
static
ApplicationContext
applicationContext
;
@Override
...
...
@@ -17,9 +14,6 @@ public class SpringUtil implements ApplicationContextAware {
if
(
SpringUtil
.
applicationContext
==
null
)
{
SpringUtil
.
applicationContext
=
applicationContext
;
}
logger
.
info
(
"========ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext="
+
SpringUtil
.
applicationContext
+
"========"
);
}
// 获取applicationContext
...
...
@@ -27,19 +21,11 @@ public class SpringUtil implements ApplicationContextAware {
return
applicationContext
;
}
// // 通过name获取 Bean.
// public static Object getBean(String name) {
// return getApplicationContext().getBean(name);
// }
//
// 通过class获取Bean.
public
static
<
T
>
T
getBean
(
Class
<
T
>
clazz
)
{
return
getApplicationContext
().
getBean
(
clazz
);
}
//
// // 通过name,以及Clazz返回指定的Bean
// public static <T> T getBean(String name, Class<T> clazz) {
// return getApplicationContext().getBean(name, clazz);
// }
}
\ No newline at end of file
license/src/main/java/iot/sixiang/license/util/Util.java
View file @
8b373441
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment