}
/** Writes a request message and reads a response or error message. */
public Object request(String messageName, Object request)
throws IOException {
Decoder in;
Message m;
RPCContext context = new RPCContext();
do {
ByteBufferOutputStream bbo = new ByteBufferOutputStream();
Encoder out = new BinaryEncoder(bbo);
writeHandshake(out); // prepend handshake
// use local protocol to write request
m = getLocal().getMessages().get(messageName);
if (m == null)
throw new AvroRuntimeException("Not a local message: "+messageName);
for (RPCPlugin plugin : rpcMetaPlugins) {
plugin.clientSendRequest(context);
}
META_WRITER.write(context.requestCallMeta(), out);
out.writeString(m.getName()); // write message name
writeRequest(m.getRequest(), request, out); // write request payload
List<ByteBuffer> response = // transceive
getTransceiver().transceive(bbo.getBufferList());
ByteBufferInputStream bbi = new ByteBufferInputStream(response);
in = new BinaryDecoder(bbi);
} while (!readHandshake(in));
// use remote protocol to read response
m = getRemote().getMessages().get(messageName);
if (m == null)
throw new AvroRuntimeException("Not a remote message: "+messageName);
context.setRequestCallMeta(META_READER.read(null, in));
if (!in.readBoolean()) { // no error
Object response = readResponse(m.getResponse(), in);
context.setResponse(response);
for (RPCPlugin plugin : rpcMetaPlugins) {
plugin.clientReceiveResponse(context);
}