Search in sources :

Example 91 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project eiger by wlloyd.

the class RandomAccessReader method read.

@Override
public // or readFully (from RandomAccessFile) will throw EOFException but this should not
int read(byte[] buff, int offset, int length) throws IOException {
    if (buffer == null)
        throw new ClosedChannelException();
    if (length == 0)
        return 0;
    if (isEOF())
        return -1;
    if (current >= bufferOffset + buffer.length || validBufferBytes == -1)
        reBuffer();
    assert current >= bufferOffset && current < bufferOffset + validBufferBytes : String.format("File (%s), current offset %d, buffer offset %d, buffer limit %d", getPath(), current, bufferOffset, validBufferBytes);
    int toCopy = Math.min(length, validBufferBytes - bufferCursor());
    System.arraycopy(buffer, bufferCursor(), buff, offset, toCopy);
    current += toCopy;
    return toCopy;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 92 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project eiger by wlloyd.

the class SequentialWriter method write.

public void write(byte[] data, int offset, int length) throws IOException {
    if (buffer == null)
        throw new ClosedChannelException();
    while (length > 0) {
        int n = writeAtMost(data, offset, length);
        offset += n;
        length -= n;
        isDirty = true;
        syncNeeded = true;
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 93 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project cloudstack by apache.

the class AgentManagerImpl method connectAgent.

protected void connectAgent(final Link link, final Command[] cmds, final Request request) {
    // send startupanswer to agent in the very beginning, so agent can move on without waiting for the answer for an undetermined time, if we put this logic into another
    // thread pool.
    final StartupAnswer[] answers = new StartupAnswer[cmds.length];
    Command cmd;
    for (int i = 0; i < cmds.length; i++) {
        cmd = cmds[i];
        if (cmd instanceof StartupRoutingCommand || cmd instanceof StartupProxyCommand || cmd instanceof StartupSecondaryStorageCommand || cmd instanceof StartupStorageCommand) {
            answers[i] = new StartupAnswer((StartupCommand) cmds[i], 0, getPingInterval());
            break;
        }
    }
    Response response = null;
    response = new Response(request, answers[0], _nodeId, -1);
    try {
        link.send(response.toBytes());
    } catch (final ClosedChannelException e) {
        s_logger.debug("Failed to send startupanswer: " + e.toString());
    }
    _connectExecutor.execute(new HandleAgentConnectTask(link, cmds, request));
}
Also used : StartupAnswer(com.cloud.agent.api.StartupAnswer) StartupProxyCommand(com.cloud.agent.api.StartupProxyCommand) StartupCommand(com.cloud.agent.api.StartupCommand) Response(com.cloud.agent.transport.Response) ClosedChannelException(java.nio.channels.ClosedChannelException) StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) StartupCommand(com.cloud.agent.api.StartupCommand) AgentControlCommand(com.cloud.agent.api.AgentControlCommand) PingCommand(com.cloud.agent.api.PingCommand) PingRoutingCommand(com.cloud.agent.api.PingRoutingCommand) SetHostParamsCommand(com.cloud.agent.api.SetHostParamsCommand) StartupSecondaryStorageCommand(com.cloud.agent.api.StartupSecondaryStorageCommand) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) StartupProxyCommand(com.cloud.agent.api.StartupProxyCommand) CheckHealthCommand(com.cloud.agent.api.CheckHealthCommand) Command(com.cloud.agent.api.Command) ReadyCommand(com.cloud.agent.api.ReadyCommand) StartupSecondaryStorageCommand(com.cloud.agent.api.StartupSecondaryStorageCommand) StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand)

Example 94 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project cloudstack by apache.

the class Agent method stop.

public void stop(final String reason, final String detail) {
    s_logger.info("Stopping the agent: Reason = " + reason + (detail != null ? ": Detail = " + detail : ""));
    if (_connection != null) {
        final ShutdownCommand cmd = new ShutdownCommand(reason, detail);
        try {
            if (_link != null) {
                final Request req = new Request(_id != null ? _id : -1, -1, cmd, false);
                _link.send(req.toBytes());
            }
        } catch (final ClosedChannelException e) {
            s_logger.warn("Unable to send: " + cmd.toString());
        } catch (final Exception e) {
            s_logger.warn("Unable to send: " + cmd.toString() + " due to exception: ", e);
        }
        s_logger.debug("Sending shutdown to management server");
        try {
            Thread.sleep(1000);
        } catch (final InterruptedException e) {
            s_logger.debug("Who the heck interrupted me here?");
        }
        _connection.stop();
        _connection = null;
    }
    if (_resource != null) {
        _resource.stop();
        _resource = null;
    }
    _ugentTaskPool.shutdownNow();
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) Request(com.cloud.agent.transport.Request) ShutdownCommand(com.cloud.agent.api.ShutdownCommand) TaskExecutionException(com.cloud.utils.exception.TaskExecutionException) AgentControlChannelException(com.cloud.exception.AgentControlChannelException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NioConnectionException(com.cloud.utils.exception.NioConnectionException)

Example 95 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project robovm by robovm.

the class ConcurrentCloseTest method test_connect_nonBlocking.

public void test_connect_nonBlocking() throws Exception {
    StuckServer ss = new StuckServer(false);
    SocketChannel s = SocketChannel.open();
    new Killer(s.socket()).start();
    try {
        System.err.println("connect (non-blocking)...");
        s.configureBlocking(false);
        s.connect(ss.getLocalSocketAddress());
        while (!s.finishConnect()) {
        // Spin like a mad thing!
        }
        fail("connect returned: " + s + "!");
    } catch (SocketException expected) {
        assertEquals("Socket closed", expected.getMessage());
    } catch (AsynchronousCloseException alsoOkay) {
    // See below.
    } catch (ClosedChannelException alsoOkay) {
    // For now, I'm assuming that we're happy as long as we get any reasonable exception.
    // It may be that we're supposed to guarantee only one or the other.
    } finally {
        ss.close();
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SocketException(java.net.SocketException) ClosedChannelException(java.nio.channels.ClosedChannelException) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) StuckServer(tests.net.StuckServer)

Aggregations

ClosedChannelException (java.nio.channels.ClosedChannelException)211 ByteBuffer (java.nio.ByteBuffer)67 IOException (java.io.IOException)60 Test (org.junit.Test)23 InetSocketAddress (java.net.InetSocketAddress)19 SelectionKey (java.nio.channels.SelectionKey)18 SocketChannel (java.nio.channels.SocketChannel)15 ArrayList (java.util.ArrayList)13 NotYetConnectedException (java.nio.channels.NotYetConnectedException)11 InterruptedIOException (java.io.InterruptedIOException)10 CancelledKeyException (java.nio.channels.CancelledKeyException)10 ShutdownCommand (com.cloud.agent.api.ShutdownCommand)9 File (java.io.File)9 ServerSocketChannel (java.nio.channels.ServerSocketChannel)9 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)8 FileChannel (java.nio.channels.FileChannel)8 ConnectException (java.net.ConnectException)7 FsVolumeReference (org.apache.hadoop.hdfs.server.datanode.fsdataset.FsVolumeReference)6 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)5 Command (com.cloud.agent.api.Command)5