Search in sources :

Example 1 with ConnectionCallback

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 ");
    }
}
Also used : ChannelConnections(org.fisco.bcos.channel.handler.ChannelConnections) ConnectionCallback(org.fisco.bcos.channel.handler.ConnectionCallback) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 2 with ConnectionCallback

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);
            }
        }
    }
}
Also used : ChannelConnections(org.fisco.bcos.channel.handler.ChannelConnections) ConnectionCallback(org.fisco.bcos.channel.handler.ConnectionCallback) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 NoSuchProviderException (java.security.NoSuchProviderException)2 CertificateException (java.security.cert.CertificateException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 ChannelConnections (org.fisco.bcos.channel.handler.ChannelConnections)2 ConnectionCallback (org.fisco.bcos.channel.handler.ConnectionCallback)2 TransactionException (org.fisco.bcos.web3j.protocol.exceptions.TransactionException)2