use of org.bcos.channel.dto.ChannelMessage 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);
}
}
use of org.bcos.channel.dto.ChannelMessage in project web3sdk by FISCO-BCOS.
the class Service method sendResponseMessage.
public void sendResponseMessage(ChannelResponse response, ConnectionInfo info, ChannelHandlerContext ctx, String fromNode, String toNode, String seq) {
try {
ChannelMessage responseMessage = new ChannelMessage();
responseMessage.setData(response.getContent().getBytes());
responseMessage.setResult(response.getErrorCode());
responseMessage.setSeq(seq);
// 链上链下回包0x21
responseMessage.setType((short) 0x21);
responseMessage.setToNode(fromNode);
responseMessage.setFromNode(toNode);
ByteBuf out = ctx.alloc().buffer();
responseMessage.writeHeader(out);
responseMessage.writeExtra(out);
ctx.writeAndFlush(out);
logger.debug("发送回包成功 seq:{} 长度:{}", response.getMessageID(), out.readableBytes());
} catch (Exception e) {
logger.error("系统错误", e);
}
}
Aggregations