Search in sources :

Example 6 with RELPFrame

use of org.apache.nifi.processors.standard.relp.frame.RELPFrame in project nifi by apache.

the class TestRELPResponse method testCreateOpenResponseNoOffers.

@Test
public void testCreateOpenResponseNoOffers() {
    final long txnr = 123456789;
    final Map<String, String> offers = new HashMap<>();
    final RELPResponse openResponse = RELPResponse.open(txnr, offers);
    final RELPFrame frame = openResponse.toFrame(StandardCharsets.UTF_8);
    Assert.assertEquals(txnr, frame.getTxnr());
    Assert.assertEquals(RELPResponse.RSP_CMD, frame.getCommand());
    final String result = new String(frame.getData(), StandardCharsets.UTF_8);
    final String expected = RELPResponse.OK + " OK\n";
    Assert.assertEquals(expected, result);
    Assert.assertEquals(expected.length(), frame.getDataLength());
}
Also used : HashMap(java.util.HashMap) RELPFrame(org.apache.nifi.processors.standard.relp.frame.RELPFrame) Test(org.junit.Test)

Example 7 with RELPFrame

use of org.apache.nifi.processors.standard.relp.frame.RELPFrame in project nifi by apache.

the class TestListenRELP method sendFrames.

private void sendFrames(final List<RELPFrame> frames, final Socket socket) throws IOException, InterruptedException {
    // send the provided messages
    for (final RELPFrame frame : frames) {
        byte[] encodedFrame = encoder.encode(frame);
        socket.getOutputStream().write(encodedFrame);
    }
    socket.getOutputStream().flush();
}
Also used : RELPFrame(org.apache.nifi.processors.standard.relp.frame.RELPFrame)

Example 8 with RELPFrame

use of org.apache.nifi.processors.standard.relp.frame.RELPFrame in project nifi by apache.

the class RELPSSLSocketChannelHandler method processBuffer.

@Override
protected void processBuffer(final SSLSocketChannel sslSocketChannel, final SocketChannel socketChannel, final int bytesRead, final byte[] buffer) throws InterruptedException, IOException {
    final InetAddress sender = socketChannel.socket().getInetAddress();
    try {
        // go through the buffer parsing the RELP command
        for (int i = 0; i < bytesRead; i++) {
            byte currByte = buffer[i];
            // if we found the end of a frame, handle the frame and mark the buffer
            if (decoder.process(currByte)) {
                final RELPFrame frame = decoder.getFrame();
                logger.debug("Received RELP frame with transaction {} and command {}", new Object[] { frame.getTxnr(), frame.getCommand() });
                final SSLSocketChannelResponder responder = new SSLSocketChannelResponder(socketChannel, sslSocketChannel);
                frameHandler.handle(frame, responder, sender.toString());
            }
        }
        logger.debug("Done processing buffer");
    } catch (final RELPFrameException rfe) {
        logger.error("Error reading RELP frames due to {}", new Object[] { rfe.getMessage() }, rfe);
        // if an invalid frame or bad data was sent then the decoder will be left in a
        // corrupted state, so lets close the connection and cause the client to re-establish
        dispatcher.completeConnection(key);
    }
}
Also used : RELPFrameException(org.apache.nifi.processors.standard.relp.frame.RELPFrameException) SSLSocketChannelResponder(org.apache.nifi.processor.util.listen.response.socket.SSLSocketChannelResponder) InetAddress(java.net.InetAddress) RELPFrame(org.apache.nifi.processors.standard.relp.frame.RELPFrame)

Example 9 with RELPFrame

use of org.apache.nifi.processors.standard.relp.frame.RELPFrame in project nifi by apache.

the class RELPSocketChannelHandler method processBuffer.

@Override
protected void processBuffer(final SocketChannel socketChannel, final ByteBuffer socketBuffer) throws InterruptedException, IOException {
    // get total bytes in buffer
    final int total = socketBuffer.remaining();
    final InetAddress sender = socketChannel.socket().getInetAddress();
    try {
        // go through the buffer parsing the RELP command
        for (int i = 0; i < total; i++) {
            byte currByte = socketBuffer.get();
            // if we found the end of a frame, handle the frame and mark the buffer
            if (decoder.process(currByte)) {
                final RELPFrame frame = decoder.getFrame();
                logger.debug("Received RELP frame with transaction {} and command {}", new Object[] { frame.getTxnr(), frame.getCommand() });
                final SocketChannelResponder responder = new SocketChannelResponder(socketChannel);
                frameHandler.handle(frame, responder, sender.toString());
                socketBuffer.mark();
            }
        }
        logger.debug("Done processing buffer");
    } catch (final RELPFrameException rfe) {
        logger.error("Error reading RELP frames due to {}", new Object[] { rfe.getMessage() }, rfe);
        // if an invalid frame or bad data was sent then the decoder will be left in a
        // corrupted state, so lets close the connection and cause the client to re-establish
        dispatcher.completeConnection(key);
    }
}
Also used : SocketChannelResponder(org.apache.nifi.processor.util.listen.response.socket.SocketChannelResponder) RELPFrameException(org.apache.nifi.processors.standard.relp.frame.RELPFrameException) InetAddress(java.net.InetAddress) RELPFrame(org.apache.nifi.processors.standard.relp.frame.RELPFrame)

Example 10 with RELPFrame

use of org.apache.nifi.processors.standard.relp.frame.RELPFrame in project nifi by apache.

the class TestRELPFrameHandler method testClose.

@Test
public void testClose() throws IOException, InterruptedException {
    final RELPFrame openFrame = new RELPFrame.Builder().txnr(1).command("close").dataLength(0).data(new byte[0]).build();
    final String sender = "sender1";
    final CapturingChannelResponder responder = new CapturingChannelResponder();
    // call the handler and verify respond() was called once with once response
    frameHandler.handle(openFrame, responder, sender);
    Assert.assertEquals(1, responder.responded);
    Assert.assertEquals(1, responder.responses.size());
    // verify the response sent back the offers that were received
    final ChannelResponse response = responder.responses.get(0);
    final String responseData = new String(response.toByteArray(), charset);
    Assert.assertTrue(responseData.contains("200 OK"));
}
Also used : ChannelResponse(org.apache.nifi.processor.util.listen.response.ChannelResponse) RELPFrame(org.apache.nifi.processors.standard.relp.frame.RELPFrame) Test(org.junit.Test)

Aggregations

RELPFrame (org.apache.nifi.processors.standard.relp.frame.RELPFrame)18 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)3 InetAddress (java.net.InetAddress)2 HashMap (java.util.HashMap)2 ChannelResponse (org.apache.nifi.processor.util.listen.response.ChannelResponse)2 RELPFrameException (org.apache.nifi.processors.standard.relp.frame.RELPFrameException)2 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)2 MockFlowFile (org.apache.nifi.util.MockFlowFile)2 BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 SSLSocketChannelResponder (org.apache.nifi.processor.util.listen.response.socket.SSLSocketChannelResponder)1 SocketChannelResponder (org.apache.nifi.processor.util.listen.response.socket.SocketChannelResponder)1 RELPEvent (org.apache.nifi.processors.standard.relp.event.RELPEvent)1 RELPEncoder (org.apache.nifi.processors.standard.relp.frame.RELPEncoder)1 SSLContextService (org.apache.nifi.ssl.SSLContextService)1 StandardSSLContextService (org.apache.nifi.ssl.StandardSSLContextService)1