Search in sources :

Example 16 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project iobserve-analysis by research-iobserve.

the class MultipleConnectionTcpReaderStage method execute.

@Override
protected void execute() {
    try {
        final ServerSocketChannel serverSocket = ServerSocketChannel.open();
        serverSocket.bind(new InetSocketAddress(this.inputPort));
        serverSocket.configureBlocking(false);
        final Selector readSelector = Selector.open();
        while (this.isActive()) {
            final SocketChannel socketChannel = serverSocket.accept();
            if (socketChannel != null) {
                this.logger.debug("Connection from {}.", socketChannel.getRemoteAddress().toString());
                // add socketChannel to list of channels
                socketChannel.configureBlocking(false);
                final SelectionKey key = socketChannel.register(readSelector, SelectionKey.OP_READ);
                final Connection connection = new Connection(socketChannel, this.bufferSize);
                key.attach(connection);
            }
            final int readReady = readSelector.selectNow();
            if (readReady > 0) {
                final Set<SelectionKey> selectedKeys = readSelector.selectedKeys();
                final Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
                while (keyIterator.hasNext()) {
                    final SelectionKey key = keyIterator.next();
                    this.readFromSocket(key);
                    keyIterator.remove();
                }
                selectedKeys.clear();
            }
        }
    } catch (final ClosedByInterruptException e) {
        this.logger.info("External shutdown called");
    } catch (final IOException e) {
        this.logger.error("Cannot establish listening port", e);
    } finally {
        this.workCompleted();
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 17 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project Bytecoder by mirkosertic.

the class FileChannelImpl method lock.

public FileLock lock(long position, long size, boolean shared) throws IOException {
    ensureOpen();
    if (shared && !readable)
        throw new NonReadableChannelException();
    if (!shared && !writable)
        throw new NonWritableChannelException();
    FileLockImpl fli = new FileLockImpl(this, position, size, shared);
    FileLockTable flt = fileLockTable();
    flt.add(fli);
    boolean completed = false;
    int ti = -1;
    try {
        begin();
        ti = threads.add();
        if (!isOpen())
            return null;
        int n;
        do {
            n = nd.lock(fd, true, position, size, shared);
        } while ((n == FileDispatcher.INTERRUPTED) && isOpen());
        if (isOpen()) {
            if (n == FileDispatcher.RET_EX_LOCK) {
                assert shared;
                FileLockImpl fli2 = new FileLockImpl(this, position, size, false);
                flt.replace(fli, fli2);
                fli = fli2;
            }
            completed = true;
        }
    } finally {
        if (!completed)
            flt.remove(fli);
        threads.remove(ti);
        try {
            end(completed);
        } catch (ClosedByInterruptException e) {
            throw new FileLockInterruptionException();
        }
    }
    return fli;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) NonReadableChannelException(java.nio.channels.NonReadableChannelException) FileLockInterruptionException(java.nio.channels.FileLockInterruptionException) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Example 18 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project fluency by komamitsu.

the class MockMultiTCPServerWithMetrics method start.

@Override
public synchronized void start() throws Exception {
    super.start();
    if (executorService != null) {
        return;
    }
    executorService = Executors.newSingleThreadExecutor();
    channel = DatagramChannel.open();
    channel.socket().bind(new InetSocketAddress(getLocalPort()));
    executorService.execute(new Runnable() {

        @Override
        public void run() {
            while (executorService != null && !executorService.isTerminated()) {
                try {
                    ByteBuffer buffer = ByteBuffer.allocate(8);
                    SocketAddress socketAddress = channel.receive(buffer);
                    assertEquals(0, buffer.position());
                    channel.send(buffer, socketAddress);
                } catch (ClosedByInterruptException e) {
                // Expected
                } catch (IOException e) {
                    LOG.warn("Failed to receive or send heartbeat", e);
                }
            }
        }
    });
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ByteBuffer(java.nio.ByteBuffer)

Example 19 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project evosuite by EvoSuite.

the class EvoFileChannel method read.

private int read(ByteBuffer[] dsts, int offset, int length, AtomicInteger posToUpdate) throws IOException {
    if (!isOpenForRead) {
        throw new NonReadableChannelException();
    }
    throwExceptionIfClosed();
    int counter = 0;
    synchronized (readWriteMonitor) {
        for (int j = offset; j < length; j++) {
            ByteBuffer dst = dsts[j];
            int r = dst.remaining();
            for (int i = 0; i < r; i++) {
                int b = NativeMockedIO.read(path, posToUpdate);
                if (b < 0) {
                    // end of stream
                    return -1;
                }
                if (closed) {
                    throw new AsynchronousCloseException();
                }
                if (Thread.currentThread().isInterrupted()) {
                    close();
                    throw new ClosedByInterruptException();
                }
                dst.put((byte) b);
                counter++;
            }
        }
    }
    return counter;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) NonReadableChannelException(java.nio.channels.NonReadableChannelException) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 20 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project vespa by vespa-engine.

the class MockFDispatch method run.

public void run() {
    try {
        ServerSocketChannel channel = createServerSocket(listenPort);
        channel.socket().setReuseAddress(true);
        while (!Thread.currentThread().isInterrupted()) {
            try {
                // can now proceed and talk to us
                synchronized (barrier) {
                    if (barrier != null) {
                        barrier.notify();
                    }
                }
                SocketChannel socketChannel = channel.accept();
                connectionThreads.add(new ConnectionThread(socketChannel));
            } catch (ClosedByInterruptException e) {
            // We'll exit
            } catch (ClosedChannelException e) {
                return;
            } catch (Exception e) {
                log.log(Level.WARNING, "Unexpected error reading request", e);
            }
        }
        channel.close();
    } catch (IOException e) {
        throw new ConfigurationException("Socket channel failure", e);
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) ConfigurationException(com.yahoo.prelude.ConfigurationException) IOException(java.io.IOException) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ConfigurationException(com.yahoo.prelude.ConfigurationException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException)

Aggregations

ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)79 IOException (java.io.IOException)48 ByteBuffer (java.nio.ByteBuffer)15 ClosedChannelException (java.nio.channels.ClosedChannelException)11 SocketTimeoutException (java.net.SocketTimeoutException)9 InetSocketAddress (java.net.InetSocketAddress)7 MappedByteBuffer (java.nio.MappedByteBuffer)7 SocketChannel (java.nio.channels.SocketChannel)7 File (java.io.File)6 ServerSocketChannel (java.nio.channels.ServerSocketChannel)6 ServerSocket (java.net.ServerSocket)5 FileChannel (java.nio.channels.FileChannel)5 FileLockInterruptionException (java.nio.channels.FileLockInterruptionException)5 InterruptedIOException (java.io.InterruptedIOException)4 Path (java.nio.file.Path)4 Test (org.junit.Test)4 BuildId (com.facebook.buck.model.BuildId)3 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 SocketException (java.net.SocketException)3