while (changes.hasNext()) {
ChangeRequest change = (ChangeRequest) changes.next();
if (change.socket.isConnected()) {
switch (change.type) {
case ChangeRequest.CHANGEOPS:
SelectionKey key = change.socket.keyFor(this.selector);
if (key.isValid()) {
key.interestOps(change.ops);
}
}
}
}
this.pendingChanges.clear();
}
synchronized (this.pendingClosed) {
Iterator<?> it = pendingClosed.iterator();
while (it.hasNext()) {
ChangeRequest change = (ChangeRequest) it.next();
if (change.socket.isConnected()) {
switch (change.type) {
case ChangeRequest.CLOSE:
try {
/*
* Force data to be sent if there is data
* waiting.
*/
if (pendingData.containsKey(change.socket)) {
SelectionKey key = change.socket.keyFor(selector);
if (key.isValid()) {
write(key);
}
}
/*
* Close the socket
*/
change.socket.close();
} catch (Exception e) {
logger.info("Exception happened when closing socket", e);
}
break;
}
} else {
logger.info("Closing a not connected socket");
}
}
pendingClosed.clear();
}
// Wait for an event one of the registered channels
this.selector.select();
// Iterate over the set of keys for which events are available
Iterator<?> selectedKeys = this.selector.selectedKeys().iterator();
while (selectedKeys.hasNext()) {
SelectionKey key = (SelectionKey) selectedKeys.next();
selectedKeys.remove();
if (!key.isValid()) {
continue;
}
// Check what event is available and deal with it
if (key.isAcceptable()) {
this.accept(key);
} else if (key.isReadable()) {
this.read(key);
} else if (key.isWritable()) {
this.write(key);
}
}
} catch (IOException e) {
logger.error("Error on NIOServer", e);