Search in sources :

Example 1 with NodeConnectionStatusResponseMessage

use of org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage in project nifi by apache.

the class NodeClusterCoordinator method handleNodeConnectionStatusRequest.

private NodeConnectionStatusResponseMessage handleNodeConnectionStatusRequest() {
    final NodeConnectionStatusResponseMessage msg = new NodeConnectionStatusResponseMessage();
    final NodeIdentifier self = getLocalNodeIdentifier();
    if (self != null) {
        final NodeConnectionStatus connectionStatus = nodeStatuses.get(self);
        msg.setNodeConnectionStatus(connectionStatus);
    }
    return msg;
}
Also used : NodeConnectionStatusResponseMessage(org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier)

Example 2 with NodeConnectionStatusResponseMessage

use of org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage in project nifi by apache.

the class TestJaxbProtocolUtils method testRoundTripConnectionStatusResponse.

@Test
public void testRoundTripConnectionStatusResponse() throws JAXBException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final NodeConnectionStatusResponseMessage msg = new NodeConnectionStatusResponseMessage();
    final NodeIdentifier nodeId = new NodeIdentifier("id", "localhost", 8000, "localhost", 8001, "localhost", 8002, 8003, true);
    final NodeConnectionStatus nodeStatus = new NodeConnectionStatus(nodeId, DisconnectionCode.NOT_YET_CONNECTED);
    msg.setNodeConnectionStatus(nodeStatus);
    JaxbProtocolUtils.JAXB_CONTEXT.createMarshaller().marshal(msg, baos);
    final Object unmarshalled = JaxbProtocolUtils.JAXB_CONTEXT.createUnmarshaller().unmarshal(new ByteArrayInputStream(baos.toByteArray()));
    assertTrue(unmarshalled instanceof NodeConnectionStatusResponseMessage);
    final NodeConnectionStatusResponseMessage unmarshalledMsg = (NodeConnectionStatusResponseMessage) unmarshalled;
    final NodeConnectionStatus unmarshalledStatus = unmarshalledMsg.getNodeConnectionStatus();
    assertEquals(nodeStatus, unmarshalledStatus);
}
Also used : NodeConnectionStatusResponseMessage(org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus) Test(org.junit.Test)

Example 3 with NodeConnectionStatusResponseMessage

use of org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage in project nifi by apache.

the class StandardClusterCoordinationProtocolSender method requestNodeConnectionStatus.

@Override
public NodeConnectionStatus requestNodeConnectionStatus(final String hostname, final int port) {
    Objects.requireNonNull(hostname);
    final NodeConnectionStatusRequestMessage msg = new NodeConnectionStatusRequestMessage();
    final byte[] msgBytes;
    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        final ProtocolMessageMarshaller<ProtocolMessage> marshaller = protocolContext.createMarshaller();
        marshaller.marshal(msg, baos);
        msgBytes = baos.toByteArray();
    } catch (final IOException e) {
        throw new ProtocolException("Failed to marshal NodeIdentifierRequestMessage", e);
    }
    try (final Socket socket = createSocket(hostname, port, true)) {
        // marshal message to output stream
        socket.getOutputStream().write(msgBytes);
        final ProtocolMessage response;
        try {
            // unmarshall response and return
            final ProtocolMessageUnmarshaller<ProtocolMessage> unmarshaller = protocolContext.createUnmarshaller();
            response = unmarshaller.unmarshal(socket.getInputStream());
        } catch (final IOException ioe) {
            throw new ProtocolException("Failed unmarshalling '" + MessageType.RECONNECTION_RESPONSE + "' protocol message due to: " + ioe, ioe);
        }
        if (MessageType.NODE_CONNECTION_STATUS_RESPONSE == response.getType()) {
            return ((NodeConnectionStatusResponseMessage) response).getNodeConnectionStatus();
        } else {
            throw new ProtocolException("Expected message type '" + MessageType.NODE_CONNECTION_STATUS_RESPONSE + "' but found '" + response.getType() + "'");
        }
    } catch (final IOException ioe) {
        throw new ProtocolException("Failed to request Node Identifer from " + hostname + ":" + port, ioe);
    }
}
Also used : ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) NodeConnectionStatusResponseMessage(org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NodeConnectionStatusRequestMessage(org.apache.nifi.cluster.protocol.message.NodeConnectionStatusRequestMessage) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) Socket(java.net.Socket)

Aggregations

NodeConnectionStatusResponseMessage (org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Socket (java.net.Socket)1 NodeConnectionStatus (org.apache.nifi.cluster.coordination.node.NodeConnectionStatus)1 ProtocolException (org.apache.nifi.cluster.protocol.ProtocolException)1 NodeConnectionStatusRequestMessage (org.apache.nifi.cluster.protocol.message.NodeConnectionStatusRequestMessage)1 ProtocolMessage (org.apache.nifi.cluster.protocol.message.ProtocolMessage)1 Test (org.junit.Test)1