use of org.fisco.bcos.channel.handler.ConnectionInfo in project web3sdk by FISCO-BCOS.
the class Service method asyncSendChannelMessage2.
public void asyncSendChannelMessage2(ChannelRequest request, ChannelResponseCallback2 callback) {
try {
if (request.getContentByteArray().length >= 32 * 1024 * 1024) {
logger.error("send byte length should not greater than 32M now length:{}", request.getContentByteArray().length);
throw new AmopException("send byte length should not greater than 32M");
}
logger.debug("ChannelRequest:{} ", request.getMessageID());
callback.setService(this);
ChannelMessage2 channelMessage = new ChannelMessage2();
channelMessage.setSeq(request.getMessageID());
channelMessage.setResult(0);
// channelMessage.setType((short) ChannelMessageType.AMOP_REQUEST.getType());
if (request.getType() == 0) {
channelMessage.setType((short) ChannelMessageType.AMOP_REQUEST.getType());
} else {
channelMessage.setType(request.getType());
}
channelMessage.setData(request.getContentByteArray());
channelMessage.setTopic(request.getToTopic());
logger.info("msgid:{} type:{} topic:{}", request.getMessageID(), channelMessage.getType(), request.getToTopic());
try {
List<ConnectionInfo> fromConnectionInfos = new ArrayList<ConnectionInfo>();
// select send node
ChannelConnections fromChannelConnections = allChannelConnections.getAllChannelConnections().stream().filter(x -> x.getGroupId() == groupId).findFirst().get();
if (fromChannelConnections == null) {
if (orgID != null) {
logger.error("not found:{}", orgID);
throw new Exception("not found orgID");
} else {
logger.error("not found:{}", agencyName);
throw new Exception("not found agencyName");
}
}
fromConnectionInfos.addAll(fromChannelConnections.getConnections());
logger.debug("FromOrg:{} nodes:{}", request.getFromOrg(), fromChannelConnections.getConnections().size());
callback.setFromChannelConnections(fromChannelConnections);
callback.setFromConnectionInfos(fromConnectionInfos);
// set request content
callback.setRequest(channelMessage);
logger.info("put msgid:{} into callback map", request.getMessageID());
seq2Callback.put(request.getMessageID(), callback);
if (request.getTimeout() > 0) {
logger.info("timeoutms:{}", request.getTimeout());
final ChannelResponseCallback2 callbackInner = callback;
callback.setTimeout(timeoutHandler.newTimeout(new TimerTask() {
ChannelResponseCallback2 _callback = callbackInner;
@Override
public void run(Timeout timeout) throws Exception {
// process timeout logic
_callback.onTimeout();
logger.error("process channel message timeout, seq: {}, timeout: {}", channelMessage.getSeq(), request.getTimeout());
}
}, request.getTimeout(), TimeUnit.MILLISECONDS));
}
callback.retrySendMessage();
} catch (Exception e) {
logger.error("send message fail:", e);
ChannelResponse response = new ChannelResponse();
response.setErrorCode(ChannelMessageError.MESSAGE_SEND_EXCEPTION.getError());
response.setMessageID(request.getMessageID());
response.setErrorMessage(e.getMessage());
response.setContent("");
callback.onResponse(response);
return;
}
} catch (Exception e) {
logger.error("system error", e);
}
}
use of org.fisco.bcos.channel.handler.ConnectionInfo in project web3sdk by FISCO-BCOS.
the class Service method asyncMulticastChannelMessage2.
public void asyncMulticastChannelMessage2(ChannelRequest request) {
try {
logger.debug("ChannelRequest:{} ", request.getMessageID());
ChannelMessage2 channelMessage = new ChannelMessage2();
channelMessage.setSeq(request.getMessageID());
channelMessage.setResult(0);
channelMessage.setType((short) ChannelMessageType.AMOP_MULBROADCAST.getType());
channelMessage.setData(request.getContentByteArray());
channelMessage.setTopic(request.getToTopic());
try {
// set request content
ChannelConnections fromChannelConnections = allChannelConnections.getAllChannelConnections().stream().filter(x -> x.getGroupId() == groupId).findFirst().get();
if (fromChannelConnections == null) {
if (orgID != null) {
logger.error("not found:{}", orgID);
throw new Exception("not found orgID");
} else {
logger.error("not found:{}", agencyName);
throw new Exception("not found agencyName");
}
}
logger.debug("FromOrg:{} nodes:{}", request.getFromOrg(), fromChannelConnections.getConnections().size());
for (ConnectionInfo connectionInfo : fromChannelConnections.getConnections()) {
ChannelHandlerContext ctx = fromChannelConnections.getNetworkConnectionByHost(connectionInfo.getHost(), connectionInfo.getPort());
if (ctx != null && ChannelHandlerContextHelper.isChannelAvailable(ctx)) {
ByteBuf out = ctx.alloc().buffer();
channelMessage.writeHeader(out);
channelMessage.writeExtra(out);
ctx.writeAndFlush(out);
logger.debug("send message to{}:{} success ", connectionInfo.getHost(), connectionInfo.getPort());
} else {
logger.error("sending node unavailable, {}:{}", connectionInfo.getHost(), connectionInfo.getPort());
}
}
} catch (Exception e) {
logger.error("send message fail:{}", e);
ChannelResponse response = new ChannelResponse();
response.setErrorCode(ChannelMessageError.MESSAGE_SEND_EXCEPTION.getError());
response.setMessageID(request.getMessageID());
response.setErrorMessage(e.getMessage());
response.setContent("");
return;
}
} catch (Exception e) {
logger.error("system error:{}", e);
}
}
Aggregations