TcpTransport(Properties configuration) { extensionFactory = TcpExtensionFactory.newInstance(); acceptor = new NioSocketAcceptor(configuration, extensionFactory); connector = new NioSocketConnector(configuration); }
@Override // Used for resource injection public Collection<?> getExtensions() { return extensionFactory.availableExtensions(); }
@Override public void rebuildSelectors() { for (DistributedNioWorker worker: workers) { worker.rebuildSelector(); } }
public DistributedNioWorkerPool(Executor workerExecutor, int workerCount) { if (workerExecutor == null) { throw new NullPointerException("workerExecutor"); } if (workerCount <= 0) { throw new IllegalArgumentException("workerCount (" + workerCount + ") must be a positive integer"); } DistributedNioWorker[] workers = new DistributedNioWorker[workerCount]; for (int i=0; i < workers.length; i++) { // we cannot allow shutdown on idle, otherwise worker may end up running on a different thread DistributedNioWorker worker = new DistributedNioWorker(workerExecutor); // we must set the current worker in the right thread local before any in-bound connections FutureTask<NioWorker> future = new FutureTask<>(new SetCurrentWorkerTask(worker)); worker.executeInIoThread(future, /*alwaysAsync*/ true); try { NioWorker workerFromTask = future.get(); assert (workerFromTask == worker); } catch (Exception e) { e.printStackTrace(); } workers[i] = worker; } this.workers = workers; this.workerExecutor = workerExecutor; }
public Worker[] getWorkers() { return ((NioSocketAcceptor) getAcceptor()).getWorkers(); } }
@Override public void bind(final ResourceAddress address, IoHandler handler, BridgeSessionInitializer<? extends IoFuture> initializer) throws NioBindException { Collection<TcpExtension> extensions = extensionFactory.bind(address); BridgeSessionInitializer<? extends IoFuture> newInitializer = initializer; if (extensions.size() > 0) { newInitializer = new ExtensionsSessionInitializer(extensions, initializer); } super.bind(address, handler, newInitializer); }
WorkerPool<NioWorker> workerPool = tcpAcceptor.initWorkerPool(logger, "TCP connector: {}", getConfiguration()); int bossCount = 1; NioClientSocketChannelFactory clientChannelFactory = new NioClientSocketChannelFactory(
WorkerPool<NioWorker> workerPool = initWorkerPool(logger, "TCP acceptor: {}", configuration); NioServerSocketChannelFactory serverChannelFactory = new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), acceptor = new NioSocketChannelIoAcceptor(new DefaultNioSocketChannelIoSessionConfig(), serverChannelFactory, new AffinityIoAcceptorChannelHandlerFactory()); acceptor.setIoSessionInitializer(initializer);
/** * Creates a new instance of WebSocketExtensionFactory. It uses the specified {@link ClassLoader} to load * {@link WebSocketExtensionFactorySpi} objects that are registered using META-INF/services. * * @return WebSocketExtensionFactory */ static TcpExtensionFactory newInstance(ClassLoader cl) { ServiceLoader<TcpExtensionFactorySpi> services = load(TcpExtensionFactorySpi.class, cl); return TcpExtensionFactoryImpl.newInstance(services); }
@Override protected IoConnectorEx initConnector() { DatagramChannelIoSessionConfig config = new DefaultDatagramChannelIoSessionConfig(); WorkerPool<NioWorker> workerPool = tcpAcceptor.initWorkerPool(logger, "UDP connector: {}", getConfiguration()); /*NioClientDatagramChannelFactory*/ channelFactory = new NioClientDatagramChannelFactory(workerPool); NioDatagramChannelIoConnector connector = new NioDatagramChannelIoConnector(config, channelFactory); String readBufferSize = configuration.getProperty("org.kaazing.gateway.transport.udp.READ_BUFFER_SIZE"); String minimumReadBufferSize = configuration.getProperty("org.kaazing.gateway.transport.udp.MINIMUM_READ_BUFFER_SIZE"); String maximumReadBufferSize = configuration.getProperty("org.kaazing.gateway.transport.udp.MAXIMUM_READ_BUFFER_SIZE"); if (readBufferSize != null) { connector.getSessionConfig().setReadBufferSize(Integer.parseInt(readBufferSize)); logger.debug("READ_BUFFER_SIZE setting for UDP connector: {}", readBufferSize); } if (minimumReadBufferSize != null) { connector.getSessionConfig().setMinReadBufferSize(Integer.parseInt(minimumReadBufferSize)); logger.debug("MINIMUM_READ_BUFFER_SIZE setting for UDP connector: {}", minimumReadBufferSize); } if (maximumReadBufferSize != null) { connector.getSessionConfig().setMaxReadBufferSize(Integer.parseInt(maximumReadBufferSize)); logger.debug("MAXIMUM_READ_BUFFER_SIZE setting for UDP connector: {}", maximumReadBufferSize); } return connector; }
public NioSocketAcceptor(Properties configuration) { this(configuration, TcpExtensionFactory.newInstance()); }
public Worker[] getWorkers() { // KG-10074: if only non-TCP binds, worker pool may not yet be initialized (causing NPE below) initIfNecessary(); return currentWorkerPool.get().workers; }
@Override public void dispose() { super.dispose(); if (tcpAcceptor != null) { tcpAcceptor.dispose(); } } }
static TcpExtensionFactory newInstance(ServiceLoader<TcpExtensionFactorySpi> services) { List<TcpExtensionFactorySpi> factories = new ArrayList<>(); for (TcpExtensionFactorySpi service : services) { factories.add(service); } return new TcpExtensionFactoryImpl(unmodifiableList(factories)); }
@Override public void releaseExternalResources() { // (KG-7319) Since NioSocketConnector and NioSocketAcceptor share the same worker pool we must only release // resources when there are no more active users. Note that during dispose, shutdown gets called (by Netty) // followed by releaseExternalResources, so we only make the latter decrement the reference count. if (referenceCount.decrementAndGet() <= 0) { shutdown(); ExecutorUtil.terminate(workerExecutor); } }
@Override public void childChannelOpen(ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception { NioSocketChannel childChannel = (NioSocketChannel) e.getChildChannel(); DistributedNioWorker worker = (DistributedNioWorker) childChannel.getWorker(); worker.childChannelOpen(childChannel); super.childChannelOpen(ctx, e); }
worker = super.nextWorker();
Worker[] workers = tcpAcceptor.getWorkers(); assert preparedConnectionCount == 0 || preparedConnectionCount >= workers.length : "Prepared connection count must be 0, or >= number of IO threads"; int minCountPerThread = preparedConnectionCount / workers.length;
/** * Creates a new instance of WebSocketExtensionFactory. It uses the default {@link ClassLoader} to load * {@link WebSocketExtensionFactorySpi} objects that are registered using META-INF/services. * * @return WebSocketExtensionFactory */ static TcpExtensionFactory newInstance() { ServiceLoader<TcpExtensionFactorySpi> services = load(TcpExtensionFactorySpi.class); return TcpExtensionFactoryImpl.newInstance(services); }
@Override protected IoAcceptorEx initAcceptor(final IoSessionInitializer<? extends IoFuture> initializer) { DatagramChannelIoSessionConfig config = new DefaultDatagramChannelIoSessionConfig(); WorkerPool<NioWorker> workerPool = tcpAcceptor.initWorkerPool(logger, "UDP acceptor: {}", configuration); NioServerDatagramChannelFactory channelFactory = new NioServerDatagramChannelFactory(newCachedThreadPool(), 1, workerPool); NioDatagramChannelIoAcceptor acceptor = new NioDatagramChannelIoAcceptor(config, channelFactory); acceptor.setIoSessionInitializer(initializer); String readBufferSize = configuration.getProperty("org.kaazing.gateway.transport.udp.READ_BUFFER_SIZE"); if (readBufferSize != null) { acceptor.getSessionConfig().setReadBufferSize(Integer.parseInt(readBufferSize)); logger.debug("READ_BUFFER_SIZE setting for UDP acceptor: {}", readBufferSize); } String minimumReadBufferSize = configuration.getProperty(TCP_MINIMUM_READ_BUFFER_SIZE.getPropertyName()); if (minimumReadBufferSize != null) { acceptor.getSessionConfig().setMinReadBufferSize(Integer.parseInt(minimumReadBufferSize)); logger.debug("MINIMUM_READ_BUFFER_SIZE setting for UDP acceptor: {}", minimumReadBufferSize); } String maximumReadBufferSize = configuration.getProperty(TCP_MAXIMUM_READ_BUFFER_SIZE.getPropertyName()); if (maximumReadBufferSize != null) { acceptor.getSessionConfig().setMaxReadBufferSize(Integer.parseInt(maximumReadBufferSize)); logger.debug("MAXIMUM_READ_BUFFER_SIZE setting for UDP acceptor: {}", maximumReadBufferSize); } int idleTimeout = UDP_IDLE_TIMEOUT.getIntProperty(configuration); acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, idleTimeout); return acceptor; }