// Path: src/org/gearman/core/GearmanCallbackHandler.java
// public interface GearmanCallbackHandler<D,R extends GearmanCallbackResult> {
// 	public void onComplete(D data, R result);
// }
// 
// Path: src/org/gearman/core/GearmanConnectionManager.java
// public enum ConnectCallbackResult implements GearmanCallbackResult {
// 	SUCCESS,
// 	SERVICE_SHUTDOWN,
// 	CONNECTION_FAILED;
// 		
// 	@Override
// 	public boolean isSuccessful() {
// 		return this.equals(SUCCESS);
// 	}
// }


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AcceptPendingException;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.nio.channels.NotYetBoundException;
import java.nio.channels.ShutdownChannelGroupException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import org.gearman.core.GearmanCallbackHandler;
import org.gearman.core.GearmanConnectionManager.ConnectCallbackResult;
	
	public boolean isShutdown() {
		return asyncChannelGroup.isShutdown();
	}
	
	public synchronized boolean closePort(int port) throws IOException {
		final AsynchronousServerSocketChannel server = this.ports.remove(port);
		if(server==null) return false;
		
		server.close();
		return true;
	}
	
	public synchronized void closePorts() {
		Iterator<AsynchronousServerSocketChannel> it = this.ports.values().iterator();
		while(it.hasNext()) {
			try {
				AsynchronousServerSocketChannel assc = it.next();
				assc.close();
				it.remove();
			} catch (IOException e) {
				// TODO log error
			}
		}
	}
	
	public synchronized Set<Integer> getOpenPorts() {
		return Collections.unmodifiableSet(this.ports.keySet());
	}
	
// Path: src/org/gearman/core/GearmanCallbackHandler.java
// public interface GearmanCallbackHandler<D,R extends GearmanCallbackResult> {
// 	public void onComplete(D data, R result);
// }
// 
// Path: src/org/gearman/core/GearmanConnectionManager.java
// public enum ConnectCallbackResult implements GearmanCallbackResult {
// 	SUCCESS,
// 	SERVICE_SHUTDOWN,
// 	CONNECTION_FAILED;
// 		
// 	@Override
// 	public boolean isSuccessful() {
// 		return this.equals(SUCCESS);
// 	}
// }


// Path: src/org/gearman/reactor/NioReactor.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AcceptPendingException;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.nio.channels.NotYetBoundException;
import java.nio.channels.ShutdownChannelGroupException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import org.gearman.core.GearmanCallbackHandler;
import org.gearman.core.GearmanConnectionManager.ConnectCallbackResult;

	
	public boolean isShutdown() {
		return asyncChannelGroup.isShutdown();
	}
	
	public synchronized boolean closePort(int port) throws IOException {
		final AsynchronousServerSocketChannel server = this.ports.remove(port);
		if(server==null) return false;
		
		server.close();
		return true;
	}
	
	public synchronized void closePorts() {
		Iterator<AsynchronousServerSocketChannel> it = this.ports.values().iterator();
		while(it.hasNext()) {
			try {
				AsynchronousServerSocketChannel assc = it.next();
				assc.close();
				it.remove();
			} catch (IOException e) {
				// TODO log error
			}
		}
	}
	
	public synchronized Set<Integer> getOpenPorts() {
		return Collections.unmodifiableSet(this.ports.keySet());
	}
	
	public final <X> void openSocket(final InetSocketAddress adrs, final SocketHandler<X> sHandler, final GearmanCallbackHandler<InetSocketAddress, ConnectCallbackResult> callback) {