public final int getDispatcherPort() { return dispatcher == null ? 0 : dispatcher.getPort(); }
@OnUnscheduled public void onUnscheduled() { if (dispatcher != null) { dispatcher.close(); } }
@Override public void completeConnection(SelectionKey key) { // connection is done. Return the buffer to the pool SocketChannelAttachment attachment = (SocketChannelAttachment) key.attachment(); try { bufferPool.put(attachment.getByteBuffer()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } currentConnections.decrementAndGet(); }
@Before public void setup() { eventFactory = new TestEventHolderFactory(); channelHandlerFactory = new RELPSocketChannelHandlerFactory<>(); byteBuffers = new LinkedBlockingQueue<>(); byteBuffers.add(ByteBuffer.allocate(4096)); events = new LinkedBlockingQueue<>(); logger = Mockito.mock(ComponentLog.class); maxConnections = 1; sslContext = null; charset = StandardCharsets.UTF_8; dispatcher = new SocketChannelDispatcher<>(eventFactory, channelHandlerFactory, byteBuffers, events, logger, maxConnections, sslContext, charset); }
@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); } }
sslSocketChannel = attachment.getSslSocketChannel(); final ByteBuffer socketBuffer = attachment.getByteBuffer(); byte[] socketBufferArray = new byte[socketBuffer.limit()]; if(eof == true) { IOUtils.closeQuietly(sslSocketChannel); dispatcher.completeConnection(key); } else { dispatcher.addBackForSelection(key);
final ByteBuffer socketBuffer = attachment.getByteBuffer(); if(eof == true) { IOUtils.closeQuietly(socketChannel); dispatcher.completeConnection(key); } else { dispatcher.addBackForSelection(key);
protected ChannelDispatcher createChannelReader(final ProcessContext context, final String protocol, final BlockingQueue<ByteBuffer> bufferPool, final BlockingQueue<RawSyslogEvent> events, final int maxConnections, final SSLContextService sslContextService, final Charset charset) throws IOException { final EventFactory<RawSyslogEvent> eventFactory = new RawSyslogEventFactory(); if (UDP_VALUE.getValue().equals(protocol)) { return new DatagramChannelDispatcher(eventFactory, bufferPool, events, getLogger()); } else { // if an SSLContextService was provided then create an SSLContext to pass down to the dispatcher SSLContext sslContext = null; SslContextFactory.ClientAuth clientAuth = null; if (sslContextService != null) { final String clientAuthValue = context.getProperty(CLIENT_AUTH).getValue(); sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.valueOf(clientAuthValue)); clientAuth = SslContextFactory.ClientAuth.valueOf(clientAuthValue); } final ChannelHandlerFactory<RawSyslogEvent<SocketChannel>, AsyncChannelDispatcher> handlerFactory = new SocketChannelHandlerFactory<>(); return new SocketChannelDispatcher(eventFactory, handlerFactory, bufferPool, events, getLogger(), maxConnections, sslContext, clientAuth, charset); } }
@Override protected ChannelDispatcher createDispatcher(final ProcessContext context, final BlockingQueue<StandardEvent> events) throws IOException { final String sendingHost = context.getProperty(SENDING_HOST).evaluateAttributeExpressions().getValue(); final Integer sendingHostPort = context.getProperty(SENDING_HOST_PORT).evaluateAttributeExpressions().asInteger(); final Integer bufferSize = context.getProperty(RECV_BUFFER_SIZE).asDataSize(DataUnit.B).intValue(); final BlockingQueue<ByteBuffer> bufferPool = createBufferPool(context.getMaxConcurrentTasks(), bufferSize); final EventFactory<StandardEvent> eventFactory = new StandardEventFactory(); return new DatagramChannelDispatcher<>(eventFactory, bufferPool, events, getLogger(), sendingHost, sendingHostPort); }
@Override protected ChannelDispatcher createDispatcher(final ProcessContext context, final BlockingQueue<LumberjackEvent> events) throws IOException { final EventFactory<LumberjackEvent> eventFactory = new LumberjackEventFactory(); final ChannelHandlerFactory<LumberjackEvent, AsyncChannelDispatcher> handlerFactory = new LumberjackSocketChannelHandlerFactory<>(); final int maxConnections = context.getProperty(MAX_CONNECTIONS).asInteger(); final int bufferSize = context.getProperty(RECV_BUFFER_SIZE).asDataSize(DataUnit.B).intValue(); final Charset charSet = Charset.forName(context.getProperty(CHARSET).getValue()); // initialize the buffer pool based on max number of connections and the buffer size final BlockingQueue<ByteBuffer> bufferPool = createBufferPool(maxConnections, bufferSize); // if an SSLContextService was provided then create an SSLContext to pass down to the dispatcher SSLContext sslContext = null; final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class); if (sslContextService != null) { sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.REQUIRED); } // if we decide to support SSL then get the context and pass it in here return new SocketChannelDispatcher<>(eventFactory, handlerFactory, bufferPool, events, getLogger(), maxConnections, sslContext, charSet); }
@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); } }
protected int getPort() { return channelDispatcher == null ? 0 : channelDispatcher.getPort(); }
@OnUnscheduled public void onUnscheduled() { if (channelDispatcher != null) { channelDispatcher.close(); } }
@Override protected ChannelDispatcher createDispatcher(final ProcessContext context, final BlockingQueue<StandardEvent> events) throws IOException { final String sendingHost = context.getProperty(SENDING_HOST).evaluateAttributeExpressions().getValue(); final Integer sendingHostPort = context.getProperty(SENDING_HOST_PORT).evaluateAttributeExpressions().asInteger(); final Integer bufferSize = context.getProperty(RECV_BUFFER_SIZE).asDataSize(DataUnit.B).intValue(); final BlockingQueue<ByteBuffer> bufferPool = createBufferPool(context.getMaxConcurrentTasks(), bufferSize); final EventFactory<StandardEvent> eventFactory = new StandardEventFactory(); return new DatagramChannelDispatcher<>(eventFactory, bufferPool, events, getLogger(), sendingHost, sendingHostPort); }
@Override protected ChannelDispatcher createDispatcher(final ProcessContext context, final BlockingQueue<BeatsEvent> events) throws IOException { final EventFactory<BeatsEvent> eventFactory = new BeatsEventFactory(); final ChannelHandlerFactory<BeatsEvent, AsyncChannelDispatcher> handlerFactory = new BeatsSocketChannelHandlerFactory<>(); final int maxConnections = context.getProperty(MAX_CONNECTIONS).asInteger(); final int bufferSize = context.getProperty(RECV_BUFFER_SIZE).asDataSize(DataUnit.B).intValue(); final Charset charSet = Charset.forName(context.getProperty(CHARSET).getValue()); // initialize the buffer pool based on max number of connections and the buffer size final BlockingQueue<ByteBuffer> bufferPool = createBufferPool(maxConnections, bufferSize); // if an SSLContextService was provided then create an SSLContext to pass down to the dispatcher SSLContext sslContext = null; final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class); if (sslContextService != null) { sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.REQUIRED); } // if we decide to support SSL then get the context and pass it in here return new SocketChannelDispatcher<>(eventFactory, handlerFactory, bufferPool, events, getLogger(), maxConnections, sslContext, charSet); }
public void handle(final RELPFrame frame, final ChannelResponder<SocketChannel> responder, final String sender) throws IOException, InterruptedException { // respond to open and close commands immediately, create and queue an event for everything else if (CMD_OPEN.equals(frame.getCommand())) { Map<String,String> offers = RELPResponse.parseOffers(frame.getData(), charset); ChannelResponse response = new RELPChannelResponse(encoder, RELPResponse.open(frame.getTxnr(), offers)); responder.addResponse(response); responder.respond(); } else if (CMD_CLOSE.equals(frame.getCommand())) { ChannelResponse response = new RELPChannelResponse(encoder, RELPResponse.ok(frame.getTxnr())); responder.addResponse(response); responder.respond(); dispatcher.completeConnection(key); } else { final Map<String, String> metadata = EventFactoryUtil.createMapWithSender(sender.toString()); metadata.put(RELPMetadata.TXNR_KEY, String.valueOf(frame.getTxnr())); metadata.put(RELPMetadata.COMMAND_KEY, frame.getCommand()); final E event = eventFactory.create(frame.getData(), metadata, responder); events.offer(event); } }
@Override protected ChannelDispatcher createDispatcher(final ProcessContext context, final BlockingQueue<StandardEvent> events) throws IOException { final int maxConnections = context.getProperty(MAX_CONNECTIONS).asInteger(); final int bufferSize = context.getProperty(RECV_BUFFER_SIZE).asDataSize(DataUnit.B).intValue(); final Charset charSet = Charset.forName(context.getProperty(CHARSET).getValue()); // initialize the buffer pool based on max number of connections and the buffer size final BlockingQueue<ByteBuffer> bufferPool = createBufferPool(maxConnections, bufferSize); // if an SSLContextService was provided then create an SSLContext to pass down to the dispatcher SSLContext sslContext = null; SslContextFactory.ClientAuth clientAuth = null; final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class); if (sslContextService != null) { final String clientAuthValue = context.getProperty(CLIENT_AUTH).getValue(); sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.valueOf(clientAuthValue)); clientAuth = SslContextFactory.ClientAuth.valueOf(clientAuthValue); } final EventFactory<StandardEvent> eventFactory = new StandardEventFactory(); final ChannelHandlerFactory<StandardEvent<SocketChannel>, AsyncChannelDispatcher> handlerFactory = new SocketChannelHandlerFactory<>(); return new SocketChannelDispatcher(eventFactory, handlerFactory, bufferPool, events, getLogger(), maxConnections, sslContext, clientAuth, charSet); }
dispatcher.completeConnection(key);
@Override protected ChannelDispatcher createDispatcher(final ProcessContext context, final BlockingQueue<RELPEvent> events) throws IOException { final EventFactory<RELPEvent> eventFactory = new RELPEventFactory(); final ChannelHandlerFactory<RELPEvent,AsyncChannelDispatcher> handlerFactory = new RELPSocketChannelHandlerFactory<>(); final int maxConnections = context.getProperty(MAX_CONNECTIONS).asInteger(); final int bufferSize = context.getProperty(RECV_BUFFER_SIZE).asDataSize(DataUnit.B).intValue(); final Charset charSet = Charset.forName(context.getProperty(CHARSET).getValue()); // initialize the buffer pool based on max number of connections and the buffer size final BlockingQueue<ByteBuffer> bufferPool = createBufferPool(maxConnections, bufferSize); // if an SSLContextService was provided then create an SSLContext to pass down to the dispatcher SSLContext sslContext = null; SslContextFactory.ClientAuth clientAuth = null; final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class); if (sslContextService != null) { final String clientAuthValue = context.getProperty(CLIENT_AUTH).getValue(); sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.valueOf(clientAuthValue)); clientAuth = SslContextFactory.ClientAuth.valueOf(clientAuthValue); } // if we decide to support SSL then get the context and pass it in here return new SocketChannelDispatcher<>(eventFactory, handlerFactory, bufferPool, events, getLogger(), maxConnections, sslContext, clientAuth, charSet); }
dispatcher.completeConnection(key);