}
if( req == null )
break;
DatagramChannel ch = null;
try
{
ch = DatagramChannel.open();
DatagramSessionConfig cfg;
if( req.config.getSessionConfig() instanceof DatagramSessionConfig )
{
cfg = ( DatagramSessionConfig ) req.config.getSessionConfig();
}
else
{
cfg = ( DatagramSessionConfig ) getDefaultConfig().getSessionConfig();
}
ch.socket().setReuseAddress( cfg.isReuseAddress() );
ch.socket().setBroadcast( cfg.isBroadcast() );
ch.socket().setReceiveBufferSize( cfg.getReceiveBufferSize() );
ch.socket().setSendBufferSize( cfg.getSendBufferSize() );
if( ch.socket().getTrafficClass() != cfg.getTrafficClass() )
{
ch.socket().setTrafficClass( cfg.getTrafficClass() );
}
ch.configureBlocking( false );
ch.socket().bind( req.address );
if( req.address == null || req.address.getPort() == 0 )
{
req.address = ( InetSocketAddress ) ch.socket().getLocalSocketAddress();
}
ch.register( selector, SelectionKey.OP_READ, req );
synchronized( channels )
{
channels.put( req.address, ch );
}
getListeners().fireServiceActivated(
this, req.address, req.handler, req.config);
}
catch( Throwable t )
{
req.exception = t;
}
finally
{
synchronized( req )
{
req.done = true;
req.notify();
}
if( ch != null && req.exception != null )
{
try
{
ch.disconnect();
ch.close();
}
catch( Throwable e )
{
ExceptionMonitor.getInstance().exceptionCaught( e );
}