/** * Parse the header info out of the buffer. */ private void parseHeaderFromBuffer() { int magic = header[0]; assert magic == RES_MAGIC : "Invalid magic: " + magic; responseCmd = header[1]; assert cmd == DUMMY_OPCODE || responseCmd == cmd : "Unexpected response command value"; keyLen = decodeShort(header, 2); errorCode = decodeShort(header, 6); int bytesToRead = decodeInt(header, 8); payload = new byte[bytesToRead]; responseOpaque = decodeInt(header, 12); responseCas = decodeLong(header, 16); assert opaqueIsValid() : "Opaque is not valid"; }
protected void finishedPayload(byte[] pl) throws IOException { if(errorCode != 0) { OperationStatus status=getStatusForErrorCode(errorCode, pl); if(status == null) { handleError(OperationErrorType.SERVER, new String(pl)); } else { getCallback().receivedStatus(status); transitionState(OperationState.COMPLETE); } } else { decodePayload(pl); transitionState(OperationState.COMPLETE); } }
/** * Read from the incoming {@link ByteBuffer}. * * Reading from the buffer is done in stages, depending on how much data * can be read at once. First, the header is read and then parsed (24 * bytes, indicated by {@link #MIN_RECV_PACKET}). Then, the payload is read * (if one is available for this operation and can be loaded fully). * * @param buffer the buffer to read from. * @throws IOException if an error happened during parsing/reading. */ @Override public void readFromBuffer(final ByteBuffer buffer) throws IOException { if (headerOffset < MIN_RECV_PACKET) { readHeaderFromBuffer(buffer); if (headerOffset == MIN_RECV_PACKET) { parseHeaderFromBuffer(); } } if (headerOffset >= MIN_RECV_PACKET && payload == null) { finishedPayload(EMPTY_BYTES); } else if (payload != null) { readPayloadFromBuffer(buffer); } else { getLogger().debug("Only read %d of the %d needed to fill a header", headerOffset, MIN_RECV_PACKET); } }
int available = b.remaining(); toRead = Math.min(toRead, available); getLogger().debug("Reading %d header bytes", toRead); b.get(header, headerOffset, toRead); headerOffset += toRead; assert cmd == DUMMY_OPCODE || responseCmd == cmd : "Unexpected response command value"; keyLen = decodeShort(header, 2); errorCode = decodeShort(header, 6); int bytesToRead = decodeInt(header, 8); payload = new byte[bytesToRead]; responseOpaque = decodeInt(header, 12); responseCas = decodeLong(header, 16); assert opaqueIsValid() : "Opaque is not valid"; finishedPayload(EMPTY_BYTES); } else if (payload != null) { int toRead = payload.length - payloadOffset; int available = b.remaining(); toRead = Math.min(toRead, available); getLogger().debug("Reading %d payload bytes", toRead); b.get(payload, payloadOffset, toRead); payloadOffset += toRead; finishedPayload(payload); getLogger().debug("Only read %d of the %d needed to fill a header",
/** * Validate an opaque value from the header. This may be overridden from a * subclass where the opaque isn't expected to always be the same as the * request opaque. */ protected boolean opaqueIsValid() { if (responseOpaque != opaque) { getLogger().warn("Expected opaque: %d, got opaque: %d\n", responseOpaque, opaque); } return responseOpaque == opaque; }
/** * Read the payload from the buffer. * * @param buffer the buffer to read from. * @throws IOException if an error occures during payload finishing. */ private void readPayloadFromBuffer(final ByteBuffer buffer) throws IOException { int toRead = payload.length - payloadOffset; int available = buffer.remaining(); toRead = Math.min(toRead, available); getLogger().debug("Reading %d payload bytes", toRead); buffer.get(payload, payloadOffset, toRead); payloadOffset += toRead; if (payloadOffset == payload.length) { finishedPayload(payload); } }
/** * Decode the given payload for this command. * * @param pl the payload. */ protected void decodePayload(byte[] pl) { assert pl.length == 0 : "Payload has bytes, but decode isn't overridden"; getCallback().receivedStatus(STATUS_OK); }
@Override protected void finishedPayload(byte[] pl) throws IOException { if (errorCode == SASL_CONTINUE) { getCallback().receivedStatus(new OperationStatus(true, new String(pl), StatusCode.SUCCESS)); transitionState(OperationState.COMPLETE); } else if (errorCode == 0) { getCallback().receivedStatus(new OperationStatus(true, "", StatusCode.SUCCESS)); transitionState(OperationState.COMPLETE); } else { super.finishedPayload(pl); } }
case ERR_2BIG: case ERR_INTERNAL: handleError(OperationErrorType.SERVER, new String(errPl)); case ERR_INVAL: case ERR_DELTA_BADVAL:
int available=b.remaining(); toRead=Math.min(toRead, available); getLogger().debug("Reading %d header bytes", toRead); b.get(header, headerOffset, toRead); headerOffset+=toRead; assert cmd == -1 || responseCmd == cmd : "Unexpected response command value"; keyLen=decodeShort(header, 2); errorCode=decodeShort(header, 6); int bytesToRead=decodeInt(header, 8); payload=new byte[bytesToRead]; responseOpaque=decodeInt(header, 12); responseCas=decodeLong(header, 16); assert opaqueIsValid() : "Opaque is not valid"; finishedPayload(EMPTY_BYTES); } else if(payload != null) { int toRead=payload.length - payloadOffset; int available=b.remaining(); toRead=Math.min(toRead, available); getLogger().debug("Reading %d payload bytes", toRead); b.get(payload, payloadOffset, toRead); payloadOffset+=toRead; finishedPayload(payload); getLogger().debug("Only read %d of the %d needed to fill a header",
/** * Validate an opaque value from the header. * This may be overridden from a subclass where the opaque isn't expected * to always be the same as the request opaque. */ protected boolean opaqueIsValid() { if(responseOpaque != opaque) { getLogger().warn("Expected opaque: %d, got opaque: %d\n", responseOpaque, opaque); } return responseOpaque == opaque; }
/** * Read the payload from the buffer. * * @param buffer the buffer to read from. * @throws IOException if an error occures during payload finishing. */ private void readPayloadFromBuffer(final ByteBuffer buffer) throws IOException { int toRead = payload.length - payloadOffset; int available = buffer.remaining(); toRead = Math.min(toRead, available); getLogger().debug("Reading %d payload bytes", toRead); buffer.get(payload, payloadOffset, toRead); payloadOffset += toRead; if (payloadOffset == payload.length) { finishedPayload(payload); } }
/** * Decode the given payload for this command. * * @param pl the payload. */ protected void decodePayload(byte[] pl) { assert pl.length == 0 : "Payload has bytes, but decode isn't overridden"; getCallback().receivedStatus(STATUS_OK); }
@Override protected void finishedPayload(byte[] pl) throws IOException { if (errorCode == SASL_CONTINUE) { getCallback().receivedStatus(new OperationStatus(true, new String(pl))); transitionState(OperationState.COMPLETE); } else if (errorCode == 0) { getCallback().receivedStatus(new OperationStatus(true, "")); transitionState(OperationState.COMPLETE); } else { super.finishedPayload(pl); } }
CASResponse.NOT_FOUND, statusCode); case ERR_INTERNAL: handleError(OperationErrorType.SERVER, new String(errPl)); case ERR_2BIG: case ERR_INVAL:
protected void finishedPayload(byte[] pl) throws IOException { if(errorCode != 0) { OperationStatus status=getStatusForErrorCode(errorCode, pl); if(status == null) { handleError(OperationErrorType.SERVER, new String(pl)); } else { getCallback().receivedStatus(status); transitionState(OperationState.COMPLETE); } } else { decodePayload(pl); transitionState(OperationState.COMPLETE); } }
int available = b.remaining(); toRead = Math.min(toRead, available); getLogger().debug("Reading %d header bytes", toRead); b.get(header, headerOffset, toRead); headerOffset += toRead; assert cmd == -1 || responseCmd == cmd : "Unexpected response command value"; keyLen = decodeShort(header, 2); errorCode = decodeShort(header, 6); int bytesToRead = decodeInt(header, 8); payload = new byte[bytesToRead]; responseOpaque = decodeInt(header, 12); responseCas = decodeLong(header, 16); assert opaqueIsValid() : "Opaque is not valid"; finishedPayload(EMPTY_BYTES); } else if (payload != null) { int toRead = payload.length - payloadOffset; int available = b.remaining(); toRead = Math.min(toRead, available); getLogger().debug("Reading %d payload bytes", toRead); b.get(payload, payloadOffset, toRead); payloadOffset += toRead; finishedPayload(payload); getLogger().debug("Only read %d of the %d needed to fill a header",
/** * Parse the header info out of the buffer. */ private void parseHeaderFromBuffer() { int magic = header[0]; assert magic == RES_MAGIC : "Invalid magic: " + magic; responseCmd = header[1]; assert cmd == DUMMY_OPCODE || responseCmd == cmd : "Unexpected response command value"; keyLen = decodeShort(header, 2); errorCode = decodeShort(header, 6); int bytesToRead = decodeInt(header, 8); payload = new byte[bytesToRead]; responseOpaque = decodeInt(header, 12); responseCas = decodeLong(header, 16); assert opaqueIsValid() : "Opaque is not valid"; }
/** * Read from the incoming {@link ByteBuffer}. * * Reading from the buffer is done in stages, depending on how much data * can be read at once. First, the header is read and then parsed (24 * bytes, indicated by {@link #MIN_RECV_PACKET}). Then, the payload is read * (if one is available for this operation and can be loaded fully). * * @param buffer the buffer to read from. * @throws IOException if an error happened during parsing/reading. */ @Override public void readFromBuffer(final ByteBuffer buffer) throws IOException { if (headerOffset < MIN_RECV_PACKET) { readHeaderFromBuffer(buffer); if (headerOffset == MIN_RECV_PACKET) { parseHeaderFromBuffer(); } } if (headerOffset >= MIN_RECV_PACKET && payload == null) { finishedPayload(EMPTY_BYTES); } else if (payload != null) { readPayloadFromBuffer(buffer); } else { getLogger().debug("Only read %d of the %d needed to fill a header", headerOffset, MIN_RECV_PACKET); } }
/** * Validate an opaque value from the header. This may be overridden from a * subclass where the opaque isn't expected to always be the same as the * request opaque. */ protected boolean opaqueIsValid() { if (responseOpaque != opaque) { getLogger().warn("Expected opaque: %d, got opaque: %d\n", responseOpaque, opaque); } return responseOpaque == opaque; }