/** * Configure {@link TCPSelectorHandler} */ protected void configureSelectorHandler(TCPSelectorHandler selectorHandler) { selectorHandler.setSelector(selector); selectorHandler.setPort(port); selectorHandler.setInet(inet); selectorHandler.setLinger(linger); selectorHandler.setLogger(logger); selectorHandler.setReuseAddress(reuseAddress); selectorHandler.setSelectTimeout(selectorTimeout); selectorHandler.setServerTimeout(serverTimeout); selectorHandler.setSocketTimeout(keepAliveTimeoutInSeconds * 1000); selectorHandler.setSsBackLog(ssBackLog); selectorHandler.setTcpNoDelay(tcpNoDelay); }
public Selector getSelector(){ if (selector != null) { return selector; } else { selectorHandler.getSelector(); } return null; }
/** * Returns the Grizzly TCP selector handler. * * @return The Grizzly TCP selector handler. */ public TCPSelectorHandler getSelectorHandler() { // Lazy initialization with double-check. TCPSelectorHandler s = this.selectorHandler; if (s == null) { synchronized (this) { s = this.selectorHandler; if (s == null) { this.selectorHandler = s = new TCPSelectorHandler(); } } } return s; }
/** * Handle OP_ACCEPT. * @param ctx {@link Context} * @return always returns false */ public boolean onAcceptInterest(SelectionKey key, Context ctx) throws IOException{ SelectableChannel channel = acceptWithoutRegistration(key); if (channel != null) { configureChannel(channel); SelectionKey readKey = channel.register(selector, SelectionKey.OP_READ); readKey.attach(System.currentTimeMillis()); } return false; }
@Override protected void configure(Controller controller) throws Exception { // Get the TCP select handler of the controller TCPSelectorHandler selectorHandler = getSelectorHandler(); // Configure it selectorHandler.setPort(getHelped().getPort()); if (getHelped().getAddress() != null) { selectorHandler.setInet(InetAddress.getByName(getHelped() .getAddress())); } // Create the Grizzly filters final ReadFilter readFilter = new ReadFilter(); final HttpParserFilter httpParserFilter = new HttpParserFilter(this); // Create the Grizzly controller controller .setProtocolChainInstanceHandler(new DefaultProtocolChainInstanceHandler() { @Override public ProtocolChain poll() { ProtocolChain protocolChain = this.protocolChains .poll(); if (protocolChain == null) { protocolChain = new DefaultProtocolChain(); protocolChain.addFilter(readFilter); protocolChain.addFilter(httpParserFilter); } return protocolChain; } }); }
tcpSelectorHandler.setPortRange(new PortRange(this.tcpStartPort, this.tcpEndPort)); tcpSelectorHandler.setSelectionKeyHandler( new GrizzlyCacheableSelectionKeyHandler( highWaterMark, numberToReclaim, this ) ); tcpSelectorHandler.setInet( localInetAddress );
SelectionKey key = socketChannel.keyFor(selectorHandler.getSelector()); int nWrite = 1; int totalWriteBytes = 0; selectorHandler.register(key,SelectionKey.OP_WRITE);
/** * Close the underlying connection. */ public void close() throws IOException{ if (socketChannel != null){ if (selectorHandler != null){ SelectionKey key = socketChannel.keyFor(selectorHandler.getSelector()); if (key == null) return; selectorHandler.getSelectionKeyHandler().cancel(key); } else { socketChannel.close(); } } if (controller != null && isStandalone){ controller.stop(); controller = null; } isStandalone = false; isConnected = false; connectionTimeout = DEFAULT_CONNECTION_TIMEOUT; }
/** * {@inheritDoc} */ public void writeToAsyncQueue(SocketAddress dstAddress, ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler, AsyncQueueDataProcessor writePreProcessor, boolean isCloneByteBuffer) throws IOException { selectorHandler.getAsyncQueueWriter().write( socketChannel.keyFor(selectorHandler.getSelector()), dstAddress, buffer, callbackHandler, writePreProcessor, isCloneByteBuffer); }
/** * Cancel the current {@link SelectionKey} */ public void cancelKey(SelectionKey key){ selectorHandler.getSelectionKeyHandler().cancel(key); }
public void copyTo(Copyable copy) { TCPSelectorHandler copyHandler = (TCPSelectorHandler) copy; copyHandler.selector = selector; if (selectionKeyHandler != null) { copyHandler.setSelectionKeyHandler(Cloner.clone(selectionKeyHandler)); } copyHandler.attributes = attributes; copyHandler.selectTimeout = selectTimeout; copyHandler.serverTimeout = serverTimeout; copyHandler.inet = inet; copyHandler.port = port; copyHandler.ssBackLog = ssBackLog; copyHandler.tcpNoDelay = tcpNoDelay; copyHandler.linger = linger; copyHandler.socketTimeout = socketTimeout; copyHandler.logger = logger; copyHandler.reuseAddress = reuseAddress; copyHandler.connectorInstanceHandler = connectorInstanceHandler; copyHandler.stateHolder = stateHolder; }
/** * {@inheritDoc} */ public void readFromAsyncQueue(ByteBuffer buffer, AsyncReadCallbackHandler callbackHandler, AsyncReadCondition condition, AsyncQueueDataProcessor readPostProcessor) throws IOException { selectorHandler.getAsyncQueueReader().read( socketChannel.keyFor(selectorHandler.getSelector()), buffer, callbackHandler, condition, readPostProcessor); }
/** * {@inheritDoc} */ @Override public void copyTo(Copyable copy) { super.copyTo(copy); SelectorThreadHandler copyHandler = (SelectorThreadHandler) copy; copyHandler.selectorThread = selectorThread; }
/** * Register a {@link SelectionKey} to this {@link Selector} * running of this thread. */ public void registerKey(SelectionKey key){ selectorHandler.register(key, SelectionKey.OP_READ); }
@Override public void configureChannel(SelectableChannel channel) throws IOException { if (channel instanceof SocketChannel) { selectorThread.setSocketOptions(((SocketChannel) channel).socket()); } super.configureChannel(channel); }
/** * Similar to <code>getPort()</code>, but getting port number directly from * connection ({@link ServerSocket}, {@link DatagramSocket}). * So if default port number 0 was set during initialization, then <code>getPort()</code> * will return 0, but getPortLowLevel() will return port number assigned by OS. * * @return port number, or -1 if {@link SelectorThread} was not started * @deprecated - uses {@link #getPort} instead */ public int getPortLowLevel() { if (selectorHandler != null){ return selectorHandler.getPort(); } else { return port; } }
SelectionKey key = socketChannel.keyFor(selectorHandler.getSelector()); if (blocking){ inputStream.setSelectionKey(key); selectorHandler.register(key,SelectionKey.OP_READ);
/** * {@inheritDoc} */ public void writeToAsyncQueue(ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler, AsyncQueueDataProcessor writePreProcessor, boolean isCloneByteBuffer) throws IOException { selectorHandler.getAsyncQueueWriter().write( socketChannel.keyFor(selectorHandler.getSelector()), buffer, callbackHandler, writePreProcessor, isCloneByteBuffer); }