<commit_before><commit_msg>add Server api<commit_after>package edu.ouc.reactor;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * MainReactor play a role in accepting connection.
 * and dispatch to SubReactor
 * 
 * @author wqx
 *
 */
public class Server{

	private static final Logger LOG = LoggerFactory.getLogger(Server.class);
	
	Reactor mainReactor;
	
	Reactor subReactor;
	
	private ServerSocketChannel ssc;
	
	/**
	 * @throws IOException
	 */
	public Server(){
		
		try {
			initChannel();
		} catch (IOException e) {
			LOG.error("Unexpected Exception during init Channel, e=" + e);
		}
	}
	private void initChannel() throws IOException{
		ssc = ServerSocketChannel.open();
		ssc.configureBlocking(false);
		ssc.register(mainReactor.next().getSelector()
						, SelectionKey.OP_ACCEPT, new Acceptor(subReactor));
	}
	public void bind(InetSocketAddress remoteAddress){
		try {
			ssc.socket().bind(remoteAddress);
		} catch (IOException e) {
			LOG.error("Unexpected Exception during bind remoteAddress:" + remoteAddress);
		}
	}
	
	class Acceptor implements ChannelHandler{
		
		Reactor subReactor;
		
		public Acceptor(Reactor subReactor){
			this.subReactor = subReactor;
		}
		@Override
		public void handle(Object msg) {
			/**
			 * In this place, we can do some authenticating or authorizing work
			 * If permitted,  deliver to subReactor
			 */
			SelectionKey key = (SelectionKey)msg;
			SocketChannel sc = (SocketChannel) key.channel();
			try {
				EventLoop loop = subReactor.next();
				sc.register(loop.getSelector(),
						SelectionKey.OP_READ,
						new DefaultChannelHandler(loop, key));
			} catch (ClosedChannelException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void close(){
		try {
			ssc.close();
			if(LOG.isDebugEnabled()){
				LOG.debug("Close selector");
			}
		} catch (IOException e) {
			LOG.warn("Ignoring exception during close selector, e=" + e);
		}
	}
}
{'fraction_non_alphanumeric': 0.11157407407407408, 'fraction_numerical': 0.000925925925925926, 'mean_word_length': 3.8781038374717833, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4542867', 'n_tokens_mistral': 764, 'n_tokens_neox': 652, 'n_words': 162}