vince
2023-06-27 5e4cee87dd9bc2629503ab27760365a023a49147
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.ld.igds.protocol.bhzn.grainv1.server;
 
import com.ld.igds.util.BytesUtil;
import com.ld.io.api.*;
import com.ld.io.netty.NettyServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
/**
 * @Desc: 邦海智能纯粮情协议
 * @author: Andy
 * @update-time: 2023/5/11
 */
@Slf4j
@Service
public class BhznGrainV1ServerEngine {
 
    public static final Integer PORT = 9309;
 
    @Autowired
    private BhznGrainV1MessageConsumer defaultMessageConsumer;
    @Autowired
    private BhznGrainV1SessionListener defaultSessionListener;
    // 心跳提供
    private static HeartbeatProvider heartbeatProvider = new BhznGrainV1HeartbeatImpl();
 
    private IoSessionQuery sessionQuery;
 
    public void start(Integer port) {
        IoServerOption ioServerOption = new IoServerOption();
 
        if (null != port) {
            ioServerOption.setPort(port);
        } else {
            ioServerOption.setPort(PORT);
        }
 
        //确认结尾标志
        ioServerOption.setDelimiter(BhznGrainV1ServerUtils.MSG_END.getBytes());
 
        // 配置系统心跳间隔
        ioServerOption.setReaderIdleTime(5 * 60);
 
        NettyServer ioServer = new NettyServer(ioServerOption, defaultMessageConsumer, defaultSessionListener, heartbeatProvider);
        ioServer.startup();
 
        sessionQuery = ioServer.getSessionQuery();
 
 
        log.info("* ");
        log.info("* ========================");
        log.info("* ");
        log.info("* [GRAIN-SERVER:BHZN_GRAIN,PORT={}]", PORT);
        log.info("* ");
        log.info("* ========================");
        log.info("* ");
 
        System.out.println("* ========================");
        System.out.println("* ");
        System.out.println("* [GRAIN-SERVER:BHZN_GRAIN,PORT={}]" + port);
        System.out.println("* ");
        System.out.println("* ========================");
    }
 
    public IoSessionQuery getSessionQuery() {
        return sessionQuery;
    }
 
    /**
     * 发送信息
     *
     * @param ip
     * @param port
     * @param msg
     * @return
     */
    public InvokeResult push(String ip, int port, byte[] msg) {
        IoSessionQuery sessionFactory = getSessionQuery();
        List<IoSession> sessions = sessionFactory.getAllSession();
        IoSession session = null;
        for (IoSession ioSession : sessions) {
            if (BhznGrainV1ServerUtils.getServerKey(ip, port).equals(ioSession.getBusinessKey())) {
                session = ioSession;
                break;
            }
        }
        if (null == session) {
            return InvokeResult.CHANNEL_CLOSED;
        }
        return session.invoke(msg);
    }
 
 
//    public InvokeResult push(String ip, int port, String hexStr) {
//
//        byte[] msg = BytesUtil.hexStrToBytes(hexStr);
//
//        IoSessionQuery sessionFactory = getSessionQuery();
//        List<IoSession> sessions = sessionFactory.getAllSession();
//        IoSession session = null;
//        for (IoSession ioSession : sessions) {
//            if (BhznGrainV1ServerUtils.getServerKey(ip, port).equals(ioSession.getBusinessKey())) {
//                session = ioSession;
//                break;
//            }
//        }
//
//        if (null == session) {
//            return InvokeResult.CHANNEL_CLOSED;
//        }
//        return session.invoke(msg);
//    }
 
    /**
     * 直接使用内存的Session执行
     */
    public InvokeResult pushByMin(String hexStr) {
 
        IoSession session = BhznGrainV1ServerUtils.getSession();
 
        if (null == session) {
            return InvokeResult.CHANNEL_CLOSED;
        }
        byte[] msg = BytesUtil.hexStrToBytes(hexStr);
        return session.invoke(msg);
    }
 
}