/** * Add a {@link Channel} * to be processed by{@link ReadController}'s * {@link SelectorHandler} * * @param channel new channel to be managed by ReadController * @param protocol name of the protocol channel corresponds to */ public void addChannel(SelectableChannel channel, SelectorHandler selectorHandler) { selectorHandler.register(channel, SelectionKey.OP_READ); }
protected static void flushAsyncWriteQueueAndClose( final SelectorHandler selectorHandler, final SelectionKey key) { try { selectorHandler.getAsyncQueueWriter().write(key, NULL_BUFFER, new AsyncWriteCallbackHandler() { public void onWriteCompleted(SelectionKey key, AsyncQueueWriteUnit writtenRecord) { close(); } public void onException(Exception exception, SelectionKey key, ByteBuffer buffer, Queue<AsyncQueueWriteUnit> remainingQueue) { close(); } private void close() { selectorHandler.addPendingKeyCancel(key); } }); } catch (Exception e) { selectorHandler.addPendingKeyCancel(key); } }
@Override public void doRegisterKey(SelectionKey key, int ops, long currentTime) { final Object attachment = key.attachment(); if (attachment != null && attachment.equals(SelectionKeyAttachment.DEREGISTERED)) { return; } if (!key.isValid()) { selectorHandler.getSelectionKeyHandler().cancel(key); } else { key.interestOps(key.interestOps() | ops); addExpirationStamp(key, currentTime); } }
/* package */ public void configureContext(Context ctx,SelectorHandler selectorHandler){ ctx.setSelectorHandler(selectorHandler); ctx.setPipeline(selectorHandler.pipeline()); ctx.setAsyncQueueReader(selectorHandler.getAsyncQueueReader()); ctx.setAsyncQueueWriter(selectorHandler.getAsyncQueueWriter()); }
if (selectorHandler.getSelectionKeyHandler() == null){ if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "Set DefaultSelectionKeyHandler to SelectorHandler: " + selectorHandler); if (selectorHandler.getPreferredSelectionKeyHandler() != null) { Class<? extends SelectionKeyHandler> keyHandlerClass = selectorHandler.getPreferredSelectionKeyHandler(); try { assgnSelectionKeyHandler = keyHandlerClass.newInstance(); selectorHandler.setSelectionKeyHandler(assgnSelectionKeyHandler); selectorHandler.preSelect(serverCtx); readyKeys = selectorHandler.select(serverCtx); selectorState = readyKeys.size(); if (stateHolder.getState(false) == State.STARTED && selectorHandler.getStateHolder().getState(false) == State.STARTED && selectorState != 0) { iterator = readyKeys.iterator(); == SelectionKey.OP_ACCEPT){ if (readThreadsCount > 0 && multiReadThreadSelectorHandler.supportsProtocol(selectorHandler.protocol())) { if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "OP_ACCEPT on " + key + " passed to multi readthread handler"); onAcceptInterest(key, serverCtx);
public boolean postProcess(ProcessorTask processorTask) { final boolean keepAlive = processorTask.isKeepAlive(); final SelectorHandler selectorHandler = processorTask.getSelectorHandler(); final SelectionKey selectionKey = processorTask.getSelectionKey(); final SelectorThread selectorThread = processorTask.getSelectorThread(); if (keepAlive) { selectorHandler.register(selectionKey, SelectionKey.OP_READ); } else { if (selectorThread.isAsyncHttpWriteEnabled()) { flushAsyncWriteQueueAndClose(selectorHandler, selectionKey); } else { selectorHandler.addPendingKeyCancel(selectionKey); } } ((InputReader) processorTask.getInputStream()).recycle(); processorTask.recycle(); selectorThread.returnTask(processorTask); return true; }
/** * If no bytes were available, close the connection by cancelling the * SelectionKey. If bytes were available, register the SelectionKey * for new bytes. * * @return <tt>true</tt> if the previous ProtocolFilter postExecute method * needs to be invoked. */ public boolean postExecute(Context ctx) throws IOException { if (ctx.getKeyRegistrationState() == Context.KeyRegistrationState.CANCEL){ ctx.getSelectorHandler().getSelectionKeyHandler(). cancel(ctx.getSelectionKey()); } else if (ctx.getKeyRegistrationState() == Context.KeyRegistrationState.REGISTER){ saveSecuredBufferRemainders(ctx.getSelectionKey()); ctx.getSelectorHandler().register(ctx.getSelectionKey(), SelectionKey.OP_READ); ctx.setKeyRegistrationState(Context.KeyRegistrationState.NONE); } return true; }
private void close() { selectorHandler.addPendingKeyCancel(key); } });
@Override public boolean onAcceptInterest(SelectionKey key, Context context) throws IOException { ReadController auxController = nextController(); SelectorHandler protocolSelectorHandler = context.getSelectorHandler(); SelectableChannel channel = protocolSelectorHandler.acceptWithoutRegistration(key); if (channel != null) { protocolSelectorHandler.configureChannel(channel); SelectorHandler relativeSelectorHandler = auxController.getSelectorHandlerClone(protocolSelectorHandler); if (relativeSelectorHandler == null) { // Clone was not found - take correspondent protocol SelectorHandler relativeSelectorHandler = auxController.getSelectorHandler(protocolSelectorHandler.protocol()); } auxController.addChannel(channel, relativeSelectorHandler); } return false; }
/** * Pre process the request by decoding the request line and the header. * @param input the InputStream to read bytes * @param output the OutputStream to write bytes */ public void preProcess(InputStream input, OutputStream output) throws Exception { // Make sure this object has been initialized. if ( !started ){ initialize(); } // Setting up the I/O inputBuffer.setInputStream(input); inputBuffer.setMaxSwallowingInputBytes(selectorThread.getMaxSwallowingInputBytes()); if (key != null) { inputStream = input; outputBuffer.setAsyncHttpWriteEnabled( isAsyncHttpWriteEnabled); outputBuffer.setAsyncQueueWriter( selectorHandler.getAsyncQueueWriter()); outputBuffer.setSelectionKey(key); response.setSelectionKey(key); } configPreProcess(); }
protected void closeChannel(SelectionKey key) { if (selectorHandler != null) { selectorHandler.closeChannel(key.channel()); } else { closeChannel(key.channel()); } }
public ConnectorHandler acquireConnectorHandler(Protocol protocol){ ConnectorHandler ch = null; SelectorHandler selectorHandler = controller.getSelectorHandler(protocol); if (selectorHandler != null) { ch = selectorHandler.acquireConnectorHandler(); ch.setController(controller); } return ch; }
if (processor.getReRegisterSelectionKey()) { setSelectionKeyTimeout(processorSelectionKey, SelectionKeyAttachment.UNLIMITED_TIMEOUT); selectorHandler.register(processorSelectionKey, SelectionKey.OP_READ); flushAsyncWriteQueueAndClose(selectorHandler, processorSelectionKey); } else { selectorHandler.addPendingKeyCancel(processorSelectionKey);
} else { if (state == Context.KeyRegistrationState.CANCEL){ selectorHandler.getSelectionKeyHandler().cancel(key); } else if (state == Context.KeyRegistrationState.REGISTER){ selectorHandler.register(key, SelectionKey.OP_READ);
@Override public void onRead(IOEvent<Context> ioEvent) { // We don't expect any read, so if any data comes - we suppose it's "close" notification final Context context = ioEvent.attachment(); final SelectionKey selectionKey = context.getSelectionKey(); // close the channel context.getSelectorHandler().addPendingKeyCancel(selectionKey); }
private void registerForReading(SelectionKey key) { selectorHandler.register(key, SelectionKey.OP_READ); }
protected void afterCall() { SelectionKey currentKey = context.getSelectionKey(); SelectionKeyHandler selectionKeyHandler = context. getSelectorHandler().getSelectionKeyHandler(); selectionKeyHandler.postProcess(currentKey); }
public boolean execute(Context ctx) throws IOException { final ProtocolChain chain = ctx.getProtocolChain(); synchronized (ssl) { if (!isSslInitialized) { isSslInitialized = true; isInitilizedOk = sslConfigHolder.configureSSL(); if (!isInitilizedOk) { logger.log(Level.SEVERE, "Failed to configure SSL on protocol ''{0}'' on network-listener ''{1}''", new Object[]{protocolName, networkListenerName}); } } } if (isInitilizedOk) { doConfigureFilters(chain); } else { logger.log(Level.FINE, "Closing connection to misconfigured SSL protocol"); ctx.getSelectorHandler().addPendingKeyCancel(ctx.getSelectionKey()); } return false; }
protected void registerForWriting(SelectionKey key) { selectorHandler.register(key, SelectionKey.OP_WRITE); }
protected void beforeCall() { SelectionKey currentKey = context.getSelectionKey(); SelectionKeyHandler selectionKeyHandler = context. getSelectorHandler().getSelectionKeyHandler(); selectionKeyHandler.process(currentKey); }