private void serveRequest() throws IOException {
BSONObject reqHead = decoder.readObject(clientSocket.getInputStream());
BSONObject reqBody = decoder.readObject(clientSocket.getInputStream());
BSONObject respHead = new BasicBSONObject();
respHead.put(Constants.SERVICE_METHOD,
((String) reqHead.get(Constants.SERVICE_METHOD)).getBytes());
respHead.put(Constants.SEQ, (UnsignedLong) reqHead.get(Constants.SEQ));
if (!"Arith.Multiply".equals((String) reqHead.get(Constants.SERVICE_METHOD))) {
respHead
.put(Constants.ERROR, ("rpc: can't find method " + (String) reqHead
.get(Constants.SERVICE_METHOD)).getBytes());
}
BSONObject respBody = new BasicBSONObject();
if (!respHead.containsField(Constants.ERROR)) {
long A = 0;
long B = 0;
if (reqBody.containsField("A")) {
A = (long) reqBody.get("A");
}
if (reqBody.containsField("B")) {
B = (long) reqBody.get("B");
}
respBody.put(BsonClientCodec.MAGIC_TAG, A * B);
}
clientSocket.getOutputStream().write(encoder.encode(respHead));
clientSocket.getOutputStream().write(encoder.encode(respBody));
}