void parse(){
  ByteBuffer bb=byteBuffer;
  bb.clear();
  size=bb.getShort();
  if (size > buffer.length) {
    throw new IllegalStateException("Message size " + size + " is > buffer size "+ buffer.length);
  }
  bb.limit(size);
  type=bb.get();
  fromMemberId=bb.getShort();
  term=bb.getInt();
  commandSize=0;
  numCommands=0;
  timestamp=0;
switch (type) {
case TYPE_APPEND_ENTRY_REQ:
    prevRid.set(bb.getInt(),bb.getInt());
  commitIndex=bb.getInt();
entryTerm=bb.getInt();
isHeartbeat=entryTerm == 0;
commandSize=bb.getShort();
bufferOffset=bb.position();
if (messageTracing) {
if (isHeartbeat) {
if (heartbeatTracing) {
tracingInfo=String.format("H(cterm=%d eterm=%d index=%d ci=%d)",term,prevRid.term,prevRid.index,commitIndex);
}
}
 else {
tracingInfo=String.format("AE(cterm=%d pterm=%d eterm=%d index=%d ci=%d size=%d)",term,prevRid.term,entryTerm,prevRid.index + 1,commitIndex,size - overhead[TYPE_APPEND_ENTRY_REQ]);
}
}
numCommands=1;
int s=overhead[TYPE_APPEND_ENTRY_REQ] + commandSize;
int bo=bufferOffset;
int cs=commandSize;
while (s < size) {
numCommands++;
bo+=cs;
cs=(0x0ff & buffer[bo]) << 8 | (0x0ff & buffer[bo + 1]);
bo+=2;
s+=cs + 2;
}
break;
case TYPE_APPEND_ENTRY_REP:
mnIndex=bb.getInt();
success=bb.get() == 1;
isHeartbeat=bb.get() == 1;
if (messageTracing && (heartbeatTracing || !isHeartbeat)) {
tracingInfo=String.format("%s(cterm=%d %s=%d %s)",isHeartbeat ? "h" : "ae",term,success ? "matchIndex" : "nextIndex",mnIndex,success ? "ok" : "fail");
}
break;
case TYPE_REQUEST_VOTE_REQ:
isPrevote=bb.get() == 1;
prevRid.set(bb.getInt(),bb.getInt());
if (messageTracing) {
tracingInfo=String.format("RV(cterm=%d pterm=%d index=%d) %s",term,prevRid.term,prevRid.index,isPrevote ? "prevote" : "");
}
break;
case TYPE_REQUEST_VOTE_REP:
isPrevote=bb.get() == 1;
voteGranted=bb.get() == 1;
if (messageTracing) {
tracingInfo=String.format("rv(cterm=%d %s) %s",term,voteGranted ? "yes" : "no",isPrevote ? "prevote" : "");
}
break;
default :
throw new IllegalStateException("Unknown message type " + type);
}
}

void parse ( ) { ByteBuffer bb = byteBuffer ; bb . clear ( ) ; size = bb . getShort ( ) ; if ( size > buffer . length ) { throw new IllegalStateException ( "Message size " + size + " is > buffer size " + buffer . length ) ; } bb . limit ( size ) ; type = bb . get ( ) ; fromMemberId = bb . getShort ( ) ; term = bb . getInt ( ) ; commandSize = 0 ; numCommands = 0 ; timestamp = 0 ; switch ( type ) { case TYPE_APPEND_ENTRY_REQ : prevRid . set ( bb . getInt ( ) , bb . getInt ( ) ) ; commitIndex = bb . getInt ( ) ; entryTerm = bb . getInt ( ) ; isHeartbeat = entryTerm == 0 ; commandSize = bb . getShort ( ) ; bufferOffset = bb . position ( ) ; if ( messageTracing ) { if ( isHeartbeat ) { if ( heartbeatTracing ) { tracingInfo = String . format ( "H(cterm=%d eterm=%d index=%d ci=%d)" , term , prevRid . term , prevRid . index , commitIndex ) ; } } else { tracingInfo = String . format ( "AE(cterm=%d pterm=%d eterm=%d index=%d ci=%d size=%d)" , term , prevRid . term , entryTerm , prevRid . index + 1 , commitIndex , size - overhead [ TYPE_APPEND_ENTRY_REQ ] ) ; } } numCommands = 1 ; int s = overhead [ TYPE_APPEND_ENTRY_REQ ] + commandSize ; int bo = bufferOffset ; int cs = commandSize ; while ( s < size ) { numCommands ++ ; bo += cs ; cs = ( 0x0ff & buffer [ bo ] ) << 8 | ( 0x0ff & buffer [ bo + 1 ] ) ; bo += 2 ; s += cs + 2 ; } break ; case TYPE_APPEND_ENTRY_REP : mnIndex = bb . getInt ( ) ; success = bb . get ( ) == 1 ; isHeartbeat = bb . get ( ) == 1 ; if ( messageTracing && ( heartbeatTracing || ! isHeartbeat ) ) { tracingInfo = String . format ( "%s(cterm=%d %s=%d %s)" , isHeartbeat ? "h" : "ae" , term , success ? "matchIndex" : "nextIndex" , mnIndex , success ? "ok" : "fail" ) ; } break ; case TYPE_REQUEST_VOTE_REQ : isPrevote = bb . get ( ) == 1 ; prevRid . set ( bb . getInt ( ) , bb . getInt ( ) ) ; if ( messageTracing ) { tracingInfo = String . format ( "RV(cterm=%d pterm=%d index=%d) %s" , term , prevRid . term , prevRid . index , isPrevote ? "prevote" : "" ) ; } break ; case TYPE_REQUEST_VOTE_REP : isPrevote = bb . get ( ) == 1 ; voteGranted = bb . get ( ) == 1 ; if ( messageTracing ) { tracingInfo = String . format ( "rv(cterm=%d %s) %s" , term , voteGranted ? "yes" : "no" , isPrevote ? "prevote" : "" ) ; } break ; default : throw new IllegalStateException ( "Unknown message type " + type ) ; } }
parses the binary message into variables that will be used when handlers are called . the message should be never be mutated after parse ( ) is called .
