AbstractMatrixIoHandler.java
2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.example.mina.client.base;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
/**
* @author dy
*/
@Data
@Slf4j
public abstract class AbstractMatrixIoHandler extends IoHandlerAdapter {
protected MatrixConnectConfig getConnectConfig(IoSession session) {
Object boxConnectConfig = session.getAttribute(MatrixConstants.SESSION_CONFIG_NAME);
if( boxConnectConfig instanceof MatrixConnectConfig) {
return (MatrixConnectConfig) boxConnectConfig;
}
return null;
}
protected MatrixDataProxy getMatrixDataProxy(IoSession session) {
Object matrixDataProxy = session.getAttribute(MatrixConstants.SESSION_DATA_PROXY_NAME);
if( matrixDataProxy instanceof MatrixDataProxy) {
return (MatrixDataProxy) matrixDataProxy;
}
return null;
}
@Override
public void exceptionCaught(IoSession session, Throwable throwable) {
}
@Override
public void messageReceived(IoSession session, Object message) {
if (!(message instanceof MatrixResponse)) {
log.error("客户端接收到的消息不为定义的响应类! message:" + message);
throw new RuntimeException("Unsupported response message");
}
//TODO 客户端将数据服务器的字符串数组读取出来
MatrixResponse response = (MatrixResponse) message;
log.info("the client recieved the device response, device:{}, response is: {}",
getConnectConfig(session), response);
handleCommandResponse(response);
}
@Override
public void messageSent(IoSession session, Object message) {
log.info("=======44444============");
}
@Override
public void inputClosed(IoSession session) {
}
@Override
public void sessionClosed(IoSession session) {
}
@Override
public void sessionCreated(IoSession session) {
}
@Override
public void sessionIdle(IoSession session, IdleStatus status) {
log.info("the session with box was idle, device is {}", getConnectConfig(session));
}
@Override
public void sessionOpened(IoSession session) {
log.info("the system has connected to device, device is {}", getConnectConfig(session));
}
abstract public boolean handleCommandResponse(MatrixResponse response);
}