List of usage examples for java.nio.channels SelectionKey cancel
public abstract void cancel();
From source file:org.pvalsecc.comm.MultiplexedServer.java
private void readyToReceive(SelectionKey key) { ByteBuffer buffer = ByteBuffer.allocate(1024); //noinspection ConstantConditions SocketChannel socket = (SocketChannel) key.channel(); ServerConnection connection = (ServerConnection) key.attachment(); int size;/* w w w .j a v a2 s . c o m*/ try { size = socket.read(buffer); } catch (IOException e) { //message logging and call to "closed" is done in "error" method connection.error("Cannot receive data", e); key.cancel(); SystemUtilities.safeClose(socket); return; } if (size >= 0) { if (size != buffer.position()) { throw new RuntimeException( "[" + threadName + "] Inconsistent buffer: " + size + "!=" + buffer.position()); } nbBytesReceived += size; nbReceived++; buffer.flip(); connection.received(buffer); } else { LOGGER.info("[" + threadName + "] Connection closed by " + connection); connection.closed(); key.cancel(); } }
From source file:org.pvalsecc.comm.MultiplexedServer.java
private void readyToSend(SelectionKey key) { SocketChannel socket = (SocketChannel) key.channel(); ServerConnection connection = (ServerConnection) key.attachment(); try {//from w w w .j av a 2 s. c o m nbBytesSent += connection.send(socket); nbSent++; } catch (IOException e) { connection.error("Cannot send data", e); key.cancel(); SystemUtilities.safeClose(socket); } }
From source file:voldemort.common.nio.AbstractSelectorManager.java
public void close() { // Attempt to close, but if already closed, then we've been beaten to // the punch... if (!isClosed.compareAndSet(false, true)) return;//w ww . ja v a 2 s .c o m try { for (SelectionKey sk : selector.keys()) { try { if (logger.isTraceEnabled()) logger.trace("Closing SelectionKey's channel"); sk.channel().close(); Object attachment = sk.attachment(); if (attachment instanceof Closeable) { IOUtils.closeQuietly((Closeable) attachment); } } catch (Exception e) { if (logger.isEnabledFor(Level.WARN)) logger.warn(e.getMessage(), e); } try { if (logger.isTraceEnabled()) logger.trace("Cancelling SelectionKey"); sk.cancel(); } catch (Exception e) { if (logger.isEnabledFor(Level.WARN)) logger.warn(e.getMessage(), e); } } } catch (Exception e) { if (logger.isEnabledFor(Level.WARN)) logger.warn(e.getMessage(), e); } try { selector.close(); } catch (Exception e) { if (logger.isEnabledFor(Level.WARN)) logger.warn(e.getMessage(), e); } }
From source file:voldemort.common.nio.SelectorManagerWorker.java
protected void closeInternal() { if (logger.isDebugEnabled()) logger.debug("Closing remote connection from " + socketChannel.socket()); try {/*from w w w. j ava2 s . c o m*/ socketChannel.socket().close(); } catch (IOException e) { if (logger.isEnabledFor(Level.WARN)) logger.warn(e.getMessage(), e); } try { socketChannel.close(); } catch (IOException e) { if (logger.isEnabledFor(Level.WARN)) logger.warn(e.getMessage(), e); } SelectionKey selectionKey = socketChannel.keyFor(selector); if (selectionKey != null) { try { selectionKey.attach(null); selectionKey.cancel(); } catch (Exception e) { if (logger.isEnabledFor(Level.WARN)) logger.warn(e.getMessage(), e); } } // close the streams, so we account for comm buffer frees IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); }
From source file:x10.x10rt.yarn.ApplicationMaster.java
protected void handleX10() { // handle X10 place requests Iterator<SelectionKey> events = null; while (running) { try {/* w w w . j a v a 2s . c o m*/ SelectionKey key; // check for previously unhandled events if (events != null && events.hasNext()) { key = events.next(); events.remove(); } else if (selector.select() == 0) // check for new events continue; // nothing to process, go back and block on select again else { // select returned some events events = selector.selectedKeys().iterator(); key = events.next(); events.remove(); } // process the selectionkey if (key.isAcceptable()) { LOG.info("New connection from X10 detected"); // accept any connections on the server socket, and look for things to read from it ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); SocketChannel sc = ssc.accept(); sc.configureBlocking(false); sc.register(selector, SelectionKey.OP_READ); } if (key.isReadable()) { SocketChannel sc = (SocketChannel) key.channel(); ByteBuffer incomingMsg; if (pendingReads.containsKey(sc)) incomingMsg = pendingReads.remove(sc); else incomingMsg = ByteBuffer.allocateDirect(headerLength).order(ByteOrder.nativeOrder()); LOG.info("Reading message from X10"); try { if (sc.read(incomingMsg) == -1) { // socket closed sc.close(); key.cancel(); pendingReads.remove(sc); } else if (incomingMsg.hasRemaining()) { LOG.info("Message header partially read. " + incomingMsg.remaining() + " bytes remaining"); pendingReads.put(sc, incomingMsg); } else { // buffer is full if (incomingMsg.capacity() == headerLength) { // check to see if there is a body associated with this message header int datalen = incomingMsg.getInt(headerLength - 4); //System.err.println("Byte order is "+incomingMsg.order()+" datalen="+datalen); if (datalen == 0) processMessage(incomingMsg, sc); else { // create a larger array to hold the header+body ByteBuffer newBuffer = ByteBuffer.allocateDirect(headerLength + datalen) .order(ByteOrder.nativeOrder()); incomingMsg.rewind(); newBuffer.put(incomingMsg); incomingMsg = newBuffer; sc.read(incomingMsg); // read in the body, if available if (incomingMsg.hasRemaining()) { LOG.info("Message partially read. " + incomingMsg.remaining() + " bytes remaining"); pendingReads.put(sc, incomingMsg); } else processMessage(incomingMsg, sc); } } } } catch (IOException e) { LOG.warn("Error reading in message from socket channel", e); } } } catch (IOException e) { LOG.warn("Error handling X10 links", e); } } }