private static void receiveVersionNegotiation(final VersionNegotiator negotiator, final DataInputStream dis, final DataOutputStream dos) throws IOException, HandshakeException { final int version = dis.readInt(); if (negotiator.isVersionSupported(version)) { dos.write(RESOURCE_OK); dos.flush(); negotiator.setVersion(version); } else { final Integer preferred = negotiator.getPreferredVersion(version); if (preferred == null) { dos.write(ABORT); dos.flush(); throw new HandshakeException("Unable to negotiate an acceptable version of the Distributed Cache Protocol"); } dos.write(DIFFERENT_RESOURCE_VERSION); dos.writeInt(preferred); dos.flush(); receiveVersionNegotiation(negotiator, dis, dos); } } }
private static void initiateVersionNegotiation(final VersionNegotiator negotiator, final DataInputStream dis, final DataOutputStream dos) throws IOException, HandshakeException { // Write the classname of the RemoteStreamCodec, followed by its version dos.writeInt(negotiator.getVersion()); dos.flush(); // wait for response from server. final int statusCode = dis.read(); switch (statusCode) { case RESOURCE_OK: // server accepted our proposal of codec name/version return; case DIFFERENT_RESOURCE_VERSION: // server accepted our proposal of codec name but not the version // Get server's preferred version final int newVersion = dis.readInt(); // Determine our new preferred version that is no greater than the server's preferred version. final Integer newPreference = negotiator.getPreferredVersion(newVersion); // If we could not agree with server on a version, fail now. if (newPreference == null) { throw new HandshakeException("Could not agree on protocol version"); } negotiator.setVersion(newPreference); // Attempt negotiation of resource based on our new preferred version. initiateVersionNegotiation(negotiator, dis, dos); return; case ABORT: throw new HandshakeException("Remote destination aborted connection with message: " + dis.readUTF()); default: throw new HandshakeException("Received unexpected response code " + statusCode + " when negotiating version with remote server"); } }
@Override public List<Integer> getSupportedVersions() { return versionNegotiator.getSupportedVersions(); }
public static FlowFileCodec createCodec(final String codecName, final int version) { final FlowFileCodec codec = createCodec(codecName); final VersionNegotiator negotiator = codec.getVersionNegotiator(); if (!negotiator.isVersionSupported(version)) { throw new IllegalArgumentException("FlowFile Codec " + codecName + " does not support version " + version); } negotiator.setVersion(version); return codec; }
if (negotiator.isVersionSupported(requestedVersion)) { protocolVersion = requestedVersion; phase = TransactionPhase.SEND_CONNECTION_ID; final Integer preferred = negotiator.getPreferredVersion(requestedVersion); if (preferred == null) { logger.debug("Peer {} requested version {} of the Load Balance Protocol. This version is not acceptable. Aborting communications.", peerDescription, requestedVersion);
@Override public String toString() { return "Standard FlowFile Codec, Version " + versionNegotiator.getVersion(); }
public static boolean isCodecSupported(final String codecName, final int version) { if (!isCodecSupported(codecName)) { return false; } final FlowFileCodec codec = createCodec(codecName); final VersionNegotiator negotiator = codec.getVersionNegotiator(); return (negotiator.isVersionSupported(version)); }
final boolean supported = negotiator.isVersionSupported(requestedVersion); if (supported) { logger.debug("Peer {} requested version {} of the Load Balance Protocol. Accepting version.", peerDescription, requestedVersion); final Integer preferredVersion = negotiator.getPreferredVersion(requestedVersion); if (preferredVersion == null) { logger.debug("Peer {} requested version {} of the Load Balance Protocol. This version is not acceptable. Aborting communications.", peerDescription, requestedVersion);
@Override public Transaction startTransaction(final Peer peer, final FlowFileCodec codec, final TransferDirection direction) throws IOException, ProtocolException { if (!handshakeComplete) { throw new IllegalStateException("Handshake has not been performed"); } if (!readyForFileTransfer) { throw new IllegalStateException("Cannot start transaction; handshake resolution was " + handshakeResponse); } return new SocketClientTransaction(versionNegotiator.getVersion(), destination.getIdentifier(), peer, codec, direction, useCompression, (int) destination.getYieldPeriod(TimeUnit.MILLISECONDS), eventReporter); }
public static FlowFileCodec createCodec(final String codecName, final int version) { final FlowFileCodec codec = createCodec(codecName); final VersionNegotiator negotiator = codec.getVersionNegotiator(); if (!negotiator.isVersionSupported(version)) { throw new IllegalArgumentException("FlowFile Codec " + codecName + " does not support version " + version); } negotiator.setVersion(version); return codec; }
public static boolean isCodecSupported(final String codecName, final int version) { if (!isCodecSupported(codecName)) { return false; } final FlowFileCodec codec = createCodec(codecName); final VersionNegotiator negotiator = codec.getVersionNegotiator(); return (negotiator.isVersionSupported(version)); }
@SuppressWarnings("unchecked") public static <T extends ServerProtocol> T receiveServerProtocolNegotiation(final DataInputStream dis, final DataOutputStream dos) throws IOException, HandshakeException { final String protocolName = dis.readUTF(); final int version = dis.readInt(); final T protocol = (T) RemoteResourceManager.createServerProtocol(protocolName); final VersionNegotiator negotiator = protocol.getVersionNegotiator(); if (negotiator.isVersionSupported(version)) { dos.write(RESOURCE_OK); dos.flush(); negotiator.setVersion(version); return protocol; } else { final Integer preferred = negotiator.getPreferredVersion(version); if (preferred == null) { dos.write(ABORT); dos.flush(); throw new HandshakeException("Unable to negotiate an acceptable version of the ServerProtocol " + protocolName); } dos.write(DIFFERENT_RESOURCE_VERSION); dos.writeInt(preferred); dos.flush(); return receiveServerProtocolNegotiation(dis, dos); } }
dos.writeUTF(resource.getResourceName()); final VersionNegotiator negotiator = resource.getVersionNegotiator(); dos.writeInt(negotiator.getVersion()); dos.flush(); final Integer newPreference = negotiator.getPreferredVersion(newVersion); negotiator.setVersion(newPreference);
protected Integer negotiateTransportProtocolVersion(final HttpServletRequest req, final VersionNegotiator transportProtocolVersionNegotiator) throws BadRequestException { String protocolVersionStr = req.getHeader(HttpHeaders.PROTOCOL_VERSION); if (isEmpty(protocolVersionStr)) { throw new BadRequestException("Protocol version was not specified."); } final Integer requestedProtocolVersion; try { requestedProtocolVersion = Integer.valueOf(protocolVersionStr); } catch (NumberFormatException e) { throw new BadRequestException("Specified protocol version was not in a valid number format: " + protocolVersionStr); } Integer protocolVersion; if (transportProtocolVersionNegotiator.isVersionSupported(requestedProtocolVersion)) { return requestedProtocolVersion; } else { protocolVersion = transportProtocolVersionNegotiator.getPreferredVersion(requestedProtocolVersion); } if (protocolVersion == null) { throw new BadRequestException("Specified protocol version is not supported: " + protocolVersionStr); } return protocolVersion; }
@Override public Set<PeerStatus> getPeerStatuses(final Peer peer) throws IOException { if (!handshakeComplete) { throw new IllegalStateException("Handshake has not been performed"); } logger.debug("{} Get Peer Statuses from {}", this, peer); final CommunicationsSession commsSession = peer.getCommunicationsSession(); final DataInputStream dis = new DataInputStream(commsSession.getInput().getInputStream()); final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream()); final boolean queryPeersForOtherPeers = getVersionNegotiator().getVersion() >= 6; RequestType.REQUEST_PEER_LIST.writeRequestType(dos); dos.flush(); final int numPeers = dis.readInt(); final Set<PeerStatus> peers = new HashSet<>(numPeers); for (int i = 0; i < numPeers; i++) { final String hostname = dis.readUTF(); final int port = dis.readInt(); final boolean secure = dis.readBoolean(); final int flowFileCount = dis.readInt(); peers.add(new PeerStatus(new PeerDescription(hostname, port, secure), flowFileCount, queryPeersForOtherPeers)); } logger.debug("{} Received {} Peer Statuses from {}", this, peers.size(), peer); return peers; }
@SuppressWarnings("unchecked") public static <T extends FlowFileCodec> T receiveCodecNegotiation(final DataInputStream dis, final DataOutputStream dos) throws IOException, HandshakeException { final String codecName = dis.readUTF(); final int version = dis.readInt(); final T codec = (T) RemoteResourceManager.createCodec(codecName, version); final VersionNegotiator negotiator = codec.getVersionNegotiator(); if (negotiator.isVersionSupported(version)) { dos.write(RESOURCE_OK); dos.flush(); negotiator.setVersion(version); return codec; } else { final Integer preferred = negotiator.getPreferredVersion(version); if (preferred == null) { dos.write(ABORT); dos.flush(); throw new HandshakeException("Unable to negotiate an acceptable version of the FlowFileCodec " + codecName); } dos.write(DIFFERENT_RESOURCE_VERSION); dos.writeInt(preferred); dos.flush(); return receiveCodecNegotiation(dis, dos); } }
if (versionNegotiator.getVersion() >= 5) { if (batchCount > 0) { properties.put(HandshakeProperty.BATCH_COUNT, String.valueOf(batchCount)); if (versionNegotiator.getVersion() >= 3) { dos.writeUTF(peer.getUrl()); transitUriPrefix = peer.getUrl();
@SuppressWarnings("unchecked") public static <T extends ClientProtocol> T receiveClientProtocolNegotiation(final DataInputStream dis, final DataOutputStream dos) throws IOException, HandshakeException { final String protocolName = dis.readUTF(); final int version = dis.readInt(); final T protocol = (T) RemoteResourceManager.createClientProtocol(protocolName); final VersionNegotiator negotiator = protocol.getVersionNegotiator(); if (negotiator.isVersionSupported(version)) { dos.write(RESOURCE_OK); dos.flush(); negotiator.setVersion(version); return protocol; } else { final Integer preferred = negotiator.getPreferredVersion(version); if (preferred == null) { dos.write(ABORT); dos.flush(); throw new HandshakeException("Unable to negotiate an acceptable version of the ClientProtocol " + protocolName); } dos.write(DIFFERENT_RESOURCE_VERSION); dos.writeInt(preferred); dos.flush(); return receiveClientProtocolNegotiation(dis, dos); } }
private CommsSession leaseCommsSession() throws IOException { CommsSession session = queue.poll(); if (session != null && !session.isClosed()) { return session; } session = createCommsSession(configContext); final VersionNegotiator versionNegotiator = new StandardVersionNegotiator(1); try { ProtocolHandshake.initiateHandshake(session.getInputStream(), session.getOutputStream(), versionNegotiator); session.setProtocolVersion(versionNegotiator.getVersion()); } catch (final HandshakeException e) { IOUtils.closeQuietly(session); throw new IOException(e); } return session; }
if (negotiator.isVersionSupported(version)) { dos.write(RESOURCE_OK); dos.flush(); negotiator.setVersion(version); return resource; } else { final Integer preferred = negotiator.getPreferredVersion(version); if (preferred == null) { dos.write(ABORT);