package com.datastax.oss.protocol.internal.response.error;
import com.datastax.oss.protocol.internal.Message;
import com.datastax.oss.protocol.internal.PrimitiveCodec;
import com.datastax.oss.protocol.internal.PrimitiveSizes;
import com.datastax.oss.protocol.internal.ProtocolConstants;
import com.datastax.oss.protocol.internal.response.Error;
public class Unavailable extends Error {
public final int consistencyLevel;
public final int required;
public final int alive;
public Unavailable(String message, int consistencyLevel, int required, int alive) {
super(ProtocolConstants.ErrorCode.UNAVAILABLE, message);
this.consistencyLevel = consistencyLevel;
this.required = required;
this.alive = alive;
}
public static class SubCodec extends Error.SubCodec {
public SubCodec(int protocolVersion) {
super(ProtocolConstants.ErrorCode.UNAVAILABLE, protocolVersion);
}
@Override
public <B> void encode(B dest, Message message, PrimitiveCodec<B> encoder) {
Unavailable unavailable = (Unavailable) message;
encoder.writeString(unavailable.message, dest);
encoder.writeUnsignedShort(unavailable.consistencyLevel, dest);
encoder.writeInt(unavailable.required, dest);
encoder.writeInt(unavailable.alive, dest);
}
@Override
public int encodedSize(Message message) {
Unavailable unavailable = (Unavailable) message;
return PrimitiveSizes.sizeOfString(unavailable.message)
+ PrimitiveSizes.SHORT
+ PrimitiveSizes.INT
+ PrimitiveSizes.INT;
}
@Override
public <B> Message decode(B source, PrimitiveCodec<B> decoder) {
String message = decoder.readString(source);
int consistencyLevel = decoder.readUnsignedShort(source);
int required = decoder.readInt(source);
int alive = decoder.readInt(source);
return new Unavailable(message, consistencyLevel, required, alive);
}
}
}