use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class Service method sendChannelMessage.
public ChannelResponse sendChannelMessage(ChannelRequest request) {
class Callback extends ChannelResponseCallback {
Callback() {
try {
semaphore.acquire(1);
} catch (InterruptedException e) {
logger.error("错误:", e);
}
}
@Override
public void onResponseMessage(ChannelResponse response) {
channelResponse = response;
logger.debug("收到响应: {}", response.getContent());
semaphore.release();
}
public ChannelResponse channelResponse;
public Semaphore semaphore = new Semaphore(1, true);
}
;
Callback callback = new Callback();
asyncSendChannelMessage(request, callback);
try {
callback.semaphore.acquire(1);
} catch (InterruptedException e) {
logger.error("系统错误:", e);
}
return callback.channelResponse;
}
use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class Service method sendChannelMessage2.
public ChannelResponse sendChannelMessage2(ChannelRequest request) {
class Callback extends ChannelResponseCallback2 {
Callback() {
try {
semaphore.acquire(1);
} catch (InterruptedException e) {
logger.error("错误:", e);
}
}
@Override
public void onResponseMessage(ChannelResponse response) {
channelResponse = response;
logger.debug("收到响应: {}", response.getContent());
semaphore.release();
}
public ChannelResponse channelResponse;
public Semaphore semaphore = new Semaphore(1, true);
}
;
Callback callback = new Callback();
asyncSendChannelMessage2(request, callback);
try {
callback.semaphore.acquire(1);
} catch (InterruptedException e) {
logger.error("系统错误:", e);
}
return callback.channelResponse;
}
use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class Service method onReceiveChannelMessage.
public void onReceiveChannelMessage(ChannelHandlerContext ctx, ChannelMessage message) {
ChannelResponseCallback callback = (ChannelResponseCallback) seq2Callback.get(message.getSeq());
logger.debug("收到消息 seq:{}", message.getSeq());
if (message.getType() == 0x20) {
// 链上链下请求
logger.debug("channel请求消息 PUSH");
if (callback != null) {
// 清空callback再处理
logger.debug("seq已存在,清除:{}", message.getSeq());
seq2Callback.remove(message.getSeq());
}
try {
ChannelPush push = new ChannelPush();
if (pushCallback != null) {
push.setService(this);
push.setCtx(ctx);
push.setMessageID(message.getSeq());
push.setFromNode(message.getFromNode());
push.setToNode(message.getToNode());
push.setSeq(message.getSeq());
push.setMessageID(message.getSeq());
push.setContent(new String(message.getData(), 0, message.getData().length));
pushCallback.onPush(push);
} else {
logger.error("无法push消息,未设置push callback");
}
} catch (Exception e) {
logger.error("处理PUSH消息失败:", e);
}
} else if (message.getType() == 0x21) {
// 链上链下回包
logger.debug("channel回包消息:{}", message.getSeq());
if (callback != null) {
logger.debug("已找到callback 回包消息");
ChannelResponse response = new ChannelResponse();
if (message.getResult() != 0) {
response.setErrorCode(message.getResult());
response.setErrorMessage("回包错误");
}
response.setErrorCode(message.getResult());
response.setMessageID(message.getSeq());
if (message.getData() != null) {
response.setContent(new String(message.getData()));
}
callback.onResponse(response);
} else {
logger.error("未找到回包callback,可能已超时:{}", message.getData());
return;
}
}
}
use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class Service method onReceiveChannelMessage2.
public void onReceiveChannelMessage2(ChannelHandlerContext ctx, ChannelMessage2 message) {
ChannelResponseCallback2 callback = (ChannelResponseCallback2) seq2Callback.get(message.getSeq());
logger.debug("收到消息 seq:{}", message.getSeq());
if (message.getType() == 0x30) {
// 链上链下请求
logger.debug("channel请求消息 PUSH");
if (callback != null) {
// 清空callback再处理
logger.debug("seq已存在,清除:{}", message.getSeq());
seq2Callback.remove(message.getSeq());
}
try {
ChannelPush2 push = new ChannelPush2();
if (pushCallback != null) {
// pushCallback.setInfo(info);
push.setSeq(message.getSeq());
push.setService(this);
push.setCtx(ctx);
push.setTopic(message.getTopic());
push.setSeq(message.getSeq());
push.setMessageID(message.getSeq());
push.setContent(new String(message.getData(), 0, message.getData().length));
pushCallback.onPush(push);
} else {
logger.error("无法push消息,未设置push callback");
}
} catch (Exception e) {
logger.error("处理PUSH消息失败:", e);
}
} else if (message.getType() == 0x31) {
// 链上链下回包
logger.debug("channel回包消息:{}", message.getSeq());
if (callback != null) {
logger.debug("已找到callback 回包消息");
ChannelResponse response = new ChannelResponse();
if (message.getResult() != 0) {
response.setErrorCode(message.getResult());
response.setErrorMessage("回包错误");
}
response.setErrorCode(message.getResult());
response.setMessageID(message.getSeq());
if (message.getData() != null) {
response.setContent(new String(message.getData()));
}
callback.onResponse(response);
} else {
logger.error("未找到回包callback,可能已超时:{}", message.getData());
return;
}
}
}
use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class ConnectionPair method retrySendRemoteMessage.
public void retrySendRemoteMessage() {
Integer errorCode = 0;
try {
// 选取客户端节点
logger.debug("远端节点数:{}", remoteConnectionInfos.size());
remoteConnectionInfo = null;
if (remoteConnectionInfos.size() > 0) {
Random random = new Random();
Integer index = random.nextInt(remoteConnectionInfos.size());
logger.debug("选取:{}", index);
remoteConnectionInfo = remoteConnectionInfos.get(index);
remoteConnectionInfos.remove(remoteConnectionInfos.get(index));
}
if (remoteConnectionInfo == null) {
// 所有节点已尝试,无法再重试了
logger.error("发送消息失败,所有重试均失败");
errorCode = 99;
throw new Exception("发送消息失败,所有重试均失败");
}
ChannelHandlerContext ctx = remoteChannelConnections.getNetworkConnectionByHost(remoteConnectionInfo.getHost(), remoteConnectionInfo.getPort());
remoteConnection = ctx;
if (ctx != null && ctx.channel().isActive()) {
ByteBuf out = ctx.alloc().buffer();
message.writeHeader(out);
message.writeExtra(out);
ctx.writeAndFlush(out);
logger.debug("发送消息至 " + remoteConnectionInfo.getHost() + ":" + String.valueOf(remoteConnectionInfo.getPort()) + " 成功");
} else {
logger.error("发送节点不可用");
retrySendRemoteMessage();
}
} catch (Exception e) {
logger.error("发送消息异常", e);
ChannelResponse response = new ChannelResponse();
response.setErrorCode(errorCode);
response.setErrorMessage(e.getMessage());
// 找不到连接,错误
logger.error("发送连接异常,返回错误99");
if (message.getType() == 0x20 || message.getType() == 0x21) {
message.setType((short) 0x21);
} else if (message.getType() == 0x30 || message.getType() == 0x31) {
message.setType((short) 0x31);
} else {
// ethereum消息,不用改类型
}
message.setResult(99);
ByteBuf out = localConnection.alloc().buffer();
message.writeHeader(out);
message.writeExtra(out);
localConnection.writeAndFlush(out);
// 彻底失败后,删掉这个seq
if (message.getSeq() != null) {
server.getSeq2Connections().remove(message.getSeq());
}
if (timeout != null) {
timeout.cancel();
}
return;
}
}
Aggregations