Search in sources :

Example 1 with RELPFrame

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

the class RELPFrameProducer method main.

public static void main(String[] args) {
    if (args == null || args.length != 5) {
        System.err.println("USAGE: RELPFrameProducer <HOST> <PORT> <NUM_MSGS> <DELAY_INTERVAL> <DELAY_MILLIS>");
        System.exit(1);
    }
    final String host = args[0];
    final int port = Integer.parseInt(args[1]);
    final int numMessages = Integer.parseInt(args[2]);
    final int delayInterval = Integer.parseInt(args[3]);
    final long delay = Long.parseLong(args[4]);
    final RELPEncoder encoder = new RELPEncoder(StandardCharsets.UTF_8);
    Socket socket = null;
    try {
        socket = new Socket(host, port);
        try (final OutputStream out = new BufferedOutputStream(socket.getOutputStream())) {
            // send the open frame
            out.write(encoder.encode(OPEN_FRAME));
            // send the specified number of syslog messages
            for (int i = 2; i < (numMessages + 2); i++) {
                final byte[] data = ("this is message # " + i).getBytes(StandardCharsets.UTF_8);
                final RELPFrame syslogFrame = new RELPFrame.Builder().txnr(i).command("syslog").dataLength(data.length).data(data).build();
                out.write(encoder.encode(syslogFrame));
                if (i % delayInterval == 0) {
                    System.out.println("Sent " + i + " messages");
                    out.flush();
                    Thread.sleep(delay);
                }
            }
            // send the close frame
            out.write(encoder.encode(CLOSE_FRAME));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(socket);
    }
}
Also used : RELPEncoder(org.apache.nifi.processors.standard.relp.frame.RELPEncoder) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket) RELPFrame(org.apache.nifi.processors.standard.relp.frame.RELPFrame)

Example 2 with RELPFrame

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

the class TestRELPFrameHandler method testCommand.

@Test
public void testCommand() throws IOException, InterruptedException {
    final String data = "this is a syslog message";
    final RELPFrame openFrame = new RELPFrame.Builder().txnr(1).command("syslog").dataLength(data.length()).data(data.getBytes(charset)).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(0, responder.responded);
    Assert.assertEquals(0, responder.responses.size());
    Assert.assertEquals(1, events.size());
    final RELPEvent event = events.poll();
    Assert.assertEquals(data, new String(event.getData(), charset));
}
Also used : RELPEvent(org.apache.nifi.processors.standard.relp.event.RELPEvent) RELPFrame(org.apache.nifi.processors.standard.relp.frame.RELPFrame) Test(org.junit.Test)

Example 3 with RELPFrame

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

the class TestRELPFrameHandler method testOpen.

@Test
public void testOpen() throws IOException, InterruptedException {
    final String offer1 = "relp_version=0";
    final String offer2 = "relp_software=librelp,1.2.7,http://librelp.adiscon.com";
    final String offer3 = "commands=syslog";
    final String data = offer1 + "\n" + offer2 + "\n" + offer3;
    final RELPFrame openFrame = new RELPFrame.Builder().txnr(1).command("open").dataLength(data.length()).data(data.getBytes(charset)).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(offer1));
    Assert.assertTrue(responseData.contains(offer2));
    Assert.assertTrue(responseData.contains(offer3));
}
Also used : ChannelResponse(org.apache.nifi.processor.util.listen.response.ChannelResponse) RELPFrame(org.apache.nifi.processors.standard.relp.frame.RELPFrame) Test(org.junit.Test)

Example 4 with RELPFrame

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

the class TestRELPResponse method testCreateOkResponse.

@Test
public void testCreateOkResponse() {
    final long txnr = 123456789;
    final RELPResponse openResponse = RELPResponse.ok(txnr);
    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";
    Assert.assertEquals(expected, result);
    Assert.assertEquals(expected.length(), frame.getDataLength());
}
Also used : RELPFrame(org.apache.nifi.processors.standard.relp.frame.RELPFrame) Test(org.junit.Test)

Example 5 with RELPFrame

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

the class TestRELPResponse method testCreateErrorResponse.

@Test
public void testCreateErrorResponse() {
    final long txnr = 123456789;
    final RELPResponse openResponse = RELPResponse.error(txnr);
    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.ERROR + " ERROR";
    Assert.assertEquals(expected, result);
    Assert.assertEquals(expected.length(), frame.getDataLength());
}
Also used : 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