I'm having a small issue setting the client id and getting the client
id using the protobuf interface. I'm new to using protobufs so I'm
probably doing something stupid. Below is my code, everything works
up until the last assert. For some reason, the client ids retrieved
from the message are gibberish (at least to me)... Any help would be
appreciated!
public static void test(String host, int port) {
InetSocketAddress address = new InetSocketAddress(host, port);
Socket socket = new Socket();
socket.setSoTimeout(1000);
socket.connect(address, 2000);
CodedOutputStream out =
CodedOutputStream.newInstance(socket.getOutputStream());
CodedInputStream in =
CodedInputStream.newInstance(socket.getInputStream());
//ping
out.writeRawBytes(new byte[] {0, 0, 0, 1, 1});
out.flush();
//pong {0, 0, 0, 1, 2}
int size = parseToInt32(in.readRawBytes(4))-1;
byte msgCode = in.readRawByte();
assert msgCode == 2 : "Error pinging server: " +
getErrorMessage(in, msgCode, size);
System.out.println("Successfully pinged server");
//get server info
out.writeRawBytes(new byte[] {0, 0, 0, 1, 7});
out.flush();
size = parseToInt32(in.readRawBytes(4))-1;
msgCode = in.readRawByte();
assert msgCode == 8 : "Error getting server info: " +
getErrorMessage(in, msgCode, size);
byte[] bytes = in.readRawBytes(size);
RpbGetServerInfoResp serverInfoResp =
RpbGetServerInfoResp.parseFrom(bytes);
System.out.println("Connected to: "
+ serverInfoResp.getNode().toStringUtf8()
+ ", version "
+ serverInfoResp.getServerVersion().toStringUtf8());
//get client id
out.writeRawBytes(new byte[] {0, 0, 0, 1, 3});
out.flush();
size = parseToInt32(in.readRawBytes(4))-1;
msgCode = in.readRawByte();
assert msgCode == 4 : "Error getting client id: " +
getErrorMessage(in, msgCode, size);
bytes = in.readRawBytes(size);
System.out.println("Client id is: " +
RpbGetClientIdResp.parseFrom(bytes).getClientId().toStringUtf8());
String clientId = "client" + hashCode();
RpbSetClientIdReq.Builder reqBuilder = RpbSetClientIdReq.newBuilder();
reqBuilder.setClientId(ByteString.copyFromUtf8(clientId));
assert reqBuilder.isInitialized() : "Set client id request not
fully initialized";
RpbSetClientIdReq req = reqBuilder.build();
size = req.getSerializedSize()+1;
out.writeRawBytes(parseFromInt32(size));
out.writeRawByte(5);
bytes = req.toByteArray();
assert bytes.length == size-1;
out.writeRawBytes(bytes);
out.flush();
size = parseToInt32(in.readRawBytes(4));
msgCode = in.readRawByte();
assert msgCode == 6 : "Error setting client id: " +
getErrorMessage(in, msgCode, size);
//get client id
out.writeRawBytes(new byte[] {0, 0, 0, 1, 3});
out.flush();
size = parseToInt32(in.readRawBytes(4))-1;
msgCode = in.readRawByte();
assert msgCode == 4 : "Error getting client id: " +
getErrorMessage(in, msgCode, size);
bytes = in.readRawBytes(size);
RpbGetClientIdResp clientIdResp = RpbGetClientIdResp.parseFrom(bytes);
String serverClientId =
clientIdResp.getClientId().toString("ASCII");//Utf8();
assert clientId.equals(serverClientId)
: "Requested client id " + clientId + " does not match
server's " + serverClientId;
System.out.println("Set client id to: " + clientId);
}
//support methods - in case you were wondering...
public static int parseToInt32(byte[] bytes) {
return (bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3];
}
public static byte[] parseFromInt32(int value) {
byte[] bytes = new byte[4];
bytes[0] = (byte)(value & 0xFF000000);
bytes[1] = (byte)(value & 0x00FF0000);
bytes[2] = (byte)(value & 0x0000FF00);
bytes[3] = (byte)(value & 0x000000FF);
return bytes;
}
--Matthew
_______________________________________________
riak-users mailing list
[email protected]
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com