/** 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(); } }
List<ByteBuffer> response = transceiver.transceive(byteBufferOutputStream.getBufferList());
out.flush(); if (remote == null) // handshake failed return bbo.getBufferList(); handshake = bbo.getBufferList(); writeError(Protocol.SYSTEM_ERRORS, new Utf8(e.toString()), out); if (null == handshake) { handshake = new ByteBufferOutputStream().getBufferList(); payload = bbo.getBufferList(); bbo.append(payload); return bbo.getBufferList();
/** * 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; } }
@Test public void testReuse() throws IOException { ByteBufferOutputStream bbo1 = new ByteBufferOutputStream(); ByteBufferOutputStream bbo2 = new ByteBufferOutputStream(); byte[] b1 = new byte[] { 1, 2 }; BinaryEncoder e1 = e_factory.binaryEncoder(bbo1, null); e1.writeBytes(b1); e1.flush(); BinaryEncoder e2 = e_factory.binaryEncoder(bbo2, null); e2.writeBytes(b1); e2.flush(); DirectBinaryDecoder d = new DirectBinaryDecoder( new ByteBufferInputStream(bbo1.getBufferList())); ByteBuffer bb1 = d.readBytes(null); Assert.assertEquals(b1.length, bb1.limit() - bb1.position()); d.configure(new ByteBufferInputStream(bbo2.getBufferList())); ByteBuffer bb2 = d.readBytes(null); Assert.assertEquals(b1.length, bb2.limit() - bb2.position()); }
List<ByteBuffer> buffers = os.getBufferList(); for(ByteBuffer buffer : buffers) { length += buffer.limit() - buffer.arrayOffset();
List<ByteBuffer> buffers = os.getBufferList(); for(ByteBuffer buffer : buffers) { length += buffer.limit() - buffer.arrayOffset();
/** 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(); } }
List<ByteBuffer> response = transceiver.transceive(byteBufferOutputStream.getBufferList());
out.flush(); if (remote == null) // handshake failed return bbo.getBufferList(); handshake = bbo.getBufferList(); writeError(Protocol.SYSTEM_ERRORS, new Utf8(e.toString()), out); if (null == handshake) { handshake = new ByteBufferOutputStream().getBufferList(); payload = bbo.getBufferList(); bbo.append(payload); return bbo.getBufferList();
is = new ByteBufferInputStream(os.getBufferList()); dis = new DataInputStream(is);
/** * 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; } }