}
}
else if (o instanceof ServerSocketChannel)
{
ServerSocketChannel channel = (ServerSocketChannel)o;
channel.register(getSelector(),SelectionKey.OP_ACCEPT);
}
else
throw new IllegalArgumentException(o.toString());
}
catch (CancelledKeyException e)
{
if (isRunning())
Log.warn(e);
else
Log.debug(e);
}
}
changes.clear();
long idle_next = 0;
long retry_next = 0;
long now=System.currentTimeMillis();
synchronized (this)
{
_idleTimeout.setNow(now);
_retryTimeout.setNow(now);
if (_lowResourcesConnections>0 && _selector.keys().size()>_lowResourcesConnections)
_idleTimeout.setDuration(_lowResourcesMaxIdleTime);
else
_idleTimeout.setDuration(_maxIdleTime);
idle_next=_idleTimeout.getTimeToNext();
retry_next=_retryTimeout.getTimeToNext();
}
// workout how low to wait in select
long wait = 1000L; // not getMaxIdleTime() as the now value of the idle timers needs to be updated.
if (idle_next >= 0 && wait > idle_next)
wait = idle_next;
if (wait > 0 && retry_next >= 0 && wait > retry_next)
wait = retry_next;
// Do the select.
if (wait > 10) // TODO tune or configure this
{
long before=now;
int selected=_selector.select(wait);
now = System.currentTimeMillis();
_idleTimeout.setNow(now);
_retryTimeout.setNow(now);
// Look for JVM bug
if (selected==0 && wait>0 && (now-before)<wait/2 && _selector.selectedKeys().size()==0)
{
if (_jvmBug++>5) // TODO tune or configure this
{
// Probably JVM BUG!
Iterator iter = _selector.keys().iterator();
while(iter.hasNext())
{
key = (SelectionKey) iter.next();
if (key.isValid()&&key.interestOps()==0)
{
key.cancel();
}
}
try
{
Thread.sleep(20); // tune or configure this
}
catch (InterruptedException e)
{
Log.ignore(e);
}
}
}
else
_jvmBug=0;
}
else
{
_selector.selectNow();
_jvmBug=0;
}
// have we been destroyed while sleeping\
if (_selector==null || !_selector.isOpen())
return;
// Look for things to do
Iterator iter = _selector.selectedKeys().iterator();
while (iter.hasNext())
{
key = (SelectionKey) iter.next();
try
{
if (!key.isValid())
{
key.cancel();
SelectChannelEndPoint endpoint = (SelectChannelEndPoint)key.attachment();
if (endpoint != null)
endpoint.doUpdateKey();
continue;
}
Object att = key.attachment();
if (att instanceof SelectChannelEndPoint)
{
SelectChannelEndPoint endpoint = (SelectChannelEndPoint)att;
endpoint.dispatch();
}
else if (key.isAcceptable())
{
SocketChannel channel = acceptChannel(key);
if (channel==null)
continue;
channel.configureBlocking(false);
// TODO make it reluctant to leave 0
_nextSet=++_nextSet%_selectSet.length;
// Is this for this selectset
if (_nextSet==_setID)
{
// bind connections to this select set.
SelectionKey cKey = channel.register(_selectSet[_nextSet].getSelector(), SelectionKey.OP_READ);
SelectChannelEndPoint endpoint=newEndPoint(channel,_selectSet[_nextSet],cKey);
cKey.attach(endpoint);
if (endpoint != null)
endpoint.dispatch();
}
else
{
// nope - give it to another.
_selectSet[_nextSet].addChange(channel);
_selectSet[_nextSet].wakeup();
}
}
else if (key.isConnectable())
{
// Complete a connection of a registered channel
SocketChannel channel = (SocketChannel)key.channel();
boolean connected=false;
try
{
connected=channel.finishConnect();
}
catch(Exception e)
{
connectionFailed(channel,e,att);
}