Search in sources :

Example 21 with TCPChannel

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

the class ProxyDataListener method applyBackPressure.

@Override
public void applyBackPressure(Channel channel) {
    TCPChannel proxy = lookupExistingOrCreateNew(channel);
    dataListener.applyBackPressure(proxy);
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel)

Example 22 with TCPChannel

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

the class ProxyDataListener method lookupExistingOrCreateNew.

/** 
	 * We have two choices here.
	 * 1. implement equals and hashCode in ProxyTCPChannel to delegate to TCPChannel so as we
	 *    create new ones, they are equal if the Channel they wrap is equal
	 * 2. re-use the same proxy we created for this channel by sticking it in the channel session
	 *    which avoids creating new objects that need to be garbage collected every time data
	 *    comes in
	 *       
	 * @param channel
	 * @return
	 */
private TCPChannel lookupExistingOrCreateNew(Channel channel) {
    ChannelSession session = channel.getSession();
    //This is garbage collected when the TCPChannel and it's ChannelSession are garbage
    //collected...
    ProxyTCPChannel existingProxy = (ProxyTCPChannel) session.get(EXISTING_PROXY_CHANNEL);
    if (existingProxy == null) {
        existingProxy = new ProxyTCPChannel((TCPChannel) channel, connectedChannels);
        session.put(EXISTING_PROXY_CHANNEL, existingProxy);
    }
    return existingProxy;
}
Also used : ChannelSession(org.webpieces.nio.api.channels.ChannelSession) TCPChannel(org.webpieces.nio.api.channels.TCPChannel)

Example 23 with TCPChannel

use of org.webpieces.nio.api.channels.TCPChannel 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 24 with TCPChannel

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

the class DelayServerAcceptor method connected.

public void connected(Channel channel) throws IOException {
    if (log.isTraceEnabled())
        log.trace(channel + "about to accept");
    currentChannel = clientSideChanMgr.createTCPChannel("xxx", null);
    currentChannel.setName("<not known yet>");
    InetSocketAddress addr = new InetSocketAddress(delaySvrAddr, 0);
    currentChannel.bind(addr);
    currentChannel.oldConnect(realSvr);
    if (log.isTraceEnabled())
        log.trace(channel + "connected to real server");
    if (log.isTraceEnabled())
        log.trace(channel + ":" + currentChannel + "connected all links");
    sockets.add((TCPChannel) channel);
    sockets.add(currentChannel);
    currentChannel.registerForReads(new Delayer((TCPChannel) channel));
    channel.registerForReads(new Delayer(currentChannel));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) TCPChannel(org.webpieces.nio.api.channels.TCPChannel)

Example 25 with TCPChannel

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

the class IntegTestLocalhostThroughput method createClientChannel.

private TCPChannel createClientChannel(BufferPool pool2, Executor executor) {
    ChannelManagerFactory factory = ChannelManagerFactory.createFactory();
    ChannelManager mgr = factory.createMultiThreadedChanMgr("client", pool2, executor);
    TCPChannel channel = mgr.createTCPChannel("clientChan");
    return channel;
}
Also used : ChannelManager(org.webpieces.nio.api.ChannelManager) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) ChannelManagerFactory(org.webpieces.nio.api.ChannelManagerFactory)

Aggregations

TCPChannel (org.webpieces.nio.api.channels.TCPChannel)39 InetSocketAddress (java.net.InetSocketAddress)12 ByteBuffer (java.nio.ByteBuffer)10 CalledMethod (biz.xsoftware.mock.CalledMethod)8 CloneByteBuffer (org.webpieces.nio.api.testutil.CloneByteBuffer)6 ChannelManager (org.webpieces.nio.api.ChannelManager)5 Channel (org.webpieces.nio.api.channels.Channel)5 BufferCreationPool (org.webpieces.data.api.BufferCreationPool)4 ChannelManagerFactory (org.webpieces.nio.api.ChannelManagerFactory)4 FutureOperation (org.webpieces.nio.api.handlers.FutureOperation)4 AsyncConfig (org.webpieces.asyncserver.api.AsyncConfig)3 AsyncServer (org.webpieces.asyncserver.api.AsyncServer)3 AsyncServerManager (org.webpieces.asyncserver.api.AsyncServerManager)3 BufferPool (org.webpieces.data.api.BufferPool)3 PerfTimer (org.webpieces.nio.test.PerfTimer)3 IOException (java.io.IOException)2 InetAddress (java.net.InetAddress)2 Executor (java.util.concurrent.Executor)2 Test (org.junit.Test)2 TCPServerChannel (org.webpieces.nio.api.channels.TCPServerChannel)2