All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.paralleluniverse.fibers.io.AsyncChannelGroup Maven / Gradle / Ivy

Go to download

The core library for Fibers on Java, compatible with Java 11-16. Forked from puniverse/quasar

There is a newer version: 10.0.6
Show newest version
/*
 * Quasar: lightweight threads and actors for the JVM.
 * Copyright (c) 2013-2015, Parallel Universe Software Co. All rights reserved.
 * 
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *  
 *   or (per the licensee's choosing)
 *  
 * under the terms of the GNU Lesser General Public License version 3.0
 * as published by the Free Software Foundation.
 */
package co.paralleluniverse.fibers.io;

import co.paralleluniverse.fibers.FiberScheduler;
import co.paralleluniverse.fibers.SchedulerLocal;
import co.paralleluniverse.fibers.SuspendExecution;
import java.io.IOException;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Uses an {@link AsynchronousChannelGroup} to implement a {@link ChannelGroup}.
 *
 * @author pron
 */
final class AsyncChannelGroup extends ChannelGroup {
    private final AsynchronousChannelGroup group;

    AsyncChannelGroup(AsynchronousChannelGroup group) {
        this.group = group;
    }

    @Override
    FiberSocketChannel newFiberSocketChannel() throws IOException {
        return new AsyncFiberSocketChannel(AsynchronousSocketChannel.open(group));
    }

    @Override
    FiberServerSocketChannel newFiberServerSocketChannel() throws IOException {
        return new AsyncFiberServerSocketChannel(AsynchronousServerSocketChannel.open(group));
    }

    @Override
    public void shutdown() {
        group.shutdown();
    }

    private static final SchedulerLocal defaultGroup = new SchedulerLocal() {

        @Override
        protected AsyncChannelGroup initialValue(FiberScheduler scheduler) {
            try {
                return new AsyncChannelGroup(AsynchronousChannelGroup.withThreadPool(protectScheduler(scheduler)));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };

//    private static final ThreadFactory NIO_THREAD_FACTORY = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("fiber-nio-%d").build();
//
//    public static AsynchronousChannelGroup newDefaultGroup() throws IOException {
//        return AsynchronousChannelGroup.withFixedThreadPool(1, NIO_THREAD_FACTORY);
//    }
    static AsyncChannelGroup getDefaultGroup() throws IOException, SuspendExecution {
        return defaultGroup.get();
    }

    static ExecutorService protectScheduler(FiberScheduler scheduler) {
        final Executor exec = scheduler.getExecutor();
        return new ExecutorService() {

            @Override
            public void execute(Runnable command) {
                exec.execute(command);
            }

            @Override
            public void shutdown() {
            }

            @Override
            public List shutdownNow() {
                throw new UnsupportedOperationException();
            }

            @Override
            public boolean isShutdown() {
                throw new UnsupportedOperationException();
            }

            @Override
            public boolean isTerminated() {
                throw new UnsupportedOperationException();
            }

            @Override
            public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
                throw new UnsupportedOperationException();
            }

            @Override
            public  Future submit(Callable task) {
                throw new UnsupportedOperationException();
            }

            @Override
            public  Future submit(Runnable task, T result) {
                throw new UnsupportedOperationException();
            }

            @Override
            public Future submit(Runnable task) {
                throw new UnsupportedOperationException();
            }

            @Override
            public  List> invokeAll(Collection> tasks) throws InterruptedException {
                throw new UnsupportedOperationException();
            }

            @Override
            public  List> invokeAll(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException {
                throw new UnsupportedOperationException();
            }

            @Override
            public  T invokeAny(Collection> tasks) throws InterruptedException, ExecutionException {
                throw new UnsupportedOperationException();
            }

            @Override
            public  T invokeAny(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
                throw new UnsupportedOperationException();
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy