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);
}
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());
}
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());
}
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();
}
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());
}
Aggregations