Search in sources :

Example 81 with Selector

use of java.nio.channels.Selector in project jdk8u_jdk by JetBrains.

the class LotsOfCancels method runTest.

static void runTest(int initCount, int massCount, int maxSelectTime) throws Exception {
    testStartTime = System.nanoTime();
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", 7359);
    // Create server channel, add it to selector and run epoll_ctl.
    log("Setting up server");
    Selector serverSelector = Selector.open();
    ServerSocketChannel server = ServerSocketChannel.open();
    server.configureBlocking(false);
    server.socket().bind(address, 5000);
    server.register(serverSelector, SelectionKey.OP_ACCEPT);
    serverSelector.selectNow();
    log("Setting up client");
    ClientThread client = new ClientThread(address);
    client.start();
    Thread.sleep(100);
    // Set up initial set of client sockets.
    log("Starting initial client connections");
    client.connectClients(initCount);
    // Wait for client connections to arrive
    Thread.sleep(500);
    // Accept all initial client sockets, add to selector and run
    // epoll_ctl.
    log("Accepting initial connections");
    List<SocketChannel> serverChannels1 = acceptAndAddAll(serverSelector, server, initCount);
    if (serverChannels1.size() != initCount) {
        throw new Exception("Accepted " + serverChannels1.size() + " instead of " + initCount);
    }
    serverSelector.selectNow();
    // Set up mass set of client sockets.
    log("Requesting mass client connections");
    client.connectClients(massCount);
    // Wait for client connections to arrive
    Thread.sleep(500);
    // Accept all mass client sockets, add to selector and do NOT
    // run epoll_ctl.
    log("Accepting mass connections");
    List<SocketChannel> serverChannels2 = acceptAndAddAll(serverSelector, server, massCount);
    if (serverChannels2.size() != massCount) {
        throw new Exception("Accepted " + serverChannels2.size() + " instead of " + massCount);
    }
    // Close initial set of sockets.
    log("Closing initial connections");
    closeAll(serverChannels1);
    // Now get the timing of select() call.
    log("Running the final select call");
    long startTime = System.nanoTime();
    serverSelector.selectNow();
    long duration = durationMillis(startTime);
    log("Init count = " + initCount + ", mass count = " + massCount + ", duration = " + duration + "ms");
    if (duration > maxSelectTime) {
        System.out.println("\n\n\n\n\nFAILURE: The final selectNow() took " + duration + "ms " + "- seems like O(N^2) bug is still here\n\n");
        System.exit(1);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 82 with Selector

use of java.nio.channels.Selector in project jdk8u_jdk by JetBrains.

the class SelectWrite method main.

public static void main(String[] argv) throws Exception {
    try (ByteServer server = new ByteServer();
        SocketChannel sc = SocketChannel.open(server.address())) {
        server.acceptConnection();
        try (Selector sel = Selector.open()) {
            sc.configureBlocking(false);
            sc.register(sel, SelectionKey.OP_WRITE);
            sel.select();
            sel.selectedKeys().clear();
            if (sel.select() == 0) {
                throw new Exception("Select returned zero");
            }
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) Selector(java.nio.channels.Selector)

Example 83 with Selector

use of java.nio.channels.Selector in project webpieces by deanhiller.

the class TestUdpIntegration method testRawDatagram.

//TODO: file a Sun bug.  We found another one.  This one only happens on linux
//not windows....
public void testRawDatagram() throws Exception {
    Selector sel = Selector.open();
    DatagramChannel peer1 = DatagramChannel.open();
    DatagramChannel peer2 = DatagramChannel.open();
    int port1 = 19858;
    int port2 = 19533;
    InetSocketAddress peer1Addr = new InetSocketAddress(port1);
    InetSocketAddress peer2Addr = new InetSocketAddress(port2);
    //NOTE: replace the above with the following and this test will fail on linux
    //InetSocketAddress peer1Addr = new InetSocketAddress(host, 0);
    //InetSocketAddress peer2Addr = new InetSocketAddress(host, 0);
    peer1.configureBlocking(false);
    peer2.configureBlocking(false);
    Object o = new Object();
    peer1.register(sel, SelectionKey.OP_READ, o);
    peer2.register(sel, SelectionKey.OP_READ, o);
    peer1.socket().bind(peer1Addr);
    peer2.socket().bind(peer2Addr);
    InetAddress localHost = InetAddress.getLocalHost();
    peer1Addr = new InetSocketAddress(localHost, port1);
    peer2Addr = new InetSocketAddress(localHost, port2);
    peer1.connect(peer2Addr);
    peer2.connect(peer1Addr);
    String msg = "Asfdsf";
    sendMessage(peer1, peer2, msg);
    peer1.disconnect();
    String msg2 = "shouldBedropped";
    writePacket(peer2, msg2);
    Thread.sleep(1000);
    peer1.connect(peer2Addr);
    sel.select(10000);
    sendMessage(peer1, peer2, msg2);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) MockObject(biz.xsoftware.mock.MockObject) InetAddress(java.net.InetAddress) Selector(java.nio.channels.Selector)

Aggregations

Selector (java.nio.channels.Selector)83 SelectionKey (java.nio.channels.SelectionKey)42 IOException (java.io.IOException)24 SocketChannel (java.nio.channels.SocketChannel)24 ServerSocketChannel (java.nio.channels.ServerSocketChannel)17 InetSocketAddress (java.net.InetSocketAddress)16 ByteBuffer (java.nio.ByteBuffer)9 ClosedChannelException (java.nio.channels.ClosedChannelException)8 DatagramChannel (java.nio.channels.DatagramChannel)7 CancelledKeyException (java.nio.channels.CancelledKeyException)6 ClosedSelectorException (java.nio.channels.ClosedSelectorException)4 SelectableChannel (java.nio.channels.SelectableChannel)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Test (org.junit.Test)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)3 BigDecimal (java.math.BigDecimal)2 ConnectException (java.net.ConnectException)2 DatagramPacket (java.net.DatagramPacket)2 DatagramSocket (java.net.DatagramSocket)2 InetAddress (java.net.InetAddress)2