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