Search in sources :

Example 1 with TCPServerChannel

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

the class AsyncServerManagerImpl method createTcpServerImpl.

private AsyncServer createTcpServerImpl(AsyncConfig config, AsyncDataListener listener, SSLEngineFactory sslFactory) {
    String id = config.id;
    ConnectedChannels connectedChannels = new ConnectedChannels();
    ProxyDataListener proxyListener = new ProxyDataListener(connectedChannels, listener);
    DefaultConnectionListener connectionListener = new DefaultConnectionListener(connectedChannels, proxyListener);
    TCPServerChannel serverChannel;
    if (sslFactory != null)
        serverChannel = channelManager.createTCPServerChannel(id, connectionListener, sslFactory);
    else
        serverChannel = channelManager.createTCPServerChannel(id, connectionListener);
    //MUST be called before bind...
    serverChannel.setReuseAddress(true);
    serverChannel.configure(config.functionToConfigureBeforeBind);
    return new AsyncServerImpl(serverChannel, connectionListener, proxyListener);
}
Also used : TCPServerChannel(org.webpieces.nio.api.channels.TCPServerChannel)

Example 2 with TCPServerChannel

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

the class AsyncServerManagerImpl method createTcpServerImpl.

private AsyncServer createTcpServerImpl(AsyncConfig config, AsyncDataListener listener, SSLEngineFactory sslFactory, boolean isUpgradable) {
    if (config.id == null)
        throw new IllegalArgumentException("config.id must not be null");
    String id = channelManager.getName() + "." + config.id;
    ConnectedChannels connectedChannels = new ConnectedChannels(id, metrics);
    ProxyDataListener proxyListener = new ProxyDataListener(connectedChannels, listener);
    DefaultConnectionListener connectionListener = new DefaultConnectionListener(id, connectedChannels, proxyListener);
    TCPServerChannel serverChannel;
    if (sslFactory != null) {
        if (isUpgradable) {
            // create plain text that can accept SSL immediately OR change to SSL later
            serverChannel = channelManager.createTCPUpgradableChannel(config.id, connectionListener, sslFactory);
        } else {
            serverChannel = channelManager.createTCPServerChannel(config.id, connectionListener, sslFactory);
        }
    } else {
        serverChannel = channelManager.createTCPServerChannel(config.id, connectionListener);
    }
    // MUST be called before bind...
    serverChannel.setReuseAddress(true);
    serverChannel.configure(config.functionToConfigureBeforeBind);
    return new AsyncServerImpl(serverChannel, connectionListener, proxyListener, sslFactory);
}
Also used : TCPServerChannel(org.webpieces.nio.api.channels.TCPServerChannel)

Example 3 with TCPServerChannel

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

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

the class TestExampleSessions method startServer.

private InetSocketAddress startServer() throws Exception {
    TCPServerChannel server = svc.createTCPServerChannel("server", null);
    // bind to 0 allows it to bind to any port...
    server.bind(new InetSocketAddress(0));
    server.registerServerSocketChannel(new MyServerSocketListener());
    return server.getLocalAddress();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) TCPServerChannel(org.webpieces.nio.api.channels.TCPServerChannel)

Example 5 with TCPServerChannel

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

the class TestExample method startServer.

private InetSocketAddress startServer() throws Exception {
    TCPServerChannel server = svc.createTCPServerChannel("server", null);
    // bind to 0 allows it to bind to any port...
    server.bind(new InetSocketAddress(0));
    server.registerServerSocketChannel(new MyServerSocketListener());
    return server.getLocalAddress();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) TCPServerChannel(org.webpieces.nio.api.channels.TCPServerChannel)

Aggregations

TCPServerChannel (org.webpieces.nio.api.channels.TCPServerChannel)7 InetSocketAddress (java.net.InetSocketAddress)5 ByteBuffer (java.nio.ByteBuffer)2 Test (org.junit.Test)2 TCPChannel (org.webpieces.nio.api.channels.TCPChannel)2 InetAddress (java.net.InetAddress)1 Channel (org.webpieces.nio.api.channels.Channel)1 ChannelService (org.webpieces.nio.api.deprecated.ChannelService)1 Settings (org.webpieces.nio.api.deprecated.Settings)1 SSLEngineFactory (org.webpieces.nio.api.libs.SSLEngineFactory)1 MockSSLEngineFactory (org.webpieces.nio.api.testutil.MockSSLEngineFactory)1