/** * @param channel the channel to accept * @see #accept(SelectableChannel, Object) */ public void accept(SelectableChannel channel) { accept(channel, null); }
private ManagedSelector chooseSelector() { // The ++ increment here is not atomic, but it does not matter, // so long as the value changes sometimes, then connections will // be distributed over the available selectors. long s = _selectorIndex++; int index = (int)(s % getSelectorCount()); return _selectors[index]; }
public void destroyEndPoint(EndPoint endPoint) { LOG.debug("Destroyed {}", endPoint); Connection connection = endPoint.getConnection(); if (connection != null) connectionClosed(connection); endPointClosed(endPoint); }
private EndPoint createEndPoint(SocketChannel channel, SelectionKey selectionKey) throws IOException { EndPoint endPoint = newEndPoint(channel, this, selectionKey); endPointOpened(endPoint); Connection connection = newConnection(channel, endPoint, selectionKey.attachment()); endPoint.setConnection(connection); connectionOpened(connection); LOG.debug("Created {}", endPoint); return endPoint; }
/** * <p>Registers a channel to perform non-blocking read/write operations.</p> * <p>This method is called just after a channel has been accepted by {@link ServerSocketChannel#accept()}, * or just after having performed a blocking connect via {@link Socket#connect(SocketAddress, int)}.</p> * * @param channel the channel to register */ public void accept(final SocketChannel channel) { final ManagedSelector selector = chooseSelector(); selector.submit(selector.new Accept(channel)); }
public void setConnectTimeout(long connectTimeout) { this.connectTimeout = connectTimeout; SelectorManager selector = this.selector; if (selector != null) selector.setConnectTimeout(connectTimeout); }
@Override protected void endPointOpened(EndPoint endpoint) { super.endPointOpened(endpoint); onEndPointOpened(endpoint); }
@Override protected void endPointClosed(EndPoint endpoint) { onEndPointClosed(endpoint); super.endPointClosed(endpoint); }
protected void failed(Throwable failure) { if (failed.compareAndSet(false, true)) connectionFailed(channel, failure, attachment); } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (HttpMethod.CONNECT.is(request.getMethod())) { logger.debug("CONNECT request for {}", request.getRequestURI()); baseRequest.setHandled(true); ConnectContext connectContext = new ConnectContext(request, response, HttpConnection.getCurrentConnection().getEndPoint()); try { SocketChannel channel = SocketChannel.open(); channel.socket().setTcpNoDelay(true); channel.configureBlocking(false); channel.connect(new InetSocketAddress("127.0.0.1", securePort)); selector.connect(channel, connectContext); } catch (IOException ioe) { connectContext.onConnectFailure(ioe); } } else { super.handle(target, baseRequest, request, response); } }
private EndPoint createEndPoint(SocketChannel channel, SelectionKey selectionKey) throws IOException { EndPoint endPoint = newEndPoint(channel, this, selectionKey); endPointOpened(endPoint); Connection connection = newConnection(channel, endPoint, selectionKey.attachment()); endPoint.setConnection(connection); connectionOpened(connection); LOG.debug("Created {}", endPoint); return endPoint; }
/** * <p>Registers a channel to perform non-blocking read/write operations.</p> * <p>This method is called just after a channel has been accepted by {@link ServerSocketChannel#accept()}, * or just after having performed a blocking connect via {@link Socket#connect(SocketAddress, int)}.</p> * * @param channel the channel to register */ public void accept(final SocketChannel channel) { final ManagedSelector selector = chooseSelector(); selector.submit(selector.new Accept(channel)); }
@Override protected void doStart() throws Exception { HttpClient httpClient = getHttpClient(); selectorManager = newSelectorManager(httpClient); selectorManager.setConnectTimeout(httpClient.getConnectTimeout()); addBean(selectorManager); super.doStart(); }
@Override protected void endPointOpened(EndPoint endpoint) { super.endPointOpened(endpoint); onEndPointOpened(endpoint); }
@Override protected void endPointClosed(EndPoint endpoint) { onEndPointClosed(endpoint); super.endPointClosed(endpoint); }
protected void failed(Throwable failure) { if (failed.compareAndSet(false, true)) connectionFailed(channel, failure, attachment); } }
@Override public void succeeded(SocketAddress socketAddress) { SocketChannel channel = null; try { channel = SocketChannel.open(); SocketAddress bindAddress = getBindAddress(); if (bindAddress != null) channel.bind(bindAddress); configure(channel); channel.configureBlocking(false); channel.connect(socketAddress); Future<Connection> futureConnection = new ConnectionCallback(destination, promise); selectorManager.connect(channel, futureConnection); } // Must catch all exceptions, since some like // UnresolvedAddressException are not IOExceptions. catch (Throwable x) { if (channel != null) close(channel); promise.failed(x); } }
private EndPoint createEndPoint(SocketChannel channel, SelectionKey selectionKey) throws IOException { EndPoint endPoint = _selectorManager.newEndPoint(channel, this, selectionKey); _selectorManager.endPointOpened(endPoint); Connection connection = _selectorManager.newConnection(channel, endPoint, selectionKey.attachment()); endPoint.setConnection(connection); selectionKey.attach(endPoint); _selectorManager.connectionOpened(connection); if (LOG.isDebugEnabled()) LOG.debug("Created {}", endPoint); return endPoint; }