Java Code Examples for java.nio.channels.SelectableChannel#keyFor()
The following examples show how to use
java.nio.channels.SelectableChannel#keyFor() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NIOConnectionManager.java From bt with Apache License 2.0 | 6 votes |
void connectionChecks() throws IOException { if((iterations & 0x0F) != 0) return; long now = System.currentTimeMillis(); if(now - lastConnectionCheck < 500) return; lastConnectionCheck = now; for(Selectable conn : new ArrayList<>(connections)) { conn.doStateChecks(now); SelectableChannel ch = conn.getChannel(); SelectionKey k; if(ch == null || (k = ch.keyFor(selector)) == null || !k.isValid()) connections.remove(conn); } }
Example 2
Source File: PollTcpManagerNio.java From baratine with GNU General Public License v2.0 | 6 votes |
private boolean removeConnection(PollController conn) { if (conn == null) { return false; } SocketBar socket = conn.getSocket(); SelectableChannel channel = socket.selectableChannel(); SelectionKey key = channel.keyFor(_selector); if (key != null) { key.cancel(); } remove(conn); return true; }
Example 3
Source File: NIOConnectionManager.java From mldht with Mozilla Public License 2.0 | 6 votes |
void connectionChecks() throws IOException { if((iterations & 0x0F) != 0) return; long now = System.currentTimeMillis(); if(now - lastConnectionCheck < 500) return; lastConnectionCheck = now; for(Selectable conn : new ArrayList<>(connections)) { conn.doStateChecks(now); SelectableChannel ch = conn.getChannel(); SelectionKey k; if(ch == null || (k = ch.keyFor(selector)) == null || !k.isValid()) connections.remove(conn); } }
Example 4
Source File: Progress.java From twister2 with Apache License 2.0 | 5 votes |
private void addInterest(SelectableChannel channel, int operation, SelectHandler callback) throws ClosedChannelException { SelectionKey key = channel.keyFor(selector); if (key == null) { channel.register(selector, operation, callback); } else if (!key.isValid()) { throw new RuntimeException( String.format("Unable to add %d in %s due to key is invalid", operation, channel)); } else { // Key is not null and key is valid if ((key.interestOps() & operation) != 0) { LOG.severe(String.format("%d has been registered in %s", operation, channel)); // throw new RuntimeException( // String.format("%d has been registered in %s", operation, channel)); } if (key.attachment() == null) { key.attach(callback); } else { if (callback != key.attachment()) { throw new RuntimeException("Unmatched SelectHandler has already been attached" + " for other operation"); } } key.interestOps(key.interestOps() | operation); } }
Example 5
Source File: Progress.java From twister2 with Apache License 2.0 | 5 votes |
private void removeInterest(SelectableChannel channel, int operation) { SelectionKey key = channel.keyFor(selector); // Exception would be thrown if key is null or key is inValid // We do not need double check it ahead key.interestOps(key.interestOps() & (~operation)); }
Example 6
Source File: NIOLooper.java From incubator-heron with Apache License 2.0 | 5 votes |
/** * Register an operation interest on a SelectableChannel, with ISelectHandler as callback attachment * There are two cases when trying to register an interest * 1. The whole key does not exist; no interests ever registered for this channel * 2. The key exists due to other interests registered but not the one we are adding * <p> * In 1st case, we just register this channel with operation on the given Selector * In 2nd case, we have to make sure the state of NIOLooper is clean: * 1. Key has to be valid * 2. The interest has not yet been registered * 3. If old attached ISelectHandler exists, it has to be the same as new one * If any one of above 3 conditions are not met, RuntimeException would be thrown. * * @param channel The Selectable to register operation interest * @param operation The operation interest to register * @param callback The Callback to handle * @throws ClosedChannelException if Channel is closed when trying to register an interest */ private void addInterest(SelectableChannel channel, int operation, ISelectHandler callback) throws ClosedChannelException { SelectionKey key = channel.keyFor(selector); if (key == null) { channel.register(selector, operation, callback); } else if (!key.isValid()) { throw new RuntimeException( String.format("Unable to add %d in %s due to key is invalid", operation, channel)); } else { // Key is not null and key is valid if ((key.interestOps() & operation) != 0) { throw new RuntimeException( String.format("%d has been registered in %s", operation, channel)); } if (key.attachment() == null) { key.attach(callback); } else { if (callback != key.attachment()) { throw new RuntimeException("Unmatched SelectHandler has already been attached" + " for other operation"); } // If call == key.attachment // Just skip } key.interestOps(key.interestOps() | operation); } }
Example 7
Source File: NioAcceptor.java From craft-atom with MIT License | 5 votes |
private void close(SelectableChannel sc) throws IOException { if (sc != null) { SelectionKey key = sc.keyFor(selector); if (key != null) { key.cancel(); } sc.close(); } }
Example 8
Source File: Progress.java From twister2 with Apache License 2.0 | 4 votes |
private boolean isInterestRegistered(SelectableChannel channel, int operation) { SelectionKey key = channel.keyFor(selector); return key != null && (key.interestOps() & operation) != 0; }
Example 9
Source File: Progress.java From twister2 with Apache License 2.0 | 4 votes |
public void removeAllInterest(SelectableChannel channel) { SelectionKey key = channel.keyFor(selector); if (key != null) { key.cancel(); } }
Example 10
Source File: NIOLooper.java From incubator-heron with Apache License 2.0 | 4 votes |
public void removeAllInterest(SelectableChannel channel) { SelectionKey key = channel.keyFor(selector); if (key != null) { key.cancel(); } }
Example 11
Source File: NIOLooper.java From incubator-heron with Apache License 2.0 | 4 votes |
public boolean isChannelValid(SelectableChannel channel) { SelectionKey key = channel.keyFor(selector); return key != null && key.isValid(); }
Example 12
Source File: NIOLooper.java From incubator-heron with Apache License 2.0 | 3 votes |
/** * Remove one operation interest on a SelectableChannel. * The SelectableChannel has to be registered with Selector ahead. * Otherwise, NullPointerExceptions would throw * The key for SelectableChannel has to be valid. * Otherwise, InvalidValid Exception would throw. * * @param channel the SelectableChannel to remove operation interest * @param operation the interest to remove */ private void removeInterest(SelectableChannel channel, int operation) { SelectionKey key = channel.keyFor(selector); // Exception would be thrown if key is null or key is inValid // We do not need double check it ahead key.interestOps(key.interestOps() & (~operation)); }
Example 13
Source File: NIOLooper.java From incubator-heron with Apache License 2.0 | 2 votes |
/** * Check whether an operation interest was registered on a SelectableChannel * There are two cases that interest is not registered * 1. The whole key does not exist; no interests ever registered for this channel * 2. The key exists due to other interests registered but not the one we are adding * If the key exists, the key for SelectableChannel has to be valid. * Otherwise, InvalidValid Exception would throw. * * @param channel The Selectable to check * @param operation The operation interest to check */ private boolean isInterestRegistered(SelectableChannel channel, int operation) { SelectionKey key = channel.keyFor(selector); return key != null && (key.interestOps() & operation) != 0; }