Search in sources :

Example 1 with AsynchronousServerSocketChannel

use of java.nio.channels.AsynchronousServerSocketChannel in project baseio by generallycloud.

the class AioServerAccept method main.

public static void main(String[] args) throws Exception {
    // AsynchronousChannelGroup group = AsynchronousChannelGroup.withThreadPool(Executors.newFixedThreadPool(4));
    AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress("0.0.0.0", 8013));
    for (; ; ) {
        Future<AsynchronousSocketChannel> f = server.accept();
        AsynchronousSocketChannel channel = f.get();
        DebugUtil.debug("client:" + channel.toString());
    }
// group.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
}
Also used : AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) InetSocketAddress(java.net.InetSocketAddress) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel)

Example 2 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_bind.

/* J2ObjC removed: unsupported ResourceLeakageDetector
    @Rule
    public LeakageDetectorRule leakageDetectorRule = ResourceLeakageDetector.getRule();
     */
public void test_bind() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    assertTrue(assc.isOpen());
    assertNull(assc.getLocalAddress());
    assc.bind(new InetSocketAddress(0));
    assertNotNull(assc.getLocalAddress());
    try {
        assc.bind(new InetSocketAddress(0));
        fail();
    } catch (AlreadyBoundException expected) {
    }
    assc.close();
    assertFalse(assc.isOpen());
}
Also used : AlreadyBoundException(java.nio.channels.AlreadyBoundException) InetSocketAddress(java.net.InetSocketAddress) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel)

Example 3 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_future_concurrent_close.

public void test_future_concurrent_close() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    assc.bind(new InetSocketAddress(0));
    final AtomicReference<Exception> killerThreadException = new AtomicReference<Exception>(null);
    final Thread killer = new Thread(new Runnable() {

        public void run() {
            try {
                Thread.sleep(2000);
                assc.close();
            } catch (Exception ex) {
                killerThreadException.set(ex);
            }
        }
    });
    killer.start();
    Future<AsynchronousSocketChannel> acceptFuture = assc.accept();
    try {
        // This may timeout on slow devices, they may need more time for the killer thread to
        // do its thing.
        acceptFuture.get(10000, TimeUnit.MILLISECONDS);
        fail();
    } catch (ExecutionException expected) {
        assertTrue(expected.getCause() instanceof ClosedChannelException);
    }
    assertNull(killerThreadException.get());
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) InetSocketAddress(java.net.InetSocketAddress) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionException(java.util.concurrent.ExecutionException) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) BindException(java.net.BindException) NotYetBoundException(java.nio.channels.NotYetBoundException) ExecutionException(java.util.concurrent.ExecutionException) AlreadyBoundException(java.nio.channels.AlreadyBoundException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException)

Example 4 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_bind_unresolvedAddress.

public void test_bind_unresolvedAddress() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    try {
        assc.bind(new InetSocketAddress("unresolvedname", 31415));
        fail();
    } catch (UnresolvedAddressException expected) {
    }
    assertNull(assc.getLocalAddress());
    assertTrue(assc.isOpen());
    assc.close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException)

Example 5 with AsynchronousServerSocketChannel

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

the class AsynchronousServerSocketChannelTest method test_completionHandler_concurrent_close.

public void test_completionHandler_concurrent_close() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    assc.bind(new InetSocketAddress(0));
    final AtomicReference<Exception> killerThreadException = new AtomicReference<Exception>(null);
    final Thread killer = new Thread(new Runnable() {

        public void run() {
            try {
                Thread.sleep(2000);
                assc.close();
            } catch (Exception ex) {
                killerThreadException.set(ex);
            }
        }
    });
    killer.start();
    FutureLikeCompletionHandler<AsynchronousSocketChannel> acceptCompletionHandler = new FutureLikeCompletionHandler<AsynchronousSocketChannel>();
    assc.accept(null, /* attachment */
    acceptCompletionHandler);
    try {
        // This may timeout on slow devices, they may need more time for the killer thread to
        // do its thing.
        acceptCompletionHandler.get(10000);
        fail();
    } catch (AsynchronousCloseException expected) {
    }
    assertNull(killerThreadException.get());
}
Also used : AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) InetSocketAddress(java.net.InetSocketAddress) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) BindException(java.net.BindException) NotYetBoundException(java.nio.channels.NotYetBoundException) ExecutionException(java.util.concurrent.ExecutionException) AlreadyBoundException(java.nio.channels.AlreadyBoundException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException)

Aggregations

AsynchronousServerSocketChannel (java.nio.channels.AsynchronousServerSocketChannel)21 InetSocketAddress (java.net.InetSocketAddress)15 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)12 AlreadyBoundException (java.nio.channels.AlreadyBoundException)4 ExecutionException (java.util.concurrent.ExecutionException)4 ServerSocket (java.net.ServerSocket)3 Socket (java.net.Socket)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 NotYetBoundException (java.nio.channels.NotYetBoundException)3 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)3 IOException (java.io.IOException)2 BindException (java.net.BindException)2 ByteBuffer (java.nio.ByteBuffer)2 AsynchronousChannelGroup (java.nio.channels.AsynchronousChannelGroup)2 AsynchronousCloseException (java.nio.channels.AsynchronousCloseException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Inet6Address (java.net.Inet6Address)1 SocketOption (java.net.SocketOption)1 AsynchronousChannelProvider (java.nio.channels.spi.AsynchronousChannelProvider)1 Path (java.nio.file.Path)1