@Override public boolean isOpen() { return delegate.isOpen(); }
@Override public boolean isOpen() { return delegate.isOpen(); }
@Override public boolean isOpen() { return delegate.isOpen(); }
public boolean selectorOpen() {return selector != null && selector.isOpen();} public boolean acceptorRunning() {return acceptor != null && acceptor.isAlive();}
public int available() throws IOException { if (!rs.isOpen()) throw new IOException("Input Stream Closed"); return bb.remaining(); }
/** * Quietly closes given resource ignoring possible checked exception. * * @param rsrc Resource to close. If it's {@code null} - it's no-op. */ public static void closeQuiet(@Nullable Selector rsrc) { if (rsrc != null) try { if (rsrc.isOpen()) rsrc.close(); } catch (IOException ignored) { // No-op. } }
public boolean isShutdownProperly() { return !isRunning() && !thread.isAlive() && (selectorThread == null || !selectorThread.isAlive()) && (pool == null || pool.isShutdown()) && (hsPool == null || hsPool.isShutdown()) && (clientQueueInitPool == null || clientQueueInitPool.isShutdown()) && (selector == null || !selector.isOpen()) && (tmpSel == null || !tmpSel.isOpen()); }
public void close() throws IOException { if (rs.isOpen()) { rs.close(); if (sc.isOpen()) { sc.socket().shutdownInput(); sc.socket().close(); } bb = null; sc = null; } }
/** * wake up the selector thread */ private void wakeupSelector() { Selector s = getSelector(); if (s != null && s.isOpen()) { this.selector.wakeup(); } }
/** * Closes given resource logging possible checked exception. * * @param rsrc Resource to close. If it's {@code null} - it's no-op. * @param log Logger to log possible checked exception with (optional). */ public static void close(@Nullable Selector rsrc, @Nullable IgniteLogger log) { if (rsrc != null) try { if (rsrc.isOpen()) rsrc.close(); } catch (IOException e) { warn(log, "Failed to close resource: " + e.getMessage()); } }
public int read() throws IOException { if (!rs.isOpen()) throw new IOException("Input Stream Closed"); if (!bb.hasRemaining()) { try { fill(1); } catch (ClosedChannelException e) { close(); return -1; } } return (bb.get() & 0xFF); }
public int read(byte[] b, int off, int len) throws IOException { int bytesCopied = -1; if (!rs.isOpen()) throw new IOException("Input Stream Closed"); while (bytesCopied == -1) { if (bb.hasRemaining()) { bytesCopied = (len < bb.remaining() ? len : bb.remaining()); bb.get(b, off, bytesCopied); } else { try { fill(1); } catch (ClosedChannelException e) { close(); return -1; } } } return bytesCopied; }
@Override public void run() { while (!isInterrupted() && isBind()) { try { while (selector.isOpen() && selector.select() > 0) { final Iterator<SelectionKey> it = selector.selectedKeys().iterator(); while (it.hasNext()) { final SelectionKey key = it.next(); it.remove(); // do ssc accept if (key.isValid() && key.isAcceptable()) { doAccept(key, selector, configure); } // do sc read if (key.isValid() && key.isReadable()) { doRead(byteBuffer, key); } } } } catch (IOException e) { logger.warn("selector failed.", e); } catch (ClosedSelectorException e) { logger.debug("selector closed.", e); } } } };
private synchronized void close() { if (selector != null && selector.isOpen()) { try { selector.close(); } catch (final Exception e) { logger.warn("Failed to close NIO Selector", e); } } if (channel != null && channel.isOpen()) { try { channel.close(); } catch (final Exception e) { logger.warn("Failed to close Socket Channel to {} for Load Balancing", nodeIdentifier, e); } } channel = null; selector = null; }
if (this.isRunning.compareAndSet(true, false)) { try { if (this.selector != null && this.selector.isOpen()) { // since stop must be idempotent, we need to check if selector is open to avoid ClosedSelectorException Set<SelectionKey> selectionKeys = new HashSet<>(this.selector.keys()); for (SelectionKey key : selectionKeys) {
public long skip(long n) throws IOException { long skiped = 0; if (!rs.isOpen()) throw new IOException("Input Stream Closed"); while (n > 0) { if (n <= bb.remaining()) { skiped += n; bb.position(bb.position() + (int) n); n = 0; } else { skiped += bb.remaining(); n -= bb.remaining(); bb.position(bb.limit()); try { fill((int) n); } catch (ClosedChannelException e) { close(); return skiped; } } } return skiped; }
/** * Once the handler is constructed this should be called to start the handler. Although * this method is safe to be called by multiple threads, it should only be called once. * * @throws IllegalStateException if it fails to start listening on the port that is configured * */ public void start() { if (this.isRunning.compareAndSet(false, true)) { try { if (this.selector == null || !this.selector.isOpen()) { this.selector = Selector.open(); InetSocketAddress connectedAddress = this.connect(); this.listenerTaskExecutor = Executors.newCachedThreadPool(); this.listenerTaskExecutor.execute(this.listenerTask); if (logger.isDebugEnabled()) { logger.debug("Started listener for " + AbstractSocketHandler.this.getClass().getSimpleName()); } if (logger.isInfoEnabled()) { logger.info("Successfully bound to " + connectedAddress); } } } catch (Exception e) { this.stop(); throw new IllegalStateException("Failed to start " + this.getClass().getName(), e); } } }
@Override public void run() { try { while (AbstractSocketHandler.this.rootChannel != null && AbstractSocketHandler.this.rootChannel.isOpen() && AbstractSocketHandler.this.selector.isOpen()) { if (AbstractSocketHandler.this.selector.isOpen() && AbstractSocketHandler.this.selector.select(10) > 0) { Iterator<SelectionKey> keys = AbstractSocketHandler.this.selector.selectedKeys().iterator(); while (keys.hasNext()) { SelectionKey selectionKey = keys.next(); keys.remove(); if (selectionKey.isValid()) { if (selectionKey.isAcceptable()) { this.accept(selectionKey); } else if (selectionKey.isReadable()) { this.read(selectionKey); } else if (selectionKey.isConnectable()) { this.connect(selectionKey); } } } } } } catch (Exception e) { logger.error("Exception in socket listener loop", e); } logger.debug("Exited Listener loop."); AbstractSocketHandler.this.stop(); }
@Override public synchronized Server shutdown() { Log.info("Shutting down the server..."); stopLoop(); if (ioWorkers != null) { for (RapidoidWorker worker : ioWorkers) { worker.shutdown(); } } if (serverSocketChannel != null && selector != null && serverSocketChannel.isOpen() && selector.isOpen()) { try { selector.close(); serverSocketChannel.close(); } catch (IOException e) { Log.warn("Cannot close socket or selector!", e); } } super.shutdown(); Log.info("!The server is down."); return this; }
private void openSocket() throws IOException { U.notNull(net.protocol(), "protocol"); U.notNull(net.helperClass(), "helperClass"); String blockingInfo = net.blockingAccept() ? "blocking" : "non-blocking"; Log.debug("Initializing server", "address", net.address(), "port", net.port(), "sync", net.syncBufs(), "accept", blockingInfo); serverSocketChannel = ServerSocketChannel.open(); if ((serverSocketChannel.isOpen()) && (selector.isOpen())) { serverSocketChannel.configureBlocking(net.blockingAccept()); ServerSocket socket = serverSocketChannel.socket(); Log.info("!Starting server", "!address", net.address(), "!port", net.port(), "I/O workers", net.workers(), "sync", net.syncBufs(), "accept", blockingInfo); InetSocketAddress addr = new InetSocketAddress(net.address(), net.port()); socket.setReceiveBufferSize(16 * 1024); socket.setReuseAddress(true); socket.bind(addr, MAX_PENDING_CONNECTIONS); Log.debug("Opened server socket", "address", addr); if (!net.blockingAccept()) { Log.debug("Registering accept selector"); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); } initWorkers(); } else { throw U.rte("Cannot open socket!"); } }