BinaryEncoder out = EncoderFactory.get().binaryEncoder(bbo, null); Exception error = null; RPCContext context = new RPCContext(); List<ByteBuffer> payload = null; List<ByteBuffer> handshake = null; context.setRequestCallMeta(META_READER.read(null, in)); String messageName = in.readString(null).toString(); if (messageName.equals("")) // a handshake ping context.setMessage(rm); for (RPCPlugin plugin : rpcMetaPlugins) { plugin.serverReceiveRequest(context); REMOTE.set(remote); response = respond(m, request); context.setResponse(response); } catch (Exception e) { error = e; context.setError(error); LOG.warn("user error", e); } finally { context.setError(e); bbo = new ByteBufferOutputStream(); out = EncoderFactory.get().binaryEncoder(bbo, null); context.setResponsePayload(payload); for (RPCPlugin plugin : rpcMetaPlugins) {
@Override public void serverReceiveRequest(RPCContext context) { Stopwatch t = new Stopwatch(ticks); t.start(); this.activeRpcs.put(context, t); synchronized(receivePayloads) { IntegerHistogram<?> h = receivePayloads.get(context.getMessage()); if (h == null) { h = createNewIntegerHistogram(); receivePayloads.put(context.getMessage(), h); } h.add(getPayloadSize(context.getRequestPayload())); } }
@Override public void clientSendRequest(RPCContext context) { ByteBuffer buf = ByteBuffer.wrap("ap".getBytes()); context.requestCallMeta().put(key, buf); Assert.assertNotNull(context.getMessage()); Assert.assertNotNull(context.getRequestPayload()); Assert.assertNull(context.getResponsePayload()); }
private RPCContext makeContext() { RPCContext context = new RPCContext(); context.setMessage(message); return context; }
@Override public void serverSendResponse(RPCContext context) { Stopwatch t = this.activeRpcs.remove(context); t.stop(); publish(context, t); synchronized(sendPayloads) { IntegerHistogram<?> h = sendPayloads.get(context.getMessage()); if (h == null) { h = createNewIntegerHistogram(); sendPayloads.put(context.getMessage(), h); } h.add(getPayloadSize(context.getResponsePayload())); } }
BinaryDecoder in = null; Message m; RPCContext context = new RPCContext(); do { ByteBufferOutputStream bbo = new ByteBufferOutputStream(); if (m == null) throw new AvroRuntimeException("Not a local message: "+messageName); context.setMessage(m); META_WRITER.write(context.requestCallMeta(), out); if (m == null) throw new AvroRuntimeException("Not a remote message: "+messageName); context.setRequestCallMeta(META_READER.read(null, in)); context.setResponse(response); for (RPCPlugin plugin : rpcMetaPlugins) { plugin.clientReceiveResponse(context); context.setError(error); for (RPCPlugin plugin : rpcMetaPlugins) { plugin.clientReceiveResponse(context);
@Override public void serverConnecting(RPCContext context) { Assert.assertNotNull(context.requestHandshakeMeta()); Assert.assertNotNull(context.responseHandshakeMeta()); Assert.assertNull(context.getRequestPayload()); Assert.assertNull(context.getResponsePayload()); if (!context.requestHandshakeMeta().containsKey(key)) return; ByteBuffer buf = context.requestHandshakeMeta().get(key); Assert.assertNotNull(buf); Assert.assertNotNull(buf.array()); String partialstr = new String(buf.array()); Assert.assertNotNull(partialstr); Assert.assertEquals("partial string mismatch", "ap", partialstr); buf = ByteBuffer.wrap((partialstr + "ac").getBytes()); Assert.assertTrue(buf.remaining() > 0); context.responseHandshakeMeta().put(key, buf); }
/** * 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; } }
@Override public void serverSendResponse(RPCContext context) { Assert.assertNotNull(context.requestCallMeta()); Assert.assertNotNull(context.responseCallMeta()); Assert.assertNotNull(context.getResponsePayload()); if (!context.requestCallMeta().containsKey(key)) return; ByteBuffer buf = context.requestCallMeta().get(key); Assert.assertNotNull(buf); Assert.assertNotNull(buf.array()); String partialstr = new String(buf.array()); Assert.assertNotNull(partialstr); Assert.assertEquals("partial string mismatch", "apa", partialstr); buf = ByteBuffer.wrap((partialstr + "c").getBytes()); Assert.assertTrue(buf.remaining() > 0); context.responseCallMeta().put(key, buf); }
@Override public void serverReceiveRequest(RPCContext context) { Map<String,ByteBuffer> meta = context.requestCallMeta(); Assert.assertNotNull(meta); Assert.assertNotNull(context.getMessage()); Assert.assertNull(context.getResponsePayload()); if (!meta.containsKey(key)) return; ByteBuffer buf = meta.get(key); Assert.assertNotNull(buf); Assert.assertNotNull(buf.array()); String partialstr = new String(buf.array()); Assert.assertNotNull(partialstr); Assert.assertEquals("partial string mismatch", "ap", partialstr); buf = ByteBuffer.wrap((partialstr + "a").getBytes()); Assert.assertTrue(buf.remaining() > 0); meta.put(key, buf); }
private void writeHandshake(Encoder out) throws IOException { if (getTransceiver().isConnected()) return; MD5 localHash = new MD5(); localHash.bytes(local.getMD5()); String remoteName = transceiver.getRemoteName(); MD5 remoteHash = REMOTE_HASHES.get(remoteName); if (remoteHash == null) { // guess remote is local remoteHash = localHash; remote = local; } else { remote = REMOTE_PROTOCOLS.get(remoteHash); } HandshakeRequest handshake = new HandshakeRequest(); handshake.clientHash = localHash; handshake.serverHash = remoteHash; if (sendLocalText) handshake.clientProtocol = local.toString(); RPCContext context = new RPCContext(); context.setHandshakeRequest(handshake); for (RPCPlugin plugin : rpcMetaPlugins) { plugin.clientStartConnect(context); } handshake.meta = context.requestHandshakeMeta(); HANDSHAKE_WRITER.write(handshake, out); }
RPCContext context = new RPCContext(); context.setRequestHandshakeMeta((Map<Utf8, ByteBuffer>) request.meta); response.meta = context.responseHandshakeMeta();
/** Adds timing to the histograms. */ private void publish(RPCContext context, Stopwatch t) { Message message = context.getMessage(); if (message == null) throw new IllegalArgumentException(); synchronized(methodTimings) { FloatHistogram<?> h = methodTimings.get(context.getMessage()); if (h == null) { h = createNewFloatHistogram(); methodTimings.put(context.getMessage(), h); } h.add(nanosToMillis(t.elapsedNanos())); } }
private void writeHandshake(Encoder out) throws IOException { MD5 localHash = new MD5(); localHash.bytes(local.getMD5()); String remoteName = transceiver.getRemoteName(); MD5 remoteHash = REMOTE_HASHES.get(remoteName); remote = REMOTE_PROTOCOLS.get(remoteHash); if (remoteHash == null) { // guess remote is local remoteHash = localHash; remote = local; } HandshakeRequest handshake = new HandshakeRequest(); handshake.clientHash = localHash; handshake.serverHash = remoteHash; if (sendLocalText) handshake.clientProtocol = new Utf8(local.toString()); RPCContext context = new RPCContext(); for (RPCPlugin plugin : rpcMetaPlugins) { plugin.clientStartConnect(context); } handshake.meta = context.requestHandshakeMeta(); HANDSHAKE_WRITER.write(handshake, out); }
@Override public void clientFinishConnect(RPCContext context) { Map<String,ByteBuffer> handshakeMeta = context.responseHandshakeMeta(); Assert.assertNull(context.getRequestPayload()); Assert.assertNull(context.getResponsePayload()); Assert.assertNotNull(handshakeMeta); if (!handshakeMeta.containsKey(key)) return; ByteBuffer buf = handshakeMeta.get(key); Assert.assertNotNull(buf); Assert.assertNotNull(buf.array()); String partialstr = new String(buf.array()); Assert.assertNotNull(partialstr); Assert.assertEquals("partial string mismatch", "apac", partialstr); buf = ByteBuffer.wrap((partialstr + "he").getBytes()); Assert.assertTrue(buf.remaining() > 0); handshakeMeta.put(key, buf); checkRPCMetaMap(handshakeMeta); }
context.setResponseCallMeta(META_READER.read(null, in)); context.setResponse(response); for (RPCPlugin plugin : rpcMetaPlugins) { plugin.clientReceiveResponse(context); context.setError(error); for (RPCPlugin plugin : rpcMetaPlugins) { plugin.clientReceiveResponse(context);
@Override public void clientReceiveResponse(RPCContext context) { Assert.assertNotNull(context.responseCallMeta()); Assert.assertNotNull(context.getRequestPayload()); if (!context.responseCallMeta().containsKey(key)) return; ByteBuffer buf = context.responseCallMeta().get(key); Assert.assertNotNull(buf); Assert.assertNotNull(buf.array()); String partialstr = new String(buf.array()); Assert.assertNotNull(partialstr); Assert.assertEquals("partial string mismatch", "apac", partialstr); buf = ByteBuffer.wrap((partialstr + "he").getBytes()); Assert.assertTrue(buf.remaining() > 0); context.responseCallMeta().put(key, buf); checkRPCMetaMap(context.responseCallMeta()); }
RPCContext context = new RPCContext(); context.setHandshakeRequest(request); context.setHandshakeResponse(response); for (RPCPlugin plugin : rpcMetaPlugins) { plugin.serverConnecting(context);
RPCContext context = new RPCContext(); if (handshake.meta != null) { context.setResponseHandshakeMeta((Map<Utf8, ByteBuffer>) handshake.meta);
/** * Writes a request message and returns the result through a Callback. * Clients can also use a Future interface by creating a new CallFuture<T>, * passing it in as the Callback parameter, and then waiting on that Future. * @param <T> the return type of the message. * @param messageName the name of the message to invoke. * @param request the request data to send. * @param callback the callback which will be invoked when the response is returned * or an error occurs. * @throws Exception if an error occurs sending the message. */ public <T> void request(String messageName, Object request, Callback<T> callback) throws Exception { request(new Request(messageName, request, new RPCContext()), callback); }