Search in sources :

Example 11 with ChannelResponse

use of org.fisco.bcos.channel.dto.ChannelResponse 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)

Example 12 with ChannelResponse

use of org.fisco.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.

the class Service method onReceiveRegisterEventResponse.

public void onReceiveRegisterEventResponse(ChannelHandlerContext ctx, ChannelMessage2 message) {
    ChannelResponseCallback2 callback = (ChannelResponseCallback2) seq2Callback.get(message.getSeq());
    String seq = message.getSeq();
    String content = new String(message.getData());
    if (callback == null) {
        logger.warn(" register event filter response cannot find callback, seq: {}, content: {}", seq, content);
        return;
    }
    seq2Callback.remove(seq);
    ChannelResponse response = new ChannelResponse();
    response.setCtx(ctx);
    response.setErrorCode(message.getResult());
    response.setMessageID(message.getSeq());
    if (message.getData() != null) {
        response.setContent(message.getData());
    }
    callback.onResponse(response);
    logger.info(" register event filter response, seq: {}, content: {} ", seq, content);
}
Also used : ChannelResponse(org.fisco.bcos.channel.dto.ChannelResponse)

Example 13 with ChannelResponse

use of org.fisco.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());
    if (message.getType() == ChannelMessageType.AMOP_REQUEST.getType() || message.getType() == ChannelMessageType.AMOP_MULBROADCAST.getType()) {
        logger.debug("channel PUSH");
        if (callback != null) {
            logger.debug("seq already existed,clear:{}", message.getSeq());
            seq2Callback.remove(message.getSeq());
        }
        if (message.getTopic().length() > verifyChannelPrefix.length() && verifyChannelPrefix.equals(message.getTopic().substring(0, verifyChannelPrefix.length()))) {
            try {
                signForAmop(ctx, message);
            } catch (IOException e) {
                logger.error("sign for amop failed:{}", e);
            }
        } else {
            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());
                    logger.debug("msg:{}", Arrays.toString(message.getData()));
                    push.setContent(message.getData());
                    pushCallback.onPush(push);
                } else {
                    logger.error("can not push,unset push callback");
                }
            } catch (Exception e) {
                logger.error("push error:{}", e);
            }
        }
    } else if (message.getType() == ChannelMessageType.AMOP_RESPONSE.getType()) {
        logger.debug("channel message:{}", message.getSeq());
        if (callback != null) {
            logger.debug("found callback response");
            ChannelResponse response = new ChannelResponse();
            if (message.getResult() != 0) {
                response.setErrorMessage("response errors");
            }
            // over bandwidth limit
            if (message.getResult() == ChannelMessageError.REJECT_AMOP_REQ_FOR_OVER_BANDWIDTHLIMIT.getError()) {
                logger.error("AMOP request was rejected due to over bandwidth limit, message: {}", message.getSeq());
                response.setErrorMessage("AMOP request was rejected due to over bandwidth limit");
            }
            response.setErrorCode(message.getResult());
            response.setMessageID(message.getSeq());
            if (message.getData() != null) {
                response.setContent(message.getData());
            }
            callback.onResponse(response);
        } else {
            logger.error("can not found response callback,timeout:{}", message.getData());
            return;
        }
    }
}
Also used : ChannelPush2(org.fisco.bcos.channel.dto.ChannelPush2) IOException(java.io.IOException) ChannelResponse(org.fisco.bcos.channel.dto.ChannelResponse) 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 14 with ChannelResponse

use of org.fisco.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.

the class Channel2ClientBin method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.out.println("param: target topic filename of request");
        return;
    }
    String topic = args[0];
    String filename = args[1];
    Integer count = 1;
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    logger.debug("init client");
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    System.out.println("3s ...");
    Thread.sleep(1000);
    System.out.println("2s ...");
    Thread.sleep(1000);
    System.out.println("1s ...");
    Thread.sleep(1000);
    System.out.println("start test");
    System.out.println("===================================================================");
    for (Integer i = 0; i < count; ++i) {
        Thread.sleep(2000);
        ChannelRequest request = new ChannelRequest();
        request.setToTopic(topic);
        request.setMessageID(service.newSeq());
        request.setTimeout(5000);
        /*设置为-128表示为传输二进制*/
        int flag = -128;
        byte[] byteflag = intToByteArray(flag);
        int filelength = filename.length();
        byte[] bytelength = intToByteArray(filelength);
        byte[] bytefilename = filename.getBytes();
        byte[] contentfile = toByteArrFromFile(filename);
        byte[] content = byteCat(byteCat(byteCat(byteflag, bytelength), bytefilename), contentfile);
        request.setContent(content);
        logger.info("msg:" + Arrays.toString(content));
        System.out.println(df.format(LocalDateTime.now()) + " request seq:" + String.valueOf(request.getMessageID()) + " content length:" + content.length);
        ChannelResponse response = service.sendChannelMessage2(request);
        System.out.println(df.format(LocalDateTime.now()) + "response seq:" + String.valueOf(response.getMessageID()) + ", ErrorCode:" + response.getErrorCode() + ", Content:" + response.getContent());
        if (response.getErrorCode() != 0) {
            System.out.println("Error message" + response.getErrorMessage());
        }
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ChannelRequest(org.fisco.bcos.channel.dto.ChannelRequest) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Service(org.fisco.bcos.channel.client.Service) ChannelResponse(org.fisco.bcos.channel.dto.ChannelResponse) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 15 with ChannelResponse

use of org.fisco.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.

the class Channel2ClientNeedVerify method main.

public static void main(String[] args) throws Exception {
    if (args.length < parameterNum) {
        System.out.println("param: target topic total number of request");
        return;
    }
    String topic = args[0];
    Integer count = Integer.parseInt(args[1]);
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    logger.debug("init client");
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    System.out.println("3s ...");
    Thread.sleep(1000);
    System.out.println("2s ...");
    Thread.sleep(1000);
    System.out.println("1s ...");
    Thread.sleep(1000);
    System.out.println("start test");
    System.out.println("===================================================================");
    ChannelRequest request = new ChannelRequest();
    for (Integer i = 0; i < count; ++i) {
        Thread.sleep(2000);
        request.setToTopic(topic);
        request.setMessageID(service.newSeq());
        request.setTimeout(5000);
        String content = "request seq:" + request.getMessageID();
        request.setContent(content.getBytes());
        System.out.println(df.format(LocalDateTime.now()) + " request seq:" + request.getMessageID() + ", Content:" + request.getContent() + " content:" + Arrays.toString(request.getContentByteArray()));
        ChannelResponse response = service.sendChannelMessageForVerifyTopic(request);
        System.out.println(df.format(LocalDateTime.now()) + "response seq:" + response.getMessageID() + ", ErrorCode:" + response.getErrorCode() + ", Content:" + response.getContent());
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ChannelRequest(org.fisco.bcos.channel.dto.ChannelRequest) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Service(org.fisco.bcos.channel.client.Service) ChannelResponse(org.fisco.bcos.channel.dto.ChannelResponse) DateTimeFormatter(java.time.format.DateTimeFormatter)

Aggregations

ChannelResponse (org.fisco.bcos.channel.dto.ChannelResponse)16 ChannelRequest (org.fisco.bcos.channel.dto.ChannelRequest)6 IOException (java.io.IOException)5 DateTimeFormatter (java.time.format.DateTimeFormatter)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 KeyStoreException (java.security.KeyStoreException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 NoSuchProviderException (java.security.NoSuchProviderException)4 CertificateException (java.security.cert.CertificateException)4 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)4 Service (org.fisco.bcos.channel.client.Service)4 TransactionException (org.fisco.bcos.web3j.protocol.exceptions.TransactionException)4 ApplicationContext (org.springframework.context.ApplicationContext)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 SocketChannel (io.netty.channel.socket.SocketChannel)2 ChannelMessage2 (org.fisco.bcos.channel.dto.ChannelMessage2)2 EventLogPushCallback (org.fisco.bcos.channel.event.filter.EventLogPushCallback)2