public void close() { if (clientTransceiver != null) try { clientTransceiver.close(); } catch (IOException e) {} // ignore if (subprocess != null) subprocess.destroy(); if (outputServer != null) outputServer.close(); }
List<ByteBuffer> response = transceiver.transceive(byteBufferOutputStream.getBufferList()); transceiver.close();
/** 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(); } }
/** Called by {@link Requestor#request(String,Object)} for two-way messages. * By default calls {@link #writeBuffers(List)} followed by * {@link #readBuffers()}. */ public List<ByteBuffer> transceive(List<ByteBuffer> request) throws IOException { lockChannel(); try { writeBuffers(request); return readBuffers(); } finally { unlockChannel(); } }
public void run() { while (true) { try { transceiver.writeBuffers(responder.respond(transceiver.readBuffers())); } catch (ClosedChannelException e) { return; } catch (IOException e) { LOG.warn("unexpected error", e); throw new RuntimeException(e); } } }
public void run() { try { try { this.xc = getTransceiver(channel); while (true) { xc.writeBuffers(responder.respond(xc.readBuffers(), xc)); } } catch (EOFException e) { return; } catch (ClosedChannelException e) { return; } finally { xc.close(); } } catch (IOException e) { LOG.warn("unexpected error", e); } }
throws Exception { Transceiver t = getTransceiver(); if (!t.isConnected()) { if (t.isConnected()) { } else { CallFuture<T> callFuture = new CallFuture<>(callback); t.transceive(request.getBytes(), new TransceiverCallback<>(request, callFuture)); t.lockChannel(); try { t.writeBuffers(request.getBytes()); if (callback != null) { callback.handleResult(null); t.unlockChannel(); t.transceive(request.getBytes(), new TransceiverCallback<>(request, callback));
if (m.isOneWay() && t.isConnected()) { // send one-way message t.writeBuffers(requestBytes); List<ByteBuffer> response = t.transceive(requestBytes); ByteBufferInputStream bbi = new ByteBufferInputStream(response); in = DecoderFactory.defaultFactory().createBinaryDecoder(bbi, in); throw new AvroRuntimeException("Not both one-way messages: "+messageName); if (m.isOneWay() && t.isConnected()) return null; // one-way w/ handshake
/** * Called by {@link Requestor#request(String,Object,Callback)} for two-way messages using callbacks. */ public void transceive(List<ByteBuffer> request, Callback<List<ByteBuffer>> callback) throws IOException { // The default implementation works synchronously try { List<ByteBuffer> response = transceive(request); callback.handleResult(response); } catch (IOException e) { callback.handleError(e); } }
private Protocol handshake(Decoder in, Encoder out, Transceiver connection) throws IOException { if (connection != null && connection.isConnected()) return connection.getRemote(); HandshakeRequest request = (HandshakeRequest)handshakeReader.read(null, in); Protocol remote = protocols.get(request.clientHash); connection.setRemote(remote);
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); }
private void setRemote(HandshakeResponse handshake) throws IOException { remote = Protocol.parse(handshake.serverProtocol.toString()); MD5 remoteHash = (MD5)handshake.serverHash; REMOTE_HASHES.put(transceiver.getRemoteName(), remoteHash); REMOTE_PROTOCOLS.putIfAbsent(remoteHash, remote); }
private boolean readHandshake(Decoder in) throws IOException { if (getTransceiver().isConnected()) return true; boolean established = false; HandshakeResponse handshake = HANDSHAKE_READER.read(null, in); getTransceiver().setRemote(remote); return established;
if ((lm.isOneWay() != rm.isOneWay()) && t.isConnected()) throw new AvroRuntimeException ("Not both one-way messages: "+request.getMessageName()); if (lm.isOneWay() && t.isConnected()) return null; // one-way w/ handshake
public void run() { try { try { this.xc = getTransceiver(channel); while (true) { xc.writeBuffers(responder.respond(xc.readBuffers(), xc)); } } catch (EOFException e) { return; } catch (ClosedChannelException e) { return; } finally { xc.close(); } } catch (IOException e) { LOG.warn("unexpected error", e); } }
throws Exception { Transceiver t = getTransceiver(); if (!t.isConnected()) { if (t.isConnected()) { } else { CallFuture<T> callFuture = new CallFuture<T>(callback); t.transceive(request.getBytes(), new TransceiverCallback<T>(request, callFuture)); t.lockChannel(); try { t.writeBuffers(request.getBytes()); if (callback != null) { callback.handleResult(null); t.unlockChannel(); t.transceive(request.getBytes(), new TransceiverCallback<T>(request, callback));
/** Called by {@link Requestor#request(String,Object)} for two-way messages. * By default calls {@link #writeBuffers(List)} followed by * {@link #readBuffers()}. */ public synchronized List<ByteBuffer> transceive(List<ByteBuffer> request) throws IOException { writeBuffers(request); return readBuffers(); }
/** Called by {@link Requestor#request(String,Object)} for two-way messages. * By default calls {@link #writeBuffers(List)} followed by * {@link #readBuffers()}. */ public List<ByteBuffer> transceive(List<ByteBuffer> request) throws IOException { lockChannel(); try { writeBuffers(request); return readBuffers(); } finally { unlockChannel(); } }
@Override public synchronized List<ByteBuffer> transceive(List<ByteBuffer> request) throws IOException { if (saslResponsePiggybacked) { // still need to read response saslResponsePiggybacked = false; Status status = readStatus(); ByteBuffer frame = readFrame(); switch (status) { case COMPLETE: break; case FAIL: throw new SaslException("Fail: "+toString(frame)); default: throw new IOException("Unexpected SASL status: "+status); } } return super.transceive(request); }
private Protocol handshake(Decoder in, Encoder out, Transceiver connection) throws IOException { if (connection != null && connection.isConnected()) return connection.getRemote(); HandshakeRequest request = (HandshakeRequest)handshakeReader.read(null, in); Protocol remote = protocols.get(request.clientHash); connection.setRemote(remote);