Search in sources :

Example 6 with DataListener

use of org.webpieces.nio.api.handlers.DataListener in project webpieces by deanhiller.

the class Helper method read.

private static void read(SelectionKey key, SelectorManager2 mgr, BufferPool pool) throws IOException {
    log.trace(() -> key.attachment() + "reading data");
    WrapperAndListener struct = (WrapperAndListener) key.attachment();
    DataListener in = struct.getDataHandler();
    BasChannelImpl channel = (BasChannelImpl) struct.getChannel();
    //pressure in RAM so just wait until they re-registerForReads and they will get the data then
    if (!channel.isRegisteredForReads()) {
        //do not process reads if we were unregistered
        return;
    }
    ByteBuffer chunk = pool.nextBuffer(512);
    try {
        if (logBufferNextRead)
            log.info(channel + "buffer=" + chunk);
        int bytes = channel.readImpl(chunk);
        if (logBufferNextRead) {
            logBufferNextRead = false;
            log.info(channel + "buffer2=" + chunk);
        }
        processBytes(key, chunk, bytes, mgr);
    } catch (PortUnreachableException e) {
        //this is a normal occurence when some writes out udp to a port that is not
        //listening for udp!!!  log as finer and fire to client to deal with it.
        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) {
        //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.
        process(key, mgr, in, channel, chunk, e);
    } catch (NioException e) {
        Throwable cause = e.getCause();
        if (cause instanceof IOException) {
            IOException ioExc = (IOException) cause;
            process(key, mgr, in, channel, chunk, ioExc);
        } else
            throw e;
    }
}
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.nio.api.exceptions.NioException) ByteBuffer(java.nio.ByteBuffer)

Example 7 with DataListener

use of org.webpieces.nio.api.handlers.DataListener in project webpieces by deanhiller.

the class Helper method processBytes.

/**
     * @param id
     * @param b
     * @param bytes
     * @param mgr 
     * @throws IOException
     */
private static void processBytes(SelectionKey key, ByteBuffer data, int bytes, SelectorManager2 mgr) throws IOException {
    WrapperAndListener struct = (WrapperAndListener) key.attachment();
    DataListener in = struct.getDataHandler();
    BasChannelImpl channel = (BasChannelImpl) struct.getChannel();
    ByteBuffer b = data;
    b.flip();
    if (bytes < 0) {
        apiLog.trace(() -> channel + "far end closed, cancel key, close socket");
        channel.serverClosed();
        in.farEndClosed(channel);
    } else if (bytes > 0) {
        apiLog.trace(() -> channel + "READ bytes=" + bytes);
        in.incomingData(channel, b);
    }
}
Also used : DataListener(org.webpieces.nio.api.handlers.DataListener) ByteBuffer(java.nio.ByteBuffer)

Example 8 with DataListener

use of org.webpieces.nio.api.handlers.DataListener in project webpieces by deanhiller.

the class ThreadConnectionListener method translate.

private void translate(CompletableFuture<DataListener> future, DataListener listener) {
    DataListener wrappedDataListener = new ThreadDataListener(listener, executor);
    future.complete(wrappedDataListener);
}
Also used : DataListener(org.webpieces.nio.api.handlers.DataListener)

Example 9 with DataListener

use of org.webpieces.nio.api.handlers.DataListener in project webpieces by deanhiller.

the class TestS3InitialHttpConnections method setUp.

@Before
public void setUp() throws InterruptedException, ExecutionException, TimeoutException {
    MockTcpServerChannel svrChannel = new MockTcpServerChannel();
    mockChanMgr.addTCPSvrChannelToReturn(svrChannel);
    mockTcpChannel.setIncomingFrameDefaultReturnValue(CompletableFuture.completedFuture(mockTcpChannel));
    mockListener.setDefaultRetVal(mockStreamWriter);
    mockStreamWriter.setDefaultRetValToThis();
    Http2Config config = new Http2Config();
    config.setLocalSettings(localSettings);
    InjectionConfig injConfig = new InjectionConfig(mockTime, config);
    FrontendConfig frontendConfig = new FrontendConfig("http", new InetSocketAddress("me", 8080));
    HttpFrontendManager manager = HttpFrontendFactory.createFrontEnd(mockChanMgr, mockTimer, injConfig);
    HttpServer httpServer = manager.createHttpServer(frontendConfig, mockListener);
    httpServer.start();
    ConnectionListener listener = mockChanMgr.getSingleConnectionListener();
    CompletableFuture<DataListener> futureList = listener.connected(mockTcpChannel, true);
    DataListener dataListener = futureList.get(3, TimeUnit.SECONDS);
    mockChannel.setDataListener(dataListener);
}
Also used : FrontendConfig(org.webpieces.frontend2.api.FrontendConfig) HttpFrontendManager(org.webpieces.frontend2.api.HttpFrontendManager) InetSocketAddress(java.net.InetSocketAddress) Http2Config(com.webpieces.http2engine.api.client.Http2Config) HttpServer(org.webpieces.frontend2.api.HttpServer) InjectionConfig(com.webpieces.http2engine.api.client.InjectionConfig) DataListener(org.webpieces.nio.api.handlers.DataListener) ConnectionListener(org.webpieces.nio.api.handlers.ConnectionListener) MockTcpServerChannel(org.webpieces.httpfrontend2.api.mock2.MockTcpServerChannel) Before(org.junit.Before)

Example 10 with DataListener

use of org.webpieces.nio.api.handlers.DataListener in project webpieces by deanhiller.

the class AbstractHttp2Test method simulateClientConnecting.

private void simulateClientConnecting() throws InterruptedException, ExecutionException, TimeoutException {
    ConnectionListener listener = mockChanMgr.getSingleConnectionListener();
    CompletableFuture<DataListener> futureList = listener.connected(mockTcpChannel, true);
    DataListener dataListener = futureList.get(3, TimeUnit.SECONDS);
    mockChannel.setDataListener(dataListener);
}
Also used : DataListener(org.webpieces.nio.api.handlers.DataListener) ConnectionListener(org.webpieces.nio.api.handlers.ConnectionListener)

Aggregations

DataListener (org.webpieces.nio.api.handlers.DataListener)13 InetSocketAddress (java.net.InetSocketAddress)5 ConnectionListener (org.webpieces.nio.api.handlers.ConnectionListener)5 Channel (org.webpieces.nio.api.channels.Channel)3 ByteBuffer (java.nio.ByteBuffer)2 Executor (java.util.concurrent.Executor)2 BufferCreationPool (org.webpieces.data.api.BufferCreationPool)2 BufferPool (org.webpieces.data.api.BufferPool)2 TCPChannel (org.webpieces.nio.api.channels.TCPChannel)2 NamedThreadFactory (org.webpieces.util.threading.NamedThreadFactory)2 CalledMethod (biz.xsoftware.mock.CalledMethod)1 Http2Config (com.webpieces.http2engine.api.client.Http2Config)1 InjectionConfig (com.webpieces.http2engine.api.client.InjectionConfig)1 IOException (java.io.IOException)1 PortUnreachableException (java.net.PortUnreachableException)1 SocketAddress (java.net.SocketAddress)1 NotYetConnectedException (java.nio.channels.NotYetConnectedException)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 Before (org.junit.Before)1