Skip to content

Commit

Permalink
[FAB-11304] Handling READY message from peer
Browse files Browse the repository at this point in the history
Adding support for READY message during chaincode registration
Handling 3 chaincode states in handler - CREATED/ESTABLISHED/READY

Change-Id: I173ea3cbbbe3133bfd26643aa2326a90cc6a67c3
Signed-off-by: gennady <[email protected]>
  • Loading branch information
gennadylaventman committed Aug 12, 2018
1 parent 4b27549 commit 2b35a46
Showing 1 changed file with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ public class Handler {
private final Map<String, Boolean> isTransaction = new HashMap<>();
private final Map<String, Channel<ChaincodeMessage>> responseChannel = new HashMap<>();
private Channel<ChaincodeMessage> outboundChaincodeMessages = new Channel<>();
private CCState state;

public Handler(ChaincodeID chaincodeId, Chaincode chaincode) {
this.chaincode = chaincode;
this.state = CCState.CREATED;
queueOutboundChaincodeMessage(newRegisterChaincodeMessage(chaincodeId));
}

Expand All @@ -60,14 +62,47 @@ public void onChaincodeMessage(ChaincodeMessage chaincodeMessage) {
handleChaincodeMessage(chaincodeMessage);
}
private synchronized void handleChaincodeMessage(ChaincodeMessage message) {
logger.info(format("[%-8.8s] Handling ChaincodeMessage of type: %s", message.getTxid(), message.getType()));
switch (message.getType()) {
case KEEPALIVE:
logger.info(format("[%-8.8s] Received KEEPALIVE: nothing to do", message.getTxid()));
logger.info(format("[%-8.8s] Handling ChaincodeMessage of type: %s, handler state %s", message.getTxid(), message.getType(), this.state));
if (message.getType() == KEEPALIVE) {
logger.info(format("[%-8.8s] Received KEEPALIVE: nothing to do", message.getTxid()));
return;
}
switch (this.state) {
case CREATED:
handleCreated(message);
break;
case ESTABLISHED:
handleEstablished(message);
break;
case REGISTERED:
logger.info(format("[%-8.8s] Received REGISTERED: ready for invocations", message.getTxid()));
case READY:
handleReady(message);
break;
default:
logger.info(format("[%-8.8s] Received %s: cannot handle", message.getTxid(), message.getType()));
break;
}
}

private void handleCreated(ChaincodeMessage message) {
if (message.getType() == REGISTERED) {
this.state = CCState.ESTABLISHED;
logger.info(format("[%-8.8s] Received REGISTERED: moving to established state", message.getTxid()));
} else {
logger.info(format("[%-8.8s] Received %s: cannot handle", message.getTxid(), message.getType()));
}
}

private void handleEstablished(ChaincodeMessage message) {
if (message.getType() == READY) {
this.state = CCState.READY;
logger.info(format("[%-8.8s] Received READY: ready for invocations", message.getTxid()));
} else {
logger.info(format("[%-8.8s] Received %s: cannot handle", message.getTxid(), message.getType()));
}
}

private void handleReady(ChaincodeMessage message) {
switch (message.getType()) {
case RESPONSE:
logger.info(format("[%-8.8s] Received RESPONSE: publishing to channel", message.getTxid()));
sendChannel(message);
Expand Down Expand Up @@ -482,4 +517,10 @@ private static String printStackTrace(Throwable throwable) {
return buffer.toString();
}

public enum CCState {
CREATED,
ESTABLISHED,
READY
}

}

0 comments on commit 2b35a46

Please sign in to comment.