/** * Process the contents of the buffer. Give sub-classes a chance to override this behavior. * * @param sslSocketChannel the channel the data was read from * @param socketChannel the socket channel being wrapped by sslSocketChannel * @param bytesRead the number of bytes read * @param buffer the buffer to process * @throws InterruptedException thrown if interrupted while queuing events */ protected void processBuffer(final SSLSocketChannel sslSocketChannel, final SocketChannel socketChannel, final int bytesRead, final byte[] buffer) throws InterruptedException, IOException { final InetAddress sender = socketChannel.socket().getInetAddress(); // go through the buffer looking for the end of each message for (int i = 0; i < bytesRead; i++) { final byte currByte = buffer[i]; // check if at end of a message if (currByte == getDelimiter()) { if (currBytes.size() > 0) { final SSLSocketChannelResponder response = new SSLSocketChannelResponder(socketChannel, sslSocketChannel); final Map<String, String> metadata = EventFactoryUtil.createMapWithSender(sender.toString()); final E event = eventFactory.create(currBytes.toByteArray(), metadata, response); events.offer(event); currBytes.reset(); } } else { currBytes.write(currByte); } } }
final SocketChannelResponder response = new SocketChannelResponder(socketChannel); final Map<String, String> metadata = EventFactoryUtil.createMapWithSender(sender.toString()); final E event = eventFactory.create(currBytes.toByteArray(), metadata, response);
@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); } }
@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); } }
final SocketChannelResponder responder = new SocketChannelResponder(socketChannel); frameHandler.handle(frame, responder, sender.toString());
final SSLSocketChannelResponder responder = new SSLSocketChannelResponder(socketChannel, sslSocketChannel); frameHandler.handle(frame, responder, sender.toString());
final SocketChannelResponder responder = new SocketChannelResponder(socketChannel); frameHandler.handle(frame, responder, sender.toString());
final SSLSocketChannelResponder responder = new SSLSocketChannelResponder(socketChannel, sslSocketChannel); frameHandler.handle(frame, responder, sender.toString());
@Test public void testCreateRELPEvent() { final byte[] data = "this is an event".getBytes(StandardCharsets.UTF_8); final String sender = "sender1"; final long txnr = 1; final String command = "syslog"; final Map<String,String> metadata = new HashMap<>(); metadata.put(EventFactory.SENDER_KEY, sender); metadata.put(RELPMetadata.TXNR_KEY, String.valueOf(txnr)); metadata.put(RELPMetadata.COMMAND_KEY, command); final ChannelResponder responder = new SocketChannelResponder(null); final EventFactory<RELPEvent> factory = new RELPEventFactory(); final RELPEvent event = factory.create(data, metadata, responder); Assert.assertEquals(data, event.getData()); Assert.assertEquals(sender, event.getSender()); Assert.assertEquals(txnr, event.getTxnr()); Assert.assertEquals(command, event.getCommand()); Assert.assertEquals(responder, event.getResponder()); } }
@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); } }
@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); } }
final SSLSocketChannelResponder responder = new SSLSocketChannelResponder(socketChannel, sslSocketChannel); frameHandler.handle(frame, responder, sender.toString());
final SocketChannelResponder responder = new SocketChannelResponder(socketChannel); frameHandler.handle(frame, responder, sender.toString());