Search in sources :

Example 61 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project xtext-core by eclipse.

the class SocketServerLauncher method main.

public static void main(final String[] args) {
    try {
        ServerModule _serverModule = new ServerModule();
        final Injector injector = Guice.createInjector(_serverModule);
        final LanguageServer languageServer = injector.<LanguageServer>getInstance(LanguageServer.class);
        final ServerSocketChannel serverSocket = ServerSocketChannel.open();
        InetSocketAddress _inetSocketAddress = new InetSocketAddress("localhost", 5007);
        serverSocket.bind(_inetSocketAddress);
        final SocketChannel socketChannel = serverSocket.accept();
        InputStream _newInputStream = Channels.newInputStream(socketChannel);
        OutputStream _newOutputStream = Channels.newOutputStream(socketChannel);
        PrintWriter _printWriter = new PrintWriter(System.out);
        final Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(languageServer, _newInputStream, _newOutputStream, true, _printWriter);
        launcher.startListening().get();
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : LanguageServer(org.eclipse.lsp4j.services.LanguageServer) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) Injector(com.google.inject.Injector) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ServerModule(org.eclipse.xtext.ide.server.ServerModule) PrintWriter(java.io.PrintWriter)

Example 62 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project ignite by apache.

the class GridNioServer method createSelector.

/**
 * Creates selector and binds server socket to a given address and port. If address is null
 * then will not bind any address and just creates a selector.
 *
 * @param addr Local address to listen on.
 * @return Created selector.
 * @throws IgniteCheckedException If selector could not be created or port is already in use.
 */
private Selector createSelector(@Nullable SocketAddress addr) throws IgniteCheckedException {
    Selector selector = null;
    ServerSocketChannel srvrCh = null;
    try {
        // Create a new selector
        selector = SelectorProvider.provider().openSelector();
        if (addr != null) {
            // Create a new non-blocking server socket channel
            srvrCh = ServerSocketChannel.open();
            srvrCh.configureBlocking(false);
            if (sockRcvBuf > 0)
                srvrCh.socket().setReceiveBufferSize(sockRcvBuf);
            // Bind the server socket to the specified address and port
            srvrCh.socket().bind(addr);
            // Register the server socket channel, indicating an interest in
            // accepting new connections
            srvrCh.register(selector, SelectionKey.OP_ACCEPT);
        }
        return selector;
    } catch (Throwable e) {
        U.close(srvrCh, log);
        U.close(selector, log);
        if (e instanceof Error)
            throw (Error) e;
        throw new IgniteCheckedException("Failed to initialize NIO selector.", e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 63 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project ignite by apache.

the class FileDownloader method start.

/**
 */
public InetSocketAddress start() throws IgniteCheckedException {
    try {
        ServerSocketChannel ch = ServerSocketChannel.open();
        ch.bind(null);
        serverChannel = ch;
        return (InetSocketAddress) ch.getLocalAddress();
    } catch (Exception ex) {
        throw new IgniteCheckedException(ex);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException)

Example 64 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project ignite by apache.

the class FileDownloader method download.

/**
 */
public void download(GridFutureAdapter<?> fut) {
    this.finishFut = fut;
    final ServerSocketChannel ch = serverChannel;
    fut.listen(new IgniteInClosureX<IgniteInternalFuture<?>>() {

        @Override
        public void applyx(IgniteInternalFuture<?> future) throws IgniteCheckedException {
            try {
                if (log != null && log.isInfoEnabled())
                    log.info("Server socket closed " + ch.getLocalAddress());
                ch.close();
            } catch (Exception ex) {
                U.error(log, "Fail close socket.", ex);
                throw new IgniteCheckedException(ex);
            }
        }
    });
    FileChannel writeChannel = null;
    SocketChannel readChannel = null;
    try {
        File f = new File(path.toUri().getPath());
        if (f.exists())
            f.delete();
        File cacheWorkDir = f.getParentFile();
        if (!cacheWorkDir.exists())
            cacheWorkDir.mkdir();
        writeChannel = FileChannel.open(path, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
        initFut.onDone();
        readChannel = serverChannel.accept();
        long pos = 0;
        long size = this.size.get();
        while (size == -1 || pos < size) {
            pos += writeChannel.transferFrom(readChannel, pos, CHUNK_SIZE);
            if (size == -1)
                size = this.size.get();
        }
    } catch (IOException ex) {
        initFut.onDone(ex);
        fut.onDone(ex);
    } finally {
        try {
            if (writeChannel != null)
                writeChannel.close();
        } catch (IOException ex) {
            throw new IgniteException("Could not close file: " + path);
        }
        try {
            if (readChannel != null)
                readChannel.close();
        } catch (IOException ex) {
            throw new IgniteException("Could not close socket");
        }
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) File(java.io.File) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 65 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project tomee by apache.

the class MultipointServer method doAccept.

private void doAccept(final SelectionKey key) throws IOException {
    // we are a server
    // when you are a server, we must first listen for the
    // address of the client before sending data.
    // once they send us their address, we will send our
    // full list of known addresses, followed by the "end"
    // address to signal that we are done.
    // Afterward we will only pulls our heartbeat
    final ServerSocketChannel server = (ServerSocketChannel) key.channel();
    final SocketChannel client = server.accept();
    final InetSocketAddress address = (InetSocketAddress) client.socket().getRemoteSocketAddress();
    client.configureBlocking(false);
    final Session session = new Session(client, address, null);
    session.trace("accept");
    session.state(SelectionKey.OP_READ, State.GREETING);
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Aggregations

ServerSocketChannel (java.nio.channels.ServerSocketChannel)122 SocketChannel (java.nio.channels.SocketChannel)65 InetSocketAddress (java.net.InetSocketAddress)55 IOException (java.io.IOException)43 ByteBuffer (java.nio.ByteBuffer)26 ServerSocket (java.net.ServerSocket)19 SelectionKey (java.nio.channels.SelectionKey)19 Selector (java.nio.channels.Selector)13 Socket (java.net.Socket)12 Test (org.junit.Test)11 ClosedChannelException (java.nio.channels.ClosedChannelException)10 InetAddress (java.net.InetAddress)9 ClosedSelectorException (java.nio.channels.ClosedSelectorException)8 SocketException (java.net.SocketException)6 Benchmark (org.openjdk.jmh.annotations.Benchmark)6 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)5 URISyntaxException (java.net.URISyntaxException)4 CancelledKeyException (java.nio.channels.CancelledKeyException)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4