List of usage examples for java.nio.channels SocketChannel equals
public boolean equals(Object obj)
From source file:ee.ria.xroad.proxy.clientproxy.FastestSocketSelector.java
private static void closeSelector(Selector selector, SocketChannel selectedChannel) throws IOException { for (SelectionKey key : selector.keys()) { if (selectedChannel == null || !selectedChannel.equals(key.channel())) { closeQuietly(key.channel()); }// w ww . j av a 2 s. co m } selector.close(); }
From source file:org.openhab.binding.irtrans.handler.EthernetBridgeHandler.java
protected ByteBuffer onReadable(int bufferSize, boolean isSelective) { lock.lock();/*from w ww.j av a 2 s . com*/ try { synchronized (selector) { try { selector.selectNow(); } catch (IOException e) { logger.error("An exception occurred while selecting: {}", e.getMessage()); } } Iterator<SelectionKey> it = selector.selectedKeys().iterator(); while (it.hasNext()) { SelectionKey selKey = it.next(); it.remove(); if (selKey.isValid() && selKey.isReadable()) { SocketChannel aSocketChannel = (SocketChannel) selKey.channel(); if ((aSocketChannel.equals(socketChannel) && isSelective) || !isSelective) { ByteBuffer readBuffer = ByteBuffer.allocate(bufferSize); int numberBytesRead = 0; boolean error = false; try { numberBytesRead = aSocketChannel.read(readBuffer); } catch (NotYetConnectedException e) { logger.warn("The channel '{}' is not yet connected: {}", aSocketChannel, e.getMessage()); if (!aSocketChannel.isConnectionPending()) { error = true; } } catch (IOException e) { // If some other I/O error occurs logger.warn("An IO exception occured on channel '{}': {}", aSocketChannel, e.getMessage()); error = true; } if (numberBytesRead == -1) { error = true; } if (error) { logger.debug("Disconnecting '{}' because of a socket error", getThing().getUID()); try { aSocketChannel.close(); } catch (IOException e1) { logger.debug("An exception occurred while closing the channel '{}': {}", socketChannel, e1.getMessage()); } } else { readBuffer.flip(); return readBuffer; } } } } return null; } finally { lock.unlock(); } }
From source file:org.openhab.binding.irtrans.handler.EthernetBridgeHandler.java
protected void onWritable(ByteBuffer buffer) { lock.lock();/*ww w .j a va2s .c o m*/ try { synchronized (selector) { try { selector.selectNow(); } catch (IOException e) { logger.error("An exception occurred while selecting: {}", e.getMessage()); } } Iterator<SelectionKey> it = selector.selectedKeys().iterator(); while (it.hasNext()) { SelectionKey selKey = it.next(); it.remove(); if (selKey.isValid() && selKey.isWritable()) { SocketChannel aSocketChannel = (SocketChannel) selKey.channel(); if (aSocketChannel.equals(socketChannel)) { boolean error = false; buffer.rewind(); try { logger.trace("Sending '{}' on the channel '{}'->'{}'", new String(buffer.array()), aSocketChannel.getLocalAddress(), aSocketChannel.getRemoteAddress()); aSocketChannel.write(buffer); } catch (NotYetConnectedException e) { logger.warn("The channel '{}' is not yet connected: {}", aSocketChannel, e.getMessage()); if (!aSocketChannel.isConnectionPending()) { error = true; } } catch (ClosedChannelException e) { // If some other I/O error occurs logger.warn("The channel for '{}' is closed: {}", aSocketChannel, e.getMessage()); error = true; } catch (IOException e) { // If some other I/O error occurs logger.warn("An IO exception occured on channel '{}': {}", aSocketChannel, e.getMessage()); error = true; } if (error) { try { aSocketChannel.close(); } catch (IOException e) { logger.warn("An exception occurred while closing the channel '{}': {}", aSocketChannel, e.getMessage()); } } } } } } finally { lock.unlock(); } }