vince
2023-07-01 7d84b6cbf8268a6993f661e22a842ffa12c13732
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
package com.ld.igds.protocol.bhzn.verb.command;
 
import com.ld.igds.io.constant.ProtocolEnum;
import com.ld.igds.protocol.bhzn.verb.dto.IoMessage;
import com.ld.igds.protocol.bhzn.verb.server.BhznVerbServerEngine;
import com.ld.igds.util.BytesUtil;
import com.ld.igds.util.ContextUtil;
import com.ld.io.api.InvokeResult;
import com.ld.io.api.IoSession;
import com.ld.io.api.IoSessionQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.List;
 
@Slf4j
@Component(BaseRemoteImpl.BEAN_ID)
public class BaseRemoteImpl {
    public static final String BEAN_ID = "bhzn.remoteBaseCommonService";
    public String getProtocol() {
        return ProtocolEnum.TCP_BHZH_VERB_V2.getCode();
    }
 
    @Autowired
    private BhznVerbServerEngine bhznVerbServerEngine;
 
    /**
     * 发送命令
     *
     * @param ip
     * @param port
     * @param msg
     * @return
     */
    public InvokeResult send(String ip, int port, byte[] msg) {
        log.debug("平台推送报文信息--{}", BytesUtil.bytesToString(msg));
        IoSessionQuery sessionFactory = bhznVerbServerEngine.getSessionQuery();
        List<IoSession> sessions = sessionFactory.getAllSession();
        IoSession session = null;
        for (IoSession ioSession : sessions) {
            if (ContextUtil.getServerKey(ip, port).equals(
                    ioSession.getBusinessKey())) {
                session = ioSession;
                break;
            }
        }
        if (null == session) {
            return InvokeResult.CHANNEL_CLOSED;
        }
        return session.invoke(msg);
    }
 
    public InvokeResult send(IoMessage message) {
        log.debug("平台推送报文信息--{}", message.toString());
        IoSessionQuery sessionFactory =  bhznVerbServerEngine.getSessionQuery();
        List<IoSession> sessions = sessionFactory.getAllSession();
        IoSession session = null;
        for (IoSession ioSession : sessions) {
            if (ContextUtil.getServerKey(message.getIp(), message.getPort()).equals(
                    ioSession.getBusinessKey())) {
                session = ioSession;
                break;
            }
        }
        if (null == session) {
            return InvokeResult.CHANNEL_CLOSED;
        }
        return session.invoke(message.getByteMsg());
    }
 
}