Search in sources :

Example 1 with ByteChannel

use of java.nio.channels.ByteChannel in project spring-boot by spring-projects.

the class HttpTunnelServer method getServerThread.

/**
	 * Returns the active server thread, creating and starting it if necessary.
	 * @return the {@code ServerThread} (never {@code null})
	 * @throws IOException in case of I/O errors
	 */
protected ServerThread getServerThread() throws IOException {
    synchronized (this) {
        if (this.serverThread == null) {
            ByteChannel channel = this.serverConnection.open(this.longPollTimeout);
            this.serverThread = new ServerThread(channel);
            this.serverThread.start();
        }
        return this.serverThread;
    }
}
Also used : ByteChannel(java.nio.channels.ByteChannel)

Example 2 with ByteChannel

use of java.nio.channels.ByteChannel in project spring-boot by spring-projects.

the class SocketTargetServerConnectionTests method timeout.

@Test
public void timeout() throws Exception {
    this.server.delay(1000);
    this.server.start();
    ByteChannel channel = this.connection.open(10);
    long startTime = System.currentTimeMillis();
    try {
        channel.read(ByteBuffer.allocate(5));
        fail("No socket timeout thrown");
    } catch (SocketTimeoutException ex) {
        // Expected
        long runTime = System.currentTimeMillis() - startTime;
        assertThat(runTime).isGreaterThanOrEqualTo(10L);
        assertThat(runTime).isLessThan(10000L);
    }
}
Also used : ByteChannel(java.nio.channels.ByteChannel) SocketTimeoutException(java.net.SocketTimeoutException) Test(org.junit.Test)

Example 3 with ByteChannel

use of java.nio.channels.ByteChannel in project spring-boot by spring-projects.

the class SocketTargetServerConnectionTests method writeData.

@Test
public void writeData() throws Exception {
    this.server.expect("hello".getBytes());
    this.server.start();
    ByteChannel channel = this.connection.open(DEFAULT_TIMEOUT);
    ByteBuffer buffer = ByteBuffer.wrap("hello".getBytes());
    channel.write(buffer);
    this.server.closeAndVerify();
}
Also used : ByteChannel(java.nio.channels.ByteChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with ByteChannel

use of java.nio.channels.ByteChannel in project h2o-3 by h2oai.

the class H2ONode method getTCPSocket.

ByteChannel getTCPSocket() throws IOException {
    // Under lock, claim an existing open socket if possible
    synchronized (this) {
        // Limit myself to the number of open sockets from node-to-node
        while (_socksAvail == 0) try {
            wait(1000);
        } catch (InterruptedException ignored) {
        }
        // Claim an open socket
        ByteChannel sock = _socks[--_socksAvail];
        if (sock != null) {
            // Return existing socket!
            if (sock.isOpen())
                return sock;
            // Else it's an already-closed socket, lower open TCP count
            assert TCPS.get() > 0;
            TCPS.decrementAndGet();
        }
    }
    // Must make a fresh socket
    SocketChannel sock2 = SocketChannel.open();
    sock2.socket().setReuseAddress(true);
    sock2.socket().setSendBufferSize(AutoBuffer.BBP_BIG._size);
    boolean res = sock2.connect(_key);
    assert res && !sock2.isConnectionPending() && sock2.isBlocking() && sock2.isConnected() && sock2.isOpen();
    ByteBuffer bb = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder());
    bb.put((byte) 2);
    bb.putChar((char) H2O.H2O_PORT);
    bb.put((byte) 0xef);
    bb.flip();
    ByteChannel wrappedSocket = _socketFactory.clientChannel(sock2, _key.getHostName(), _key.getPort());
    while (bb.hasRemaining()) {
        wrappedSocket.write(bb);
    }
    // Cluster-wide counting
    TCPS.incrementAndGet();
    return wrappedSocket;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ByteChannel(java.nio.channels.ByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 5 with ByteChannel

use of java.nio.channels.ByteChannel in project h2o-3 by h2oai.

the class H2ONode method openChan.

/**
   * Returns a new connection of type {@code tcpType}, the type can be either
   *   TCPReceiverThread.TCP_SMALL, TCPReceiverThread.TCP_BIG or
   *   TCPReceiverThread.TCP_EXTERNAL.
   *
   * If socket channel factory is set, the communication will considered to be secured - this depends on the
   * configuration of the {@link SocketChannelFactory}. In case of the factory is null, the communication won't be secured.
   * @return new socket channel
   */
public static ByteChannel openChan(byte tcpType, SocketChannelFactory socketFactory, InetAddress originAddr, int originPort) throws IOException {
    // Must make a fresh socket
    SocketChannel sock = SocketChannel.open();
    sock.socket().setReuseAddress(true);
    sock.socket().setSendBufferSize(AutoBuffer.BBP_BIG._size);
    InetSocketAddress isa = new InetSocketAddress(originAddr, originPort);
    // Can toss IOEx, esp if other node is still booting up
    boolean res = sock.connect(isa);
    assert res : "Should be already connected, but connection is in non-blocking mode and the connection operation is in progress!";
    sock.configureBlocking(true);
    assert !sock.isConnectionPending() && sock.isBlocking() && sock.isConnected() && sock.isOpen();
    sock.socket().setTcpNoDelay(true);
    ByteBuffer bb = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder());
    bb.put(tcpType).putChar((char) H2O.H2O_PORT).put((byte) 0xef).flip();
    ByteChannel wrappedSocket = socketFactory.clientChannel(sock, isa.getHostName(), isa.getPort());
    while (bb.hasRemaining()) {
        // Write out magic startup sequence
        wrappedSocket.write(bb);
    }
    return wrappedSocket;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ByteChannel(java.nio.channels.ByteChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteChannel (java.nio.channels.ByteChannel)11 ByteBuffer (java.nio.ByteBuffer)5 Test (org.junit.Test)5 IOException (java.io.IOException)3 SocketChannel (java.nio.channels.SocketChannel)3 Before (org.junit.Before)2 Frame (water.fvec.Frame)2 InetAddress (java.net.InetAddress)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 Timestamp (java.sql.Timestamp)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)1 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 TestFrameBuilder (water.fvec.TestFrameBuilder)1 BufferedString (water.parser.BufferedString)1