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);
}
}
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));
}
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));
}
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());
}
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());
}
Aggregations