package org.glassfish.grizzly.nio.transport;
import java.io.IOException;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.*;
import org.glassfish.grizzly.impl.FutureImpl;
import org.glassfish.grizzly.nio.RegisterChannelResult;
import org.glassfish.grizzly.utils.Futures;
public class UDPNIOServerConnection extends UDPNIOConnection {
private static final Logger LOGGER = Grizzly.logger(UDPNIOServerConnection.class);
public UDPNIOServerConnection(UDPNIOTransport transport, DatagramChannel channel) {
super(transport, channel);
}
@Override
public Processor getProcessor() {
if (processor == null) {
return transport.getProcessor();
}
return processor;
}
@Override
public ProcessorSelector getProcessorSelector() {
if (processorSelector == null) {
return transport.getProcessorSelector();
}
return processorSelector;
}
public void register() throws IOException {
final FutureImpl<RegisterChannelResult> future =
Futures.createSafeFuture();
transport.getNIOChannelDistributor().registerServiceChannelAsync(
channel,
SelectionKey.OP_READ, this,
Futures.toCompletionHandler(future,
((UDPNIOTransport) transport).registerChannelCompletionHandler
));
try {
future.get(10, TimeUnit.SECONDS);
} catch (Exception e) {
throw new IOException("Error registering server channel key", e);
}
notifyReady();
}
@Override
protected void closeGracefully0(
final CompletionHandler<Closeable> completionHandler,
final CloseReason closeReason) {
terminate0(completionHandler, closeReason);
}
@Override
protected void terminate0(final CompletionHandler<Closeable> completionHandler,
final CloseReason closeReason) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("UDPNIOServerConnection might be only closed by calling unbind().");
}
if (completionHandler != null) {
completionHandler.completed(this);
}
}
public void unbind(
final CompletionHandler<Closeable> completionHandler) {
super.terminate0(completionHandler, CloseReason.LOCALLY_CLOSED_REASON);
}
@Override
protected void preClose() {
transport.unbind(this);
super.preClose();
}
}