Search in sources :

Example 21 with ClosedSelectorException

use of java.nio.channels.ClosedSelectorException in project JGroups by belaban.

the class MemcachedConnector method run.

public void run() {
    System.out.println("MemcachedConnector listening on " + srv_sock.getLocalSocketAddress());
    while (thread != null && Thread.currentThread().equals(thread)) {
        Socket client_sock = null;
        try {
            client_sock = srv_sock.accept();
            // System.out.println("ACCEPT: " + client_sock.getRemoteSocketAddress());
            final RequestHandler handler = new RequestHandler(client_sock);
            /*new Thread() {
                    public void run() {
                        handler.run();
                    }
                }.start();
                */
            thread_pool.execute(handler);
        } catch (ClosedSelectorException closed) {
            Util.close(client_sock);
            break;
        } catch (Throwable e) {
        }
    }
}
Also used : ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 22 with ClosedSelectorException

use of java.nio.channels.ClosedSelectorException in project Mindustry by Anuken.

the class KryoServer method host.

@Override
public void host(int port) throws IOException {
    lastconnection = 0;
    connections.clear();
    missing.clear();
    server.bind(port, port);
    webServer = new SocketServer(Vars.webPort);
    webServer.start();
    serverThread = new Thread(() -> {
        try {
            server.run();
        } catch (Throwable e) {
            if (!(e instanceof ClosedSelectorException))
                handleException(e);
        }
    }, "Kryonet Server");
    serverThread.setDaemon(true);
    serverThread.start();
}
Also used : WebSocketServer(org.java_websocket.server.WebSocketServer) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 23 with ClosedSelectorException

use of java.nio.channels.ClosedSelectorException in project http-kit by http-kit.

the class HttpServer method run.

public void run() {
    while (true) {
        try {
            PendingKey k;
            while (!pending.isEmpty()) {
                k = pending.poll();
                if (k.Op == PendingKey.OP_WRITE) {
                    if (k.key.isValid()) {
                        k.key.interestOps(OP_WRITE);
                    }
                } else {
                    closeKey(k.key, k.Op);
                }
            }
            if (selector.select() <= 0) {
                continue;
            }
            Set<SelectionKey> selectedKeys = selector.selectedKeys();
            for (SelectionKey key : selectedKeys) {
                keptAlive.remove(key);
                // run hours happily after commented, but not sure.
                if (!key.isValid()) {
                    continue;
                }
                if (key.isAcceptable()) {
                    accept(key);
                } else if (key.isReadable()) {
                    doRead(key);
                } else if (key.isWritable()) {
                    doWrite(key);
                }
            }
            selectedKeys.clear();
        } catch (ClosedSelectorException ignore) {
            // stopped
            return;
        // do not exits the while IO event loop. if exits, then will not process any IO event
        // jvm can catch any exception, including OOM
        } catch (Throwable e) {
            // catch any exception(including OOM), print it
            status.set(Status.STOPPED);
            errorLogger.log("http server loop error, should not happen", e);
            eventLogger.log(eventNames.serverLoopError);
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 24 with ClosedSelectorException

use of java.nio.channels.ClosedSelectorException in project j2objc by google.

the class SelectorTest method test_keys.

/**
 * @tests java.nio.channels.Selector#keys()
 */
public void test_keys() throws IOException {
    SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT);
    Set<SelectionKey> keySet = selector.keys();
    Set<SelectionKey> keySet2 = selector.keys();
    assertSame(keySet, keySet2);
    assertEquals(1, keySet.size());
    SelectionKey key2 = keySet.iterator().next();
    assertEquals(key, key2);
    // Any attempt to modify keys will cause UnsupportedOperationException
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    SelectionKey key3 = sc.register(selector, SelectionKey.OP_READ);
    try {
        keySet2.add(key3);
        fail("should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        keySet2.remove(key3);
        fail("should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    try {
        keySet2.clear();
        fail("should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
    // expected
    }
    selector.close();
    try {
        selector.keys();
        fail("should throw ClosedSelectorException");
    } catch (ClosedSelectorException e) {
    // expected
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 25 with ClosedSelectorException

use of java.nio.channels.ClosedSelectorException in project j2objc by google.

the class SelectorTest method test_selectedKeys.

/**
 * @tests java.nio.channels.Selector#keys()
 */
public void test_selectedKeys() throws IOException {
    SocketChannel sc = SocketChannel.open();
    ssc.register(selector, SelectionKey.OP_ACCEPT);
    try {
        int count = 0;
        sc.connect(localAddress);
        count = blockingSelect(SelectType.NULL, 0);
        assertEquals(1, count);
        Set<SelectionKey> selectedKeys = selector.selectedKeys();
        Set<SelectionKey> selectedKeys2 = selector.selectedKeys();
        assertSame(selectedKeys, selectedKeys2);
        assertEquals(1, selectedKeys.size());
        assertEquals(ssc.keyFor(selector), selectedKeys.iterator().next());
        // add one key into selectedKeys
        try {
            selectedKeys.add(ssc.keyFor(selector));
            fail("Should throw UnsupportedOperationException");
        } catch (UnsupportedOperationException e) {
        // expected
        }
        // no exception should be thrown
        selectedKeys.clear();
        Set<SelectionKey> selectedKeys3 = selector.selectedKeys();
        assertSame(selectedKeys, selectedKeys3);
        ssc.keyFor(selector).cancel();
        assertEquals(0, selectedKeys.size());
        selector.close();
        try {
            selector.selectedKeys();
            fail("should throw ClosedSelectorException");
        } catch (ClosedSelectorException e) {
        // expected
        }
    } finally {
        sc.close();
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Aggregations

ClosedSelectorException (java.nio.channels.ClosedSelectorException)30 SelectionKey (java.nio.channels.SelectionKey)20 IOException (java.io.IOException)16 ServerSocketChannel (java.nio.channels.ServerSocketChannel)8 SocketChannel (java.nio.channels.SocketChannel)8 CancelledKeyException (java.nio.channels.CancelledKeyException)6 ClosedChannelException (java.nio.channels.ClosedChannelException)5 Selector (java.nio.channels.Selector)4 NioConnectionException (com.cloud.utils.exception.NioConnectionException)2 Iterator (java.util.Iterator)2 EOFException (java.io.EOFException)1 InterruptedIOException (java.io.InterruptedIOException)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)1 SelectableChannel (java.nio.channels.SelectableChannel)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 HashMap (java.util.HashMap)1