Search in sources :

Example 1 with NioException

use of org.webpieces.util.exceptions.NioException in project webpieces by deanhiller.

the class UDPChannelImpl method disconnect.

public synchronized void disconnect() {
    if (apiLog.isTraceEnabled())
        apiLog.trace(this + "Basic.disconnect called");
    try {
        channelState = ChannelState.CLOSED;
        channel.disconnect();
    } catch (IOException e) {
        throw new NioException(e);
    }
}
Also used : IOException(java.io.IOException) NioException(org.webpieces.util.exceptions.NioException)

Example 2 with NioException

use of org.webpieces.util.exceptions.NioException in project webpieces by deanhiller.

the class XFileReaderFileSystem method createFileReader.

protected ChunkFileSystemReader createFileReader(Http2Response response, RenderStaticResponse renderStatic, String fileName, VirtualFile fullFilePath, RequestInfo info, String extension, ResponseEncodingTuple tuple, ProxyStreamHandle handle) {
    Path file;
    Compression compr = compressionLookup.createCompressionStream(info.getRouterRequest().encodings, tuple.mimeType);
    // during startup as I don't feel like paying a cpu penalty for compressing while live
    if (compr != null && compr.getCompressionType().equals(routerConfig.getStartupCompression())) {
        handle.turnCompressionOff();
        response.addHeader(new Http2Header(Http2HeaderName.CONTENT_ENCODING, compr.getCompressionType()));
        File routesCache = renderStatic.getTargetCache();
        String relativeUrl = renderStatic.getRelativeUrl();
        File fileReference;
        if (relativeUrl == null) {
            fileReference = FileFactory.newFile(routesCache, fileName);
        } else {
            fileReference = FileFactory.newFile(routesCache, relativeUrl);
        }
        file = fetchFile("Compressed File from cache=", fileReference.getAbsolutePath() + ".gz");
    } else {
        file = fetchFile("File=", fullFilePath.getAbsolutePath());
    }
    AsynchronousFileChannel asyncFile;
    try {
        asyncFile = AsynchronousFileChannel.open(file, options, fileExecutor);
        ChunkFileSystemReader reader = new ChunkFileSystemReader(asyncFile, file);
        return reader;
    } catch (IOException e) {
        throw new NioException("Open Channel Exception " + file, e);
    }
}
Also used : Path(java.nio.file.Path) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) Compression(org.webpieces.router.impl.compression.Compression) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) IOException(java.io.IOException) NioException(org.webpieces.util.exceptions.NioException) File(java.io.File) VirtualFile(org.webpieces.util.file.VirtualFile)

Example 3 with NioException

use of org.webpieces.util.exceptions.NioException in project webpieces by deanhiller.

the class KeyProcessor method read.

private void read(SelectionKey key, ChannelInfo info) throws IOException {
    if (log.isTraceEnabled())
        log.trace(info.getChannel() + "reading data");
    DataListener in = info.getDataHandler();
    BasChannelImpl channel = (BasChannelImpl) info.getChannel();
    MDCUtil.setMDC(channel.isServerSide(), channel.getChannelId());
    ByteBuffer chunk = pool.nextBuffer(1024);
    try {
        if (logBufferNextRead)
            log.info(channel + " buffer=" + chunk);
        int bytes = channel.readImpl(chunk);
        if (logBufferNextRead) {
            logBufferNextRead = false;
            log.info(channel + " buffer2=" + chunk);
        }
        processBytes(key, info, chunk, bytes);
    } catch (PortUnreachableException e) {
        connectionErrors.increment();
        // listening for udp!!!  log as finer and fire to client to deal with it.
        if (log.isTraceEnabled())
            log.trace("Client sent data to a host or port that is not listening " + "to udp, or udp can't get through to that machine", e);
        in.failure(channel, null, e);
    } catch (NotYetConnectedException e) {
        connectionErrors.increment();
        // this happens in udp when I disconnect after someone had already been streaming me
        // data.  It is supposed to stop listening but selector keeps firing.
        log.error("Can't read until UDPChannel is connected", e);
        in.failure(channel, null, e);
    } catch (IOException e) {
        // kept getting the following exception so I added this as protection
        // NOTE: this exceptionn should never occur.  read should be returning -1
        // but for some reason is returning hte exception instead.
        // WARNING: [server] Processing of key failed, closing channel
        // java.io.IOException: An established connection was aborted by the software in your host machine
        // at sun.nio.ch.SocketDispatcher.read0(Native Method)
        // at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:25)
        // at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:233)
        // at sun.nio.ch.IOUtil.read(IOUtil.java:206)
        // at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:207)
        // at biz.xsoftware.impl.nio.cm.basic.TCPChannelImpl.readImpl(TCPChannelImpl.java:168)
        // at biz.xsoftware.impl.nio.cm.basic.Helper.read(Helper.java:128)
        // at biz.xsoftware.impl.nio.cm.basic.Helper.processKey(Helper.java:77)
        // at biz.xsoftware.impl.nio.cm.basic.Helper.processKeys(Helper.java:43)
        // at biz.xsoftware.impl.nio.cm.basic.SelectorManager2.runLoop(SelectorManager2.java:262)
        // at biz.xsoftware.impl.nio.cm.basic.SelectorManager2$PollingThread.run
        // (SelectorManager2.java:224)
        // another one that landes here is the Connection reset by peer"....
        // Jan 18, 2012 1:00:42 PM biz.xsoftware.impl.nio.cm.basic.Helper read
        // INFO: [stserver] Exception
        // java.io.IOException: Connection reset by peer
        // at sun.nio.ch.FileDispatcher.read0(Native Method)
        // at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
        // at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:251)
        // at sun.nio.ch.IOUtil.read(IOUtil.java:224)
        // at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:254)
        // at biz.xsoftware.impl.nio.cm.basic.chanimpl.SocketChannelImpl.read(SocketChannelImpl.java:65)
        // at biz.xsoftware.impl.nio.cm.basic.BasTCPChannel.readImpl(BasTCPChannel.java:108)
        // at biz.xsoftware.impl.nio.cm.basic.Helper.read(Helper.java:162)
        // at biz.xsoftware.impl.nio.cm.basic.Helper.processKey(Helper.java:104)
        // at biz.xsoftware.impl.nio.cm.basic.Helper.processKeys(Helper.java:51)
        // at biz.xsoftware.impl.nio.cm.basic.SelectorManager2.selectorFired(SelectorManager2.java:253)
        // at biz.xsoftware.impl.nio.cm.basic.nioimpl.SelectorImpl.runLoop(SelectorImpl.java:126)
        // at biz.xsoftware.impl.nio.cm.basic.nioimpl.SelectorImpl$PollingThread.run(SelectorImpl.java:107)
        // One other exception starts with "An existing connection was forcibly closed"
        // in the case of SSL over TCP only, sometimes instead of reading off a -1, this will
        // throw an IOException: "An existing connection was forcibly closed by the remote host"
        // we also close UDPChannels as well on IOException.  Not sure if this is good or not.
        specialConnectionErrors.increment();
        process(key, in, info, chunk, e);
    } catch (NioException e) {
        connectionErrors.increment();
        Throwable cause = e.getCause();
        if (cause instanceof IOException) {
            specialConnectionErrors.increment();
            IOException ioExc = (IOException) cause;
            process(key, in, info, chunk, ioExc);
        } else
            throw e;
    } finally {
        MDCUtil.setMDC(channel.isServerSide(), channel.getChannelId());
    }
}
Also used : PortUnreachableException(java.net.PortUnreachableException) DataListener(org.webpieces.nio.api.handlers.DataListener) NotYetConnectedException(java.nio.channels.NotYetConnectedException) IOException(java.io.IOException) NioException(org.webpieces.util.exceptions.NioException) ByteBuffer(java.nio.ByteBuffer)

Example 4 with NioException

use of org.webpieces.util.exceptions.NioException in project webpieces by deanhiller.

the class DatagramChannelImpl method bind.

/**
 * @return
 * @see org.webpieces.nio.api.channels.RegisterableChannel#bind(java.net.SocketAddress)
 */
public XFuture<Void> bind(SocketAddress addr) {
    try {
        socket = new DatagramSocket(addr);
        readerThread = new ReaderThread();
        readerThread.start();
        return XFuture.completedFuture(null);
    } catch (IOException e) {
        throw new NioException(e);
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) IOException(java.io.IOException) NioException(org.webpieces.util.exceptions.NioException)

Example 5 with NioException

use of org.webpieces.util.exceptions.NioException in project webpieces by deanhiller.

the class TestConnecting method testWriteBeforeConnection.

@Test
public void testWriteBeforeConnection() throws InterruptedException, ExecutionException, TimeoutException {
    MockDataListener listener = new MockDataListener();
    TCPChannel channel = mgr.createTCPChannel("myid");
    mockChannel.addConnectReturnValue(false);
    XFuture<Void> future = channel.connect(new InetSocketAddress(4444), listener);
    Assert.assertEquals(1, mockJdk.getNumTimesWokenUp());
    Assert.assertFalse(future.isDone());
    // simulate being on selector thread
    mockJdk.setThread(Thread.currentThread());
    // next simulate the selector waking up and firing
    mockJdk.fireSelector();
    // still not done
    Assert.assertFalse(future.isDone());
    Assert.assertEquals(0, mockChannel.getNumTimesFinishConnectCalled());
    Assert.assertFalse(mockChannel.isRegisteredForReads());
    try {
        channel.write(ByteBuffer.wrap(new byte[] { 1 }));
        Assert.fail("should have thrown exception since channel is not connected yet");
    } catch (NioException e) {
    }
    // NOW, fire the selector AND return ready for finishing connection so the connection future resolves
    mockChannel.setReadyToConnect();
    mockJdk.fireSelector();
    Assert.assertTrue(mockChannel.isRegisteredForReads());
    Assert.assertEquals(1, mockChannel.getNumTimesFinishConnectCalled());
    future.get(2, TimeUnit.SECONDS);
    channel.write(ByteBuffer.wrap(new byte[] { 1 }));
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) InetSocketAddress(java.net.InetSocketAddress) MockDataListener(org.webpieces.nio.api.mocks.MockDataListener) NioException(org.webpieces.util.exceptions.NioException) Test(org.junit.Test)

Aggregations

NioException (org.webpieces.util.exceptions.NioException)7 IOException (java.io.IOException)6 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)1 File (java.io.File)1 DatagramSocket (java.net.DatagramSocket)1 InetSocketAddress (java.net.InetSocketAddress)1 PortUnreachableException (java.net.PortUnreachableException)1 ByteBuffer (java.nio.ByteBuffer)1 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)1 NotYetConnectedException (java.nio.channels.NotYetConnectedException)1 SelectionKey (java.nio.channels.SelectionKey)1 Path (java.nio.file.Path)1 Test (org.junit.Test)1 TCPChannel (org.webpieces.nio.api.channels.TCPChannel)1 DataListener (org.webpieces.nio.api.handlers.DataListener)1 Keys (org.webpieces.nio.api.jdk.Keys)1 MockDataListener (org.webpieces.nio.api.mocks.MockDataListener)1 Compression (org.webpieces.router.impl.compression.Compression)1 VirtualFile (org.webpieces.util.file.VirtualFile)1