Search in sources :

Example 1 with ReplyListener

use of bftsmart.communication.client.ReplyListener in project bftsmart by blockchain-jd-com.

the class AsynchServiceProxy method replyReceived.

/**
 */
@Override
public void replyReceived(TOMMessage reply) {
    LOGGER.debug("Asynchronously received reply from {} with sequence number {} and operation ID {}", reply.getSender(), reply.getSequence(), reply.getOperationId());
    canReceiveLock.lock();
    try {
        RequestContext requestContext = requestsContext.get(reply.getOperationId());
        if (requestContext == null) {
            // it is not a asynchronous request
            super.replyReceived(reply);
            return;
        }
        if (contains(requestContext.getTargets(), reply.getSender()) && (reply.getSequence() == requestContext.getReqId()) && // && (reply.getOperationId() == requestContext.getOperationId())
        (reply.getReqType().compareTo(requestContext.getRequestType())) == 0) {
            LOGGER.debug("Deliverying message from {} with sequence number {} and operation ID {} to the listener", reply.getSender(), reply.getSequence(), reply.getOperationId());
            ReplyListener replyListener = requestContext.getReplyListener();
            View v = null;
            if (replyListener != null) {
                // system reconfiguration
                if ((v = newView(reply.getContent())) != null && !requestsAlias.containsKey(reply.getOperationId())) {
                    // Deal with a system
                    // reconfiguration
                    TOMMessage[] replies = requestsReplies.get(reply.getOperationId());
                    int sameContent = 1;
                    int replyQuorum = getReplyQuorum();
                    int pos = getViewManager().getCurrentViewPos(reply.getSender());
                    replies[pos] = reply;
                    for (int i = 0; i < replies.length; i++) {
                        if ((replies[i] != null) && (i != pos || getViewManager().getCurrentViewN() == 1) && (reply.getReqType() != TOMMessageType.ORDERED_REQUEST || Arrays.equals(replies[i].getContent(), reply.getContent()))) {
                            sameContent++;
                        }
                    }
                    if (sameContent >= replyQuorum) {
                        if (v.getId() > getViewManager().getCurrentViewId()) {
                            reconfigureTo(v);
                        }
                        requestContext.getReplyListener().reset();
                        Thread t = new Thread() {

                            @Override
                            public void run() {
                                int id = invokeAsynch(requestContext.getRequest(), requestContext.getTargets(), requestContext.getReplyListener(), TOMMessageType.ORDERED_REQUEST);
                                requestsAlias.put(reply.getOperationId(), id);
                            }
                        };
                        t.start();
                    }
                } else if (!requestsAlias.containsKey(reply.getOperationId())) {
                    requestContext.getReplyListener().replyReceived(requestContext, reply);
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        canReceiveLock.unlock();
    }
}
Also used : TOMMessage(bftsmart.tom.core.messages.TOMMessage) ReplyListener(bftsmart.communication.client.ReplyListener) View(bftsmart.reconfiguration.views.View)

Example 2 with ReplyListener

use of bftsmart.communication.client.ReplyListener in project aware by bergerch.

the class AsynchServiceProxy method replyReceived.

/**
 * This is the method invoked by the client side communication system.
 *
 * @param reply The reply delivered by the client side communication system
 */
@Override
public void replyReceived(TOMMessage reply) {
    logger.debug("Asynchronously received reply from " + reply.getSender() + " with sequence number " + reply.getSequence() + " and operation ID " + reply.getOperationId());
    try {
        canReceiveLock.lock();
        RequestContext requestContext = requestsContext.get(reply.getOperationId());
        if (requestContext == null) {
            // it is not a asynchronous request
            super.replyReceived(reply);
            return;
        }
        if (contains(requestContext.getTargets(), reply.getSender()) && (reply.getSequence() == requestContext.getReqId()) && // && (reply.getOperationId() == requestContext.getOperationId())
        (reply.getReqType().compareTo(requestContext.getRequestType())) == 0) {
            logger.debug("Deliverying message from " + reply.getSender() + " with sequence number " + reply.getSequence() + " and operation ID " + reply.getOperationId() + " to the listener");
            ReplyListener replyListener = requestContext.getReplyListener();
            View v = null;
            if (replyListener != null) {
                // if (reply.getViewID() > getViewManager().getCurrentViewId()) { // Deal with a system reconfiguration
                if ((v = newView(reply.getContent())) != null && !requestsAlias.containsKey(reply.getOperationId())) {
                    // Deal with a system reconfiguration
                    TOMMessage[] replies = requestsReplies.get(reply.getOperationId());
                    int sameContent = 1;
                    int replyQuorum = getReplyQuorum();
                    int pos = getViewManager().getCurrentViewPos(reply.getSender());
                    replies[pos] = reply;
                    for (int i = 0; i < replies.length; i++) {
                        if ((replies[i] != null) && (i != pos || getViewManager().getCurrentViewN() == 1) && (reply.getReqType() != TOMMessageType.ORDERED_REQUEST || Arrays.equals(replies[i].getContent(), reply.getContent()))) {
                            sameContent++;
                        }
                    }
                    if (sameContent >= replyQuorum) {
                        if (v.getId() > getViewManager().getCurrentViewId()) {
                            reconfigureTo(v);
                        }
                        requestContext.getReplyListener().reset();
                        Thread t = new Thread() {

                            @Override
                            public void run() {
                                int id = invokeAsynch(requestContext.getRequest(), requestContext.getTargets(), requestContext.getReplyListener(), TOMMessageType.ORDERED_REQUEST);
                                requestsAlias.put(reply.getOperationId(), id);
                            }
                        };
                        t.start();
                    }
                } else if (!requestsAlias.containsKey(reply.getOperationId())) {
                    requestContext.getReplyListener().replyReceived(requestContext, reply);
                }
            }
        }
    } catch (Exception ex) {
        logger.error("Error processing received request", ex);
    } finally {
        canReceiveLock.unlock();
    }
}
Also used : TOMMessage(bftsmart.tom.core.messages.TOMMessage) ReplyListener(bftsmart.communication.client.ReplyListener) View(bftsmart.reconfiguration.views.View)

Aggregations

ReplyListener (bftsmart.communication.client.ReplyListener)2 View (bftsmart.reconfiguration.views.View)2 TOMMessage (bftsmart.tom.core.messages.TOMMessage)2