use of org.apache.nifi.processor.util.listen.response.socket.SocketChannelResponder in project nifi by apache.
the class BeatsSocketChannelHandler 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 packet 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 List<BeatsFrame> frames = decoder.getFrames();
for (BeatsFrame frame : frames) {
logger.debug("Received Beats frame with transaction {} and command {}", new Object[] { frame.getSeqNumber(), frame.getSeqNumber() });
// Ignore the WINDOW SIZE type frames as they contain no payload.
if (frame.getFrameType() != 0x57) {
final SocketChannelResponder responder = new SocketChannelResponder(socketChannel);
frameHandler.handle(frame, responder, sender.toString());
}
}
socketBuffer.mark();
}
}
logger.debug("Done processing buffer");
} catch (final BeatsFrameException rfe) {
logger.error("Error reading Beats 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.processor.util.listen.response.socket.SocketChannelResponder in project nifi by apache.
the class LumberjackSocketChannelHandler 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 Lumberjack 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 List<LumberjackFrame> frames = decoder.getFrames();
for (LumberjackFrame frame : frames) {
// TODO: Clean this
logger.debug("Received Lumberjack frame with transaction {} and command {}", new Object[] { frame.getSeqNumber(), frame.getSeqNumber() });
// Ignore the WINDOWS type frames as they contain no payload.
if (frame.getFrameType() != 0x57) {
final SocketChannelResponder responder = new SocketChannelResponder(socketChannel);
frameHandler.handle(frame, responder, sender.toString());
}
}
socketBuffer.mark();
}
}
logger.debug("Done processing buffer");
} catch (final LumberjackFrameException rfe) {
logger.error("Error reading Lumberjack 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.processor.util.listen.response.socket.SocketChannelResponder in project nifi by apache.
the class StandardSocketChannelHandler method processBuffer.
/**
* Process the contents that have been read into the buffer. Allow sub-classes to override this behavior.
*
* @param socketChannel the channel the data was read from
* @param socketBuffer the buffer the data was read into
* @throws InterruptedException if interrupted when queuing events
*/
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();
// go through the buffer looking for the end of each message
currBytes.reset();
for (int i = 0; i < total; i++) {
// NOTE: For higher throughput, the looking for \n and copying into the byte stream could be improved
// Pull data out of buffer and cram into byte array
byte currByte = socketBuffer.get();
// check if at end of a message
if (currByte == getDelimiter()) {
if (currBytes.size() > 0) {
final SocketChannelResponder response = new SocketChannelResponder(socketChannel);
final Map<String, String> metadata = EventFactoryUtil.createMapWithSender(sender.toString());
final E event = eventFactory.create(currBytes.toByteArray(), metadata, response);
events.offer(event);
currBytes.reset();
// Mark this as the start of the next message
socketBuffer.mark();
}
} else {
currBytes.write(currByte);
}
}
}
use of org.apache.nifi.processor.util.listen.response.socket.SocketChannelResponder in project nifi by apache.
the class TestLumberjackEventFactory method testCreateLumberJackEvent.
@Test
public void testCreateLumberJackEvent() {
final String sender = "testsender1";
final byte[] data = "this is a test line".getBytes();
final long seqNumber = 1;
final String fields = "{\"file\":\"test\"}";
final Map<String, String> metadata = new HashMap<>();
metadata.put(EventFactory.SENDER_KEY, sender);
metadata.put(LumberjackMetadata.SEQNUMBER_KEY, String.valueOf(seqNumber));
metadata.put(LumberjackMetadata.FIELDS_KEY, String.valueOf(fields));
final ChannelResponder responder = new SocketChannelResponder(null);
final EventFactory<LumberjackEvent> factory = new LumberjackEventFactory();
final LumberjackEvent event = factory.create(data, metadata, responder);
Assert.assertEquals(sender, event.getSender());
Assert.assertEquals(seqNumber, event.getSeqNumber());
Assert.assertEquals(fields, event.getFields());
Assert.assertEquals(data, event.getData());
}
use of org.apache.nifi.processor.util.listen.response.socket.SocketChannelResponder 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);
}
}
Aggregations