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());
|
}
|
|
}
|