Search in sources :

Example 6 with Channel

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

the class IntegTestClientNotRead method testSoTimeoutOnSocket.

public void testSoTimeoutOnSocket() throws InterruptedException {
    BufferCreationPool pool = new BufferCreationPool();
    AsyncServerManager serverMgr = AsyncServerMgrFactory.createAsyncServer("server", pool);
    AsyncServer server = serverMgr.createTcpServer(new AsyncConfig("tcpServer"), new IntegTestClientNotReadListener());
    server.start(new InetSocketAddress(8080));
    BufferCreationPool pool2 = new BufferCreationPool();
    ChannelManagerFactory factory = ChannelManagerFactory.createFactory();
    ChannelManager mgr = factory.createSingleThreadedChanMgr("client", pool2);
    TCPChannel channel = mgr.createTCPChannel("clientChan");
    log.info("client");
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            logBytesTxfrd();
        }
    }, 1000, 5000);
    CompletableFuture<Channel> connect = channel.connect(new InetSocketAddress(8080), new ClientDataListener());
    connect.thenAccept(p -> runWriting(channel));
    Thread.sleep(1000000000);
}
Also used : ChannelManager(org.webpieces.nio.api.ChannelManager) 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) TimerTask(java.util.TimerTask)

Example 7 with Channel

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

the class IntegTestClientToEchoServer method testSoTimeoutOnSocket.

public void testSoTimeoutOnSocket() throws InterruptedException {
    runEchoServer();
    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(4444), listener);
    connect.thenAccept(p -> runWriting(channel));
    synchronized (this) {
        this.wait();
    }
}
Also used : BufferPool(org.webpieces.data.api.BufferPool) Executor(java.util.concurrent.Executor) NamedThreadFactory(org.webpieces.util.threading.NamedThreadFactory) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) InetSocketAddress(java.net.InetSocketAddress) Channel(org.webpieces.nio.api.channels.Channel) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) DataListener(org.webpieces.nio.api.handlers.DataListener) BufferCreationPool(org.webpieces.data.api.BufferCreationPool)

Example 8 with Channel

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

the class IntegTestClientToEchoServer method write.

private void write(Channel channel, String reason) {
    //		counter++;
    //		if(counter % 100000 == 0)
    //			log.info("counter="+counter);
    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) CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) ByteBuffer(java.nio.ByteBuffer) Executors(java.util.concurrent.Executors) ChannelManager(org.webpieces.nio.api.ChannelManager) 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 9 with Channel

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

the class ThreadChannel method connect.

@Override
public CompletableFuture<Channel> connect(SocketAddress addr, DataListener listener) {
    DataListener threaded = new ThreadDataListener(listener, sessionExecutor);
    CompletableFuture<Channel> future = tcpChannel.connect(addr, threaded);
    //not need to synchronize the ChannelSession writes/reads
    return future.thenApplyAsync(p -> this, executor);
}
Also used : Channel(org.webpieces.nio.api.channels.Channel) DataListener(org.webpieces.nio.api.handlers.DataListener)

Example 10 with Channel

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

the class TestBasicSslClientServer method testBasic.

@Test
public void testBasic() throws InterruptedException {
    pool = new BufferCreationPool();
    ChannelManagerFactory factory = ChannelManagerFactory.createFactory();
    ChannelManager mgr = factory.createSingleThreadedChanMgr("sslChanMgr", pool);
    AsyncServerManager svrFactory = AsyncServerMgrFactory.createAsyncServer(mgr);
    SSLEngineFactoryForTest f = new SSLEngineFactoryForTest();
    InetSocketAddress addr = new InetSocketAddress("localhost", 0);
    AsyncServer svr = svrFactory.createTcpServer(new AsyncConfig("sslTcpSvr"), new SvrDataListener(), f);
    svr.start(addr);
    InetSocketAddress bound = svr.getUnderlyingChannel().getLocalAddress();
    System.out.println("port=" + bound.getPort());
    TCPChannel channel = mgr.createTCPChannel("client", f.createEngineForSocket());
    CompletableFuture<Channel> connect = channel.connect(bound, new ClientListener());
    connect.thenAccept(c -> writeData(c));
    synchronized (pool) {
        while (values.size() < 10) pool.wait();
    }
    for (int i = 0; i < values.size(); i++) {
        Assert.assertEquals(new Integer(i), values.get(i));
    }
}
Also used : 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) Test(org.junit.Test)

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