package org.glassfish.grizzly.strategies;
import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import org.glassfish.grizzly.Connection;
import org.glassfish.grizzly.Context;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.IOEvent;
import org.glassfish.grizzly.IOEventLifeCycleListener;
import org.glassfish.grizzly.Transport;
import org.glassfish.grizzly.asyncqueue.AsyncQueue;
import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
public final class SameThreadIOStrategy extends AbstractIOStrategy {
private static final SameThreadIOStrategy INSTANCE = new SameThreadIOStrategy();
private static final Logger logger = Grizzly.logger(SameThreadIOStrategy.class);
private static final InterestLifeCycleListenerWhenIoEnabled LIFECYCLE_LISTENER_WHEN_IO_ENABLED = new InterestLifeCycleListenerWhenIoEnabled();
private static final InterestLifeCycleListenerWhenIoDisabled LIFECYCLE_LISTENER_WHEN_IO_DISABLED = new InterestLifeCycleListenerWhenIoDisabled();
private SameThreadIOStrategy() {
}
public static SameThreadIOStrategy getInstance() {
return INSTANCE;
}
@Override
public boolean executeIoEvent(final Connection connection, final IOEvent ioEvent, final boolean isIoEventEnabled) throws IOException {
IOEventLifeCycleListener listener = null;
if (isReadWrite(ioEvent)) {
listener = isIoEventEnabled ? LIFECYCLE_LISTENER_WHEN_IO_ENABLED : LIFECYCLE_LISTENER_WHEN_IO_DISABLED;
}
fireIOEvent(connection, ioEvent, listener, logger);
return true;
}
@Override
public Executor getThreadPoolFor(final Connection connection, final IOEvent ioEvent) {
return null;
}
@Override
public ThreadPoolConfig createDefaultWorkerPoolConfig(final Transport transport) {
return null;
}
private static final class InterestLifeCycleListenerWhenIoEnabled extends IOEventLifeCycleListener.Adapter {
@Override
public void onReregister(final Context context) throws IOException {
onComplete(context, null);
}
@Override
public void onComplete(final Context context, final Object data) throws IOException {
if (context.wasSuspended() || context.isManualIOEventControl()) {
final IOEvent ioEvent = context.getIoEvent();
final Connection connection = context.getConnection();
if (AsyncQueue.EXPECTING_MORE_OPTION.equals(data)) {
connection.simulateIOEvent(ioEvent);
} else {
connection.enableIOEvent(ioEvent);
}
}
}
@Override
public void onContextSuspend(final Context context) throws IOException {
if (!context.wasSuspended() && !context.isManualIOEventControl()) {
disableIOEvent(context);
}
}
@Override
public void onContextManualIOEventControl(final Context context) throws IOException {
if (!context.wasSuspended() && !context.isManualIOEventControl()) {
disableIOEvent(context);
}
}
private static void disableIOEvent(final Context context) throws IOException {
final Connection connection = context.getConnection();
final IOEvent ioEvent = context.getIoEvent();
connection.disableIOEvent(ioEvent);
}
}
private static final class InterestLifeCycleListenerWhenIoDisabled extends IOEventLifeCycleListener.Adapter {
@Override
public void onReregister(final Context context) throws IOException {
onComplete(context, null);
}
@Override
public void onComplete(final Context context, final Object data) throws IOException {
final IOEvent ioEvent = context.getIoEvent();
final Connection connection = context.getConnection();
if (AsyncQueue.EXPECTING_MORE_OPTION.equals(data)) {
connection.simulateIOEvent(ioEvent);
} else {
connection.enableIOEvent(ioEvent);
}
}
}
}