Search in sources :

Example 1 with DeadlineAction

use of org.spf4j.io.tcp.DeadlineAction in project spf4j by zolyfarkas.

the class TcpServerTest method testRejectingServer.

@Test(expected = IOException.class, timeout = 10000)
public void testRejectingServer() throws IOException, InterruptedException {
    String testSite = "localhost";
    ForkJoinPool pool = new ForkJoinPool(1024);
    try (TcpServer rejServer = new TcpServer(pool, new ClientHandler() {

        @Override
        public void handle(final Selector serverSelector, final SocketChannel clientChannel, final ExecutorService exec, final BlockingQueue<Runnable> tasksToRunBySelector, final UpdateablePriorityQueue<DeadlineAction> deadlineActions) throws IOException {
            clientChannel.configureBlocking(true);
            ByteBuffer allocate = ByteBuffer.allocate(1024);
            // read something
            clientChannel.read(allocate);
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
            allocate.flip();
            clientChannel.write(allocate);
            clientChannel.close();
        }
    }, 1980, 10)) {
        rejServer.startAsync().awaitRunning();
        try (TcpServer server = new TcpServer(pool, new ProxyClientHandler(HostAndPort.fromParts(testSite, 1980), null, null, 10000, 5000), 1981, 10)) {
            server.startAsync().awaitRunning();
            byte[] readfromSite = readfromSite("http://localhost:1981");
            // probably wrong charset assumtion
            LOG.debug("Response: {}", new String(readfromSite, StandardCharsets.UTF_8));
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ClientHandler(org.spf4j.io.tcp.ClientHandler) ProxyClientHandler(org.spf4j.io.tcp.proxy.ProxyClientHandler) IOException(java.io.IOException) TcpServer(org.spf4j.io.tcp.TcpServer) ByteBuffer(java.nio.ByteBuffer) ProxyClientHandler(org.spf4j.io.tcp.proxy.ProxyClientHandler) DeadlineAction(org.spf4j.io.tcp.DeadlineAction) AbstractRunnable(org.spf4j.base.AbstractRunnable) ExecutorService(java.util.concurrent.ExecutorService) ForkJoinPool(java.util.concurrent.ForkJoinPool) Selector(java.nio.channels.Selector) Test(org.junit.Test)

Example 2 with DeadlineAction

use of org.spf4j.io.tcp.DeadlineAction in project spf4j by zolyfarkas.

the class ProxyClientHandler method handle.

@Override
public void handle(final Selector serverSelector, final SocketChannel clientChannel, final ExecutorService exec, final BlockingQueue<Runnable> tasksToRunBySelector, final UpdateablePriorityQueue<DeadlineAction> deadlineActions) throws IOException {
    final InetSocketAddress socketAddress = new InetSocketAddress(fwdDestination.getHost(), fwdDestination.getPort());
    final SocketChannel proxyChannel = SocketChannel.open();
    try {
        proxyChannel.configureBlocking(false);
        proxyChannel.connect(socketAddress);
        TransferBuffer c2s = new TransferBuffer(proxyBufferSize);
        if (c2sSnifferFact != null) {
            c2s.setIncomingSniffer(c2sSnifferFact.get(clientChannel));
        }
        TransferBuffer s2c = new TransferBuffer(proxyBufferSize);
        final long connectDeadline = TimeSource.nanoTime() + TimeUnit.MILLISECONDS.toNanos(connectTimeoutMillis);
        UpdateablePriorityQueue.ElementRef daction = deadlineActions.add(new DeadlineAction(connectDeadline, new CloseChannelsOnTimeout(proxyChannel, clientChannel)));
        new ProxyBufferTransferHandler(c2s, s2c, null, clientChannel, serverSelector, exec, tasksToRunBySelector, daction).initialInterestRegistration();
        new ProxyBufferTransferHandler(s2c, c2s, s2cSnifferFact, proxyChannel, serverSelector, exec, tasksToRunBySelector, daction).initialInterestRegistration();
    } catch (IOException ex) {
        Exception exs = Closeables.closeAll(proxyChannel, clientChannel);
        if (exs != null) {
            ex.addSuppressed(exs);
        }
        throw ex;
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) DeadlineAction(org.spf4j.io.tcp.DeadlineAction) IOException(java.io.IOException) UpdateablePriorityQueue(org.spf4j.ds.UpdateablePriorityQueue) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 SocketChannel (java.nio.channels.SocketChannel)2 DeadlineAction (org.spf4j.io.tcp.DeadlineAction)2 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 Selector (java.nio.channels.Selector)1 ExecutorService (java.util.concurrent.ExecutorService)1 ForkJoinPool (java.util.concurrent.ForkJoinPool)1 Test (org.junit.Test)1 AbstractRunnable (org.spf4j.base.AbstractRunnable)1 UpdateablePriorityQueue (org.spf4j.ds.UpdateablePriorityQueue)1 ClientHandler (org.spf4j.io.tcp.ClientHandler)1 TcpServer (org.spf4j.io.tcp.TcpServer)1 ProxyClientHandler (org.spf4j.io.tcp.proxy.ProxyClientHandler)1