Search in sources :

Example 1 with ClientHandler

use of org.spf4j.io.tcp.ClientHandler 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)

Aggregations

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