Search in sources :

Example 1 with SocketAddress

use of java.net.SocketAddress in project jetty.project by eclipse.

the class SelectorManager method chooseSelector.

private ManagedSelector chooseSelector(SelectableChannel channel) {
    // Ideally we would like to have all connections from the same client end
    // up on the same selector (to try to avoid smearing the data from a single
    // client over all cores), but because of proxies, the remote address may not
    // really be the client - so we have to hedge our bets to ensure that all
    // channels don't end up on the one selector for a proxy.
    ManagedSelector candidate1 = null;
    if (channel != null) {
        try {
            if (channel instanceof SocketChannel) {
                SocketAddress remote = ((SocketChannel) channel).getRemoteAddress();
                if (remote instanceof InetSocketAddress) {
                    byte[] addr = ((InetSocketAddress) remote).getAddress().getAddress();
                    if (addr != null) {
                        int s = addr[addr.length - 1] & 0xFF;
                        candidate1 = _selectors[s % getSelectorCount()];
                    }
                }
            }
        } catch (IOException x) {
            LOG.ignore(x);
        }
    }
    // 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());
    ManagedSelector candidate2 = _selectors[index];
    if (candidate1 == null || candidate1.size() >= candidate2.size() * 2)
        return candidate2;
    return candidate1;
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 2 with SocketAddress

use of java.net.SocketAddress in project jetty.project by eclipse.

the class BadRequestLogHandlerTest method testLogHandler.

@Test(timeout = 4000)
public void testLogHandler() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.setConnectors(new Connector[] { connector });
    CaptureLog captureLog = new CaptureLog();
    RequestLogHandler requestLog = new RequestLogHandler();
    requestLog.setRequestLog(captureLog);
    requestLog.setHandler(new HelloHandler());
    server.setHandler(requestLog);
    try {
        server.start();
        String host = connector.getHost();
        if (host == null) {
            host = "localhost";
        }
        InetAddress destAddr = InetAddress.getByName(host);
        int port = connector.getLocalPort();
        SocketAddress endpoint = new InetSocketAddress(destAddr, port);
        Socket socket = new Socket();
        socket.setSoTimeout(1000);
        socket.connect(endpoint);
        try (OutputStream out = socket.getOutputStream();
            OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            InputStream in = socket.getInputStream();
            InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
            StringReader request = new StringReader(requestHeader);
            IO.copy(request, writer);
            writer.flush();
            StringWriter response = new StringWriter();
            IO.copy(reader, response);
            LOG.info("Response: {}", response);
        } finally {
            socket.close();
        }
        assertRequestLog(captureLog);
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ServerConnector(org.eclipse.jetty.server.ServerConnector) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) OutputStreamWriter(java.io.OutputStreamWriter) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) Socket(java.net.Socket) Test(org.junit.Test)

Example 3 with SocketAddress

use of java.net.SocketAddress in project jetty.project by eclipse.

the class BlockheadClient method connect.

/* (non-Javadoc)
     * @see org.eclipse.jetty.websocket.common.test.IBlockheadClient#connect()
     */
@Override
public void connect() throws IOException {
    InetAddress destAddr = InetAddress.getByName(destHttpURI.getHost());
    int port = destHttpURI.getPort();
    SocketAddress endpoint = new InetSocketAddress(destAddr, port);
    socket = new Socket();
    socket.setSoTimeout(timeout);
    socket.connect(endpoint);
    out = socket.getOutputStream();
    in = socket.getInputStream();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) Socket(java.net.Socket)

Example 4 with SocketAddress

use of java.net.SocketAddress in project k-9 by k9mail.

the class ImapConnection method connectToAddress.

private Socket connectToAddress(InetAddress address) throws NoSuchAlgorithmException, KeyManagementException, MessagingException, IOException {
    String host = settings.getHost();
    int port = settings.getPort();
    String clientCertificateAlias = settings.getClientCertificateAlias();
    if (K9MailLib.isDebug() && DEBUG_PROTOCOL_IMAP) {
        Log.d(LOG_TAG, "Connecting to " + host + " as " + address);
    }
    SocketAddress socketAddress = new InetSocketAddress(address, port);
    Socket socket;
    if (settings.getConnectionSecurity() == ConnectionSecurity.SSL_TLS_REQUIRED) {
        socket = socketFactory.createSocket(null, host, port, clientCertificateAlias);
    } else {
        socket = new Socket();
    }
    socket.connect(socketAddress, socketConnectTimeout);
    return socket;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket)

Example 5 with SocketAddress

use of java.net.SocketAddress in project flink by apache.

the class PartitionRequestClientHandler method decodeMsg.

private boolean decodeMsg(Object msg, boolean isStagedBuffer) throws Throwable {
    final Class<?> msgClazz = msg.getClass();
    // ---- Buffer --------------------------------------------------------
    if (msgClazz == NettyMessage.BufferResponse.class) {
        NettyMessage.BufferResponse bufferOrEvent = (NettyMessage.BufferResponse) msg;
        RemoteInputChannel inputChannel = inputChannels.get(bufferOrEvent.receiverId);
        if (inputChannel == null) {
            bufferOrEvent.releaseBuffer();
            cancelRequestFor(bufferOrEvent.receiverId);
            return true;
        }
        return decodeBufferOrEvent(inputChannel, bufferOrEvent, isStagedBuffer);
    } else // ---- Error ---------------------------------------------------------
    if (msgClazz == NettyMessage.ErrorResponse.class) {
        NettyMessage.ErrorResponse error = (NettyMessage.ErrorResponse) msg;
        SocketAddress remoteAddr = ctx.channel().remoteAddress();
        if (error.isFatalError()) {
            notifyAllChannelsOfErrorAndClose(new RemoteTransportException("Fatal error at remote task manager '" + remoteAddr + "'.", remoteAddr, error.cause));
        } else {
            RemoteInputChannel inputChannel = inputChannels.get(error.receiverId);
            if (inputChannel != null) {
                if (error.cause.getClass() == PartitionNotFoundException.class) {
                    inputChannel.onFailedPartitionRequest();
                } else {
                    inputChannel.onError(new RemoteTransportException("Error at remote task manager '" + remoteAddr + "'.", remoteAddr, error.cause));
                }
            }
        }
    } else {
        throw new IllegalStateException("Received unknown message from producer: " + msg.getClass());
    }
    return true;
}
Also used : RemoteTransportException(org.apache.flink.runtime.io.network.netty.exception.RemoteTransportException) PartitionNotFoundException(org.apache.flink.runtime.io.network.partition.PartitionNotFoundException) SocketAddress(java.net.SocketAddress) RemoteInputChannel(org.apache.flink.runtime.io.network.partition.consumer.RemoteInputChannel)

Aggregations

SocketAddress (java.net.SocketAddress)711 InetSocketAddress (java.net.InetSocketAddress)538 Test (org.junit.Test)169 IOException (java.io.IOException)151 Socket (java.net.Socket)103 InetAddress (java.net.InetAddress)56 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)42 Proxy (java.net.Proxy)38 ArrayList (java.util.ArrayList)37 SocketChannel (java.nio.channels.SocketChannel)36 SocketException (java.net.SocketException)34 ServerSocket (java.net.ServerSocket)33 Channel (org.jboss.netty.channel.Channel)33 UnknownHostException (java.net.UnknownHostException)32 ByteBuffer (java.nio.ByteBuffer)32 HashMap (java.util.HashMap)31 Channel (io.netty.channel.Channel)30 Map (java.util.Map)29 Set (java.util.Set)29 HashSet (java.util.HashSet)28