Search in sources :

Example 11 with Channel

use of org.webpieces.nio.api.channels.Channel in project webpieces by deanhiller.

the class ConnectedChannels method closeChannels.

public CompletableFuture<Void> closeChannels() {
    //first prevent other threads from calling above functions ever again
    closed = true;
    List<CompletableFuture<Channel>> futures = new ArrayList<>();
    for (Channel c : connectedChannels.keySet()) {
        futures.add(c.close());
    }
    @SuppressWarnings("rawtypes") CompletableFuture[] array = futures.toArray(new CompletableFuture[0]);
    return CompletableFuture.allOf(array);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Channel(org.webpieces.nio.api.channels.Channel) ArrayList(java.util.ArrayList)

Example 12 with Channel

use of org.webpieces.nio.api.channels.Channel in project webpieces by deanhiller.

the class TestBasicSsl method testBasic.

@Test
public void testBasic() throws InterruptedException, ExecutionException {
    ChannelManager svrMgr = createSvrChanMgr("server");
    SelfSignedSSLEngineFactory sslFactory = new SelfSignedSSLEngineFactory();
    TCPServerChannel svrChannel = svrMgr.createTCPServerChannel("svrChan", mockConnListener, sslFactory);
    svrChannel.bind(new InetSocketAddress(8444));
    int port = svrChannel.getLocalAddress().getPort();
    //don't really need to use a separate chan mgr but we will here..
    ChannelManager chanMgr = createSvrChanMgr("client");
    TCPChannel channel = chanMgr.createTCPChannel("client", sslFactory.createEngineForClient("cvs.xsoftware.biz", port));
    CompletableFuture<Channel> future = channel.connect(new InetSocketAddress("localhost", port), mockClientDataListener);
    future.get();
    byte[] data = new byte[] { 0, 2, 4, 6, 8, 10 };
    ByteBuffer buf = ByteBuffer.wrap(data);
    channel.write(buf);
    ByteBuffer result = mockSvrDataListener.getFirstBuffer().get();
    byte[] newData = new byte[result.remaining()];
    result.get(newData);
    Assert.assertEquals(data.length, newData.length);
    for (int i = 0; i < data.length; i++) {
        Assert.assertEquals(data[i], newData[i]);
    }
    //verify sniServerName was used in looking up security cert.
    String host = sslFactory.getCachedHost();
    Assert.assertEquals("cvs.xsoftware.biz", host);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) Channel(org.webpieces.nio.api.channels.Channel) TCPServerChannel(org.webpieces.nio.api.channels.TCPServerChannel) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) TCPServerChannel(org.webpieces.nio.api.channels.TCPServerChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 13 with Channel

use of org.webpieces.nio.api.channels.Channel in project webpieces by deanhiller.

the class BasChannelImpl method unqueueAndFailWritesThenClose.

private void unqueueAndFailWritesThenClose(CloseRunnable action) {
    List<CompletableFuture<Channel>> promises;
    synchronized (this) {
        //put here for emphasis that we are synchronizing here but not below
        promises = failAllWritesInQueue();
    }
    //TODO: This should really be inlined now.  It's a remnant of an old design since close didn't
    //work well outside the selector thread previously
    action.runDelayedAction();
    //notify clients outside the synchronization block!!!
    for (CompletableFuture<Channel> promise : promises) {
        log.info("WRITES outstanding while close was called, notifying client through his failure method of the exception");
        //we only incur the cost of Throwable.fillInStackTrace() if we will use this exception
        //(it's called in the Throwable constructor) so we don't do this on every close channel
        NioClosedChannelException closeExc = new NioClosedChannelException("There are " + promises.size() + " writes that are not complete yet(you called write but " + "they did not call success back to the client).");
        promise.completeExceptionally(closeExc);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Channel(org.webpieces.nio.api.channels.Channel) SelectableChannel(java.nio.channels.SelectableChannel) NioClosedChannelException(org.webpieces.nio.api.exceptions.NioClosedChannelException)

Example 14 with Channel

use of org.webpieces.nio.api.channels.Channel in project webpieces by deanhiller.

the class BasChannelImpl method writeAll.

/**
     * This method is reading from the queue and writing out to the socket buffers that
     * did not get written out when client called write.
     *
     */
void writeAll() {
    List<CompletableFuture<Channel>> finishedPromises = new ArrayList<>();
    synchronized (writeLock) {
        if (dataToBeWritten.isEmpty())
            throw new IllegalStateException("bug, I am not sure this is possible..it shouldn't be...look into");
        while (!dataToBeWritten.isEmpty()) {
            WriteInfo writer = dataToBeWritten.peek();
            ByteBuffer buffer = writer.getBuffer();
            int initialSize = buffer.remaining();
            int wroteOut = this.writeImpl(buffer);
            if (buffer.hasRemaining()) {
                if (buffer.remaining() + wroteOut != initialSize)
                    throw new IllegalStateException("Something went wrong.  b.remaining()=" + buffer.remaining() + " written=" + wroteOut + " total=" + initialSize);
                log.trace(() -> this + "Did not write all data out");
                int leftOverSize = buffer.remaining();
                int writtenOut = initialSize - leftOverSize;
                waitingBytesCounter -= writtenOut;
                break;
            }
            //if it finished, remove the item from the queue.  It
            //does not need to be run again.
            dataToBeWritten.poll();
            //release bytebuffer back to pool
            pool.releaseBuffer(writer.getBuffer());
            waitingBytesCounter -= initialSize;
            finishedPromises.add(writer.getPromise());
        }
        //we are only applying backpressure when queue is too large
        boolean applyPressure = !dataToBeWritten.isEmpty();
        boolean changedValue = applyingBackpressure.compareAndSet(true, applyPressure);
        if (!applyPressure && changedValue) {
            //we only fire when the value of applyingBackpressure changes
            dataListener.releaseBackPressure(this);
        }
        //we are registered for writes with ANY size queue
        if (dataToBeWritten.isEmpty() && inDelayedWriteMode) {
            inDelayedWriteMode = false;
            log.trace(() -> this + "unregister writes");
            Helper.unregisterSelectableChannel(this, SelectionKey.OP_WRITE);
        }
    }
    //MAKE SURE to notify clients outside of synchronization block so no deadlocks with their locks
    for (CompletableFuture<Channel> promise : finishedPromises) {
        promise.complete(this);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Channel(org.webpieces.nio.api.channels.Channel) SelectableChannel(java.nio.channels.SelectableChannel) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

Example 15 with Channel

use of org.webpieces.nio.api.channels.Channel in project webpieces by deanhiller.

the class DelayServerAcceptor method closeAllSockets.

public void closeAllSockets() throws IOException {
    for (int i = 0; i < sockets.size(); i++) {
        Channel channel = sockets.get(i);
        channel.oldClose();
    }
}
Also used : Channel(org.webpieces.nio.api.channels.Channel) RegisterableChannel(org.webpieces.nio.api.channels.RegisterableChannel) TCPChannel(org.webpieces.nio.api.channels.TCPChannel)

Aggregations

Channel (org.webpieces.nio.api.channels.Channel)16 TCPChannel (org.webpieces.nio.api.channels.TCPChannel)11 InetSocketAddress (java.net.InetSocketAddress)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 BufferCreationPool (org.webpieces.data.api.BufferCreationPool)7 ByteBuffer (java.nio.ByteBuffer)6 DataListener (org.webpieces.nio.api.handlers.DataListener)6 Executor (java.util.concurrent.Executor)5 AsyncConfig (org.webpieces.asyncserver.api.AsyncConfig)5 AsyncServer (org.webpieces.asyncserver.api.AsyncServer)5 AsyncServerManager (org.webpieces.asyncserver.api.AsyncServerManager)5 ChannelManager (org.webpieces.nio.api.ChannelManager)5 ChannelManagerFactory (org.webpieces.nio.api.ChannelManagerFactory)5 BufferPool (org.webpieces.data.api.BufferPool)4 NamedThreadFactory (org.webpieces.util.threading.NamedThreadFactory)4 SelectableChannel (java.nio.channels.SelectableChannel)3 Executors (java.util.concurrent.Executors)3 Logger (org.webpieces.util.logging.Logger)3 LoggerFactory (org.webpieces.util.logging.LoggerFactory)3 ArrayList (java.util.ArrayList)2