Search in sources :

Example 1 with Channel

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

the class HttpSocketImpl method actuallySendRequest.

private void actuallySendRequest(CompletableFuture<HttpChunkWriter> future, HttpRequest request, HttpResponseListener listener) {
    HttpResponseListener l = new CatchResponseListener(listener);
    ByteBuffer wrap = parser.marshalToByteBuffer(state, request);
    //put this on the queue before the write to be completed from the listener below
    responsesToComplete.offer(l);
    log.info("sending request now. req=" + request.getRequestLine().getUri());
    CompletableFuture<Channel> write = channel.write(wrap);
    write.handle((c, t) -> chainToFuture(c, t, future));
}
Also used : Channel(org.webpieces.nio.api.channels.Channel) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) ByteBuffer(java.nio.ByteBuffer) HttpResponseListener(org.webpieces.httpclient.api.HttpResponseListener)

Example 2 with Channel

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

the class IntegTestLocalhostThroughput method write.

private void write(Channel channel, String reason) {
    byte[] data = new byte[10240];
    ByteBuffer buffer = ByteBuffer.wrap(data);
    CompletableFuture<Channel> write = channel.write(buffer);
    write.thenAccept(p -> write(channel, "wrote data from client")).whenComplete((r, e) -> finished(r, e)).exceptionally(e -> {
        logIt(e);
        return null;
    });
}
Also used : Channel(org.webpieces.nio.api.channels.Channel) Logger(org.webpieces.util.logging.Logger) DataListener(org.webpieces.nio.api.handlers.DataListener) BufferPool(org.webpieces.data.api.BufferPool) Executor(java.util.concurrent.Executor) NamedThreadFactory(org.webpieces.util.threading.NamedThreadFactory) AsyncConfig(org.webpieces.asyncserver.api.AsyncConfig) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncServer(org.webpieces.asyncserver.api.AsyncServer) InetSocketAddress(java.net.InetSocketAddress) AsyncServerMgrFactory(org.webpieces.asyncserver.api.AsyncServerMgrFactory) ByteBuffer(java.nio.ByteBuffer) Executors(java.util.concurrent.Executors) ChannelManager(org.webpieces.nio.api.ChannelManager) AsyncServerManager(org.webpieces.asyncserver.api.AsyncServerManager) BufferCreationPool(org.webpieces.data.api.BufferCreationPool) ChannelManagerFactory(org.webpieces.nio.api.ChannelManagerFactory) LoggerFactory(org.webpieces.util.logging.LoggerFactory) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) Channel(org.webpieces.nio.api.channels.Channel) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) ByteBuffer(java.nio.ByteBuffer)

Example 3 with Channel

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

the class IntegTestLocalhostThroughput method testSoTimeoutOnSocket.

public void testSoTimeoutOnSocket() throws InterruptedException {
    Executor executor = Executors.newFixedThreadPool(10, new NamedThreadFactory("serverThread"));
    BufferPool pool = new BufferCreationPool();
    ChannelManagerFactory factory = ChannelManagerFactory.createFactory();
    ChannelManager mgr = factory.createMultiThreadedChanMgr("server", pool, executor);
    AsyncServerManager serverMgr = AsyncServerMgrFactory.createAsyncServer(mgr);
    AsyncServer server = serverMgr.createTcpServer(new AsyncConfig("tcpServer"), new IntegTestLocalhostServerListener());
    server.start(new InetSocketAddress(8080));
    BufferPool pool2 = new BufferCreationPool();
    DataListener listener = new ClientDataListener(pool2, recorder);
    Executor executor2 = Executors.newFixedThreadPool(10, new NamedThreadFactory("clientThread"));
    TCPChannel channel = createClientChannel(pool2, executor2);
    //TCPChannel channel = createNettyChannel();
    recorder.start();
    CompletableFuture<Channel> connect = channel.connect(new InetSocketAddress(8080), listener);
    connect.thenAccept(p -> runWriting(channel));
    synchronized (this) {
        this.wait();
    }
}
Also used : ChannelManager(org.webpieces.nio.api.ChannelManager) NamedThreadFactory(org.webpieces.util.threading.NamedThreadFactory) InetSocketAddress(java.net.InetSocketAddress) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) AsyncServer(org.webpieces.asyncserver.api.AsyncServer) Channel(org.webpieces.nio.api.channels.Channel) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) AsyncConfig(org.webpieces.asyncserver.api.AsyncConfig) AsyncServerManager(org.webpieces.asyncserver.api.AsyncServerManager) BufferCreationPool(org.webpieces.data.api.BufferCreationPool) ChannelManagerFactory(org.webpieces.nio.api.ChannelManagerFactory) Executor(java.util.concurrent.Executor) BufferPool(org.webpieces.data.api.BufferPool) DataListener(org.webpieces.nio.api.handlers.DataListener)

Example 4 with Channel

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

the class BasChannelImpl method write.

@Override
public CompletableFuture<Channel> write(ByteBuffer b) {
    if (b.remaining() == 0)
        throw new IllegalArgumentException("buffer has no data");
    else if (!getSelectorManager().isRunning())
        throw new IllegalStateException(this + "ChannelManager must be running and is stopped");
    else if (isClosed) {
        if (isRemoteEndInitiateClose)
            throw new NioClosedChannelException(this + "Client cannot write after the remote end closed the socket");
        else
            throw new NioClosedChannelException(this + "Your Application cannot write after YOUR Application closed the socket");
    } else if (doNotAllowWrites)
        throw new IllegalStateException("This channel is in a failed state.  " + "failure functions were called so look for exceptions from them");
    apiLog.trace(() -> this + "Basic.write");
    CompletableFuture<Channel> future = new CompletableFuture<Channel>();
    boolean wroteAllData = writeSynchronized(b, future);
    if (wroteAllData) {
        //since we didn't switch and were not in this mode, complete the action outside sync block
        pool.releaseBuffer(b);
        future.complete(this);
        log.trace(() -> this + " wrote bytes on client thread");
    } else {
        log.trace(() -> this + "sent write to queue");
    }
    return future;
}
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 5 with Channel

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

the class EchoServer method stop.

public void stop() throws IOException, InterruptedException {
    srvrChannel.closeServerChannel();
    for (int i = 0; i < sockets.size(); i++) {
        Channel channel = sockets.get(i);
        channel.oldClose();
    }
    chanMgr.stop();
}
Also used : Channel(org.webpieces.nio.api.channels.Channel) TCPServerChannel(org.webpieces.nio.api.channels.TCPServerChannel) 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