use of org.fisco.bcos.channel.handler.ConnectionCallback in project web3sdk by FISCO-BCOS.
the class Service method run.
public void run() throws Exception {
logger.debug("init ChannelService");
if (setJavaOpt) {
initJavaOpt();
}
parseFromTopic2KeyInfo();
int flag = 0;
for (ChannelConnections channelConnections : allChannelConnections.getAllChannelConnections()) {
if (channelConnections.getGroupId() == groupId) {
flag = 1;
try {
ConnectionCallback connectionCallback = new ConnectionCallback(topics);
connectionCallback.setChannelService(this);
channelConnections.setCallback(connectionCallback);
channelConnections.setCaCert(allChannelConnections.getCaCert());
channelConnections.setSslCert(allChannelConnections.getSslCert());
channelConnections.setSslKey(allChannelConnections.getSslKey());
channelConnections.setGmCaCert(allChannelConnections.getGmCaCert());
channelConnections.setGmEnSslCert(allChannelConnections.getGmEnSslCert());
channelConnections.setGmEnSslKey(allChannelConnections.getGmEnSslKey());
channelConnections.setGmSslCert(allChannelConnections.getGmSslCert());
channelConnections.setGmSslKey(allChannelConnections.getGmSslKey());
channelConnections.init();
channelConnections.setThreadPool(threadPool);
channelConnections.startConnect();
int sleepTime = 0;
boolean running = false;
while (true) {
Map<String, ChannelHandlerContext> networkConnection = channelConnections.getNetworkConnections();
for (ChannelHandlerContext ctx : networkConnection.values()) {
if (Objects.nonNull(ctx) && ChannelHandlerContextHelper.isChannelAvailable(ctx)) {
running = true;
break;
}
}
if (running || sleepTime > connectSeconds * 1000) {
break;
} else {
Thread.sleep(connectSleepPerMillis);
sleepTime += connectSleepPerMillis;
}
}
String baseMessage = " nodes: " + channelConnections.getConnectionsStr() + " ,groupId: " + String.valueOf(groupId) + " ,caCert: " + channelConnections.getCaCert() + " ,sslKey: " + channelConnections.getSslKey() + " ,sslCert: " + channelConnections.getSslCert() + " ,java version: " + System.getProperty("java.version") + " ,java vendor: " + System.getProperty("java.vm.vendor");
if (!running) {
String errorMessage = " Failed to connect to " + baseMessage;
logger.error(errorMessage);
throw new Exception(errorMessage);
}
logger.info(" Connect to " + baseMessage);
channelConnections.startPeriodTask();
eventLogFilterManager.start();
} catch (InterruptedException e) {
logger.warn(" thread interrupted exception: ", e);
Thread.currentThread().interrupt();
} catch (Exception e) {
logger.error(" service init failed, error message: {}, error: ", e.getMessage(), e);
throw e;
}
}
}
if (flag == 0) {
throw new Exception("Please set the right groupId ");
}
}
use of org.fisco.bcos.channel.handler.ConnectionCallback in project web3sdk by FISCO-BCOS.
the class Service method updateTopicsToNode.
public void updateTopicsToNode() {
logger.info(" updateTopicToNode, groupId: {}, topics: {}", groupId, getTopics());
// select send node
ChannelConnections channelConnections = allChannelConnections.getAllChannelConnections().stream().filter(x -> x.getGroupId() == groupId).findFirst().get();
if (Objects.isNull(channelConnections)) {
throw new IllegalArgumentException(" No group configuration was found, groupId: " + groupId);
}
ConnectionCallback callback = (ConnectionCallback) channelConnections.getCallback();
if (Objects.isNull(callback)) {
throw new IllegalArgumentException(" No callback was found for ChannelConnections, service is not initialized");
}
callback.setTopics(getTopics());
/**
* send update topic message to all connected nodes
*/
Map<String, ChannelHandlerContext> networkConnections = channelConnections.getNetworkConnections();
for (ChannelHandlerContext ctx : networkConnections.values()) {
if (Objects.nonNull(ctx) && ChannelHandlerContextHelper.isChannelAvailable(ctx)) {
try {
callback.sendUpdateTopicMessage(ctx);
} catch (Exception e) {
logger.debug(" e: ", e);
}
}
}
}
Aggregations