/** * Gets the Message associated with this request. * @return this request's message. */ public Message getMessage() { if (message == null) { message = getLocal().getMessages().get(messageName); if (message == null) { throw new AvroRuntimeException("Not a local message: "+messageName); } } return message; }
/** Return the remote protocol for a proxy. */ public static Protocol getRemote(Object proxy) throws IOException { return ((Requestor)Proxy.getInvocationHandler(proxy)).getRemote(); }
/** Return the remote protocol. Force a handshake if required. */ public Protocol getRemote() throws IOException { if (remote != null) return remote; // already have it MD5 remoteHash = REMOTE_HASHES.get(transceiver.getRemoteName()); if (remoteHash != null) { remote = REMOTE_PROTOCOLS.get(remoteHash); if (remote != null) return remote; // already cached } handshakeLock.lock(); try { // force handshake ByteBufferOutputStream bbo = new ByteBufferOutputStream(); // direct because the payload is tiny. Encoder out = ENCODER_FACTORY.directBinaryEncoder(bbo, null); writeHandshake(out); out.writeInt(0); // empty metadata out.writeString(""); // bogus message name List<ByteBuffer> response = getTransceiver().transceive(bbo.getBufferList()); ByteBufferInputStream bbi = new ByteBufferInputStream(response); BinaryDecoder in = DecoderFactory.get().binaryDecoder(bbi, null); readHandshake(in); return this.remote; } finally { handshakeLock.unlock(); } }
private boolean readHandshake(Decoder in) throws IOException { if (getTransceiver().isConnected()) return true; boolean established = false; HandshakeResponse handshake = HANDSHAKE_READER.read(null, in); case CLIENT: LOG.debug("Handshake match = CLIENT"); setRemote(handshake); established = true; sendLocalText = false; case NONE: LOG.debug("Handshake match = NONE"); setRemote(handshake); sendLocalText = true; break; getTransceiver().setRemote(remote); return established;
Encoder out = new BinaryEncoder(bbo); writeHandshake(out); // prepend handshake m = getLocal().getMessages().get(messageName); if (m == null) throw new AvroRuntimeException("Not a local message: "+messageName); writeRequest(m.getRequest(), request, out); // write request payload getTransceiver().transceive(bbo.getBufferList()); } while (!readHandshake(in)); m = getRemote().getMessages().get(messageName); if (m == null) throw new AvroRuntimeException("Not a remote message: "+messageName); Object response = readResponse(m.getResponse(), in); context.setResponse(response); for (RPCPlugin plugin : rpcMetaPlugins) { Exception error = readError(m.getErrors(), in); context.setError(error); for (RPCPlugin plugin : rpcMetaPlugins) {
("Not a remote message: "+request.getMessageName()); Transceiver t = getTransceiver(); if ((lm.isOneWay() != rm.isOneWay()) && t.isConnected()) throw new AvroRuntimeException Object response = readResponse(rm.getResponse(), lm.getResponse(), in); context.setResponse(response); for (RPCPlugin plugin : rpcMetaPlugins) { Exception error = readError(rm.getErrors(), lm.getErrors(), in); context.setError(error); for (RPCPlugin plugin : rpcMetaPlugins) {
@Override public Object request(String messageName, Object request) throws IOException { try { return super.request(messageName, request); } catch (Exception e) { if (e instanceof RuntimeException) throw (RuntimeException)e; if (e instanceof IOException) throw (IOException)e; throw new AvroRemoteException(e); } }
if (handler instanceof Requestor) { try { remote = ((Requestor) handler).getTransceiver().getRemoteName(); } catch (IOException e) {
/** Reads a response message. */ public abstract Object readResponse(Schema writer, Schema reader, Decoder in) throws IOException;
/** Reads an error message. */ public abstract Exception readError(Schema writer, Schema reader, Decoder in) throws IOException;
/** * Gets the request data, generating it first if necessary. * @return the request data. * @throws Exception if an error occurs generating the request data. */ public List<ByteBuffer> getBytes() throws Exception { if (requestBytes == null) { ByteBufferOutputStream bbo = new ByteBufferOutputStream(); BinaryEncoder out = ENCODER_FACTORY.binaryEncoder(bbo, encoder); // use local protocol to write request Message m = getMessage(); context.setMessage(m); writeRequest(m.getRequest(), request, out); // write request payload out.flush(); List<ByteBuffer> payload = bbo.getBufferList(); writeHandshake(out); // prepend handshake if needed context.setRequestPayload(payload); for (RPCPlugin plugin : rpcMetaPlugins) { plugin.clientSendRequest(context); // get meta-data from plugins } META_WRITER.write(context.requestCallMeta(), out); out.writeString(m.getName()); // write message name out.flush(); bbo.append(payload); requestBytes = bbo.getBufferList(); } return requestBytes; } }
Transceiver t = getTransceiver(); BinaryDecoder in = null; Message m; m = getLocal().getMessages().get(messageName); if (m == null) throw new AvroRuntimeException("Not a local message: "+messageName); context.setMessage(m); writeRequest(m.getRequest(), request, out); // write request payload List<ByteBuffer> payload = bbo.getBufferList(); writeHandshake(out); // prepend handshake if needed META_WRITER.write(context.requestCallMeta(), out); } while (!readHandshake(in)); Message rm = getRemote().getMessages().get(messageName); if (rm == null) throw new AvroRuntimeException("Not a remote message: "+messageName); Object response = readResponse(rm.getResponse(), in); context.setResponse(response); for (RPCPlugin plugin : rpcMetaPlugins) { Exception error = readError(rm.getErrors(), in); context.setError(error); for (RPCPlugin plugin : rpcMetaPlugins) {
("Not a remote message: "+request.getMessageName()); Transceiver t = getTransceiver(); if ((lm.isOneWay() != rm.isOneWay()) && t.isConnected()) throw new AvroRuntimeException Object response = readResponse(rm.getResponse(), lm.getResponse(), in); context.setResponse(response); for (RPCPlugin plugin : rpcMetaPlugins) { Exception error = readError(rm.getErrors(), lm.getErrors(), in); context.setError(error); for (RPCPlugin plugin : rpcMetaPlugins) {
private boolean readHandshake(Decoder in) throws IOException { if (getTransceiver().isConnected()) return true; boolean established = false; HandshakeResponse handshake = HANDSHAKE_READER.read(null, in); case CLIENT: LOG.debug("Handshake match = CLIENT"); setRemote(handshake); established = true; break; case NONE: LOG.debug("Handshake match = NONE"); setRemote(handshake); sendLocalText = true; break; getTransceiver().setRemote(remote); return established;
@Override public <T> void request(String messageName, Object request, Callback<T> callback) throws IOException { try { super.request(messageName, request, callback); } catch (Exception e) { if (e instanceof RuntimeException) throw (RuntimeException)e; if (e instanceof IOException) throw (IOException)e; throw new AvroRemoteException(e); } }
getTransceiver().transceive (handshake.getBytes(), new TransceiverCallback<>(handshake, callback));
/** Reads a response message. */ public abstract Object readResponse(Schema writer, Schema reader, Decoder in) throws IOException;
/** Reads an error message. */ public abstract Exception readError(Schema writer, Schema reader, Decoder in) throws IOException;
/** * Gets the request data, generating it first if necessary. * @return the request data. * @throws Exception if an error occurs generating the request data. */ public List<ByteBuffer> getBytes() throws Exception { if (requestBytes == null) { ByteBufferOutputStream bbo = new ByteBufferOutputStream(); BinaryEncoder out = ENCODER_FACTORY.binaryEncoder(bbo, encoder); // use local protocol to write request Message m = getMessage(); context.setMessage(m); writeRequest(m.getRequest(), request, out); // write request payload out.flush(); List<ByteBuffer> payload = bbo.getBufferList(); writeHandshake(out); // prepend handshake if needed context.setRequestPayload(payload); for (RPCPlugin plugin : rpcMetaPlugins) { plugin.clientSendRequest(context); // get meta-data from plugins } META_WRITER.write(context.requestCallMeta(), out); out.writeString(m.getName()); // write message name out.flush(); bbo.append(payload); requestBytes = bbo.getBufferList(); } return requestBytes; } }