Search in sources :

Example 1 with EventLogPushCallback

use of org.fisco.bcos.channel.event.filter.EventLogPushCallback in project web3sdk by FISCO-BCOS.

the class Service method onReceiveEventLogPush.

public void onReceiveEventLogPush(ChannelHandlerContext ctx, BcosMessage message) {
    String content = new String(message.getData());
    try {
        EventLogFilterPushResponse resp = ObjectMapperFactory.getObjectMapper().readValue(content, EventLogFilterPushResponse.class);
        if (resp == null || Strings.isEmpty(resp.getFilterID())) {
            logger.error(" event log response invalid format, content: {}", content);
            return;
        }
        EventLogPushCallback callback = (EventLogPushCallback) eventLogFilterManager.getFilterCallback(resp.getFilterID());
        if (callback == null) {
            logger.debug(" event log push message cannot find callback, filterID: {}, content: {}", resp.getFilterID(), content);
            return;
        }
        if (resp.getResult() == EventLogFilterPushStatus.SUCCESS.getStatus()) {
            if (!resp.getLogs().isEmpty()) {
                List<LogResult> logResults = new ArrayList<LogResult>();
                for (Log log : resp.getLogs()) {
                    LogResult logResult = callback.transferLogToLogResult(log);
                    if (logResult == null) {
                        logger.warn(" event log push message decode failed, filterID: {}, log: {}", resp.getFilterID(), log);
                    } else {
                        logResults.add(logResult);
                    }
                }
                callback.onPushEventLog(EventLogFilterPushStatus.SUCCESS.getStatus(), logResults);
                // update status
                callback.getFilter().updateByLogResult(logResults);
                logger.info(" log size: {}, blocknumber: {}", logResults.size(), logResults.get(0).getLog().getBlockNumber());
            }
        } else if (resp.getResult() == EventLogFilterPushStatus.PUSH_COMPLETED.getStatus()) {
            // event log push end
            callback.onPushEventLog(EventLogFilterPushStatus.PUSH_COMPLETED.getStatus(), null);
            eventLogFilterManager.removeFilterAndCallback(callback.getFilter().getRegisterID(), resp.getFilterID());
            logger.info("event log push message push end, filterID: {}, registerID: {}, content: {}", resp.getFilterID(), callback.getFilter().getRegisterID(), content);
        } else {
            callback.onPushEventLog(resp.getResult(), null);
            // should remove callback function
            eventLogFilterManager.removeFilterAndCallback(callback.getFilter().getRegisterID(), resp.getFilterID());
            logger.warn("event log push message, filterID: {}, registerID: {}, code: {}, content: {}", resp.getFilterID(), callback.getFilter().getRegisterID(), resp.getResult(), content);
        }
    } catch (Exception e) {
        logger.error("event log push message exception, error message: {}, content: {}", e.getMessage(), content);
    }
}
Also used : EventLogPushCallback(org.fisco.bcos.channel.event.filter.EventLogPushCallback) Log(org.fisco.bcos.web3j.protocol.core.methods.response.Log) LogResult(org.fisco.bcos.web3j.tx.txdecode.LogResult) ArrayList(java.util.ArrayList) EventLogFilterPushResponse(org.fisco.bcos.channel.event.filter.EventLogFilterPushResponse) 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 EventLogPushCallback

use of org.fisco.bcos.channel.event.filter.EventLogPushCallback in project web3sdk by FISCO-BCOS.

the class Service method asyncSendRegisterEventLogFilterMessage.

/**
 * @param filter
 */
public void asyncSendRegisterEventLogFilterMessage(EventLogFilter filter) {
    ChannelRequest request = new ChannelRequest();
    request.setMessageID(newSeq());
    request.setToTopic("");
    request.setType((short) ChannelMessageType.CLIENT_REGISTER_EVENT_LOG.getType());
    EventLogRequestParams params = new EventLogRequestParams(filter.generateNewParams(), String.valueOf(getGroupId()), newSeq());
    logger.info(" registerID: {}, filterID: {}, params: {}", filter.getRegisterID(), filter.getFilterID(), params);
    try {
        request.setContent(params.toJsonString());
    } catch (JsonProcessingException e1) {
        filter.getCallback().onPushEventLog(EventLogFilterPushStatus.INVALID_PARAMS.getStatus(), null);
        eventLogFilterManager.removeFilter(filter.getRegisterID());
        return;
    }
    final String filterID = params.getFilterID();
    final String registerID = filter.getRegisterID();
    filter.setFilterID(filterID);
    final EventLogPushCallback callback0 = filter.getCallback();
    // register callback
    eventLogFilterManager.addCallback(params.getFilterID(), filter.getCallback());
    asyncSendChannelMessage2(request, new ChannelResponseCallback2() {

        @Override
        public void onResponseMessage(ChannelResponse response) {
            logger.info(" event filter callback response, registerID: {}, filterID: {}, seq: {}, error code: {},  content: {}", registerID, filterID, response.getMessageID(), response.getErrorCode(), response.getContent());
            try {
                if (0 == response.getErrorCode()) {
                    // receive response successfully
                    EventLogFilterPushResponse resp = ObjectMapperFactory.getObjectMapper().readValue(response.getContent(), EventLogFilterPushResponse.class);
                    if (resp.getResult() == 0) {
                        // node response ok, event log will be pushed soon
                        eventLogFilterManager.updateEventLogFilter(callback0.getFilter(), EventLogFilterStatus.EVENT_LOG_PUSHING, response.getCtx());
                    } else {
                        // node response not ok, callback to client
                        callback0.onPushEventLog(resp.getResult(), null);
                        eventLogFilterManager.removeFilterAndCallback(registerID, filterID);
                    }
                } else {
                    // register request send failed, waiting to be re-sent
                    eventLogFilterManager.updateEventLogFilter(callback0.getFilter(), EventLogFilterStatus.WAITING_REQUEST, null);
                    // remove register callback
                    eventLogFilterManager.removeCallback(filterID);
                }
            } catch (Exception e) {
                callback0.onPushEventLog(EventLogFilterPushStatus.OTHER_ERROR.getStatus(), null);
                eventLogFilterManager.removeFilterAndCallback(filter.getRegisterID(), filterID);
                logger.error(" event filter response message exception, filterID: {}, registerID: {}, exception message: {}", filterID, registerID, e.getMessage());
            }
        }
    });
}
Also used : EventLogRequestParams(org.fisco.bcos.channel.event.filter.EventLogRequestParams) ChannelRequest(org.fisco.bcos.channel.dto.ChannelRequest) EventLogPushCallback(org.fisco.bcos.channel.event.filter.EventLogPushCallback) ChannelResponse(org.fisco.bcos.channel.dto.ChannelResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) EventLogFilterPushResponse(org.fisco.bcos.channel.event.filter.EventLogFilterPushResponse) 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 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 EventLogFilterPushResponse (org.fisco.bcos.channel.event.filter.EventLogFilterPushResponse)2 EventLogPushCallback (org.fisco.bcos.channel.event.filter.EventLogPushCallback)2 TransactionException (org.fisco.bcos.web3j.protocol.exceptions.TransactionException)2 ArrayList (java.util.ArrayList)1 ChannelRequest (org.fisco.bcos.channel.dto.ChannelRequest)1 ChannelResponse (org.fisco.bcos.channel.dto.ChannelResponse)1 EventLogRequestParams (org.fisco.bcos.channel.event.filter.EventLogRequestParams)1 Log (org.fisco.bcos.web3j.protocol.core.methods.response.Log)1 LogResult (org.fisco.bcos.web3j.tx.txdecode.LogResult)1