Search in sources :

Example 1 with XnioIoThread

use of org.xnio.XnioIoThread in project undertow by undertow-io.

the class NodePingUtil method pingHost.

/**
     * Try to open a socket connection to given address.
     *
     * @param address     the socket address
     * @param exchange    the http servers exchange
     * @param callback    the ping callback
     * @param options     the options
     */
static void pingHost(InetSocketAddress address, HttpServerExchange exchange, PingCallback callback, OptionMap options) {
    final XnioIoThread thread = exchange.getIoThread();
    final XnioWorker worker = thread.getWorker();
    final HostPingTask r = new HostPingTask(address, worker, callback, options);
    // Schedule timeout task
    scheduleCancelTask(exchange.getIoThread(), r, 5, TimeUnit.SECONDS);
    exchange.dispatch(exchange.isInIoThread() ? SameThreadExecutor.INSTANCE : thread, r);
}
Also used : XnioWorker(org.xnio.XnioWorker) XnioIoThread(org.xnio.XnioIoThread)

Example 2 with XnioIoThread

use of org.xnio.XnioIoThread in project undertow by undertow-io.

the class NodePingUtil method pingHttpClient.

/**
     * Try to ping a server using the undertow client.
     *
     * @param connection    the connection URI
     * @param callback      the ping callback
     * @param exchange      the http servers exchange
     * @param client        the undertow client
     * @param xnioSsl       the ssl setup
     * @param options       the options
     */
static void pingHttpClient(URI connection, PingCallback callback, HttpServerExchange exchange, UndertowClient client, XnioSsl xnioSsl, OptionMap options) {
    final XnioIoThread thread = exchange.getIoThread();
    final RequestExchangeListener exchangeListener = new RequestExchangeListener(callback, NodeHealthChecker.NO_CHECK, true);
    final Runnable r = new HttpClientPingTask(connection, exchangeListener, thread, client, xnioSsl, exchange.getConnection().getByteBufferPool(), options);
    exchange.dispatch(exchange.isInIoThread() ? SameThreadExecutor.INSTANCE : thread, r);
    // Schedule timeout task
    scheduleCancelTask(exchange.getIoThread(), exchangeListener, 5, TimeUnit.SECONDS);
}
Also used : XnioIoThread(org.xnio.XnioIoThread)

Example 3 with XnioIoThread

use of org.xnio.XnioIoThread in project undertow by undertow-io.

the class ProxyConnectionPool method getData.

/**
     * Gets the host data for this thread
     *
     * @return The data for this thread
     */
private HostThreadData getData() {
    Thread thread = Thread.currentThread();
    if (!(thread instanceof XnioIoThread)) {
        throw UndertowMessages.MESSAGES.canOnlyBeCalledByIoThread();
    }
    XnioIoThread ioThread = (XnioIoThread) thread;
    HostThreadData data = hostThreadData.get(ioThread);
    if (data != null) {
        return data;
    }
    data = new HostThreadData();
    HostThreadData existing = hostThreadData.putIfAbsent(ioThread, data);
    if (existing != null) {
        return existing;
    }
    return data;
}
Also used : XnioIoThread(org.xnio.XnioIoThread) XnioIoThread(org.xnio.XnioIoThread)

Example 4 with XnioIoThread

use of org.xnio.XnioIoThread in project undertow by undertow-io.

the class ServerSentEventTestCase method testConnectionFail.

@Test
public void testConnectionFail() throws IOException, InterruptedException {
    final Socket socket = new Socket(DefaultServer.getHostAddress("default"), DefaultServer.getHostPort("default"));
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch connected = new CountDownLatch(1);
    DefaultServer.setRootHandler(new ServerSentEventHandler(new ServerSentEventConnectionCallback() {

        @Override
        public void connected(final ServerSentEventConnection connection, final String lastEventId) {
            final XnioIoThread thread = (XnioIoThread) Thread.currentThread();
            connected.countDown();
            thread.execute(new Runnable() {

                @Override
                public void run() {
                    connection.send("hello", new ServerSentEventConnection.EventCallback() {

                        @Override
                        public void done(ServerSentEventConnection connection, String data, String event, String id) {
                        }

                        @Override
                        public void failed(ServerSentEventConnection connection, String data, String event, String id, IOException e) {
                            latch.countDown();
                        }
                    });
                    if (latch.getCount() > 0) {
                        WorkerUtils.executeAfter(thread, this, 100, TimeUnit.MILLISECONDS);
                    }
                }
            });
        }
    }));
    InputStream in = socket.getInputStream();
    OutputStream out = socket.getOutputStream();
    out.write(("GET / HTTP/1.1\r\nHost:" + DefaultServer.getHostAddress() + "\r\n\r\n").getBytes());
    out.flush();
    if (!connected.await(10, TimeUnit.SECONDS)) {
        Assert.fail();
    }
    out.close();
    in.close();
    if (!latch.await(10, TimeUnit.SECONDS)) {
        Assert.fail();
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) XnioIoThread(org.xnio.XnioIoThread) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

XnioIoThread (org.xnio.XnioIoThread)4 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 XnioWorker (org.xnio.XnioWorker)1