use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class PushCallback method onPush.
@Override
public void onPush(ChannelPush push) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
logger.debug("收到PUSH消息:" + push.getContent());
System.out.println(df.format(LocalDateTime.now()) + "server:收到PUSH消息:" + push.getContent());
logger.debug("获取块高");
ChannelResponse response = new ChannelResponse();
response.setContent("receive request seq:" + String.valueOf(push.getMessageID()));
response.setErrorCode(0);
push.sendResponse(response);
}
use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class PushCallback2 method onPush.
@Override
public void onPush(ChannelPush push) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
logger.debug("收到PUSH消息:" + push.getContent());
System.out.println(df.format(LocalDateTime.now()) + "server:收到PUSH消息:" + push.getContent());
logger.debug("获取块高");
ChannelResponse response = new ChannelResponse();
response.setContent("receive request seq:" + String.valueOf(push.getMessageID()));
response.setErrorCode(0);
push.sendResponse(response);
}
use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class ChannelResponseCallback method retrySendMessage.
public void retrySendMessage(Integer errorType) {
Integer errorCode = 0;
try {
if (errorType == 1 || errorType == 0) {
message.setFromNode("");
// 选取客户端节点
logger.debug("本地节点数:{}", fromConnectionInfos.size());
if (fromConnectionInfos.size() > 0) {
Random random = new Random();
Integer index = random.nextInt(fromConnectionInfos.size());
logger.debug("选取:{}", index);
setFromConnection(fromConnectionInfos.get(index));
message.setFromNode(getFromConnection().getNodeID());
Boolean res = fromConnectionInfos.remove(fromConnectionInfos.get(index));
logger.debug("处理后本地节点数:{} {}", res, fromConnectionInfos.size());
}
if (message.getFromNode().isEmpty()) {
// 所有节点已尝试,无法再重试了
logger.error("所有本地区块链节点均不可用");
errorCode = 99;
throw new Exception("所有本地区块链节点均不可用");
}
}
if (errorType == 2 || errorType == 0) {
message.setToNode("");
// 选取服务端节点
logger.debug("对端节点数:{}", toConnectionInfos.size());
if (toConnectionInfos.size() > 0) {
Random random = new Random();
Integer index = random.nextInt(toConnectionInfos.size());
logger.debug("选取:{}", index);
setToConnection(toConnectionInfos.get(index));
message.setToNode(getToConnection().getNodeID());
Boolean res = toConnectionInfos.remove(toConnectionInfos.get(index));
logger.debug("处理后对端节点数:{} {}", res, toConnectionInfos.size());
}
if (message.getToNode().isEmpty()) {
// 所有节点已尝试,无法再重试了
logger.error("所有对端区块链节点均不可用");
errorCode = 103;
throw new Exception("所有对端区块链节点均不可用");
}
}
logger.debug("尝试从{} 发送到:{}", message.getFromNode(), message.getToNode());
message.setFromNode(fromConnection.getNodeID());
ChannelHandlerContext ctx = fromChannelConnections.getNetworkConnectionByHost(getFromConnection().getHost(), getFromConnection().getPort());
if (ctx != null && ctx.channel().isActive()) {
ByteBuf out = ctx.alloc().buffer();
message.writeHeader(out);
message.writeExtra(out);
ctx.writeAndFlush(out);
logger.debug("发送消息至 " + fromConnection.getHost() + ":" + String.valueOf(fromConnection.getPort()) + " 成功");
} else {
logger.error("发送节点不可用");
retrySendMessage(1);
}
} catch (Exception e) {
logger.error("发送消息异常", e);
ChannelResponse response = new ChannelResponse();
response.setErrorCode(errorCode);
response.setErrorMessage(e.getMessage());
try {
onResponseMessage(response);
} catch (Exception ee) {
logger.error("异常处理错误", ee);
}
// 彻底失败后,删掉这个seq
if (message.getSeq() != null) {
service.getSeq2Callback().remove(message.getSeq());
}
if (timeout != null) {
timeout.cancel();
}
return;
}
}
use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class ChannelResponseCallback2 method onTimeout.
public final void onTimeout() {
logger.error("发送消息超时:{}", message.getSeq());
ChannelResponse response = new ChannelResponse();
response.setErrorCode(102);
response.setMessageID(message.getSeq());
response.setErrorMessage("发送消息超时");
response.setContent("");
try {
onResponseMessage(response);
} catch (Exception e) {
logger.error("超时处理错误:", e);
}
service.getSeq2Callback().remove(message.getSeq());
timeout.cancel();
}
use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class Service method asyncSendChannelMessage.
public void asyncSendChannelMessage(ChannelRequest request, ChannelResponseCallback callback) {
try {
logger.debug("处理链上链下请求: " + request.getMessageID());
callback.setService(this);
ChannelMessage channelMessage = new ChannelMessage();
channelMessage.setSeq(request.getMessageID());
channelMessage.setResult(0);
// 链上链下请求0x20
channelMessage.setType((short) 0x20);
channelMessage.setData(request.getContent().getBytes());
try {
List<ConnectionInfo> fromConnectionInfos = new ArrayList<ConnectionInfo>();
List<ConnectionInfo> toConnectionInfos = new ArrayList<ConnectionInfo>();
// 设置发送节点
ChannelConnections fromChannelConnections = allChannelConnections.get(orgID);
if (fromChannelConnections == null) {
// 没有找到对应的链
// 返回错误
logger.error("没有找到本机构:{}", request.getFromOrg());
throw new Exception("未找到本机构");
}
fromConnectionInfos.addAll(fromChannelConnections.getConnections());
logger.debug("发送结构:{} 节点数:{}", request.getFromOrg(), fromChannelConnections.getConnections().size());
callback.setFromChannelConnections(fromChannelConnections);
callback.setFromConnectionInfos(fromConnectionInfos);
// 设置目的节点
ChannelConnections toChannelConnections = allChannelConnections.get(request.getToOrg());
if (toChannelConnections == null) {
logger.error("未找到目的机构: {}", request.getToOrg());
throw new Exception("未找到目标机构");
}
toConnectionInfos.addAll(toChannelConnections.getConnections());
logger.debug("机构:{} 节点数:{}", request.getToOrg(), toChannelConnections.getConnections().size());
callback.setToConnectionInfos(toConnectionInfos);
// 设置消息内容
callback.setRequest(channelMessage);
seq2Callback.put(request.getMessageID(), callback);
if (request.getTimeout() > 0) {
final ChannelResponseCallback callbackInner = callback;
callback.setTimeout(timeoutHandler.newTimeout(new TimerTask() {
ChannelResponseCallback _callback = callbackInner;
@Override
public void run(Timeout timeout) throws Exception {
// 处理超时逻辑
_callback.onTimeout();
}
}, request.getTimeout(), TimeUnit.MILLISECONDS));
}
callback.retrySendMessage(0);
} catch (Exception e) {
logger.error("发送消息异常 消息未发出", e);
ChannelResponse response = new ChannelResponse();
response.setErrorCode(100);
response.setMessageID(request.getMessageID());
response.setErrorMessage(e.getMessage());
response.setContent("");
callback.onResponse(response);
return;
}
} catch (Exception e) {
logger.error("系统错误", e);
}
}
Aggregations